[Tex/LaTex] Misplaced \noalign error in table, but only when using include{}

highlightingtablesxtable

I am using xtable() in R to create a LaTeX table that I import into a .rnw file via \input{}. As documented here, I am attempting to highlight rows conditional upon a certain value of one variable.

The R side of my problem has been resolved. Now I am struggling with a LaTeX problem, which is why I am posting here.

The following R code creates my table:

  wd <- "yourpath"
  setwd(wd)
# create data frame
  my.df=data.frame(a=c(1:10),b=letters[1:10],c=c(1,1,0,0,1,1,0,0,1,1))

# identify index of rows to highlight
  row.i.1 <- which(my.df$c==1)

print(xtable(my.df),
      only.contents=TRUE,
      include.rownames=FALSE,
      include.colnames=FALSE,
      hline.after=NULL,
      type="latex",
      add.to.row=list(
        pos=list(as.list(row.i.1))[[1]],
        command=rep("\\rowcolor{green!20!white}",
                    length(seq(from=1,to=length(row.i.1),by=1)))),
      sanitize.text.function=identity,
      file="testtbl.tex"

Here are the contents of testtbl.tex:

% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Tue Nov 12 10:23:27 2013
   1 & a & 1.00 \\ 
   \rowcolor{green!20!white}  2 & b & 1.00 \\ 
   \rowcolor{green!20!white}  3 & c & 0.00 \\ 
    4 & d & 0.00 \\ 
    5 & e & 1.00 \\ 
   \rowcolor{green!20!white}  6 & f & 1.00 \\ 
   \rowcolor{green!20!white}  7 & g & 0.00 \\ 
    8 & h & 0.00 \\ 
    9 & i & 1.00 \\ 
   \rowcolor{green!20!white} 10 & j & 1.00 \\ 
   \rowcolor{green!20!white}

The row highlighting is shifted down one row for some reason. A commenter on SO instructed me to change pos=list(as.list(row.i.1))[[1]], to pos=list(as.list(row.i.1-1))[[1]], (adding the -1), which gives the correct assignment:

% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Tue Nov 12 10:26:16 2013
  \rowcolor{green!20!white}  1 & a & 1.00 \\ 
   \rowcolor{green!20!white}  2 & b & 1.00 \\ 
    3 & c & 0.00 \\ 
    4 & d & 0.00 \\ 
   \rowcolor{green!20!white}  5 & e & 1.00 \\ 
   \rowcolor{green!20!white}  6 & f & 1.00 \\ 
    7 & g & 0.00 \\ 
    8 & h & 0.00 \\ 
   \rowcolor{green!20!white}  9 & i & 1.00 \\ 
   \rowcolor{green!20!white} 10 & j & 1.00 The problem now comes when I try to include this table in a `.rnw` file. 

(1) If I paste the table directly into my .rnw file, everything works:

\documentclass{article}
\usepackage{colortbl, xcolor}
\usepackage{booktabs}
\begin{document}
\begin{table}
  \begin{tabular}{lll}
  \toprule
  var1 & var2 & var3 \\
  \midrule
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Tue Nov 12 10:26:16 2013
  \rowcolor{green!20!white}  1 & a & 1.00 \\ 
   \rowcolor{green!20!white}  2 & b & 1.00 \\ 
    3 & c & 0.00 \\ 
    4 & d & 0.00 \\ 
   \rowcolor{green!20!white}  5 & e & 1.00 \\ 
   \rowcolor{green!20!white}  6 & f & 1.00 \\ 
    7 & g & 0.00 \\ 
    8 & h & 0.00 \\ 
   \rowcolor{green!20!white}  9 & i & 1.00 \\ 
   \rowcolor{green!20!white} 10 & j & 1.00 \\
  \bottomrule
  \end{tabular}
\end{table}
\end{document}

(2) However, if I try to import the table via include, which is what I need to do for reproducibility, I get the following error: Error: testtbl.tex:3: Misplaced \noalign

\documentclass{article}
\usepackage{colortbl, xcolor}
\usepackage{booktabs}
\begin{document}
\begin{table}
  \begin{tabular}{lll}
  \toprule
  var1 & var2 & var3 \\
  \midrule
  \input{testtbl}
  \bottomrule
  \end{tabular}
\end{table}
\end{document}

(3) If I go back and use the original xtable code, pos=list(as.list(row.i.1))[[1]], thus not subtracting 1, the pdf will compile, but my highlighting is shifted.

So my question is: Why am I getting a Misplaced \noalign when grabbing my table contents via include (2) but not when copying and pasting the exact same content directly into the document (1). The fact that (3) will compile (but with incorrect highlighting) makes me think it has something to do with how I try to correct the shifted highlighting.

Best Answer

The problem is linked to the fact that the LaTeX \input is "non-expandable". Two solutions are proposed in the code, either use the TeX original primitive or first input the file contents in a macro, using, for facility the catchfile package.

\documentclass{article}
\usepackage{colortbl, xcolor}
% Plain old TeX is the best!
\makeatletter\let\expandableinput\@@input\makeatother
% alternative
% \usepackage{catchfile}
% \CatchFileDef{\testtblasmacro}{testtbl}{} % don't forget empty braces

\usepackage{booktabs}
\begin{document}\thispagestyle{empty}
\begin{table}
  \begin{tabular}{lll}
  \toprule
  var1 & var2 & var3 \\
  \midrule
  \expandableinput testtbl.tex
% or 
% \testtblasmacro
  \bottomrule
  \end{tabular}
\end{table}
\end{document}

table with colors

Related Question