我正在用 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}
我们得到:
在常规tabular
环境中,您希望使用p{width}
列类型,如 marcog 所示。但这会迫使您给出明确的宽度。
另一个解决方案是tabularx
环境:
\usepackage{tabularx}
...
\begin{tabularx}{\linewidth}{ r X }
right-aligned foo & long long line of blah blah that will wrap when the table fills the column width\\
\end{tabularx}
所有 X 列的宽度相同。\hsize
您可以通过在格式声明中设置来影响这一点:
>{\setlength\hsize{.5\hsize}} X >{\setlength\hsize{1.5\hsize}} X
但是我想所有的因素都必须加起来为 1(我从 LaTeX 同伴那里得到了这个)。还有一个包tabulary
可以调整列宽以平衡行高。texdoc tabulary
有关详细信息,您可以使用(在 TeXlive 中)获取每个包的文档。
另一种选择是在需要文本换行的每个单元格中插入一个 minipage,例如:
\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}
我喜欢tabulary
包装的简单性:
\usepackage{tabulary}
...
\begin{tabulary}{\linewidth}{LCL}
\hline
Short sentences & \# & Long sentences \\
\hline
This is short. & 173 & This is much loooooooonger, because there are many more words. \\
This is not shorter. & 317 & This is still loooooooonger, because there are many more words. \\
\hline
\end{tabulary}
在该示例中,您根据 \textwidth 排列表格的整个宽度。例如 0.4 个。然后其余的由包自动完成。
像一块蛋糕一样简单!
您可以定义一个新的列类型,例如 (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}
如果要换行但要保持对齐,则可以将该单元格换行在minipage
orvarwidth
环境中(varwidth 来自 varwidth 包)。Varwidth 将“与它的内容一样宽,但不比 X 宽”。您可以创建一个自定义列类型,其行为类似于“p{xx}”,但通过使用缩小以适应
\newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}
这可能需要array
软件包。然后,当您使用\begin{tabular}{llM{2in}}
前两列之类的内容时,我们将正常左对齐,第三列将正常左对齐,但如果它比 2in 宽,则文本将被换行。
要将文本更改为AB
表格A \r B
单元格中的文本,请将其放入单元格位置:\makecell{A \\ B}
。
在此之前,您还需要包含 package makecell
。
新tabularray
功能使在单元格中换行比以往更容易。
该包支持所有传统使用的列名,如c
、l
、r
等,但也有自己的Q
列,它接受各种键来控制宽度和垂直和水平对齐。它还提供了一个X
列,如 tabularx` 中所知,它将自动计算列的宽度以使表格适合可用的文本宽度。
另一个不错的功能是所有设置也可以为单个单元格完成。
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{table}
\begin{tblr}{|c|Q[2cm,valign=m]|X[j,valign=m]|}
\hline
Title 1 & Title 2 & Title 3 \\
\hline
one-liner & multi-line text & multi-line piece of text to show case a multi-line and justified cell \\
\hline
apple & orange & banana \\
\hline
\SetCell{h,2cm} wrapping text only in a single cell & orange & banana \\
\hline
\end{tblr}
\end{table}
\end{document}
(感谢Shayan Amani在他们的回答中提供了 MWE!)
只需使用https://www.tablesgenerator.com/来包装文本,而不是用疯狂的 LaTeX 命令浪费时间。
\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}