如何在表格中的多行中断开线条

43
我找不到如何在表格的多行中断行。在我的表格中,一个单元格应该有两行宽,并且应该有一个换行符来保持文本与其他单元格的重叠。
有什么建议吗?
代码示例:
\begin{center}
    \begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
    \hline
    \multirow{2}{*}{Long text to break} % HERE IS A PROBLEM
        & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
    \\ \cline{2-6}
        & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
\hline
\end{tabular}
\end{center}
5个回答

42

p列和\parbox也可以使用:

\usepackage{multirow}

\begin{document}
\begin{center}
\begin{tabular}{|p{1.5cm}|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{\parbox{1.5cm}{Long text to break}}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ 
    \hline
    \hline
\end{tabular}
\end{center}
\end{document}

parbox in latex document


1
谢谢,对我很有效。但是我认为你应该使用\parbox而不是TeX的\vbox - Florian Bw
谢谢!我已更新答案。Parbox 可以处理文字上方的空间。 - Tombart
你好,有没有可能将“to break”这句话居中而不是左对齐? - lllllllllllll
4
只需在 parbox 的内容中加入 \centering 即可实现居中对齐,例如:{\centering 长文本内容} - Tombart

17

你可以尝试使用minipage

\begin{center}
\begin{tabular}{|l|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{\begin{minipage}{0.5in}Long text to break\end{minipage}}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ 
    \hline
    \hline
\end{tabular}
\end{center}
然而,在你的情况下,我的建议只是放松其他列的限制,因为那里浪费了太多的空间。每个 p {} 强制其他列具有特定宽度,因此第一列没有足够的空间。
当我编译以下代码时,它看起来整齐美观:
\begin{center}
\begin{tabular}{|l|l|l|l|l|l|}
    \hline
    \multirow{2}{*}{Long text to break}
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3} \\
    \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\
    \hline
    \hline
\end{tabular}
\end{center}

我不能在tabular命令中只使用“l”参数,因为单元格太宽了,但是“\begin{minipage}{2cm}Long text to break\end{minipage}”帮了我一个大忙,谢谢。 - kokosing

16

对我来说,最简短和最实用的答案是:

{width}参数中使用\linewidth作为长度。

\usepackage{multirow}
\begin{document}

\begin{center}
\begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\hline
\multirow{2}{\linewidth}{Long text to break} % HERE IS A PROBLEM
    & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
\\ \cline{2-6}
    & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
\hline
\end{tabular}
\end{center}

\end{document}

就是这样了!

唯一可能的问题是,如果其他单元格中的文本真的很短,它可能看起来像这样: Broken text in the right width but sadly going out of the table

然而,如果通常您的表格在其他单元格中有更多文本而不仅仅是“sth1”,那么它会看起来很棒: enter image description here


1
如何防止多行列中的溢出? - Midiparse
@Midiparse 很抱歉我无法给你答案。一个可能的解决方案是将你的文本放入一个框中,并在multirow环境中使用此框[仅供参考(!)]。 - loved.by.Jesus

7

我发现使用“multirow”的内置命令非常有效 - {*}代表“{width}”


1

Also, using parbox and \\:

\documentclass{article}
\usepackage{multirow}

\begin{document}

\begin{center}
    \begin{tabular}{|p{1cm}|p{2.5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
        \hline
        \multirow{2}{*}{\parbox{1cm}{Long\\ text\\ to\\ break}} % NOT A PROBLEM?
        & Thing  & \multicolumn{2}{|c|}{Thing 2} & \multicolumn{2}{|c|}{Thing 3}    
        \\ \cline{2-6}
        & sth 1 & sth 1 & sth 2 & sth 1  & sth 2 \\ \hline
        \hline
    \end{tabular}
\end{center}

\end{document}

请务必小心,不要超出单元格的边界。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接