4

Awhile ago, @robintw asked a practical question: Make first row of table all bold

It uses the array package to cleverly inject formatting code into the header row columns.

Clever Table Row Formatting Solution

\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}} % Always set currentrowstyle to \relax in case no \rowstyle is used
\newcolumntype{^}{>{\currentrowstyle}} % set \currentrowstyle to \bfseries or whatever (>{\bfseries}c)
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}% set global definition for \currentrowstyle
  #1\ignorespaces
}

The Problematic Code

I'd like to see multicolumn and multirow support. I tried the following based on https://tex.stackexchange.com/a/4816/13552, which does not work.

\documentclass{article}
\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\begin{document}
\begin{tabular}{$l^c^c^r}
\rowstyle{\bfseries}
%\multicolumn{2}{^l}{span2} & \multicolumn{2}{^r}{span2} \\ % Uncomment this to see problem
col1 & col2 & col3 & col4 \\
dat1 & dat2 & dat3 & dat4 \\
\end{tabular}
\end{document}
  • the problem is in \rowstyle{\bfseries} – touhami Apr 25 '15 at 8:36
  • Your example works for me (TeXLive 2014, pdflatex), and produces the desired result. What error do you get? – anderstood Jun 9 '15 at 15:41
  • @anderstood, uncommenting the problematic lines results in: ! Misplaced \omit. \multispan ->\omit \@multispan with This is XeTeX, Version 3.14159265-2.6-0.99991 (TeX Live 2014) (preloaded format=xelatex) – Jonathan Komar Jun 11 '15 at 12:11
3
+50

The problem has to do with the fact that the $> columntype is never used in the first row of the tabular at all.

You can address this by using:

\multicolumn{2}{$l}{}

For the first set of spanned columns. However, the problem then is that things get done in the wrong order. \rowstyle{\bfseries} is now before the start of the first cell. To address this, you can include it in the specification of the first cell

\multicolumn{2}{$l}{\rowstyle{\bfseries}span2}

which produces

spanned row styles

which, if I've understood correctly, is the expected result.

Complete code

\documentclass{article}
\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\begin{document}
  \begin{tabular}{$l^c^c^r}
    \multicolumn{2}{$l}{\rowstyle{\bfseries}span2} & \multicolumn{2}{^r}{span2} \\ % Uncomment this to see problem
    col1 & col2 & col3 & col4 \\
    dat1 & dat2 & dat3 & dat4 \\
  \end{tabular}
\end{document}
  • Thanks! Finally an answer to this. Not a clean solution (because the $ and ^ have to be mentioned again :/), but it will suffice for now! Thanks for providing a nice explanation. It is always helpful to understand things. – Jonathan Komar Jun 12 '15 at 6:19
  • @macmadness86 I agree it is not a perfect solution. However, since this question had been unanswered for a while, I thought even an imperfect one preferable to none at all. Maybe somebody else will suggest a nice trick to improve things further (I just followed my nose). Hopefully latex3 will eventually offer more significant improvements! – cfr Jun 12 '15 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.