显示两列表格

显示两列表格

我有一张长表,大约有 50 行,有两列。由于它很窄,我想将其分成两列(每列包含表格的两列),两列之间留有空格,如图所示(线条代表表格中的内容)。

一种方法是制作一个有 5 列的表格,然后手动将数据输入到我想要的位置,中间一列留空,以在两部分之间留出空间。但是,我想知道是否有办法自动完成此操作,这样如果我添加任何数据,它就会自动放置在应有的位置。我还希望能够添加\toprule\midrule\bottomrule在两列之间留出一个空格。

如果有任何区别,我希望它出现在包含不应出现在列中的文本的页面上。

在此处输入图片描述

下面是包含我所描述的方法和常规表的示例代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multicol}


\begin{document}

\begin{center}
  \begin{longtable}{ l l p{1cm} l l }
    \caption{Caption.}\\
    \toprule
    Column 1 & Column 2 &  & Column 1 & Column 2 \\
    \midrule
    \endfirsthead

    \midrule
    Column 1 & Column 2 &  & Column 1 & Column 2 \\
    \midrule
    \endhead
    
%    \bottomrule
%    \endfoot

      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      data1 & data2 &  & data1 & data2\\
      \bottomrule
  \end{longtable}
\end{center}


\begin{center}
  \begin{longtable}{ l l }
    \caption{Caption.}\\
    
    \toprule
    Column 1 & Column 2 \\
    \midrule
    \endfirsthead

    \midrule
    Column 1 & Column 2 \\
    \midrule
    \endhead
    
%    \bottomrule
%    \endfoot

      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      data1 & data2 \\
      \bottomrule
  \end{longtable}
\end{center}

\end{document}

答案1

您可以使用 LaTeX3 编程层将单元格分成两列:


第二个答案

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{caption,float}

\usepackage{tabularray}
\NewTableCommand{\toprule}{\cline[0.08em]{1-2}\cline[0.08em]{4-5}}
\NewTableCommand{\bottomrule}{\cline[0.08em]{1-2}\cline[0.08em]{4-5}}
\NewTableCommand{\midrule}{\cline[0.05em]{1-2}\cline[0.05em]{4-5}}

\ExplSyntaxOn
\NewDocumentEnvironment{mytabular}{+b}
  {
    \seq_set_split:Nnn \l_tmpa_seq { \\ } {#1}
    \int_set:Nn \l_tmpa_int { \int_div_round:nn { \seq_count:N \l_tmpa_seq - 1 } {2} }
    \tl_set:Nn \l_tmpa_tl
      {
        \toprule
        Column 1 & Column 2 & & Column 1 & Column 2 \\
        \midrule
      }
    \int_step_inline:nn { \l_tmpa_int }
      {
        \tl_put_right:Nx \l_tmpa_tl
          {
            \seq_item:Nn \l_tmpa_seq {##1} \exp_not:n{ & & }
            \seq_item:Nn \l_tmpa_seq {##1 + \l_tmpa_int } \exp_not:n{ \\ }
          }
      }    
    \tl_put_right:Nn \l_tmpa_tl
      {
        \bottomrule
      }     
    \begin{tblr}[expand=\l_tmpa_tl]{l l p{1cm} l l}
      \l_tmpa_tl
    \end{tblr}
  } { }
\ExplSyntaxOff

\begin{document}

Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text

\begin{table}[H]
\centering
\caption{Table Caption}
\begin{mytabular}
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
\end{mytabular}
\end{table}
    
Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text

\end{document}

在此处输入图片描述


第一个答案

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption,float}

\ExplSyntaxOn
\NewDocumentEnvironment{mytabular}{+b}
  {
    \seq_set_split:Nnn \l_tmpa_seq { \\ } {#1}
    \int_set:Nn \l_tmpa_int { \int_div_round:nn { \seq_count:N \l_tmpa_seq - 1 } {2} }
    \begin{tabular}{l l p{1cm} l l}
    \toprule
    Column 1 & Column 2 & & Column 1 & Column 2 \\
    \midrule
    \int_step_inline:nn { \l_tmpa_int }
      {
        \seq_item:Nn \l_tmpa_seq {##1} & & \seq_item:Nn \l_tmpa_seq {##1 + \l_tmpa_int }
        \int_compare:nNnTF {##1} = {\l_tmpa_int} { \\ \bottomrule } { \\ }
      }
    \end{tabular}
  } { }
\ExplSyntaxOff

\begin{document}

Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text

\begin{table}[H]
\centering
\caption{Table Caption}
\begin{mytabular}
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
  data1 & data2 \\
\end{mytabular}
\end{table}
    
Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text

\end{document}

在此处输入图片描述

相关内容