在尝试创建具有多行单元格的表格时,我遇到了一些行距和对齐问题。以下代码创建了两个表格:
\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{pbox}
\usepackage{multirow}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \begin{tabular}{l} cell221 \\ cell222 \\ \end{tabular} & cell32\\
\pbox[t]{5cm}{cell131 \\ cell132} & cell23 & cell33\\
cell14 & cell24 & \multirow{2}{*}{\pbox[t]{5cm}{cell341 \\ cell342}} \\
\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
& cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
& & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}
右表显示了期望的结果。它是使用空单元格创建的,在更改内容时缺乏灵活性。在左表中,显示了创建多行/多行单元格的尝试。请注意行距和对齐问题。第 5 行和第 6 行(没有任何多行单元格)彼此之间的距离正确。 (单元格 221 和 222)tabular
内的Atable
给出了正确的单元格内和单元格间行距,但是水平对齐不正确。此外,单元格 12 和 32 的垂直对齐不正确。使用pbox
(单元格 131 和 132)可以正确对齐整行。单元格内的行距正确,但到下一个单元格(从 132 到 14)的间距太小。这可能会让人觉得 132 和 14 应该在一起。使用multirow
(单元格 341 和 342)时也会出现同样的问题。在单元格内,行距正确,但 342 和 35 之间的间距太小。
问题:如何创建具有多行单元格和正确间距/对齐的表格?
对我来说,David 和 Steven 提供的两个答案都同样有效。由于我喜欢尽可能简化文档,因此我决定使用 David 的解决方案,因为不需要额外的软件包。因此,我接受了他的答案。这只是我的个人偏好,并不对解决方案的质量做出评判。
答案1
\documentclass[10pt]{article}
\usepackage[english]{babel}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \begin{tabular}[t]{@{}l@{}} cell221 \\ cell222 \\ \end{tabular} & cell32\\
\begin{tabular}[t]{@{}l@{}} cell131 \\ cell132\end{tabular} & cell23 & cell33\\
cell14 & cell24 & \begin{tabular}[t]{@{}l@{}} cell341 \\ cell342\end{tabular} \\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
& cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
& & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}
答案2
在这里,我用\multirow
stacks 替换了你的。我定义\mrstack{}{}
为重新创建与右手相同的垂直间距tabular
。mrstacks 将左对齐。
\documentclass[10pt]{article}
\usepackage[english]{babel}
\newlength\svblskp
\svblskp=\the\baselineskip\relax
\usepackage{stackengine}
\def\stacktype{L}
\def\mrstack#1#2{\def\stackalignment{l}\stackunder[\svblskp]{\strut#1}{\strut#2}}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \mrstack{cell221}{cell222} & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
& cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
& & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}
虽然上述答案适用于 99% 的情况,但 OP 指出,如果要将tabular
设置为与 不同的字体大小,则\normalsize
上述方法不会产生适当的堆叠间隙。因此,可以使用 来解决这个问题\f@size
:
\documentclass[10pt]{article}
%\usepackage[english]{babel}
\usepackage{stackengine}
\def\stacktype{L}
\newlength\tmplen
\makeatletter
\def\mrstack#1#2{\def\stackalignment{l}%
\tmplen=\f@size pt\relax%
\stackunder[1.2\tmplen]{\strut#1}{\strut#2}}
\makeatother
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \mrstack{cell221}{cell222} & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
& cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
& & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\Huge\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \mrstack{cell221}{cell222} & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
& cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
& & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}