Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Welcome to TeX.SX! –  Sean Allred Apr 10 '13 at 15:50

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
Thanks thats awesome... I am a noob at latex... checked it and it still carries column labels over pagebreaks. Cpuple of additional questions. 1 - Is it possible to have the shading not do the headings or have it a different shade? 2 - At the moment it randomly starts a new table with teh next shading from previous table... it is possible to clean start each table 3 - can probably find this myself but is there a lighter grey than lightgray... need the shading to be less noticible. –  Joop Apr 11 '13 at 12:32
    
\global\let\zz\zza before any table will reset it, \definecolor{lightgray}{gray}{0.9}can be any number between 0 and 1 pick a shade:-) to replace the row color on any row ,in the first column of that row do \multicolumn{1}{@{\myzz\extracolsep{\fill}} |l |}{... the content of first cell} where \myzz is defined to be \color{yellow}\leaders\vrule\hskip\textwidth\hskip-\textwidth\kern0pt} or whatever colour you need –  David Carlisle Apr 11 '13 at 12:44
    
thanks will look at it got the grey sorted at moment thanks for the excellent help –  Joop Apr 11 '13 at 14:01
    
defined: \def\myzz{% \color{yellow}\leaders\vrule\hskip\textwidth\hskip-\textwidth\kern0pt} table in body starts: \begin{longtable}{ @{\extracolsep{\fill}} |l | r | r | r | r | r|} \hline % \multicolumn{1}{@{\myzz\extracolsep{\fill}} |l |}{... the content of first cell} \multicolumn{1}{@{\myzz\extracolsep{\fill}} |l |}{% {} & Beta Exp & Phys Exp & Phys Longs & Phys Shorts & compVAR \Industry Code & {} & {} & {} & {} & {} \endhead} \hline Basic Materials & 26,997,027 & 23,149,286 & 46,043,540 & -22,894,254 & -297,445 \ gives error –  Joop Apr 15 '13 at 7:39
    
can change the color of the 1st line of multiline header in this way, but bet error if I try it with second line containg the \endhead tag error message ! Misplaced \omit. \multispan ->\omit \@multispan l.59 Industry Code} –  Joop Apr 15 '13 at 7:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.