# 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}


## 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}

• What's \nopagebreak supposed to do? – egreg Dec 2 '13 at 18:56
• @egreg In this context, \nopagebreak is intended to keep the table from breaking within table sections (e.g., ok to break between l, m and nums but not between the individual subrows. See LaTeX help, this question1 and this question2 which describe \nopagebreak. – penguinv22 Dec 2 '13 at 20:11
• It has no place where you put it. – egreg Dec 2 '13 at 20:48
• @egreg, knowing that my small reproducible example is intended to demonstrate a problem with long data tables, and that the tex code was generated dynamically, what do you think about putting nopagebreak after each \\  except at the end of each grouping of rows? This seems to compile correctly. Can you anticipate inherent problems with this approach? Ideally, the solution would be generalizable to the tool chain. If I suggest making a change to the R tables package regarding its handling of page breaks in longtables, I want to make the suggestion as robust as possible. – penguinv22 Dec 2 '13 at 21:08
• In order to avoid a page break at \\, use \\* – egreg Dec 2 '13 at 21:15

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}


• In my question, I note that removing \nopagebreak avoids the misplaced \align error message. I'm interested in your rationale. Why are all \nopagebreak undesired? Is it a technical reason? In my context of needing to present long tables of categorical data, I would prefer that the longtable breaks between row categories (e.g., m, l, or nums) but not between the sub-rows. – penguinv22 Dec 2 '13 at 20:18
• @penguinv22 saying undesired is just a polite way of saying they are a syntax error that can do nothing but generate an error in the position used. You used them in the middle column so I can't really guess the intended meaning as there is no possibility of page breaks being considered in the middle of the row. If you want to prevent page breaking between rows use \\* rather than \\  You can use \nopagebreak but it has to be at the start of the row \\\nopagebreak is equivalent to \\* – David Carlisle Dec 2 '13 at 22:57

This isn't the answer to the original post, but I came here Googling for "Longtable misplaced noalign" so my eventual solution might be useful for someone.

In my case I needed a \\ after the \caption and \label. This seems incredibly counterintuitive to me so if anyone can explain why or tell me where this is documented I'd be grateful.

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\begin{document}
\begin{longtable}{ll}
% Uncomment the \\ on the next line and the misplaced \noalign disappears!
\caption{Captions should appear on top of tables}\label{tbl:table}%\\
\toprule
& foo \\
\midrule
bar           & 1 \\
baz           & 2 \\
\bottomrule
\end{longtable}
\end{document}

• I can't tell you why that works but I had the same problem and this worked perfectly, thanks! – Konrad Höffner Jan 15 '18 at 11:47