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 have a quite complex longtable design and I would like to add white column separators. I can add black separators using | but the placement of the lines does not seem ideal either.

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\linewidth}%
\kern-\linewidth}

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

\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{@{\extracolsep{\fill}}=C|+C|+C@{}}
\rowcolor{Blue} 
\rowstyle{\color{white}}
A & B & C \\
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{longtable}

\end{document}

Ideally I would like the table to look something like this:

enter image description here

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Simply redefine the new column type C to be:

\newcolumntype{C}{>{\rowfont\centering\arraybackslash}p{\dimexpr.33\textwidth-2\tabcolsep}}

MWE

\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\linewidth}%
\kern-\linewidth}

\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\newcolumntype{C}{>{\rowfont\centering\arraybackslash}p{\dimexpr.33\textwidth-2\tabcolsep}}

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

\begin{document}

\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{@{\extracolsep{\fill}}=C|+C|+C@{}}
\rowcolor{Blue}
\rowstyle{\color{white}}
A & B & C \\
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{longtable}

\end{document} 

Output:

enter image description here


EDIT

If you want to generalise it for an undefined number of columns, you can replace that definition with the following

\newcolumntype{C}[1]{>{\rowfont\centering\arraybackslash}p{\dimexpr\textwidth/#1-2\tabcolsep}}

which takes the number of columns as argument.

So, in your case, you have to write

\begin{longtable}{@{\extracolsep{\fill}}=C{3}|+C{3}|+C{3}@{}}
share|improve this answer
    
Is it possible to generalise this solution to tables with 4 or more columns? –  Daniel Feb 18 at 11:38
    
@Daniel See the edit in the answer. –  karlkoeller Feb 18 at 12:17
    
Great thanks. This solution works great for smaller tables but doesn't handle tables with lots of columns all too well. Luckily the original problem isn't an issue with larger tables. Just for reference I managed to get the vertical lines by replacing | with !{\color{white}\vrule width 1pt}. –  Daniel Feb 18 at 15:20

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.