制作一个表格,每个框中有几行

制作一个表格,每个框中有几行

我正在尝试制作一个大表格,其中每个框下方都有几行文本。我尝试过\newline进入\linebreak新行,但都不起作用。该命令\\确实给出了新行,但它会结束整个表格中的整行。

有人能建议我如何获得一条新线路,或者使用更好的软件包吗?

答案1

使用parbox{}{}方式如下:

\documentclass{standalone}

\begin{document}

    \begin{tabular}{ll}
        \hline
        row 1, column 1, line 1 & \parbox{5cm}{row 1, column 2, line 1 \\ row 1, column 2, line 2 \\ row 1, column 2, line 3} \\ \hline
        \parbox{5cm}{row 2, column 1, line 1 \\ row 2, column 1, line 2} & row 2, column 2, line 1 \\ 
        \hline
    \end{tabular}

\end{document}

在此处输入图片描述

答案2

有几种可能的解决方案。下面演示了 2 种方法。第一种方法仅依赖于标准命令。虽然我booktabs出于美观原因使用了它,但这对于解决方案来说并不是必不可少的。第二种方法使用tabularx

在表格中获取 <code>\newline</code> 的 2 种方法

\documentclass{article}
\usepackage{tabularx,booktabs}
\begin{document}

One method involves using the standard \verb|p{<width>}| column specifier. This requires knowing how wide you want the columns but it allows you to use \verb|\newline| and does not require additional packages. Table \ref{tab:standard} does use commands from \verb|booktabs| to improve the tabular's appearance but you could replace with \verb|\hline| etc.\ if preferred.
\begin{table}
\centering
\begin{tabular}{*{2}{p{.285\linewidth}}}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabular}
\caption{Tabular with standard commands}\label{tab:standard}
\end{table}
\bigskip

If you don't know how wide the columns should be and don't wish to figure it out, but you can specify the overall width of the tabular, \verb|tabularx| can be used. This supports the \verb|X| column specifier which figures out the width based on the overall tabular width. It also allows \verb|\newline|. Table \ref{tab:tabularx} again uses \verb|booktabs| but that is for merely aesthetic reasons.
\begin{table}
\centering
\begin{tabularx}{.65\linewidth}{XX}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabularx}
\caption{Tabular with tabularx}\label{tab:tabularx}
\end{table}

\end{document}

答案3

前面的链接中没有提到,该makecell包定义了\makecellthead和命令diaghead\multirowcell这些命令接受使用\\并可以进一步定制(选择字体、旋转、垂直空间等)。可以更改 hlines 和 clines 的粗细。

此主题提供其使用示例。

相关内容