我正在LaTeX中创建一个报告,其中涉及到几个表。我卡住了,因为我在表格中的单元格数据超过了页面的宽度。我能否以某种方式将文本换行,使其落入表中相同单元格中的下一行?
它是否与桌子的宽度有关?但由于它超出了页面的宽度,会有什么不同吗?
我正在LaTeX中创建一个报告,其中涉及到几个表。我卡住了,因为我在表格中的单元格数据超过了页面的宽度。我能否以某种方式将文本换行,使其落入表中相同单元格中的下一行?
它是否与桌子的宽度有关?但由于它超出了页面的宽度,会有什么不同吗?
当前回答
使用p{width}作为列说明符,而不是l/r/c。
\begin{tabular}{|p{1cm}|p{3cm}|}
This text will be wrapped & Some more text \\
\end{tabular}
编辑:(根据评论)
\begin{table}[ht]
\centering
\begin{tabular}{p{0.35\linewidth} | p{0.6\linewidth}}
Column 1 & Column2 \\ \hline
This text will be wrapped & Some more text \\
Some text here & This text maybe wrapped here if its tooooo long \\
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
我们得到:
其他回答
使用p{width}作为列说明符,而不是l/r/c。
\begin{tabular}{|p{1cm}|p{3cm}|}
This text will be wrapped & Some more text \\
\end{tabular}
编辑:(根据评论)
\begin{table}[ht]
\centering
\begin{tabular}{p{0.35\linewidth} | p{0.6\linewidth}}
Column 1 & Column2 \\ \hline
This text will be wrapped & Some more text \\
Some text here & This text maybe wrapped here if its tooooo long \\
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
我们得到:
\begin{table}
\caption{ Example of force text wrap}
\begin{center}
\begin{tabular}{|c|c|}
\hline
cell 1 & cell 2 \\ \hline
cell 3 & cell 4 & & very big line that needs to be wrap. \\ \hline
cell 5 & cell 6 \\ \hline
\end{tabular}
\label{table:example}
\end{center}
\end{table}
另一种选择是在每个需要文本换行的单元格中插入一个迷你页,例如:
\begin{table}[H]
\begin{tabular}{l}
\begin{minipage}[t]{0.8\columnwidth}%
a very long line a very long line a very long line a very long line
a very long line a very long line a very long line a very long line
a very long line a very long line a very long line %
\end{minipage}\tabularnewline
\end{tabular}
\end{table}
简单得像一块蛋糕!
你可以定义一个新的列类型,比如(L),同时保持当前的对齐(c, r或L):
\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}
\begin{document}
\begin{table}
\begin{tabular}{|c|L|L|}
\hline
Title 1 & Title 2 & Title 3 \\
\hline
one-liner & multi-line and centered & \multicolumn{1}{m{3cm}|}{multi-line piece of text to show case a multi-line and justified cell} \\
\hline
apple & orange & banana \\
\hline
apple & orange & banana \\
\hline
\end{tabular}
\end{table}
\end{document}
如果你想要包装你的文本,但保持对齐,那么你可以包装单元格在一个迷你页面或varwidth环境(varwidth来自varwidth包)。Varwidth将“与内容一样宽,但不超过X”。您可以创建一个自定义列类型,它的作用类似于“p{xx}”,但是使用
\newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}
这可能需要数组包。然后当你使用像\begin{tabular}{llM{2in}}这样的东西时,前两列我们是正常的左对齐,第三列将是正常的左对齐,但如果它超过2in,那么文本将被换行。