如何定义具有自定义规则和间距的新表格环境?

如何定义具有自定义规则和间距的新表格环境?

问题

我正在使用以下手动代码创建具有自定义规则和间距的表格。但是,它存在两个问题:

  1. \topspace在每条规则之后和\bottomspace每条规则之前手动添加很不方便。
  2. 我想在表中使用标准的\toprule、、\midrule命令\bottomrule和标准列类型,而不全局覆盖它们的外观。|

如何定义一个自定义mytable环境,自动在规则周围插入自定义间距并暂时覆盖规则宽度,从而产生与手动代码相同的视觉输出?

当前代码

\documentclass{article}
\usepackage{tabularx}

\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{:}{!{\vrule width 0.05em}}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}
\makeatletter
\def\hlinewd#1{%
\noalign{\ifnum0=`}\fi\hrule \@height #1 \futurelet
\reserved@a\@xhline}
\makeatother
\newcommand\topspace{\rule{0pt}{2.6ex}}
\newcommand\bottomspace{\rule[-1.2ex]{0pt}{0pt}}

\begin{document}
\begin{tabular}{: L{6em} : C{6em} C{6em} C{6em} :}
\hlinewd{0.08em}
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third}\topspace\bottomspace \\
\hlinewd{0.05em}
First  & $ 3 $ & $ 5 $ & $ 2 $\topspace \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $\bottomspace \\
\hlinewd{0.05em}
Average & $ 2 $ & $ 2 $ & $ 1 $\topspace\bottomspace \\
\hlinewd{0.08em}
\end{tabular}
\end{document}

具有自定义规则和间距的表格

期望的结果

\documentclass{article}
\usepackage{tabularx}

% TODO: Define custom mytable environment here...

\begin{document}
\begin{mytabular}{| L{6em} | C{6em} C{6em} C{6em} |}
\toprule
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\midrule
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{mytabular}
\end{document}

具有自定义规则和间距的表格

答案1

booktabs有了新的 LaTeX3 包环境,一切都变得轻而易举tabularray

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\NewColumnType{L}[1]{Q[l,#1]}
\NewColumnType{C}[1]{Q[c,#1]}
\NewColumnType{R}[1]{Q[r,#1]}

\begin{document}

\begin{booktabs}{| L{6em} | C{6em} C{6em} C{6em} |}
\toprule
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\midrule
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{booktabs}

\bigskip

\begin{booktabs}{
  colspec = {| L{6em} | C{6em} C{6em} C{6em} |},
  row{1} = {font = \bfseries},
}
\toprule
Column & First & Second & Third \\
\midrule
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{booktabs}

\end{document}

在此处输入图片描述


另外还有一个具有自定义垂直空间的更新示例:

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\NewColumnType{L}[1]{Q[l,#1]}
\NewColumnType{C}[1]{Q[c,#1]}
\NewColumnType{R}[1]{Q[r,#1]}

\usepackage{environ}
\NewEnviron{mytabular}[1]{%
  \setlength{\aboverulesep}{1ex}%
  \setlength{\belowrulesep}{1.5ex}% 
  \begin{booktabs}[expand=\BODY]{#1}
    \BODY
  \end{booktabs}
}

\begin{document}

\begin{mytabular}{| L{6em} | C{6em} C{6em} C{6em} |}
\toprule
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\midrule
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{mytabular}

\bigskip

\begin{mytabular}{
  colspec = {| L{6em} | C{6em} C{6em} C{6em} |},
  row{1} = {font = \bfseries},
}
\toprule
Column & First & Second & Third \\
\midrule
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{mytabular}

\end{document}

在此处输入图片描述

答案2

makecell使用、cellspacenicematrix的一系列替代方案tabularray

\documentclass{article}

\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}

\usepackage{makecell}
\setcellgapes{3pt}

\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{3pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}

\usepackage{nicematrix}

\usepackage{tabularray}

\begin{document}

{
\makegapedcells
\begin{tabular}{| L{6em} | C{6em} C{6em} C{6em} |}
\hline
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\hline
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\hline
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\hline
\end{tabular}
}

\begin{tabular}{| 0{L{6em}} | 0{C{6em}} 0{C{6em}} 0{C{6em}} |}
\hline
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\hline
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\hline
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\hline
\end{tabular}

{
\NiceMatrixOptions{cell-space-limits = 3pt}
\begin{NiceTabular}{| L{6em} | C{6em} C{6em} C{6em} |}
\hline
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\hline
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\hline
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\hline
\end{NiceTabular}
}

\begin{tblr}{| Q[t,l,6em] | Q[t,c,6em] Q[t,c,6em] Q[t,c,6em] |}
\hline
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\hline
First  & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third  & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\hline
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\hline
\end{tblr}
\end{document}

答案3

像这样?我定义了一个环境,删除了 booktabs 规则的垂直填充,并将其替换为\setcellgapes{…}\makegapedcellsfrom malecell

\documentclass{article}
\usepackage{tabularx, booktabs, makecell}
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}

% TODO: Define custom mytable environment here...
\newenvironment{mytable}[1][]{\table[#1]
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setcellgapes{4pt}\makegapedcells}%
{\endtable}

\begin{document}

\begin{mytable}[!h]
\begin{tabular}{!{\vrule width 0.08em}L{6em}!{\vrule width 0.06em} *{3}{C{6em}}!{\vrule width 0.08em}}
\toprule
\textbf{Column} & \textbf{First} & \textbf{Second} & \textbf{Third} \\
\midrule
First & $ 3 $ & $ 5 $ & $ 2 $ \\
Second & $ 1 $ & $ 0 $ & $ 0 $ \\
Third & $ 4 $ & $ 3 $ & $ 2 $ \\
Fourth & $ 0 $ & $ 0 $ & $ 0 $ \\
\midrule
Average & $ 2 $ & $ 2 $ & $ 1 $ \\
\bottomrule
\end{tabular}
\end{mytable}

\end{document} 

在此处输入图片描述

相关内容