长表中交替颜色的行:根据行号填充空白

长表中交替颜色的行:根据行号填充空白

我正在使用该longtable包以及booktabs样式规则和交替行颜色。MWE

\documentclass{article}
\usepackage[a6paper]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

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

\usepackage{calc}

\begin{document}

\begin{ThreePartTable}
    \rowcolors{2}{white}{gray}
    \begin{longtable}{p{.333\textwidth-2\tabcolsep}p{.333\textwidth-2\tabcolsep}p{.333\textwidth-2\tabcolsep}}
        \caption{Lorem Ipsum.}\\
        \toprule
            Lorem & Ipsum & Dolor\\
            \midrule
        \endfirsthead
            Lorem & Ipsum & Dolor\\
            \midrule
        \endhead
            \hline 
            \multicolumn{3}{r}{(\emph{continued on next page})} \\
        \endfoot
            \bottomrule
        \endlastfoot
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo\newline foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
        \end{longtable}
    \end{ThreePartTable}
\end{document}

会给:

在此处输入图片描述

\midrule第 3 页的第一行和最后一行之间有一些空白。\bottomrule我从这个答案可以使用\belowrulesepcolor和来填补这些空白\aboverulesepcolor

\documentclass{article}
\usepackage[a6paper]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

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

\usepackage{calc}

\newcommand*{\belowrulesepcolor}[1]{%
  \noalign{%
    \kern-\belowrulesep
    \begingroup
      \color{#1}%
      \hrule height\belowrulesep
    \endgroup
  }%
}
\newcommand*{\aboverulesepcolor}[1]{%
  \noalign{%
    \begingroup
      \color{#1}%
      \hrule height\aboverulesep
    \endgroup
    \kern-\aboverulesep
  }%
}

\begin{document}

\begin{ThreePartTable}
    \rowcolors{2}{white}{gray}
    \begin{longtable}{p{.333\textwidth-2\tabcolsep}p{.333\textwidth-2\tabcolsep}p{.333\textwidth-2\tabcolsep}}
        \caption{Lorem Ipsum.}\\
        \toprule
            Lorem & Ipsum & Dolor\\
            \midrule
        \endfirsthead
            Lorem & Ipsum & Dolor\\
            %\aboverulesepcolor{gray}
            \midrule
            \belowrulesepcolor{gray}
        \endhead
            \hline 
            \multicolumn{3}{r}{(\emph{continued on next page})} \\
        \endfoot
            \aboverulesepcolor{gray}
            \bottomrule
        \endlastfoot
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo\newline foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
            foo & bar & foo\\
        \end{longtable}
    \end{ThreePartTable}
\end{document}

这将给出:

在此处输入图片描述

但是,尽管第 2 页的第一行是白色的,但它前面也会有一个灰色条。有没有办法让它更强大,例如使用一些使用当前行号的 if-else 语句?谢谢。

答案1

使用新 LaTeX3 包的替代解决方案tabularray

\documentclass{article}

\usepackage[a6paper]{geometry}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
  caption = {Lorem Ipsum},
  note{a} = {First note.},
  note{b} = {Second note.},
]{
  colspec={XXX},
  row{odd} = {gray8},
  row{1} = {white},
  rowhead = 1,
  rowsep = 1pt,
}
\toprule
    Lorem & Ipsum & Dolor\\
\midrule
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo\newline foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
    foo & bar & foo\\
\bottomrule
\end{longtblr}

\end{document}

在此处输入图片描述

答案2

这个解决方案怎么样?这个包似乎longtable以一种奇怪的方式执行代码,这意味着需要一些变通方法才能解决问题。如果你对此没意见,我认为这应该足够了。

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs}
\usepackage[a6paper]{geometry}
\usepackage{expl3}
\usepackage{array}
\usepackage{xcolor, colortbl}
\usepackage{longtable}

\begin{document}


% define some colors
\definecolor{colora}{HTML}{00bfff}
\definecolor{colorb}{HTML}{ffffff}
\definecolor{colorc}{HTML}{00ff7f}
\definecolor{colord}{HTML}{ffffff}


\ExplSyntaxOn

% set up the list of colors to be used
\clist_new:N \l_colors_clist
\clist_set:Nn \l_colors_clist {colora, colorb, colorc, colord}

\int_new:N \l_color_ind_int

% the function that computes color index
\cs_new:Npn \__compute_color_index:n #1 {
    \int_set:Nn \l_tmpb_int { \clist_count:N \l_colors_clist }
    \int_set:Nn \l_tmpa_int { \int_div_truncate:nn {#1} {\l_tmpb_int} }
    \int_gset:Nn \l_color_ind_int { #1 - \l_tmpa_int * \l_tmpb_int + 1}
}


\int_new:N \l_list_ind_int
\bool_new:N \l_is_header_bool

\cs_new:Npn \resetlistindex {
    \int_gset:Nn \l_list_ind_int {0}
    \bool_gset_false:N \l_is_header_bool
    \__compute_color_index:n {\l_list_ind_int}
}

\cs_new:Npn \calloneachrow {
    \bool_if:NTF \l_is_header_bool {
        \bool_gset_false:N \l_is_header_bool
    } {
        % increment row counter
        \int_gincr:N \l_list_ind_int
        \__compute_color_index:n {\l_list_ind_int}
    }
}

% this function sets correct cell color
\cs_new:Npn \calloneachcell {
    \bool_if:NTF \l_is_header_bool {}
    {
        % get color name
        \tl_set:Nx \l_tmpa_tl { \clist_item:Nn \l_colors_clist {\l_color_ind_int} }
        % use this color
        \exp_args:No \cellcolor {\l_tmpa_tl}
    }
}

% suppress color in header
\cs_new:Npn \callonheader {
    \bool_gset_true:N \l_is_header_bool
}


\ExplSyntaxOff


% unfortunately, using \rowcolor directly doesn't seem to work
% therefore, I use the following workaround:

% declare a new column type, which calls \calloneachcell every time
\newcolumntype{C}{>{\calloneachcell}c}

% set list index to 0
\resetlistindex
\begin{longtable}{CCC<{\calloneachrow}}
\caption{Caption}\\
\toprule
\callonheader %suppress background color for headers
% here, I need to explicitly execute \cellcolor{white}
% because the code of column type C will be executed
% before \callonheader, which means the first cell will
% have background color. I simply just override it with
% white color.
\cellcolor{white} Lorem & Ipsum & Dolor\\
\midrule
\endfirsthead
\callonheader %suppress background color for headers
\cellcolor{white} Lorem & Ipsum & Dolor\\
\midrule
\endhead
\hline 
\multicolumn{3}{r}{(\emph{continued on next page})} \\
\endfoot

\bottomrule
\endlastfoot
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
% need to use a \parbox to wrap around \newline
\parbox{3ex}{foo\newline} & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
foo & bar & foo\\
\end{longtable}


\end{document}

相关内容