LaTex - 表格格式(多列、多行、parbox)

LaTex - 表格格式(多列、多行、parbox)

我创建了下面的表格。但是,有一些格式问题我无法修复。

桌子看起来像这样 atm

在第一行中,“内容”和“这是一个非常”不是从同一个地方开始的。

最后一列第一行的内容横跨整行。实际上,第一行应该简单地适应内容,即自行扩展

左列的文本始终位于中间列的文本下方。但它们应从同一高度开始

“副标题 1”与其下方文本之间的间距大于“这是一个非常...长的居中副标题 2...”与其下方文本之间的间距。

\documentclass[9pt,twocolumn]{article}

\usepackage{tabularx}
\usepackage[format=plain, indention=1cm]{caption}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

Here some text.

%.......................................Table..................................
\begin{table*}[!htb]
\captionsetup{size=footnotesize}
\caption{Heading}
\label{tab:Test1}
\setlength\tabcolsep{0pt}
 
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}p{2cm}p{5cm}p{5cm}p{5cm}}

\hline \toprule
Col 1 & \multicolumn{2}{c}{Col 2} & Col 3\\
\hline \midrule

\multirow{4}{*}{\parbox{1.5cm}{This is row 1}}
  & \multicolumn{2}{c}{Subheading 1}
  & \multirow{4}{*}{\parbox{4.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text \\ with a line break \\ and a second one}}\\
 
& \parbox[t][][t]{4.5cm}{content, content, content, content, content, content, content, content, content, content, content}
& \parbox[t][][t]{4.5cm}{content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content} & \\

  & \multicolumn{2}{c}{\parbox[t][][t]{10cm}{\centering This is a very very very very very long centered Subheading 2 over two lines}} & \\
 
& \multicolumn{2}{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & \\

\hline

    \multirow{3}{*}{\parbox[t][][c]{1.5cm}{This is row 2}}
  & \multicolumn{2}{c}{\parbox[t][][c]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}}
 
    & \multirow{6}{*}{\parbox{4.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} \\
  & \multicolumn{2}{c}{\parbox[t][][t]{2cm}{\centering Subheading 3}} & \\
 
& \multicolumn{2}{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & \\

\cline{1-3}

  \multirow{3}{*}{\parbox[t][][c]{1.5cm}{This is row 3}}
  & \multicolumn{2}{c}{\parbox[t][][c]{9.5cm}{fleet, older generation, preceding generation, previous generation}} & \\
  & \multicolumn{2}{c}{\parbox[t][][t]{2cm}{\centering Subheading 4}} & \\
& \multicolumn{2}{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & \\

\bottomrule
\hline

\end{tabular*}
\end{table*}
%................
\end{document}

答案1

对于复杂的表格,这multirow不是正确的解决方案。它超出了它的能力,因为它实际上是按行计算的,而不是按多行表格行计算的。

该包tabularray更适合这些类型的表。这里有一个解决方案。它可能需要一些微调。例如,一些\parboxes 可能被删除。

我把大多数\multirow参数放在括号中,尽管它们并不需要这样。

\documentclass[twocolumn]{article}

\usepackage[format=plain, indention=1cm]{caption}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

Here some text.

%.......................................Table..................................
\begin{table*}[!htb]
\captionsetup{size=footnotesize}
\caption{Heading}
\label{tab:Test1}
 
\begin{tblr}{colspec={p{1.5cm} l l p{4.5cm}},colsep=2pt,width=\textwidth}
  \toprule
  Col 1 & \SetCell[c=2]{c}{Col 2} & & Col 3\\
  \midrule
  \SetCell[r=4]{l}This is row 1 & \SetCell[c=2]{c}Subheading 1 & &
      \SetCell[r=4]{l}\parbox{4.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text \\ with a line break \\ and a second one} \\
& \parbox[t][][t]{4.5cm}{content, content, content, content, content, content, content, content, content, content, content}
& \parbox[t][][t]{4.5cm}{content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content} & \\
  & \SetCell[c=2]{c}{\parbox[t][][t]{10cm}{\centering This is a very very very very very long centered Subheading 2 over two lines}} & & \\
& \SetCell[c=2]{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & \\
\hline
    \SetCell[r=3]{l}This is row 2
  & \SetCell[c=2]{c}{\parbox[t][][c]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}}
  &
    & \SetCell[r=6]{l}{\parbox{4.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} \\
  & \SetCell[c=2]{c}{\parbox[t][][t]{2cm}{\centering Subheading 3}} & & \\
  & \SetCell[c=2]{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & & \\
\cline{1-3}
  \SetCell[r=3]{l}This is row 3
  & \SetCell[c=2]{c}{\parbox[t][][c]{9.5cm}{fleet, older generation, preceding generation, previous generation}} & & \\
  & \SetCell[c=2]{c}{\parbox[t][][t]{2cm}{\centering Subheading 4}} & \\
& \SetCell[c=2]{c}{\parbox[t][][t]{9.5cm}{This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text}} & & \\

\bottomrule

\end{tblr}

\end{table*}
%................
\end{document}

在此处输入图片描述

答案2

  • 如果您愿意使用tabularray包,那么您的表格并不难写。
  • 您的表格示例非常庞大,无法简单化。要将其插入文档,您可以采取以下方法:
    • 表格写成跨两页的长表格。在这种情况下,tblr应该使用longtblr表格环境。但是,现在表格不必封装在table浮动中,您需要更改
    • 通过使用geometry包来增加文本区域的大小。
    • 使用较小的字体大小,例如,footnotesize也减少\baselineskip
  • 如果实际的桌子不是那么大,则不需要上述测量。

长表解决方案:

\documentclass[twocolumn]{article}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

%\usepackage[format=plain, indention=1cm]{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, varwidth}

\begin{document}
\onecolumn
\begin{longtblr}[
caption = {Heading},
  label = {tab:Test1}
                ]{%
        colspec={@{} Q[l, 3em] X[j] X[j] X[j] @{}},
        row{1} = {font=\bfseries, c},
        hspan = minimal,
             }
    \toprule
Col 1   &   \SetCell[c=2]{c}   Col 2
            &   &   Col 3           \\
    \midrule
        & \SetCell[c=2]{c}  Subheading 1
            &   &                   \\
    \midrule[dashed]
\SetCell[r=2]{h}  This is row 1
        & content, content, content, content, content, content, content, content, content, content, content
            &   content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content
                &   \SetCell[r=2]{l}  {This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text\\
            with a line break           \\
            and a second one}           \\
        &   &   &                       \\
        & content, content, content, content, content, content, content, content, content, content, content
            &   content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content
                &                       \\
    \midrule    
    \pagebreak  % <--- insert manualy
        &   \SetCell[c=2]{c}  This is a very very very very very long centered Subheading 2 over two lines
            &   &                       \\
    \midrule[dashed]
\SetCell[r=2]{h}  This is row 2
        &   \SetCell[c=2]{j}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &   \SetCell[r=3]{l}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text\\
        &   \SetCell[c=2]{c}  Subheading 3
            &   &                       \\
        &   \SetCell[c=2]{j}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &                       \\
    \midrule
\SetCell[r=2]{h}  This is row 3
        &   \SetCell[c=2]{c}  fleet, older generation, preceding generation, previous generation
            &   &                       \\
        &   \SetCell[c=2]{c}  Subheading 4
            &   &                       \\
        &   \SetCell[c=2]{c}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &                       \\
    \bottomrule
\end{longtblr}
\end{document}

在此处输入图片描述

(红线显示页面布局)。

在以下 MWE 中,将考虑上述选项中的最后一个。表格以table*浮点形式插入:

\documentclass[twocolumn]{article}

\usepackage[format=plain, indention=1cm]{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, varwidth}

\begin{document}
    \begin{table*}[!htb]
\caption{Heading}
\label{tab:Test1}

\begin{tblr}{colspec={@{} Q[l, 3em] X[j] X[j] X[j] @{}},
             row{1} = {font=\bfseries, c}, 
             hspan = minimal, 
             rows = {font=\footnotesize\linespread{0.84}\selectfont}
             }
    \toprule
Col 1   &   \SetCell[c=2]{c}   Col 2   
            &   &   Col 3           \\
    \midrule
        & \SetCell[c=2]{c}  Subheading 1
            &   &                   \\          
\SetCell[r=2]{h}  This is row 1
        & content, content, content, content, content, content, content, content, content, content, content
            &   content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content
                &   \SetCell[r=2]{l}  {This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text\\
            with a line break\\
            and a second one}           \\
        &   &   &       \\
        & content, content, content, content, content, content, content, content, content, content, content 
            &   content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content, content
                &                       \\
    \midrule
        &   \SetCell[c=2]{c}  This is a very very very very very long centered Subheading 2 over two lines
            &   &                       \\
\SetCell[r=2]{h}  This is row 2 
        &   \SetCell[c=2]{j}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &   \SetCell[r=3]{l}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text\\
        &   \SetCell[c=2]{c}  Subheading 3
            &   &                       \\
        &   \SetCell[c=2]{j}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &                       \\
    \midrule
\SetCell[r=2]{h}  This is row 3
        &   \SetCell[c=2]{c}  fleet, older generation, preceding generation, previous generation
            &   &                       \\
        &   \SetCell[c=2]{c}  Subheading 4
            &   &                       \\
        &   \SetCell[c=2]{c}  This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
            &   &                       \\
    \bottomrule
\end{tblr}
    \end{table*}
\end{document}

在此处输入图片描述

相关内容