嵌套表格在单元格中失去顶部对齐并插入额外的换行符

嵌套表格在单元格中失去顶部对齐并插入额外的换行符

当我使用嵌套表格在表格对象中插入换行符时,该行中的其他单元格出现了意外的对齐

例如,在 A 行中,我希望列 1 和列 4 保持与 B 行相同的顶部对齐,并且我当然不希望列 4 换行第三行,而这行中的任何单元格都不需要这样做。我不确定我是否解释得很清楚,但下面的例子应该可以清楚地说明这一点……

\documentclass{article}
\begin{document}
\begin{table}[!ht]
\centering
\caption{my caption}
\begin{tabular}{p{3.5cm}llp{7cm}}
\hline
col 1 & col 2 & col 3 & col 4 \\ \hline
A & \begin{tabular}[c]{@{}l@{}}v1\\ v2\end{tabular} & \begin{tabular}[c]    {@{}l@{}}v3\\ v4\end{tabular} & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\ \hline
B & v5 & v6 & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\ \hline

\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案1

您已使用 要求表格相对于当前基线居中[c]。因此,表格在该基线上居中。要查看此内容,请注意换行符位于A第一列的垂直中心。p{}列类型与顶行对齐,因此此列的第一行也与当前基线对齐。也就是说,您看到的输出正是根据您选择的选项所期望的。

此外,就目前情况而言,当以明显的方式完成创建适当的最小示例时,您的代码也会产生坏框,因此有必要p{}稍微修剪列以使表格适合文本块的宽度。

要修复对齐问题,您可能需要t而不是c。我对此不太确定,因为我不明白您为什么会认为您想要c或您期望输出是什么。我假设您想要这样的东西:

可能期望的输出

\documentclass{article}
\begin{document}
\begin{table}[!ht]
  \centering
  \caption{my caption}
  \begin{tabular}{p{3cm}llp{6cm}}
    \hline
    col 1 & col 2 & col 3 & col 4 \\
    \hline
    A & \begin{tabular}[t]{@{}l@{}}v1\\v2\end{tabular} & \begin{tabular}[t]{@{}l@{}}v3\\v4\end{tabular} & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\
    \hline
    B & v5 & v6 & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\
    \hline
  \end{tabular}
\end{table}
\end{document}

不过,我更推荐这样的东西:

建议输出

当然,前提是第一列确实包含更多内容。否则,您显然应该重新平衡列宽。(在这种情况下,我可能会使用X为最后一列使用表格型以及lll前 3 个。)

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
  \centering
  \caption{my caption}
  \begin{tabular}{p{3cm}llp{6cm}}
    \toprule
    col 1 & col 2 & col 3 & col 4 \\
    \midrule
    A & \begin{tabular}[t]{@{}l@{}}v1\\v2\end{tabular} & \begin{tabular}[t]{@{}l@{}}v3\\v4\end{tabular} & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\
    \cmidrule(lr){1-4}
    B & v5 & v6 & here is a long line that tends to go for somewhat longer than the assigned 7cm that we have assigned it \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

答案2

另一种方法是使用包中的宏来代替嵌套的tabulars :\makecell[lt]{...}makecell

\documentclass{article}
\usepackage{makecell}
\usepackage{caption}

\begin{document}
    \begin{table}[!ht]
    \centering
\caption{my caption}
    \begin{tabular}{p{3.5cm}llp{7cm}}
\hline
col 1   &   col 2   & col 3 & col 4                     \\ \hline
A       &   \makecell[lt]{v1\\ v2} 
                    &   \makecell[lt]{v3\\ v4} 
                            &   here is a long line that tends to go for 
                                somewhat longer than the assigned 7cm that 
                                we have assigned it     \\ \hline
B       &   v5      &   v6  &   here is a long line that tends to go 
                                for somewhat longer than the assigned 7cm that 
                                we have assigned it     \\ \hline
    \end{tabular}
\end{table}
\end{document}

代码更短,更易读(至少在我看来)。宏lt中的选项含义\makecell是:l左对齐(水平)和t上对齐(垂直)。结果是:

在此处输入图片描述

相关内容