代码之家  ›  专栏  ›  技术社区  ›  Arnkrishn

如何在LaTeX表中包装文本?

  •  366
  • Arnkrishn  · 技术社区  · 17 年前

    我正在用LaTeX创建一个包含几个表的报告。因为我在表格中的单元格数据超过了页面的宽度,所以我被卡在上面了。我是否可以将文本包装起来,使其落在表的同一单元格中的下一行?

    7 回复  |  直到 7 年前
        1
  •  625
  •   Thamme Gowda    5 年前

    使用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}
    

    我们得到:

    enter image description here

        2
  •  155
  •   Damien Pollet    17 年前

    与常规 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
    

    tabulary 这将调整列宽以平衡行高。有关详细信息,您可以使用获取每个包的文档 texdoc tabulary (现场直播)。

        3
  •  56
  •   Neil Rubens    16 年前

    \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}
    
        4
  •  43
  •   ozi    12 年前

    我喜欢简单的生活 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
  •  34
  •   Shayan Amani    7 年前

    简单得像一块蛋糕!

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

    enter image description here

        6
  •  7
  •   cheshirekow    13 年前

    如果您希望包装文本但保持对齐,则可以将该单元格包装为 minipage 或 varwidth

    \newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}
    

    这可能需要 array \begin{tabular}{llM{2in}} 前两列为正常左对齐,第三列为正常左对齐,但如果宽度超过2in,则文本将被包装。

        7
  •  2
  •   FlowerBeach    7 年前

    AB 进入 A \r B 在表格单元格中,将其置于单元格位置: \makecell{A \\ B} .

    makecell .

        8
  •  -14
  •   Md Monjur Ul Hasan    13 年前
    \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}
    
    推荐文章