10

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

Werner
  • 603,163

3 Answers3

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

Moriambar
  • 11,466
3

This is a hack into tabu code. I'm redefining the internal of \rowfont so that you can specify the equivalent of the > preamble construction and the equivalent of the < preamble construction (this one is new). Not sure it is fully robust.

\documentclass{standalone}
\usepackage{tabu}
\makeatletter
\renewcommand*\tabu@row@font[3][]{%
  \ifnum7=\currentgrouptype
    \global\let\tabu@@cellleft    \tabu@cellleft
    \global\let\tabu@@cellright   \tabu@cellright
    \global\let\tabu@@celllalign  \tabu@celllalign
    \global\let\tabu@@cellralign  \tabu@cellralign
    \global\let\tabu@@rowfontreset\tabu@rowfontreset
  \fi
  \global\let\tabu@rowfontreset \tabu@rowfont@reset
  \expandafter\gdef\expandafter\tabu@cellleft\expandafter{\tabu@cellleft #2}%
  \expandafter\gdef\expandafter\tabu@cellright\expandafter{\tabu@cellright #3}%
  \ifcsname tabu@cell@#1\endcsname       % row alignment
        \csname tabu@cell@#1\endcsname \fi
  \ifnum0=`{\fi}% end of group / noalign group
}% \rowfont
\begin{document}
\newsavebox{\mybox}
\begin{tabu}{ccc}
  One & Two & Three \\ \hline
  1 & 2 & 3 \\
  \rowfont{\begin{lrbox}{\mybox}}{\end{lrbox}\fbox{\usebox{\mybox}}} 4 & 5 & 6 \\
  7 & 8 & 9 \\ \hline
\end{tabu}
\end{document}
David Carlisle
  • 757,742
cjorssen
  • 10,032
  • 4
  • 36
  • 126
2

An easy solution with tblr environment of the new LaTeX package tabularray:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{ colspec = {ccc}, row{3} = {cmd=\fbox}, } One & Two & Three \ \hline 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 \ \hline \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932