製作一個表格,每個方塊中有幾行

製作一個表格,每個方塊中有幾行

我正在嘗試製作一張大桌子,其中每個框的下方都有幾行文字。我嘗試過\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它們接受使用\\並且可以進一步自訂(選擇字體、旋轉、垂直空格等)。人們可以改變斜線和斜線的厚度。

這個線程有一個使用的例子。

相關內容