我在背面有以下示例代码来生成表格。
\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{stackengine}
\newcommand\xrowht[2][0]{\addstackgap[0.5\dimexpr#2\relax]{\vphantom{#1}}}
\title{table}
\begin{document}
\maketitle
\begin{table}[ht]
\footnotesize
\centering
\begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|}
\hline\xrowht[()]{15pt}
\multirow{2}{*}{Test} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{*}{Cell} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\
\cline{3-5}\cline{7-10}\xrowht[()]{15pt}
No & & x & y & z & Size & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
\hline\xrowht[()]{15pt}
1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
\hline\xrowht[()]{15pt}
2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1} & 0.1 & 0.1 & 0.1 \\
\hline
\end{tabular}
\vspace{0.5cm}
\caption{Tests}
\end{table}
\end{document}
这给了我,
问题:我无法将表格内的文本垂直对齐到中心或任何其他位置。例如,文本“Test No”、“Name”、“Cell Size”、“C1”和“C2”应该垂直居中对齐。此外,我发现使用
\hline\xrowht[()]{15pt}
给了我不同的框宽度,参见“Domain”和“MLR”框。有人可以帮我以正确的方式设置这张桌子吗?
谢谢大家
您可以更改
\arraystretch
,而不是手动弄乱表格的行高,所有单元格将自动居中
其他一些要点:
不要手动在标题上方添加空格,请查看
caption
包
请查看http://betterposters.blogspot.com/2012/08/the-data-prison.html或https://www.inf.ethz.ch/personal/markusp/teaching/ guides/guide-tables.pdf 有关漂亮表格布局的一些指南。把你的数据关进监狱绝对不是一个好的设计
请勿在数学模式下使用
avg
或 out
等多字母表达式。这会完全搞乱字偶距
\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{caption}
\title{table}
\begin{document}
\maketitle
\begin{table}[ht]
\renewcommand{\arraystretch}{2}
\footnotesize
\centering
\begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|}
\hline
\multirow{2}{0.75cm}{\centering Test\\ No} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{0.75cm}{\centering Cell\\ Size} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\
\cline{3-5}\cline{7-10}
& & x & y & z & & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
\hline
1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
\hline
2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1} & 0.1 & 0.1 & 0.1 \\
\hline
\end{tabular}
% \vspace{0.5cm}
\caption{Tests}
\end{table}
\end{document}
使用
tabularray
包,现在可以更轻松地合并单元格并使其自动居中:
\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[margin=1in]{geometry}
\usepackage{caption}
\usepackage{tabularray}
\title{table}
\begin{document}
\maketitle
\begin{table}[ht]
\footnotesize
\begin{tblr}{
colspec = {*{12}{X}},
cells={halign=c},
vlines,
hlines,
stretch=2
}
\SetCell[r=2]{} Test No & \SetCell[r=2]{} Name & \SetCell[c=3]{} Domain && & \SetCell[r=2]{} Cell Size & \SetCell[c=4]{} MLR &&&& \SetCell[r=2]{} $C_1$ & \SetCell[r=2]{} $C_2$\\
& & x & y & z & & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \SetCell[c=2]{} 0.1 & & 0.1 & 0.1 & 0.1 \\
\end{tblr}
\caption{Tests}
\end{table}
\end{document}