# Fit coloured longtable to pagewidth

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

\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:

-
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

\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

\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{%
\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}

-
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