換行時的表格行放置

換行時的表格行放置

我有一個包含三列的表,其中包括描述、簡短縮寫和值。描述可能是一個很長的條目,因此我使用 p 列類型自動換行。我使用 longtable 因為最終的表格將跨越幾頁。

目前,我使用這段程式碼

\documentclass{article}
\usepackage{longtable}

\begin{document}

\begin{longtable}{p{0.5\textwidth}cc} 
\hline
Column 2 &  Column 2 & Column 3\\ 
\hline
short entry & Entry s.1 & Value v.s1\\
  & Entry s.2 & Value v.s2 \\
\hline
here is a very long text in the first column that will be broken into multiple rows & Entry l.1 &  Value v.l1 \\
 & Entry l.2 &  Value v.l2 \\
\hline
\end{longtable}

\end{document}

得到這個表:

目前版本

正如您所看到的,如果第一列中的條目很短,則很容易對齊最後兩列。但是,如果第一列中的文字被分成多行,那麼最後兩行中自然會出現類似但現在為空的空格。因此,在範例中,我想要做的是將 Entry l.2 和 Value v.l2 向上「移動」兩「行」。

答案1

您可以使用套件\multirow中的multirow內容將儲存格跨越多行,例如:

\documentclass{article}
\usepackage{longtable}
\usepackage{multirow}

\begin{document}

\begin{longtable}{p{0.5\textwidth}cc} 
\hline
Column 2 &  Column 2 & Column 3\\ 
\hline
short entry & Entry s.1 & Value v.s1\\
  & Entry s.2 & Value v.s2 \\
\hline
\multirow{2}{0.5\textwidth}{here is a very long text in the first column that will be broken into multiple rows} & Entry l.1 &  Value v.l1 \\
 & Entry l.2 &  Value v.l2 \\
 \\
\hline
\end{longtable}

\end{document}

對於文字換行,您需要手動設定列的寬度 - 在範例中設定為.5\textwidth。也可以看看這是「多行列中的文本換行」的答案

編輯: 為了避免手動檢查文字所跨越的行,您可以對最後兩列使用巢狀表,而不是使用\multirow, fe.:

\documentclass{article}
\usepackage{longtable}

\begin{document}

\begin{longtable}{p{0.5\textwidth}c} 
\hline
Column 2 &\begin{tabular}{cc}Column 2 & Column 3\end{tabular}\\ 
\hline
short entry &\begin{tabular}{cc}
    Entry s.1 & Value v.s1\\
    Entry s.2 & Value v.s2
\end{tabular}\\
\hline
here is a very long text in the first column that will be broken into multiple rows &\begin{tabular}{cc}
    Entry l.1 & Value v.l1 \\
    Entry l.2 & Value v.l2
\end{tabular}\\
\hline
\end{longtable}

\end{document}

我想我更願意檢查兩次,因為如果可能的話,我會盡量避免表格中的長文本。

答案2

您也可以使用 booktabs 套件。

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\begin{document}
\begin{longtable}{p{0.5\textwidth}p{3cm} p{3cm}} 
\toprule
Column 1 &  Column 2 & Column 3\\ 
\midrule
short entry & Entry s.1 & Value v.s1\\
& Entry s.2 & Value v.s2 \\
\midrule
There is a very long text in the  & Entry l.1 &  Value v.l1 \\
first column that will be broken  &Entry 1.2 & Value v.12\\
into multi rows&&\\
\bottomrule
\end{longtable}
\end{document}

書本標籤

答案3

由於所有列都是,因此您可以在儲存格內部p{some width}使用,並且行數較少。\newline或者,您可以使用該makecell包,它允許在單元格內換行:

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{makecell}
\renewcommand\cellalign{lt}

\begin{document}

\begin{longtable}{p{0.5\textwidth}p{3cm} p{3cm}}
  \toprule
  Column 1 & Column 2 & Column 3 \\
  \midrule
  short entry & Entry s.1 & Value v.s1 \\
                                                                                    & Entry s.2 & Value v.s2 \\
  \midrule
  There is a very long text in the first column that will be broken into multi rows & \makecell{Entry l.1 & \\Entry 1.2} & \makecell{Value v.l1\\Value v.12} \\
  \addlinespace
  There is a very long text in the first column that will be broken into multi rows & Entry l.1\newline Entry 1.2 & Value v.l1\newline Value v.12 \\
  \bottomrule
\end{longtable}

\end{document} 

在此輸入影像描述

相關內容