#StackBounty: #longtable #booktabs #rowcolor #threeparttablex rows of alternating colors in longtable: filling whitespace depending on …

Bounty: 50

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{.333textwidth-2tabcolsep}p{.333textwidth-2tabcolsep}p{.333textwidth-2tabcolsep}}
        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\
            foonewline 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 heightbelowrulesep
    endgroup
  }%
}
newcommand*{aboverulesepcolor}[1]{%
  noalign{%
    begingroup
      color{#1}%
      hrule heightaboverulesep
    endgroup
    kern-aboverulesep
  }%
}

begin{document}

begin{ThreePartTable}
    rowcolors{2}{white}{gray}
    begin{longtable}{p{.333textwidth-2tabcolsep}p{.333textwidth-2tabcolsep}p{.333textwidth-2tabcolsep}}
        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\
            foonewline 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.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.