表格中的自动换行

表格中的自动换行

我需要一个简短的表格添加到我的文档中。恐怕 LaTeX 似乎不会在空格处换行。我的表格目前看起来像这样:

\begin{table}[!ht]
    \centering
    \begin{tabular}{|c|c|c|}\hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3}  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
    \end{tabular}
    \caption{This is the caption}
\end{table}

有人知道快速解决这个问题的方法吗?

答案1

我建议您使用一个tabularx环境,将其宽度设置为\textwidth,并使用列类型的修改版本,X以便列的内容居中而不是完全对齐。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X} % centered "X" column
\begin{document} 

\begin{table}[!ht]
\setlength\extrarowheight{2pt} % for a bit of visual "breathing space"
\begin{tabularx}{\textwidth}{|C|C|C|}
\hline
\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3}  \\\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
\end{tabularx}
\caption{This is the caption}
\end{table}
\end{document}

答案2

makecell软件包thead及其\makecell命令用于以下目的:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier, erewhon}
\usepackage{geometry} \usepackage{array, makecell}%
\setcellgapes{4pt}

\begin{document}

\begin{table}[!ht]
    \centering\makegapedcells
    \begin{tabular}{|c|c|c|}\hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} &\makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} &\makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} \\
        \hline
    \end{tabular}
    \caption{This is the caption}
\end{table}

\end{document} 

在此处输入图片描述

答案3

虽然我觉得有点晚了,但是我目前遇到了同样的问题,并通过 ShareLaTeX 示例解决了它。

这是一个对我有用的简短示例:

\documentclass{article}

%-----------------PREAMBLE-START

\usepackage[utf8]{inputenc}

\usepackage{tabu}

%-----------------PREAMBLE-END

\begin{document}

\listoftables

\newpage

    %Example of table with fixed length (tabu package required)
    \begin{table}[htbp]
    \caption{Advantages and drawbacks for using an commercial audio cable}
    \begin{tabu} to 1\textwidth { | X[l] | X[l] | }
       \hline
       \textbf{Advantages} & \textbf{Drawbacks} \\
       \hline
       no annoying sound anymore & signal transmission not anymore on 'real' material as with piezo-transducers \\
        \hline
        fast and easy rebuild of replica  & no control of build-in DAC at laptop's jack output, like sampling rate\\
        \hline
        replaces signal and noise generators a input equipment & not enough control when converting data to audio file \\
        \hline
        can still be used with piezo-transducers & annoying sound if again used with piezo-transducers \\
        \hline
   \end{tabu}
   \label{tab:audio}
   \end{table}

\end{document}

以下是我的 ShareLaTeX 项目空间上的示例链接: https://www.sharelatex.com/project/58a0600c1eeba6d05d1c28aa

或者对我来说它是这样的:

这对我有用,我认为重要的是不是所有的行都有两行,否则表格看起来很奇怪

相关内容