从表格内部调用交替行颜色?

从表格内部调用交替行颜色?

是否有一个包或选项可以从表中开始交替行颜色,这样无论插入多少手动着色的行,都可以再次使用相同的每次都着色?(在单个表中,我想手动为几个“标题”行着色,每行后面跟着不确定数量的交替灰色/白色行 - 总是从灰色开始。)

答案1

像这样(根据您的喜好改变颜色和对齐方式)?

在此处输入图片描述

代码:

\documentclass{article}

\usepackage[table]{xcolor}

\begin{document}
    
    \begin{center}
        \rowcolors{2}{gray!50}{white}
        
        \begin{tabular}{lcr}
            \rowcolor{red!50}
            Table head1 & Table head1 & Table head1\\
            \rowcolor{green!50}
            Table head2 & Table head2 & Table head2\\
            odd     & odd   & odd \\
            even    & even  & even\\
            odd     & odd   & odd \\
            even    & even  & even\\
            odd     & odd   & odd \\
            even    & even  & even\\
        \end{tabular}
    \end{center}
    
\end{document}

答案2

编辑

使用最新版本nicematrix(v. 6.21 2023-07-14),您有一个\rowcolors可以在表格行中使用的命令。

\documentclass{article}

\usepackage{nicematrix}

\begin{document}
    
The command \verb|\myrowcolors{gray!50}{white}| is in the third row.

\begin{center}
    \begin{NiceTabular}{lcr}[colortbl-like]
        \rowcolor{red!50}
        Table head1 & Table head1 & Table head1\\
        \rowcolor{green!50}
        Table head2 & Table head2 & Table head2\\
        \rowcolors{gray!50}{white}%
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
    \end{NiceTabular}
\end{center}

\bigskip
The command \verb|\myrowcolors{gray!50}{white}| is in the fourth row.

\begin{center}
    \begin{NiceTabular}{lcr}[colortbl-like]
        \rowcolor{red!50}
        Table head1 & Table head1 & Table head1\\
        \rowcolor{green!50}
        Table head2 & Table head2 & Table head2\\
        odd     & odd   & odd \\
        \rowcolors{gray!50}{white}
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
    \end{NiceTabular}
\end{center}

\end{document}

第一个代码的输出


该软件包nicematrix没有为其环境提供这样的功能{NiceTabular},但可以对其进行编程。

\documentclass{article}

\usepackage{nicematrix,tikz}

\ExplSyntaxOn
\makeatletter

\cs_new_protected:Nn \__pantigny_rowcolors:nnn 
  {
    \int_step_inline:nnn { #1 } { \int_use:N \c@iRow } 
      {
        \tikz \fill [color= \int_if_even:nTF { ##1 - #1 } { #2 } { #3 } ] 
              (##1-|1) rectangle (\int_eval:n { ##1 + 1 } -|last) ; 
      }
  }

\NewDocumentCommand { \myrowcolors } { m m }
  {
    \tl_gput_right:Nx \g_nicematrix_code_before_tl 
      { \__pantigny_rowcolors:nnn { \int_use:N \c@iRow } { #1 } { #2 } }
  }

\makeatother
\ExplSyntaxOff


\begin{document}
    
The command \verb|\myrowcolors{gray!50}{white}| is in the third row.

\begin{center}
    \begin{NiceTabular}{lcr}[colortbl-like]
        \rowcolor{red!50}
        Table head1 & Table head1 & Table head1\\
        \rowcolor{green!50}
        Table head2 & Table head2 & Table head2\\
        \myrowcolors{gray!50}{white}%
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
    \end{NiceTabular}
\end{center}

\bigskip
The command \verb|\myrowcolors{gray!50}{white}| is in the fourth row.

\begin{center}
    \begin{NiceTabular}{lcr}[colortbl-like]
        \rowcolor{red!50}
        Table head1 & Table head1 & Table head1\\
        \rowcolor{green!50}
        Table head2 & Table head2 & Table head2\\
        odd     & odd   & odd \\
        \myrowcolors{gray!50}{white}%
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
        odd     & odd   & odd \\
        even    & even  & even\\
    \end{NiceTabular}
\end{center}

\end{document}

第二段代码的输出

相关内容