[LaTex 技巧] 表格:字数太多时自动换行(及强制换行方法)

文章目录

    • 前言
    • 具体流程
    • 如何强制换行
    • 参考文献

前言

如题。在表格中,一个格子中文字太多,会直接溢出到文档外面… 如何自动换行,让文字合理的显示,便是本文要解决的问题。

具体流程

1)添加package (备注:在\begin{document}之前):

1
2
% for table
\usepackage{tabularx}

2)在正文中添加如下格式的表格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\begin{table*}[!htbp]
    \centering
    \begin{tabularx}{\textwidth}{lXXX}
        \toprule
        &  \textbf{``long sentence ''} & \textbf{long sentence } & \textbf{Notes}  \\
        \midrule
        Motivation
        & long sentence long sentence long sentence long sentence long sentence
        & long sentence long sentence long sentence long sentence long sentence long sentence long sentencelong sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence
        & long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence long sentence
        \\
        \midrule
        Methodology
        &
        &
        & \\
        \bottomrule
    \end{tabularx}%
    \label{tab:addlabel}%
    \caption{A table with line breaks}
\end{table*}%

备注:关键在于:{lXXX} 以及 tabularx

显示效果如下:
在这里插入图片描述

这就看着很舒服了。

如何强制换行

基于以上代码,使用\newline就可以在表格内强制换行(而不是使用\\)。

参考文献

  • Automatic line breaks in LaTeX tables
  • How to add a forced line break inside a table cell