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 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?

share|improve this question
2  
Perhaps An improved \rowstyle that takes the cell contents as an argument will help; you would use conditionals to (de)activate a specific column operation. –  Werner Jan 9 '12 at 21:47
1  
You might exploit the table making features of the siunitx package, for numeric tables. –  egreg Jan 9 '12 at 21:56
    
@egreg yes, yet I'm using different commands than \num alone and I would like an approach that works for the other commands too. Good tip though! –  romeovs Jan 9 '12 at 22:21
    
@Werner not a bad idea. Can't come up with a way that doesn't use save boxes though. (I can't put a save box inside, say, a \num, or even a \textbf). –  romeovs Jan 9 '12 at 22:38

1 Answer 1

up vote 7 down vote accepted

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}
share|improve this answer
    
Exactly what I need! Cool thank you. –  romeovs Jan 10 '12 at 10:13

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.