[Tex/LaTex] An improved \rowstyle that takes the cell contents as an argument

formattingtables

It is possible to format the column entries of a tabular in an argument-style fashion using the lrbox environment to

  1. store the column cell in a box (say) \mybox; and
  2. supply \mybox as an argument to some other macro.

Here is a short example illustrating this fact*:

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\begin{document}
\newsavebox{\mybox}
\begin{tabular}{c>{\begin{lrbox}{\mybox}}c<{\end{lrbox}\fbox{\usebox{\mybox}}}c}
  One & Two & Three \\ \hline
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\ \hline
\end{tabular}
\end{document}

enter image description here

The above example uses the array package to achieve this column-specific formatting. More specifically, each cell in the column is supplied as an argument to \fbox. Does there exist an equivalent automation that would yield

enter image description here

\documentclass{article}
\begin{document}
\begin{tabular}{ccc}
  One & Two & Three \\ \hline
  1 & 2 & 3 \\
  \fbox{4} & \fbox{5} & \fbox{6} \\
  7 & 8 & 9 \\ \hline
\end{tabular}
\end{document}

without having to specify \fbox for each entry in the row?

As a start, I know that the tabu package provides \rowfont[<alignment>]{<font specification>}. However, this only applies macros <font specification> to each cell in a row and these macros can't take the cell contents as an argument. Moreover, you need to use the tabu environment in order to use this. So that's a little restricting. Also, trying to decipher the colortbl package macros for colouring a tabular or array row is just confusing.

The UK TeX FAQ entry on How to change a whole row of a table modifies the row entries via column specifications and therefore requires the addition of "subtle" identifiers like

\begin{tabular}{|$l|^l|^l|}

to initiate $ and propagate ^ whatever row style is defined. Is there a way around this or at least a cleaner alternative that doesn't use columns to format entries in a row? In particular, a solution should provide an lrbox-style flexibility, since the contents of the cell in the to-be-styled row could be anything, even a paragraph (say).

* There are other alternatives, as indicated by the post How to apply a macro to each column of a table.

Best Answer

\documentclass{article}
\usepackage{array}
\newsavebox\TBox
\newif\iffbox \fboxfalse
\newcommand\FB[1][true]{\global\csname fbox#1\endcsname}
\newcolumntype{C}{%
  >{\begin{lrbox}{\TBox}} 
  c 
  <{\end{lrbox}\iffbox\fbox{\usebox\TBox}\else\usebox\TBox\fi}}
\begin{document}
\begin{tabular}{*3C}
  One & Two & Three \\ \hline
  1 & 2 & 3 \\
  \fbox{4} & \fbox{5} & \fbox{6} \\
  4 & 5 & 6 \\\FB
  7 & 8 & 9 \\ \FB[false]
  1 & 2 & 3 
\end{tabular}
\end{document}

enter image description here