0

I found out from this question that one can make a longtable zebra striped by using

\rowcolors{2}{white}{gray!25}

So I tried it but noticed that it doesn't look good in my project - the stripe sticks out of the table and has some holes in it:
With @{\extracolsep{\fill}}
After trying out a few things I noticed the @{\extracolsep{\fill}} is the problem.
Without it the color is perfect:
Without @{\extracolsep{\fill}}
But my table is not wide enough then.

Is there a better alternative for @{\extracolsep{\fill}} or for \rowcolors{2}{white}{gray!25} to make both things work together (while staying with longtable)?

Here's the full minimal example (I build it with XeLaTeX btw., haven't tried if it works with PdfLaTeX):

\documentclass[a4paper, 11pt]{article}

\usepackage{fontspec}
\usepackage{longtable}
\usepackage[table]{xcolor}

\begin{document}
    \rowcolors{2}{white}{gray!25}
    \begin{longtable}{@{\extracolsep{\fill}}clr}
%   \begin{longtable}{clr}
        \hline
        \noalign{\smallskip}
        {Test Col1\hfill} &
        {Test Col2\hfill} &
        {Test Col3}\\
        \noalign{\smallskip}
        \hline
        \noalign{\smallskip}
        \endhead
        \noalign{\smallskip}
        \hline
        \endfoot
        \endlastfoot
        Test & Test & Test \\
        Test & Test & Test \\
        Test & Test & Test \\
        Test & Test & Test \\
    \end{longtable}
\end{document}
1
  • while the first answers are nice for this minimal example, unfortunately it's too complicated for me to make it work with something different than longtable in my original project - when I tried everything somehow ended up in one column. So I'm still hoping maybe someone knows how to solve this problem using longtable
    – Cold_Class
    Apr 16, 2022 at 12:54

3 Answers 3

1

Your table code is, frankly said, very strange. Do you really have only table headers and nothing in table body?

I suspect that you after something like this:

\documentclass[a4paper, 11pt]{article}
\usepackage{fontspec}

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
\begin{longtblr}[
caption = {table caption},
  label = {tab:long},
                ]{hline{1,2,Z},
                  colspec = {X[c]X[l]X[r]},
                  row{even} = {bg=gray!25},
                  row{1} = {font=\bfseries},
                  rowhead = 1
                 }
% table body
Test Col1 & Test Col2   & Test Col3 \\
%
Test & Test & Test \\
Test & Test & Test \\
Test & Test & Test \\
Test & Test & Test \\
    \end{longtblr}
\end{document}

enter image description here

1
  • thx for the hint - when making the minimal example I added the content at the wrong place - I'm gonna correct that in my original question...
    – Cold_Class
    Apr 16, 2022 at 12:38
0

How about this?

It uses the tabularray package.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
    caption={Caption}
]{
    colspec = {X[c]X[l]X[r]},
    row{even} = {gray!25},
    rowhead = 1
}  
\toprule
    Test Col1 & Test Col2 & Test Col3 \\
    Test & Test & Test \\
    Test & Test & Test \\
    Test & Test & Test \\
    Test & Test & Test \\
\bottomrule
\end{longtblr}

\end{document}

enter image description here

0

This approach uses empty fixed-width columns to create the extra whitespace:

\documentclass[a4paper,11pt]{article}

\usepackage{fontspec}
\usepackage{longtable}
\usepackage[table]{xcolor}

\begin{document}
    \rowcolors{2}{white}{gray!25}
    \begin{longtable}{cp{2cm}lp{2cm}r}
        \hline
        \noalign{\smallskip}
        Test Col1 &&
        Test Col2 &&
        Test Col3 \\
        \noalign{\smallskip}
        \hline
        \noalign{\smallskip}
        \endhead
        \noalign{\smallskip}
        \hline
        \endfoot
        \endlastfoot
        Test && Test && Test \\
        Test && Test && Test \\
        Test && Test && Test \\
        Test && Test && Test \\
    \end{longtable}
\end{document}

enter image description here

You must log in to answer this question.

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