![指定 latex 表的列宽和居中垂直对齐](https://linux22.com/image/468910/%E6%8C%87%E5%AE%9A%20latex%20%E8%A1%A8%E7%9A%84%E5%88%97%E5%AE%BD%E5%92%8C%E5%B1%85%E4%B8%AD%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90.png)
我有一个多行多列的表格。我需要指定某些列的列宽,因为表格对于页面来说太宽了。但我找不到这样做的方法,也无法垂直居中文本。
我目前有
\begin{table}[t!]
\centering
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately $3\times$ more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an $\ast$, and the values of the other controllers are shown as multiples of the respective lowest value.
}
\begin{tabular}{\textwidth} { |c|c|c|c|c|c|c| }\hline
\multicolumn{2}{|c|}{} & Min-Torque & Max-Force & Greedy, $q = 0$ & Greedy, $q = 1$ & Greedy, $q = 3$ \\
\hline
\multirow{2}{6.3em}{Settling Time} & $\alpha = 0.97$ & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& $\alpha = 0.99$ & 1.45 & 1.05 & 1.05 & * & 1.41 \\
\hline
\multirow{2}{6.3em}{Normalized Path Length} & $\alpha = 0.97$ & 1.09 & * & * & 1.02 & 1.07 \\
& $\alpha = 0.99$ & 1.08 & * & * & 1.01 & 1.07 \\
\hline
\multirow{2}{6.3em}{Final Angular Velocity} & $\alpha = 0.97$ & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& $\alpha = 0.99$ & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\hline
\end{tabular}
\label{table:boxplot}
\end{table}
生成结果:
我也尝试过:
\begin{table}[t!]
\centering
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately $3\times$ more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an $\ast$, and the values of the other controllers are shown as multiples of the respective lowest value.
}
\begin{tabularx}{\textwidth} {
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X | }\hline
\multicolumn{2}{|c|}{} & Min-Torque & Max-Force & Greedy, $q = 0$ & Greedy, $q = 1$ & Greedy, $q = 3$ \\
\hline
\multirow{2}{6.3em}{Settling Time} & $\alpha = 0.97$ & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& $\alpha = 0.99$ & 1.45 & 1.05 & 1.05 & * & 1.41 \\
\hline
\multirow{2}{6.3em}{Normalized Path Length} & $\alpha = 0.97$ & 1.09 & * & * & 1.02 & 1.07 \\
& $\alpha = 0.99$ & 1.08 & * & * & 1.01 & 1.07 \\
\hline
\multirow{2}{6.3em}{Final Angular Velocity} & $\alpha = 0.97$ & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& $\alpha = 0.99$ & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\hline
\end{tabularx}
\label{table:boxplot}
\end{table}
生成结果:
有什么帮助吗?
好的,对不起,
我仍然遇到一些问题。以下代码:
\begin{table}[t]
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately three times more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an asterisk, and the values of the other controllers are shown as multiples of the respective lowest value. }
\label{table:boxplot}
\begin{tblr}{hlines, vlines,
colspec = {X[2,l] c *{5}{X[c, m]}},
row{3} = {mode=math},
cell{1}{1,2} = {r=3}{},
cell{1}{3} = {c=5}{},
cell{2}{3,4} = {r=2}{},
cell{2}{5} = {c=3}{},
cell{even}{1} = {r=2}{}
}
& $\alpha$
& Controller type
& & & & \\
& & Min Torque
& Max Force
& Greedy
& & \\
& & & & q = 0 & q = 1 & q = 3 \\
Settling Time
& 0.97 & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& 0.99 & 1.45 & 1.05 & 1.05 & * & 1.41 \\
Normalized Path Length
& 0.97 & 1.09 & * & * & 1.02 & 1.07 \\
& 0.99 & 1.08 & * & * & 1.01 & 1.07 \\
Final Angular Velocity
& 0.97 & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& 0.99 & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\end{tblr}
\end{table}
产生以下内容:
Overleaf 在 \end{tblr} 行上给我以下错误:“package xcolor 错误:未定义颜色‘模式’。” 我还想去掉黑线。任何帮助都将不胜感激。
另外,我显然需要一些帮助才能让代码在此处显示为代码,而不仅仅是难以阅读的文本。我在文本编辑器中单击表示“代码示例”的 {} 符号,然后将代码粘贴到它所显示的位置,但它仍然显示为文本。有什么帮助吗?
答案1
像这样?请注意,我 (a) 为标题材料提供了更多结构,并且 (b) 通过删除所有垂直规则并使用更少但间距适当的水平规则,为表格提供了更开放的“外观”。
\documentclass{article}
\usepackage{tabularx,ragged2e,booktabs,amsmath}
\newcolumntype{C}{>{\Centering}X}
\newcommand\mytabL[1]{\smash[b]{\begin{tabular}[t]{@{} l @{}} #1 \end{tabular}}}
\newcommand\mytabC[1]{\smash[b]{\begin{tabular}[t]{@{} c @{}} #1 \end{tabular}}}
\begin{document}
\begin{table}[t!]
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately three times more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an asterisk, and the values of the other controllers are shown as multiples of the respective lowest value. }
\label{table:boxplot}
\medskip
\begin{tabularx}{\textwidth}{@{} l c CCCCC @{}}
\toprule
& $\alpha$ & \multicolumn{5}{c@{}}{Controller type}\\
\cmidrule(l){3-7}
& & \mytabC{Min-\\Torque} & \mytabC{Max-\\Force} & \multicolumn{3}{c@{}}{Greedy}\\
\cmidrule(l){5-7}
& & & & $q = 0$ & $q = 1$ & $q = 3$ \\
\midrule
\mytabL{Median simul.\\settling time} & 0.97 & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& 0.99 & 1.45 & 1.05 & 1.05 & * & 1.41 \\
\addlinespace
\mytabL{Normalized\\path length} & 0.97 & 1.09 & * & * & 1.02 & 1.07 \\
& 0.99 & 1.08 & * & * & 1.01 & 1.07 \\
\addlinespace
\mytabL{Final angular\\ velocity} & 0.97 & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& 0.99 & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
由于您希望表格中包含所有行,因此该tabularray
包可能会派上用场。使用它并在需要的地方定义多行单元格,MWE 可以细化为:
\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs}
\begin{document}
\begin{table}[ht]
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately three times more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an asterisk, and the values of the other controllers are shown as multiples of the respective lowest value. }
\label{table:boxplot}
\begin{tblr}{hlines, vlines,
colspec = {X[2,l] c *{5}{X[c, m]}},
row{3} = {mode=math},
cell{1}{1,2} = {r=3}{},
cell{1}{3} = {c=5}{},
cell{2}{3,4} = {r=2}{},
cell{2}{5} = {c=3}{},
cell{even}{1} = {r=2}{}
}
& $\alpha$
& Controller type
& & & & \\
& & Min Torque
& Max Force
& Greedy
& & \\
& & & & q = 0 & q = 1 & q = 3 \\
Median simul. settling time
& 0.97 & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& 0.99 & 1.45 & 1.05 & 1.05 & * & 1.41 \\
Normalized espace
& 0.97 & 1.09 & * & * & 1.02 & 1.07 \\
& 0.99 & 1.08 & * & * & 1.01 & 1.07 \\
Final angular velocity
& 0.97 & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& 0.99 & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\end{tblr}
\end{table}
\end{document}
答案3
如果您希望在表格中包含所有规则,可以使用 来创建表格{NiceTabular}
。但是,与许多人一样,我建议使用精神的nicematrix
版本。booktabs
booktabs
\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{nicematrix}
\begin{document}
\begin{table}[ht]
\caption{Comparison of the median simulated settling time, normalized path length, and final angular velocity of the minimize-torque controller, maximize-force controller, and greedy controller with $q=0$, $q=1$, and $q=3$, using a gentle position controller ($\alpha = 0.99$) and one that is approximately three times more aggressive ($\alpha = 0.97$). For each metric, the lowest (i.e., best) value is denoted with an asterisk, and the values of the other controllers are shown as multiples of the respective lowest value. }
\label{table:boxplot}
\begin{NiceTabular}{X[2,l] c *{5}{X[c, m]}}[hvlines,cell-space-limits=2pt]
\Block{3-1}{} & \Block{3-1}{$\alpha$}
& \Block{1-*}{Controller type} \\
& & \Block{2-1}{Min Torque}
& \Block{2-1}{Max Force}
& \Block{1-*}{Greedy} \\
& & & & $q = 0$ & $q = 1$ & $q = 3$ \\
\Block{2-1}{Median simul. settling time}
& 0.97 & 1.46 & 1.04 & 1.04 & * & 1.41 \\
& 0.99 & 1.45 & 1.05 & 1.05 & * & 1.41 \\
\Block{2-1}{Normalized espace}
& 0.97 & 1.09 & * & * & 1.02 & 1.07 \\
& 0.99 & 1.08 & * & * & 1.01 & 1.07 \\
\Block{2-1}{Final angular velocity}
& 0.97 & * & 9.14 & 11.58 & 9.33 & 9.29 \\
& 0.99 & * & 9.10 & 10.06 & 9.08 & 9.79 \\
\end{NiceTabular}
\end{table}
\end{document}