Text 如何在LaTeX表中包装文本?
我正在用LaTeX创建一个包含几个表的报告。因为我在表格中的单元格数据超过了页面的宽度,所以我被卡在上面了。我是否可以将文本包装起来,使其落在表的同一单元格中的下一行Text 如何在LaTeX表中包装文本?,text,latex,word-wrap,Text,Latex,Word Wrap,我正在用LaTeX创建一个包含几个表的报告。因为我在表格中的单元格数据超过了页面的宽度,所以我被卡在上面了。我是否可以将文本包装起来,使其落在表的同一单元格中的下一行 它是否与桌子的宽度有关?但是,由于它超出了页面的宽度,它会有所不同吗?使用p{width}作为列说明符,而不是l/r/c \begin{tabular}{|p{1cm}|p{3cm}|} This text will be wrapped & Some more text \\ \end{tabular} 编辑:(
它是否与桌子的宽度有关?但是,由于它超出了页面的宽度,它会有所不同吗?使用p{width}作为列说明符,而不是l/r/c
\begin{tabular}{|p{1cm}|p{3cm}|}
This text will be wrapped & Some more text \\
\end{tabular}
编辑:(根据评论) 我们得到:
对于常规的
表格
环境,您希望使用p{width}
列类型,如marcog所示。但这迫使你给出明确的宽度
另一个解决方案是tablerx
环境:
\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中)获取每个软件包的文档。另一个选项是在需要文本包装的每个单元格中插入迷你页,例如:
\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}
如果要包装文本但保持对齐,则可以在
minipage
或varwidth
环境中包装该单元格(varwidth来自varwidth包)。Varwidth将“与内容一样宽,但不超过X”。您可以创建一个自定义列类型,其行为类似于“p{xx}”,但通过使用
\newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}
\newcolumntype{M}[1]{>{\begin{varwidth}[t]{{1}}l我喜欢表格
包的简单性:
\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
):
要将表格单元格中的文本AB
更改为A\r B
,请将其置于单元格位置:\makecell{A\\B}
在这样做之前,您还需要包括packagemakecell
,这看起来非常有用。当选择列宽时,它有多智能?例如,如果您有两列需要包装,但其中一列的文本比另一列长得多,是否仍然将它们等宽?我编辑了我的答案。B但实际上,在实践中,我尝试简化我的表格,这样我只需要X来表示一列。我刚刚发现了表格:)谢谢,这允许我在单元格中放置逐项列出列表。我认为答案应该解释\columnwidth
的含义:当我尝试时,似乎是表格宽度大于列宽,所以我必须设置一个手动比例,如0.2\columnwidth
,以获得合理的宽度。这是一个很好的解决方案,但如果您不想在表格周围添加边框,则会丢失“|”。它将变成\begin{tabular}{p{1cm}p{3cm}
在每个单元格中指定对齐方式的同时,有没有办法做到这一点?我找到了在每个单元格中指定对齐方式的方法!创建一个宏!\newcolumntype{L}{>{\centering\arraybackslash}m{.8cm}\begin{table*}[t]\small\caption{Comparison}\centering%\begin begin{tabler}{如果我有两列,其中第一列的长度应与容纳其内容所需的长度相同,而另一列的长度应与换行时的线宽相同,那该怎么办?因此,基本上,我需要begin{tabular}{lp{}
Great solution。不过,我建议使用相对值而不是任意维度,例如p{0.2\linewidth}p{0.6\linewidth}
我正在寻找一种包装长单词的方法,另一种解决方案更适合我的需要tex.stackexchange.com/questions/198325/wrap-word-in-table-cellDownvote。这是一个只包含代码的答案,没有任何解释。此外,这是错误的,因为第二行包含4个单元格,而表的设计仅容纳两列。无论如何,它与包装单元格内容无关。如何使文本位于“多行块…”中心align@Jung请注意,在相应的列\begin{tabular}{c | L | L | L}
\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}
\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}
\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}