0

I'm using the longtable package along with booktabs-style rules and alternating row colors. The 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}

will give:

enter image description here

There is some whitespace between \midrule and the first row as well as \bottomrule and the last row on page 3. I learnt from this answer that one can fill those gaps using \belowrulesepcolor and \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}

Which will give:

enter image description here

However, the first row on page 2 will also be preceded by a gray bar, although the first row is white. Is there any chance to make this more robust, i.e. by using some if-else-statement using the current row number? Thanks.

4
  • 1
    How about removing the extra vertical spaces by \setlength{\aboverulesep}{0pt}\setlength{\belowrulesep}{0pt}? Since rows are filled with color, keeping rows with equal height seems reasonable. Jul 20, 2020 at 23:58
  • You are right, you can get rid of the whitespace by doing so. But, as a matter of fact, I do like the visual appeal of booktabs-style tables and would like to preserve the spacing introduced with the different rules. Could have mentioned that in my initial post.
    – tstone-1
    Jul 21, 2020 at 7:00
  • The number of current line is stored in TeX count \rownum, but \endhead and \endfoot both stores collected staff in a box, hence everything is pre-expanded before the head/foot is inserted. Not sure if this can be easily solved/hacked. Jul 21, 2020 at 7:31
  • Why don't you just write foo & &\\ instead of foo\newline?
    – AndréC
    Jul 23, 2020 at 7:42

2 Answers 2

3

An altetnative solution with the new LaTeX3 package 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}

enter image description here

0

How about this solution? It seems like the longtable package executes code in a funny way, which means some workarounds are needed to get it right. If you are OK with it, I think this should be enough.

enter image description here

\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}
1
  • Thanks a lot for your effort! I didn't quite understand the suggestion made by @AndréC above. However, taking into account your answer, I'm afraid that I obviously wasn't able to describe the problem in a proper manner. Your suggestion is perfect to introduce three alternating row colors! However, I'm perfectly fine with two colors. My question aimed at filling the whitespace introduced below \midrule and above \bottomrule. See page 3 of the first image. You can see the unpleasant spacing also from your example (whitespace between \midrule and the first rows). Sorry for being unprecise.
    – tstone-1
    Jul 25, 2020 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .