9

I have several column types defined using the array and collcell package. An example

\newcolumntype{N}{>{\collectcell\num}c<{\endcollectcell}}

This alleviates making tables a lot, and makes them more readable in the source file. Yet when I make a table with some header rows, I have to escape the column commands (the headers don't need to be inside the \num) using \multicolumn:

\begin{tabular}{NN}
    \hline
    \multicolumn{1}{c}{Header 1} & \multicolumn{1}{c}{Header 2}\\
    \hline
    1.234 & 2.345\\
    ....
\end{tabular}

is there a quicker way of doing this? Perhaps by redefining the column type mid-table, or having a command that will generate an escape row?

David Carlisle
  • 757,742
romeovs
  • 9,102

1 Answers1

9

One way to do this is to use a switch that is enabled when the header is completed. This switch is checked to see if the column macros are to be applied. When the header is completed, a call to \EndTableHeader needs to be made.

In the test below, the N column type applies the color red to each cell, and the result of:

\begin{tabular}{NN}
    \hline
    Header 1 & Header 1\EndTableHeader\\ 
    \hline 
    1.234 & 2.345\\
\end{tabular}

is that header column is not typeset in red:

enter image description here

Notes:

  • The solution here used the \newtoggle from the etoolbox package, as I find that syntax more readable. This can be adapted to the numerous other options for evaluating conditionals.
  • I initialized the toggle when it is defined. Can probably get away with this, but I prefer to initialize variables when defined.
  • Both the begin and end of the table are set to reset the toggle. This to protect against any accident set/reset that may be applied outside of the table, and also allows for the case of header rows that may occur in the middle of a table.
  • I am sure there unnecessary % at the end of the lines in the preamble, but I find it safer to just always add them in my preamble, rather than figure out if they are necessary

Code:

\documentclass{article}
\usepackage{collcell}
\usepackage{xcolor}% only needed for testing
\usepackage{etoolbox}

\newtoggle{inTableHeader}% Track if still in header of table \toggletrue{inTableHeader}% Set initial value \newcommand{\StartTableHeader}{\global\toggletrue{inTableHeader}}% \newcommand{\EndTableHeader}{\global\togglefalse{inTableHeader}}%

% Redefine tabular to initialize \StartTableHeader at start and end \let\OldTabular\tabular% \let\OldEndTabular\endtabular% \renewenvironment{tabular}{\StartTableHeader\OldTabular}{\OldEndTabular\StartTableHeader}%

% Define the column type: \newcommand*{\myColorCell}[1]{\iftoggle{inTableHeader}{#1}{\textcolor{red}{#1}}}% \newcolumntype{N}{>{\collectcell\myColorCell}c<{\endcollectcell}}%

\begin{document} \noindent \begin{tabular}{NN} \hline Header 1 & Header 1\EndTableHeader\ \hline 1.234 & 2.345\ \end{tabular}

\bigskip\noindent Check again to make sure that settings are correct at start of table:

\noindent \begin{tabular}{NN} \hline Header 1 & Header 1\EndTableHeader\ \hline 1.234 & 2.345\ \end{tabular} \end{document}

Peter Grill
  • 223,288