【问题标题】:How to wrap text in LaTeX tables?如何在 LaTeX 表格中换行?
【发布时间】:2010-10-21 21:56:01
【问题描述】:

我正在用 LaTeX 创建一个包含几个表格的报告。我坚持这一点,因为表格中的单元格数据超出了页面的宽度。我可以以某种方式包装文本,使其落入表格同一单元格的下一行吗?

它是否与表格的宽度有关?但是由于它超出了页面的宽度,会有所不同吗?

【问题讨论】:

  • 我正在寻找一种包装长词的方法,另一种解决方案更适合我的需求 tex.stackexchange.com/questions/198325/wrap-word-in-table-cell

标签: text latex word-wrap


【解决方案1】:

使用 p{width} 作为列说明符,而不是 l/r/c。

\begin{tabular}{|p{1cm}|p{3cm}|}
  This text will be wrapped & Some more text \\
\end{tabular}

编辑:(基于 cmets)

\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{tabular}{p{1cm}p{3cm}}
  • 有什么方法可以做到这一点,同时仍然在每个单元格中指定对齐方式?
  • 我找到了一种在每个单元格中指定对齐方式的方法!创建宏! \newcolumntype{L}{>{\centering\arraybackslash}m{.8cm}} \begin{table*}[t] %\small \caption{Comparison} \centering %\begin{tabular}{|L|L| L|L|L|L|L|L|L|L|L|L|L|}
  • 如果我有两列,其中第一列的长度应满足其内容的需要,而另一列应在换行时填充到行宽?所以基本上,我需要begin{tabular}{lp{<whatever is left to fill the line width>}}
  • 很好的解决方案。但是,我建议使用相对值而不是任意维度,例如p{0.2\linewidth}p{0.6\linewidth}}
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    简单得像一块蛋糕!

    您可以在保持当前对齐方式crl)的同时定义新的列类型,例如(在本例中为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}
    

    【讨论】:

    • 如何让'多行片...'中的文字居中对齐
    • @Jung 注意L 在对应的列\begin{tabular}{|c|L|L|}
    【解决方案4】:

    我喜欢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 个。然后其余的由包自动完成。

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

    【讨论】:

      【解决方案5】:
      \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}
      

      【讨论】:

      • 投反对票。这是一个纯代码的答案,根本没有任何解释。
      • 此外,这是错误的,因为第二行包含 4 个单元格,其中表格设计为仅容纳两列。无论如何,它与包装单元格内容无关。
      【解决方案6】:

      如果您想换行但保持对齐,则可以将该单元格换行在minipagevarwidth 环境中(varwidth 来自 varwidth 包)。 Varwidth 将“与它的内容一样宽,但不比 X 宽”。您可以创建一个自定义列类型,其行为类似于“p{xx}”,但通过使用

      缩小以适应
      \newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}
      

      这可能需要array 包。然后,当您使用\begin{tabular}{llM{2in}} 之类的内容时,前两列我们正常左对齐,第三列正常左对齐,但如果它变得比 2in 宽,那么文本将被换行。

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

        • 谢谢,这让我可以在我的单元格中放置itemize 列表。
        • 我觉得答案应该能解释\columnwidth的意思:我试过的时候,好像是table-width而不是column-width,所以只好手动设置一个像@这样的比例987654324@ 得到一个合理的宽度。
        【解决方案8】:

        对于常规的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 中)获取每个包的文档。

        【讨论】:

        • 有趣,看起来真的很有用。在选择列宽时有多智能?例如,如果您有两列需要换行,但其中一列的文本比另一列长得多,是否仍将它们分配为相等的宽度?
        • 我编辑了我的答案。但实际上在实践中,我尝试简化我的表格,以便我只需要 X 用于单个列。我刚刚发现了表格 :)
        猜你喜欢
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多