[Tex/LaTex] Alternate Row colors in longtable

colorgeometrylongtable

I am using
the method proposed by Nikos Alexandris in "How to apply alternate row coloring in a longtable in LyX?"

works to a limited extent but the shaded rows does not fill the whole width of page. Shading per cell, but the shading breaks column lines and there are gaps in shading.

here is my example tex code that does the broken shading. The tex gets auto generated so I am just showing a short table. real data is longer and should typically span pages with repeated headers.

\date{\today}

\documentclass[9pt]{article}

\usepackage{graphicx}

\usepackage{longtable}

\usepackage[margin=0.5in]{geometry}

\usepackage{fancyhdr}

\LTchunksize=200

\setlength{\parindent}{0cm}

\title{test}
\author{R}
\setlength{\headheight}{0.4in}
\setlength{\headwidth}{\textwidth}

\usepackage[table]{xcolor}

% define lightgray

\definecolor{lightgray}{gray}{0.9}

% alternate rowcolors for all long-tables

\let\oldlongtable\longtable

\let\endoldlongtable\endlongtable

\renewenvironment{longtable}{\rowcolors{2}{white}{lightgray}\oldlongtable} {
\endoldlongtable}

\begin{document}

\section{Sector}

\begin{longtable}{ @{\extracolsep{\fill}} |l | r | r | r | r | r|}

\hline 

{} & Beta & Phys  & Phys & Phys  & comp \\

Industry Code & {} & {} & {} & {} & {} \endhead

\hline 

Materials &   16,365,792 &   13,419,455 &   13,419,455 &       0.00 &     -201,593 \\
Goods &      471,925 &    1,110,412 &    1,110,412 &       0.00 &      -11,947 \\
Services &    7,122,991 &    8,511,225 &    9,492,570 &     -981,345 &      -75,117 \\
Banks &    5,078,280 &    6,123,238 &   12,084,671 &   -5,961,433 &      -69,738 \\
Health &    2,141,346 &    5,059,382 &    5,059,382 &       0.00 &      -26,319 \\
Industrials &   -6,013,491 &   -4,758,346 &    6,036,445 &  -10,794,791 &        3,722 \\
row &   -4,093,746 &   -3,755,730 &       0.00 &   -3,755,730 &       45,628 \\
row &   -1,753,606 &   -1,006,601 &    2,533,792 &   -3,540,393 &      -18,962 \\
Consumer &    2,227,195 &    2,317,581 &    2,317,581 &       0.00 &      -30,632 \\
All &   20,808,544 &   25,160,345 &   54,728,908 &  -29,568,563 & nan \\

\hline 

\end{longtable}

\end{document}

Best Answer

colortbl doesn't much like \extracolsep . For individual cells you can fiddle with the optional overhang arguments, but for rows that's a bit difficult.

Also your table isn't full width as the \extracolsep glue is balanced by \LTleft and \LTright glue that is either side of the table.

If you make it full width, making the colour bands is much easier, as you know in advance how big they need to be:

enter image description here

\documentclass[9pt]{article}

\usepackage{graphicx}

\usepackage{longtable}

\usepackage[margin=0.5in]{geometry}

\usepackage{fancyhdr}

\LTchunksize=200

\setlength{\parindent}{0cm}

\title{test}
\author{R}
\setlength{\headheight}{0.4in}
\setlength{\headwidth}{\textwidth}

\usepackage[table]{xcolor}

% define lightgray

\definecolor{lightgray}{gray}{0.9}

% alternate rowcolors for all long-tables

\let\oldlongtable\longtable

\let\endoldlongtable\endlongtable

%\renewenvironment{longtable}{\rowcolors{2}{white}{lightgray}\oldlongtable} {
%\endoldlongtable}

\def\zza{\global\let\zz\zzb
\fullwidthcolor{lightgray}}%

\def\zzb{\global\let\zz\zza}

\def\fullwidthcolor#1{\color{#1}\leaders\vrule\hskip\textwidth\hskip-\textwidth\kern0pt}
\def\resetLTcolor{\global\let\zz\zza}

\LTleft0pt
\LTright0pt
\begin{document}

\section{Sector}

\resetLTcolor
\begin{longtable}{ @{\zz\extracolsep{\fill}} |l | r | r | r | r | r|}

\hline 
\multicolumn{1}{@{\fullwidthcolor{yellow}\extracolsep{\fill}} |l |}{}
  & Beta & Phys  & Phys & Phys  & comp \\
\multicolumn{1}{@{\fullwidthcolor{yellow}\extracolsep{\fill}} |l |}{Industry Code}
 & {} & {} & {} & {} & {} \endhead

\hline 

Materials &   16,365,792 &   13,419,455 &   13,419,455 &       0.00 &     -201,593 \\
Goods &      471,925 &    1,110,412 &    1,110,412 &       0.00 &      -11,947 \\
Services &    7,122,991 &    8,511,225 &    9,492,570 &     -981,345 &      -75,117 \\
Banks &    5,078,280 &    6,123,238 &   12,084,671 &   -5,961,433 &      -69,738 \\
Health &    2,141,346 &    5,059,382 &    5,059,382 &       0.00 &      -26,319 \\
Industrials &   -6,013,491 &   -4,758,346 &    6,036,445 &  -10,794,791 &        3,722 \\
row &   -4,093,746 &   -3,755,730 &       0.00 &   -3,755,730 &       45,628 \\
row &   -1,753,606 &   -1,006,601 &    2,533,792 &   -3,540,393 &      -18,962 \\
Consumer &    2,227,195 &    2,317,581 &    2,317,581 &       0.00 &      -30,632 \\
All &   20,808,544 &   25,160,345 &   54,728,908 &  -29,568,563 & nan \\

\hline 

\end{longtable}

\end{document}