我正在LaTeX中创建一个报告,其中涉及到几个表。我卡住了,因为我在表格中的单元格数据超过了页面的宽度。我能否以某种方式将文本换行,使其落入表中相同单元格中的下一行?

它是否与桌子的宽度有关?但由于它超出了页面的宽度,会有什么不同吗?


当前回答

另一种选择是在每个需要文本换行的单元格中插入一个迷你页,例如:

\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}

其他回答

另一种选择是在每个需要文本换行的单元格中插入一个迷你页,例如:

\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}

在常规的表格环境中,您希望使用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同伴那里得到的)。还有包表,它将调整列宽以平衡行高。有关详细信息,您可以通过texdoc表格(在TeXlive中)获取每个包的文档。

简单得像一块蛋糕!

你可以定义一个新的列类型,比如(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}

我喜欢表格包的简单性:

\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。然后其余的工作由包自动完成。

大部分示例摘自http://en.wikibooks.org/wiki/LaTeX/Tables。

要在表格单元格中将文本AB更改为A \r B,请将其放入单元格位置:\makecell{A \\ B}。

在此之前,还需要包含包makecell。