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.

There is a similar question and solution posted here. However, I can't use this solution as I have a coloured table, and the given solution causes white gaps between each column.

Is there a way to fit a coloured long table to the width of a page (including landscape pages) so that the row colouring is maintained?

Here is an example of my current code:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{float}
\usepackage{array}
\usepackage{longtable}

\restylefloat{table}

\definecolor{Gray}{gray}{0.65}
\definecolor{Blue}{RGB}{0,176,240}
\definecolor{light-gray}{gray}{0.90}

% Margins
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=8.5in
\headsep=0.25in 

\linespread{1.1} % Line spacing

\newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\newcommand\setrowfont[1]{\noalign{\gdef\rowfont{#1}}}
\gdef\rowfont{}

\makeatletter
\newcommand*{\@rowstyle}{}

\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}

\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}}%
}

\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\newcolumntype{C}{>{\rowfont}c}

\let\oldlongtable\longtable
\let\endoldlongtable\endlongtable
\renewenvironment{longtable}{\rowcolors{2}{light-gray}{Gray}\oldlongtable} {
\endoldlongtable}

\begin{document}

% Normal
\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{-10cm plus -1fill}
\setlength\LTright{\LTleft}
\begin{longtable}{=C+C+C}
\rowcolor{Blue} 
\rowstyle{\color{white}}
A & B & C \\
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{longtable}

% To page width but has white gaps between columns
\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{@{\extracolsep{\fill}}=C+C+C}
\rowcolor{Blue} 
\rowstyle{\color{white}}
A1 & B1 & C1 \\
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{longtable}

\end{document}

Generates:

enter image description here

share|improve this question
2  
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. –  karlkoeller Feb 17 at 9:47
    
Let me emphasize the importance of @karlkoeller's comment: There are several LaTeX packages that provide color-related macros, and having to guess at which package(s) you chose is both a waste of time and possibly quite unproductive since any solution one comes up with doesn't work with your setup. Thus, please help eliminate the guesswork by making your code an MWE (minimum working example) that generates the problem behavior you're looking to solve. –  Mico Feb 17 at 10:15
    
Appologies, example is now complete with the current best solution added. –  Daniel Feb 17 at 10:32

1 Answer 1

up vote 6 down vote accepted

enter image description here

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{float}
\usepackage{array}
\usepackage{longtable}

\restylefloat{table}

\definecolor{Gray}{gray}{0.65}
\definecolor{Blue}{RGB}{0,176,240}
\definecolor{light-gray}{gray}{0.90}

% Margins
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=8.5in
\headsep=0.25in 

\linespread{1.1} % Line spacing

\newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\newcommand\setrowfont[1]{\noalign{\gdef\rowfont{#1}}}
\gdef\rowfont{}

\makeatletter
\newcommand*{\@rowstyle}{}

\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}

\newcolumntype{=}{% resets the row style
  @{\zz}>{\gdef\@rowstyle{}}%
}

\makeatletter
\def\zz{%
{\let\CT@color\color\CT@row@color\leaders\vrule\hskip\textwidth}%
\kern-\textwidth}

\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\newcolumntype{C}{>{\rowfont}c}

\let\oldlongtable\longtable
\let\endoldlongtable\endlongtable
\renewenvironment{longtable}{\rowcolors{2}{light-gray}{Gray}\oldlongtable} {
\endoldlongtable}

\begin{document}


% To page width but has white gaps between columns
\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{=C@{\extracolsep{\fill}}+C+C@{}}
\rowcolor{Blue} 
\rowstyle{\color{white}}
A1 & B1 & C1 \\
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{longtable}

\end{document}
share|improve this answer
    
Hi David, this works great in potrait mode but doesn't seem to work in landscape. I can create a new question or edit the above to show details if needed, but I am using the pdflscape package. –  Daniel Feb 17 at 12:21
    
@Daniel use \linewidth not \textwidth in \zz –  David Carlisle Feb 17 at 12:44

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.