4

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

1 Answer 1

5

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}@{}}
3
  • Is it possible to generalise this solution to tables with 4 or more columns?
    – Daniel
    Commented Feb 18, 2014 at 11:38
  • @Daniel See the edit in the answer. Commented Feb 18, 2014 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
    Commented Feb 18, 2014 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .