1

I'm typesetting a complex table with the contents created by an external program. When I use \input I get unexpected behavior from \rowcolor. See the MWE below

\documentclass{article}
\usepackage{xcolor}
\usepackage{colortbl}
\begin{document}
Works\par
\begin{tabular}{ll}
  \rowcolor{blue!20}%
  A & B
\end{tabular}

Doesn't work\par
\begin{tabular}{ll}
  % Uncomment line below to get the unexpected behavior
  %\input{mwe-bit.tex}
\end{tabular}
\end{document}

where mwe-bit.tex simply contains

\rowcolor{blue!20}%
A & B

being the same two lines as directly included above. When I run this, the first part works, but the second generates (when uncommented):

! Misplaced \noalign.
\rowcolor ->\noalign 
                     {\ifnum 0=`}\fi \global \let \CT@do@color \CT@@do@color...
l.1 \rowcolor
             {blue!20}

I added the % after the \rowcolor in an abundance of caution, but it doesn't seem to make a difference.

I'm still something of a newbie when it comes to posting here, so apologies for any missteps.

(Update, original post had a pair of \rowcolor instructions for each attempt. I'd been thinking it was something to do with changing an already-specified color, but actually, just having one inside the inputted file is sufficient to generate the error)

frougon
  • 24,283
  • 1
  • 32
  • 55
  • Doesn't rowcolor have to be the very first thing in the first cell in a row? Here something else is being executed as well (the input), thus rowcolor is no longer first. Why are you inputting inside a tabular in the first place? – daleif May 28 '19 at 05:40
  • Thanks for the pointer, it seems consistent with the answer below. I'm \input-ing the content because, for various reasons, it's auto-generated by an off-line program. – Nathaniel Livesey May 28 '19 at 06:38

1 Answers1

2

If it must be subject to \rowcolor, you need to treat the first tabular line out of your \input file (or do something else like putting also the tabular preamble in the \input file). This is because \rowcolor expands to tokens, one of which is \noalign, and \noalign can only happen at very specific places in a TeX alignment such as tabular. See here for a detailed explanation about this (this is also explained below).

The following works:

main.tex:

\documentclass{article}
\usepackage{xcolor}
\usepackage{colortbl}
\begin{document}
Works\par
\begin{tabular}{ll}
  \rowcolor{blue!20}%
  A & B
\end{tabular}

Also works\par
\begin{tabular}{ll}
  \rowcolor{green!20}% first line must be colored out of the \input file
  foobar
  \input{mwe-bit.tex}
\end{tabular}
\end{document}

mwe-bit.tex:

\\
\rowcolor{blue!20}%
A & B

Screenshot

In your case, you get the dreaded “Misplaced \noalign” error because \input executes \@ifnextchar, which executes \let (at least, this is one reason why this error must happen; there might be others quite similar, e.g., from some command other than \@ifnextchar). \let being a non-expandable, non-space token, TeX stops his expansion of tokens looking for \noalign or \omit at the beginning of the tabular or after the end of a tabular line (after the \cr or \crcr command used internally by tabular). Because of this, it doesn't expand \rowcolor at this point, and when it eventually does, it is already processing the first tabular entry contents, where \noalign is invalid. \noalign is only valid during the special expansion step looking for \noalign or \omit at the start of a tabular1 or after a \cr or \crcr, special step which stops at the first non-expandable, non-space token.

My example works because:

  1. The first \rowcolor at the very beginning of the tabular gets expanded during the “special step” I just described, and the first non-expandable, non-space token is \noalign (from \rowcolor's definition), which allows \rowcolor to work as designed.

  2. Other \rowcolor commands from mwe-bit.tex occur right after \\, and if we look at the definition of \\ in tabular, it is probably not very different from \crcr (I guess there is an \unskip before, but “before” contents can't cause any problem: what counts is what there is between \cr or \crcr and \noalign material). When performing the “special expansion step” started by \\, TeX finds no intervening non-expandable, non-space token before arriving at the \noalign from \rowcolor; therefore this one can also work as designed (i.e., put the color \special material where it belongs).


Footnote

  1. More generally, a TeX alignement such as \halign or \valign.
frougon
  • 24,283
  • 1
  • 32
  • 55
  • Thanks, this was very helpful and clearly fixed the problems. Rather than move the preamble into the auto-generated input file I think I'll move that first rowcolor out of it into the enclosing source file.

    Many thanks again.

    – Nathaniel Livesey May 28 '19 at 06:41
  • Glad it helped. In the answer I linked to, you can find ideas in order not to duplicate the color information in several places if this is important to you (maybe not that much, since you can probably handle such “unity” at the level of your program that generates the LaTeX code). Anyway, the ideas in question are not rocket science. :-) – frougon May 28 '19 at 06:43
  • Ha! Thanks again. – Nathaniel Livesey May 28 '19 at 06:50