完整 MWE

完整 MWE

我试图让我的表格默认看起来更美观。我希望填充顶部行和中间行之间的每一行。

\rowcolor{tableheadcolor}通过附加命令可以轻松填充顶行之后的下一行 。

我遇到的问题是让它与多行表标题一起工作。我设法进行了修改\@xarraycr,这样在将布尔值设置为 false 之前,它会将下一行颜色设置为tableheadcolor。我遇到的问题是,这总是针对一行到多行运行。我设法通过将行颜色覆盖到存储的变量 来几乎解决这个问题page。但是,如果表格位于任何其他内容之上,这看起来会很奇怪,这很可能在某个时候发生。

尽管我花了很多心思,但我还是无法让它发挥作用,所以我向 StackExchange 寻求帮助。不幸的是,我看到的类似问题都无法提供帮助。

这就是我目前得到的(为了演示目的,颜色比较鲜艳)

演示图像

完整 MWE

\documentclass{article}

\usepackage{etoolbox}
\usepackage[table]{xcolor}
\usepackage{ctable}


\colorlet{text}{black}
\colorlet{page}{white}


\newbool{tabularTitleRow}
\colorlet{tableheadcolor}{black!5} % Table header colour = 25% gray

\AfterEndEnvironment{tabular}{%
    \global\boolfalse{tabularTitleRow}
}

\renewcommand{\toprule}{
    \arrayrulecolor{text}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
    \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
    \arrayrulecolor{text}\rowcolor{tableheadcolor}%
    \global\booltrue{tabularTitleRow}%
}

\let\oldmidrule\midrule
\renewcommand{\midrule}{
    \ifbool{tabularTitleRow}{
        \arrayrulecolor{tableheadcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
        \arrayrulecolor{text}\specialrule{\lightrulewidth}{0pt}{0pt}%
        \arrayrulecolor{page}\specialrule{\belowrulesep}{0pt}{0pt}%
        \arrayrulecolor{text}%
        \rowcolor{page}%
        % \global\rownum=0\relax%
        \global\boolfalse{tabularTitleRow}%
    }{%
        \oldmidrule
    }
}







\colorlet{tableheadcolor}{orange!30!page}

\catcode`@=11 % or \catcode"0040=11 or \makeatletter to change category code of @ to 11 and temporarily to access kernel macro \@xarraycr

\let\old@xarraycr\@xarraycr

\def\@xarraycr{%
{\texttt{\tiny EOL}}
\old@xarraycr \ifbool{tabularTitleRow}{\rowcolor{tableheadcolor}}{}}

\catcode`@=12 % or \catcode"0040=12 or \makeatother to restore category code of @ to 12




\begin{document}




\pagecolor{blue!10}

\begin{tabular}{lll}
    & & \ifbool{tabularTitleRow}{T}{F} \\
    \toprule
    a & b & \ifbool{tabularTitleRow}{T}{F} \\
    \midrule
    1 & 2 & \ifbool{tabularTitleRow}{T}{F} \\
    \midrule
    4 & 5 & \ifbool{tabularTitleRow}{T}{F} \\
    % \meaning\\ & & \\
    \toprule
    7 & 8 & \ifbool{tabularTitleRow}{T}{F}
\end{tabular}
\qquad
\begin{tabular}{lll}
    & & \ifbool{tabularTitleRow}{T}{F} \\
    \toprule
    a & b & \ifbool{tabularTitleRow}{T}{F} \\
    d & e & \ifbool{tabularTitleRow}{T}{F} \\
    \midrule
    1 & 2 & \ifbool{tabularTitleRow}{T}{F} \\
    4 & 5 & \ifbool{tabularTitleRow}{T}{F} \\
    \bottomrule
    7 & 8 & \ifbool{tabularTitleRow}{T}{F} \\
\end{tabular}


\end{document}

如果也可以使标题颜色透明,我一直无法找到如何做到这一点,但我确信您比我知道的更多。

也对采用更好的方法来实现整体效果感兴趣。

答案1

您之所以会将行颜色错开一个,是因为当您的修改将布尔标志\midrule设置tabularTitleRow为 false 时,已经太晚了:前面的\\命令已经调用了\rowcolor设置标题行颜色的调用。此外,您的修改方式\@xarraycr很糟糕,因为它会阻止语法在或\\[〈dimen〉]内部工作。因此,最好不要重新定义(如下所示)。tabulararray\@xarraycr

基于 Ulrike Fischer 的想法使用\hiderowcolors,这里有一种方法可以让表格标题上方的空间\midrule呈现出适当的颜色(但请参阅下面基于此技术的更复杂的解决方案):

\documentclass{article}
\usepackage{etoolbox}
\usepackage[table]{xcolor}
\usepackage{ctable} % or booktabs

\colorlet{text}{black}
\colorlet{page}{white}
\colorlet{tableheadcolor}{orange!30!page}

% Use of \showrowcolors and \hiderowcolors is an idea from Ulrike
% Fischer's answer here: <https://tex.stackexchange.com/a/494954/73317>
\renewcommand{\toprule}{%
    \showrowcolors
    \arrayrulecolor{text}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
    \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
    \arrayrulecolor{text}%
    \rowcolor{tableheadcolor}%
}

\apptocmd\midrule{\hiderowcolors}{}{\FAILED}

\makeatletter

\let\@BTrule@ORI=\@BTrule
\let\my@BTrule=\@BTrule

% Modified version of \@BTrule that doesn't do \vskip\@aboverulesep, for use
% when the corresponding vertical space should be coloured.
\patchcmd{\my@BTrule}{%
    \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
  }{%
    \ifnum\@lastruleclass=\z@\else
  }{}{\FAILED}

\newcommand*{\myendtablehead}{%
 \\[\aboverulesep] % this colours the additional space with the current row
                   % color
 \noalign{\global\let\@BTrule\my@BTrule}% temporarily modify \@BTrule
 \midrule
 \noalign{\global\let\@BTrule\@BTrule@ORI}% restore it
}

\makeatother

\rowcolors{1}{tableheadcolor}{tableheadcolor}

\begin{document}
\pagecolor{blue!10}

\begin{tabular}{ll}
    \toprule
    a & b \\
    d & e \myendtablehead
    1 & 2 \\
    4 & 5 \\
    7 & 8 \\
    \bottomrule
\end{tabular}

\end{document}

截屏

\\这里有一个解决方案,它允许您像往常一样在 之前编写\midrule,而无需明确调用\myendtablehead。请注意,为了\midrule被修改的 识别\@arraycr,它必须紧跟在\\命令之后(当然,它们之间可能有空格标记,但没有其他内容)。

由于\rowcolors命令尊重分组,我们定义了一个coloredtableheaders环境,其中的对齐根据此方案着色。在此环境之外,\toprule\midrule具有\@arraycr其通常含义。在环境\rowcolors之外使用的任何命令都coloredtableheaders与我们在环境内部执行的操作无关,并且应照常运行。

\documentclass{article}
\usepackage{etoolbox}
\usepackage[table]{xcolor}
\usepackage{booktabs} % or ctable

\colorlet{text}{black}
\colorlet{page}{white}
\colorlet{tableheadcolor}{orange!30!page}

\makeatletter

% The use of \showrowcolors and \hiderowcolors is an idea from Ulrike
% Fischer's answer here: <https://tex.stackexchange.com/a/494954/73317>
\newcommand*{\my@coloredtoprule}{%
  \showrowcolors
  \arrayrulecolor{text}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
  \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
  \arrayrulecolor{text}%
  \rowcolor{tableheadcolor}%
}

\let\my@coloredmidrule\midrule
\apptocmd\my@coloredmidrule{\hiderowcolors}{}{\FAILED}

\let\@BTrule@ORI=\@BTrule
\let\my@BTrule=\@BTrule

% Modified version of \@BTrule that doesn't do \vskip\@aboverulesep, for use
% when the corresponding vertical space should be coloured.
\patchcmd{\my@BTrule}{%
    \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
  }{%
    \ifnum\@lastruleclass=\z@\else
  }{}{\FAILED}

\let\@arraycrORI=\@arraycr

% The “master counter” hackery is explained in the TeXbook appendix D (Dirty
% Tricks), pp. 385-386. It is also mentioned in the array.sty implementation
% notes concerning \@arraycr.
\newcommand*{\my@colored@arraycr}{%
  % Increase the master counter. This is needed to prevent TeX from
  % prematurely finishing the alignment entry in case \\ was followed by '&'
  % (when the \futurelet from \@ifnextchar causes TeX to read a '&', this
  % finishes the entry unless the master counter has a different value than it
  % had when the entry was started).
  \relax\iffalse{\fi\ifnum 0=`}\fi
  % Each of the two branches takes care of decreasing the master counter.
  \@ifnextchar\midrule
    {\@firstoftwo{\my@endtablehead}}% gobble the following \midrule
    {\my@closebrace@and@arraycrORI}%
}

\newcommand*{\my@endtablehead}{%
  \ifnum 0=`{}\fi % the second brace decreases the master counter
  \@arraycrORI[\aboverulesep]% this colours the additional space with the
                             % current row color
  \noalign{\global\let\@BTrule\my@BTrule}% temporarily modify \@BTrule
  \midrule
  \noalign{\global\let\@BTrule\@BTrule@ORI}% restore it
}

\newcommand*{\my@closebrace@and@arraycrORI}{%
  \ifnum 0=`{}\fi % the second brace decreases the master counter
  \@arraycrORI
}

\newenvironment{coloredtableheaders}{%
  \let\toprule\my@coloredtoprule
  \let\midrule\my@coloredmidrule
  \let\@arraycr\my@colored@arraycr
  \rowcolors{1}{tableheadcolor}{tableheadcolor}%
  \ignorespaces
}{%
  \unskip\ignorespacesafterend
}

\makeatother

\begin{document}
\pagecolor{blue!10}

No colored header in the following \verb|tabular|:\quad
\begin{tabular}{l}
   a \\
   b \\
   c
\end{tabular}

\bigskip
\begin{coloredtableheaders}
  \begin{tabular}{ll}
    \toprule
    a & b \\
    d & e \\
    \midrule
    1 & 2 \\
    4 & 5 \\
    7 & 8 \\
    \bottomrule
  \end{tabular}

  \vspace{4ex}
  \begin{tabular}{>{\hspace{3pt}\normalsize}l>{\hspace{5pt}}*{3}{p{7.9em}}}
    \toprule
    Category & \multicolumn{3}{l}{\normalsize Packages} \\
    \midrule
    General  & etb & xpt & sil \\
             & tts & ttc & frm \\
    \bottomrule
  \end{tabular}
\end{coloredtableheaders}

\vspace{4ex}
No colored header in the following \verb|tabular|:\quad
\begin{tabular}{lr}
  Foo     & 1\\
  Bar     & 2\\
  And baz & 3
\end{tabular}
\end{document}

截屏

\cline在彩色标题中使用

正如手册中所述colortbl

\cline如果您使用,则由 生成的线条是彩色的\arrayrulecolor,但您可能不会注意到,因为它们被下一行中的任何彩色面板覆盖。这是 的“功能” \cline。如果使用此包,您可能最好-\hhline参数中使用规则类型,而不是\cline

使用上述代码和hhline包的示例:

  \begin{tabular}{lll}
    \toprule
    a & \multicolumn{2}{c}{b} \\
    % \cline{2-3} % problem: covered by the next colored row
    % \hhline provides a viable alternative:
    \hhline{>{\arrayrulecolor{tableheadcolor}}->{\arrayrulecolor{black}}--}
      & c & d\\ \midrule
    e & f & g \\ \bottomrule
  \end{tabular}

截屏

答案2

您可以使用 \hiderowcolors 清除颜色:

\documentclass{article}

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


\colorlet{text}{black}
\colorlet{page}{white}


\renewcommand{\toprule}{%
    \showrowcolors\arrayrulecolor{text}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
    \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
    \arrayrulecolor{text}}%

\colorlet{tableheadcolor}{orange!30!page}
\apptocmd\midrule{\hiderowcolors}{}{\fail}
\rowcolors{1}{tableheadcolor}{tableheadcolor}
\begin{document}

\pagecolor{blue!10}

\begin{tabular}{lll}
    & & \\
    \toprule
    a & b & \\
    \midrule
    1 & 2 & \\
    \midrule
    4 & 5 & \\
    \midrule
    7 & 8 & 
\end{tabular}
\quad
\begin{tabular}{lll}
    \toprule
    a & b & \\
    \midrule
    1 & 2 & \\
    \midrule
    4 & 5 & \\
    % \meaning\\ & & \\
    \midrule
    7 & 8 & 
\end{tabular}

\end{document}

在此处输入图片描述

答案3

这是(≥ 6.25)环境下{NiceTabular}的解决方案。nicematrix

\documentclass{article}
\usepackage{booktabs}
\usepackage{nicematrix}

\colorlet{text}{black}
\colorlet{page}{white}
\colorlet{tableheadcolor}{orange!30!page}

\NewCommandCopy{\oldmidrule}{\midrule}
\RenewExpandableDocumentCommand{\midrule}{}{\oldmidrule\rowlistcolors{}}

\begin{document}
\pagecolor{blue!10}

\begin{NiceTabular}{ll}[color-inside]
    \toprule
    \rowlistcolors{tableheadcolor}
    a & b \\
    d & e \\
    \midrule
    1 & 2 \\
    4 & 5 \\
    7 & 8 \\
    \bottomrule
\end{NiceTabular}

\end{document}

但是,与往常一样nicematrix,您需要进行多次编译。

上述代码的输出

相关内容