表格中除最后一行之外的行交替显示颜色

表格中除最后一行之外的行交替显示颜色

我正在为表格中的交替组(5 行)着色。我想对 40 行数据执行此操作。我不想为表格中的最后一行(质量控制清单)着色。

我或多或少想对我的配色方案设置一个总行数限制。下面是可编译的代码来重现我的输出,我附上了当前输出的图像,其中最后一行的颜色不正确。

输出: 在此处输入图片描述

代码:

\documentclass[letterpaper,11pt,oneside, notitlepage]{article}% calls document type
\usepackage[usenames, dvipsnames, table, xcdraw]{xcolor}
\usepackage{booktabs}% table utilities
\usepackage{siunitx}% number and symbol alignment
\usepackage{graphicx}
\usepackage{afterpage}
\pagenumbering{gobble}
\setlength\arrayrulewidth{0.7pt}
\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}

\makeatletter
\newcommand{\groupedRowColors}[5][-1]{% [#1: offset], #2: group size, #3: start line, #4: color 1, #5: color 2
    % copied from xcolor.sty
    \global\rownum=\z@
    \global\@rowcolorstrue
    \@ifxempty{#4}%
        {\def\@oddrowcolor{\@norowcolor}}%
        {\def\@oddrowcolor{\gdef\CT@row@color{\CT@color{#4}}}}%
    \@ifxempty{#5}%
        {\def\@evenrowcolor{\@norowcolor}}%
        {\def\@evenrowcolor{\gdef\CT@row@color{\CT@color{#5}}}}%
    % simplified (no check for \if@rowcmd)
    \def\@rowcolors{%
        \if@rowcolors
            \noalign{%
                \relax
                \ifnum\rownum<#3
                    \@norowcolor
                % I have changed this check:
                \else \ifodd \numexpr (\rownum-#1)/#2\relax
                    \@oddrowcolor
                \else
                    \@evenrowcolor
                \fi \fi
            }%
        \fi
    }%
    \CT@everycr{\@rowc@lors\the\everycr}%
    \ignorespaces
}
\makeatother

\usepackage[letterpaper,bindingoffset=-.35in,%
left=1in,right=1in,top=.25in,bottom=.25in,%
footskip=.15in]{geometry}

\begin{document}
\thispagestyle{empty}
\belowrulesep=0pt
\aboverulesep=0pt
\heavyrulewidth=2pt

\begin{table}[t!]
\begin{tabular}[l]{|p{.6in}|p{2.46in}|p{1in}|p{1.34in}| }
\toprule
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Date}\rule{.8in}{.1pt} \hspace{.69in} \textbf{Crew}\rule{2.4in}{.1pt}\hspace{.87in} \textbf{Page}\rule{.2in}{.1pt}  \textbf{of}\rule{.25in}{.1pt}}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Species}\rule{.95in}{.1pt}\hfill \textbf{Location}\rule{.95in}{.1pt}\hfill \textbf{Project}\rule{.95in}{.1pt}}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\bottomrule
\end{tabular}
\end{table}

\vspace*{-2.75\baselineskip}
\begin{centering}
\begin{table}[bh!]
\groupedRowColors{5}{-1}{gray!15}{white}
\begin{tabular}{!{\VRule[2pt]}p{.6in}|p{1.5in}|p{.65in}|p{.65in}|p{2.6in}!{\VRule[2pt]}}
    \toprule
    \textbf{Count} & \textbf{Tag Number(s)} & \textbf{Start} & \textbf{End} & \textbf{Comments}\\
    \bottomrule
    & & & &\\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\

\toprule
\multicolumn{5}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Scanned}\rule{.6in}{.1pt}\hfill \textbf{Entered}\rule{.6in}{.1pt}\hfill \textbf{Checked}\rule{.6in}{.1pt}\hfill \textbf{Double Checked}\rule{.6in}{.1pt}}\\
\bottomrule
\end{tabular}
\end{table}
\end{centering}
\vspace*{-1\baselineskip}
\small\textit{\textbf{*} Max 40 Tests per sheet}\hspace{.5in}\textbf{Notes:}


\end{document}

答案1

一个简单的解决方案:\rowcolor在最后一行添加。

\documentclass[letterpaper,11pt,oneside, notitlepage]{article}% calls document type
\usepackage[usenames, dvipsnames, table, xcdraw]{xcolor}
\usepackage{booktabs}% table utilities
\usepackage{siunitx}% number and symbol alignment
\usepackage{graphicx}
\usepackage{afterpage}
\pagenumbering{gobble}
\setlength\arrayrulewidth{0.7pt}
\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}

\makeatletter
\newcommand{\groupedRowColors}[5][-1]{% [#1: offset], #2: group size, #3: start line, #4: color 1, #5: color 2
    % copied from xcolor.sty
    \global\rownum=\z@
    \global\@rowcolorstrue
    \@ifxempty{#4}%
        {\def\@oddrowcolor{\@norowcolor}}%
        {\def\@oddrowcolor{\gdef\CT@row@color{\CT@color{#4}}}}%
    \@ifxempty{#5}%
        {\def\@evenrowcolor{\@norowcolor}}%
        {\def\@evenrowcolor{\gdef\CT@row@color{\CT@color{#5}}}}%
    % simplified (no check for \if@rowcmd)
    \def\@rowcolors{%
        \if@rowcolors
            \noalign{%
                \relax
                \ifnum\rownum<#3
                    \@norowcolor
                % I have changed this check:
                \else \ifodd \numexpr (\rownum-#1)/#2\relax
                    \@oddrowcolor
                \else
                    \@evenrowcolor
                \fi \fi
            }%
        \fi
    }%
    \CT@everycr{\@rowc@lors\the\everycr}%
    \ignorespaces
}
\makeatother

\usepackage[letterpaper,bindingoffset=-.35in,%
left=1in,right=.9in,top=.25in,bottom=.25in,%
footskip=.15in]{geometry}

\begin{document}
\thispagestyle{empty}
\belowrulesep=0pt
\aboverulesep=0pt
\heavyrulewidth=2pt

\begin{table}[t!]
\begin{tabular}[l]{|p{.6in}|p{2.46in}|p{1in}|p{1.34in}| }
\toprule
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Date}\rule{.8in}{.1pt} \hspace{.69in} \textbf{Crew}\rule{2.4in}{.1pt}\hspace{.87in} \textbf{Page}\rule{.2in}{.1pt}  \textbf{of}\rule{.25in}{.1pt}}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Species}\rule{.95in}{.1pt}\hfill \textbf{Location}\rule{.95in}{.1pt}\hfill \textbf{Project}\rule{.95in}{.1pt}}\\
\multicolumn{4}{!{\VRule[2pt]}l!{\VRule[2pt]}}{}\\
\bottomrule
\end{tabular}
\end{table}

\vspace*{-2.75\baselineskip}
\begin{centering}
\begin{table}[bh!]
\groupedRowColors{5}{-1}{gray!15}{white}
\begin{tabular}{!{\VRule[2pt]}p{.6in}|p{1.5in}|p{.65in}|p{.65in}|p{2.6in}!{\VRule[2pt]}}
    \toprule
    \textbf{Count} & \textbf{Tag Number(s)} & \textbf{Start} & \textbf{End} & \textbf{Comments}\\
    \bottomrule
    & & & &\\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\
    \hline
    & & & & \\

\toprule
\rowcolor{white} \multicolumn{5}{!{\VRule[2pt]}l!{\VRule[2pt]}}{\textbf{Scanned}\rule{.6in}{.1pt}\hfill \textbf{Entered}\rule{.6in}{.1pt}\hfill \textbf{Checked}\rule{.6in}{.1pt}\hfill \textbf{Double Checked}\rule{.6in}{.1pt}}\\
\bottomrule
\end{tabular}
\end{table}
\end{centering}
\vspace*{-1\baselineskip}
\small\textit{\textbf{*} Max 40 Tests per sheet}\hspace{.5in}\textbf{Notes:}


\end{document}

在此处输入图片描述

相关内容