带有 booktabs 的特殊 cmidrule - 使用 cmidrule 进行颜色和自定义间距

带有 booktabs 的特殊 cmidrule - 使用 cmidrule 进行颜色和自定义间距

我想创建一个灰色的 cmidrule a la booktabs。我gmidrule通过以下方式创建了一个灰色的 midrule(延伸整个表格宽度),具有不同的更紧密的空间

\newcommand{\gmidrule{\arrayrulecolor{lightgray}\specialrule{\lightrulewidth}{0.4\aboverulesep}{0.6\belowrulesep}\arrayrulecolor{black}}

现在,我想应用浅灰色和如上所示的相同分离,作为cmidrule(部分规则),并将此变化称为gcmidrule

第一个问题是 cmidrules 是交错的。我想解决这个问题,但也要调整上下规则。

\documentclass{article}

\usepackage{xparse}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\newcommand{\gmidrule}{\arrayrulecolor{lightgray}\specialrule{\lightrulewidth}{0.4\aboverulesep}{0.6\belowrulesep}\arrayrulecolor{black}}

\NewExpandableDocumentCommand{\gcmidrule}{ O{} D(){} m }{%
    %% I would like to reduce the distance here as well, but the midrules are staggered
    \arrayrulecolor{lightgray}%
    \cmidrule{#3}%
    %  \cmidrule[#1](#2){#3}% supposed to be like this, but causes error. I'll worry about this later
    \arrayrulecolor{black}%
}

\begin{document}

    \begin{tabular}{lllll}\toprule
        a & b & c & d & e \\\gmidrule
        a & b & c & d & e \\\gcmidrule{1-2}\gcmidrule{4-5} % notice the stagger
        a & b & c & d & e \\\bottomrule
    \end{tabular}

\end{document}

更新

这个问题有点帮助,因为它有助于对齐 cmidrules,但它不会调整上下 sep:当使用不同颜色的 cmidrule 时,如何省略垂直重新调整?

%% added this
% Correct for \cmidrule colour adjustment/vertical skip
\newcommand{\corcmidrule}[1][0pt]{% \corcmidrule[<len>]
  \\[\dimexpr-\normalbaselineskip-\belowrulesep-\aboverulesep-#1\relax]%
}

%% add this in gcmidrule

\corcmidrule%

静叶交错,并调整[0pt]静叶交错。

在此处输入图片描述

答案1

\cmidrule以检查是否还有更多 s 跟随的方式编写\cmidrule。如果是,它会故意执行垂直重新调整,以便\cmidrules 跟随在同一行上。因此使用(例如)

\gcmidrule[<A>](<B>){<C>}\gcmidrule[<D>](<E>){<F>}`

按照你的方式可以有效地转化为

\arrayrulecolor{lightgray}% ----\
\cmidrule[<A>](<B>){<C>}%       | = First \gcmidrule
\arrayrulecolor{black}% --------/
\arrayrulecolor{lightgray}% ----\
\cmidrule[<D>](<E>){<F>}%       | = Second \gcmidrule
\arrayrulecolor{black}% --------/

虽然来回切换颜色并没有错,但第一个颜色\cmidrule就是没有发现后面跟着另一个颜色\cmidrule。因此,没有垂直校正。

最简单、最有意义的方法是复制原始定义 if \cmidrule(和附属命令)并将它们更改为显示为\gcmidrule(和附属命令),包括“提前检查”部分。

在此处输入图片描述

\documentclass{article}

\usepackage{booktabs}
\usepackage[table]{xcolor}

\newcommand{\gmidrule}{%
  \arrayrulecolor{lightgray}%
  \specialrule{\lightrulewidth}{0.4\aboverulesep}{0.6\belowrulesep}%
  \arrayrulecolor{black}%
}

% Copy \cmidrule from
%  http://mirrors.ctan.org/macros/latex/contrib/booktabs/booktabs.dtx
% and modify it for insertion of \arrayrulecolor{lightgray} at the start,
% and \arrayrulecolor{black} at the end
\makeatletter
\def\gcmidrule{\arrayrulecolor{lightgray}% Switch colour to lightgray
    \noalign{\ifnum0=`}\fi
    \@ifnextchar[{\@gcmidrule}{\@gcmidrule[\cmidrulewidth]}}
\def\@gcmidrule[#1]{\@ifnextchar({\@@gcmidrule[#1]}{\@@gcmidrule[#1]()}}
\def\@@gcmidrule[#1](#2)#3{\@@@gcmidrule[#3]{#1}{#2}}
\def\@@@gcmidrule[#1-#2]#3#4{\global\@cmidla#1\relax
    \global\advance\@cmidla\m@ne
    \ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else
    \global\let\@gtempa\@cmidruleb\fi
    \global\@cmidlb#2\relax
    \global\advance\@cmidlb-\@cmidla
    \global\@thisrulewidth=#3
    \@setrulekerning{#4}
    \ifnum\@lastruleclass=\z@\vskip \aboverulesep\fi
    \ifnum0=`{\fi}\@gtempa
    \noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xgcmidrule}
\def\@xgcmidrule{%
   \ifx\@tempa\gcmidrule
       \vskip-\@thisrulewidth
       \global\@lastruleclass=\@ne
   \else \ifx\@tempa\morecmidrules
       \vskip \cmidrulesep
       \global\@lastruleclass=\@ne\else
       \vskip \belowrulesep
       \global\@lastruleclass=\z@
   \fi\fi
   \ifnum0=`{\fi}
  \arrayrulecolor{black}}% Switch colour back to black
\makeatother

\begin{document}

\begin{tabular}{ *{5}{c} }
  \toprule
  a & b & c & d & e \\
  \gmidrule
  a & b & c & d & e \\
  \gcmidrule{1-2}\gcmidrule{4-5}
  a & b & c & d & e \\
  \bottomrule
\end{tabular}

\end{document}

答案2

我将使用新的 LaTeX3 包给出一个可能的解决方案tabularray。您也可以用类似的方法调整abovesep和。belowsep

\documentclass{article}
\usepackage[a6paper,margin=15mm]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{xcolor}

\ExplSyntaxOn
\NewChildSelector { mainfive }
  {
    \int_step_inline:nnnn {2} {5} { \l_tblr_childs_total_tl }
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
\NewChildSelector { subfive }
  {
    \int_step_inline:nnnn {3} {5} { \l_tblr_childs_total_tl }
      {
        \clist_put_right:Nx \l_tblr_childs_clist
          {
            ##1, \int_eval:n {##1 + 1 }, \int_eval:n {##1 + 2 }, \int_eval:n {##1 + 3 }
          }
      }
  }
\ExplSyntaxOff

\begin{document}

\begin{longtblr}[
  caption = {Long Title},
  label = {tblr:test},
]{
  rowhead = 1, cells = {c},
  hline{mainfive} = {0.05em,lightgray},
  hline{subfive} = {1-2,4-5}{0.03em,lightgray},
  hline{1,Z} = {0.08em,black}, % Z stands for the last
}
  Head & Head & Head & Head & Head \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
  a    & b    & c    & d    & e    \\
\end{longtblr}

\end{document}

在此处输入图片描述

相关内容