[Tex/LaTex] longtable misplaced noalign

longtablepage-breakingrsweavetables

Question

Why am I getting multiple misplaced \noalign errors?

Reproducible example

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage{booktabs}

\begin{document}

\section{Table test}

\begin{longtable}{llc}
\hline
 &  & \multicolumn{1}{c}{All} \\ 
\hline
m & \rule{0pt}{1.7\normalbaselineskip}April  & $1$ \\
 & \nopagebreak July  & $1$ \\
 & \nopagebreak June  & $1$ \\
l & \rule{0pt}{1.7\normalbaselineskip}C  & $1$ \\
 & \nopagebreak F  & $1$ \\
 & \nopagebreak S  & $1$ \\
nums & \rule{0pt}{1.7\normalbaselineskip}28  & $2$ \\
 & \nopagebreak 62  & $1$ \\
\hline 
\end{longtable}
\end{document}

Desired output (through reproducible research toolchain—see below)

desired output

Troubleshooting—Manual Tex Editing

It seems misplaced /noalign errors can be vexing. My \hline is preceded by \\. Does the comment about booktabs apply here? If so, how do I fix it? Changing \\ to \\* or \tabularnewline doesn't seem to help. As far as I can tell, this is not a \centering issue.

Removing \nopagebreak seems to make the errors go away but I don't think this is the right solution for me since the solution must work with long data tables in the context of a report using longtable. Please see below.

Context

I believe that this is really a LaTeX question and not an R question but some context might help.

I am trying to generate a long report with multiple tables while adhering to principles of reproducible research by using R and the tables and Hmisc packages.

I would like to dynamically create a table with frequency counts of observations across multiple factored variables with some vertical space between each factored variable.

The tables package helps me do that using a nice formula interface, reminiscent of SAS proc tabulate. The RowFactor() function with the suppressfirst=FALSE and spacing=X arguments produces the necessary output provided I specify nopagebreak = "" instead of nopagebreak = "\\nopagebreak " or if I use a tabular environment instead of the longtable package. Unfortunatley, some of my tables are long and span page breaks. The tables package supports the LaTeX longtable package, as implemented on pages 26-28 of the tables vignette; but it doesn't seem to work with my data tables or with my minimal reproducible example.

Reproducible R/Sweave code:

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage{booktabs}

\begin{document}
\SweaveOpts{concordance=TRUE}
\section{Table test}

<<test, echo=TRUE, results=tex>>=
options(width=80)
require(tables)
set.seed(10)
df <- data.frame(id=seq(1,3,1), 
                 m=sample(month.name, 3, replace=TRUE), 
                 l=sample(LETTERS, 3, replace=TRUE), 
                 nums=sample(1:100, 3, replace=TRUE))
latex(
  tabular(
    RowFactor(m, 
              suppressfirst=FALSE,
              ## default argument
              nopagebreak = "\\nopagebreak ",
              ## works if specify the following
              #nopagebreak = "",
              spacing=3)
    +RowFactor(l, 
              suppressfirst=FALSE,
              ## default argument
              nopagebreak = "\\nopagebreak ",
              ## works if specify the following
              #nopagebreak = "",
              spacing=3)
    +RowFactor(nums, 
               suppressfirst=FALSE,
               ## default argument
               nopagebreak = "\\nopagebreak ",
               ## works if specify the following
               #nopagebreak = "",
               spacing=3) ~1, data=df), 
  options=list(tabular="longtable")
)

@
\section{Session info}
<<sessionInfo>>=
sessionInfo()
@


\end{document}

Best Answer

All \nopagebreak's are undesired:

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage{booktabs}

\begin{document}

\section{Table test}

\begin{longtable}{llc}
\hline
 &  & \multicolumn{1}{c}{All} \\ 
\hline
m & \rule{0pt}{1.7\normalbaselineskip}April  & $1$ \\
 &  July  & $1$ \\
 &  June  & $1$ \\
l & \rule{0pt}{1.7\normalbaselineskip}C  & $1$ \\
 &  F  & $1$ \\
 &  S  & $1$ \\
nums & \rule{0pt}{1.7\normalbaselineskip}28  & $2$ \\
 & 62  & $1$ \\
\hline 
\end{longtable}
\end{document}

enter image description here