# How to fix misplaced \noalign on afterpage with \clearpage and too high longtable head?

TeX Live 2014 on Xubuntu 14.04 LTS.

Minimal working example:

\documentclass[11pt, oneside]{report}
\usepackage{afterpage}
\usepackage{booktabs}
\usepackage{longtable}

\begin{document}
\afterpage{
\clearpage
}

\rule{1mm}{16cm}
\begin{longtable}{l}
\caption[]{\rule{2mm}{2cm}} \\
\toprule
Col 1 \\
\caption[]{\textit{continued from the previous page}} \\
\toprule
Col 1 \\
\hline
\multicolumn{1}{r}{\textit{continued on the next page}} \\
\endfoot
\bottomrule
\endlastfoot
1 \\
1 \\
1 \\
1 \\
\end{longtable}
\end{document}


Trying to compile this MWE results in the following message:

! Misplaced \noalign.
\newpage ->\noalign
{\break }
l.30 \end{longtable}


The \clearpage is necessary for the effect. Also, the first table head must not fit on the page. Reducing the first rule from 16cm to 14cm works.

On a side note: at 15cm other fancy stuff is happening, but I am not concerned about that right now.

Question(s): How can I avoid this error? What causes this problem exactly?

Both questions probably supplement each other.

Update 2015-12-03: After some rest, I realized that there probably is no simple solution to this question. Both, afterpage and longtable, need to work at the end of a page. When longtable wants to start a new page, afterpage would/should want to kick in. That may not be a technical explanation, but probably hits the nail on the head as well. I assume that one would have to make afterpage longtable-aware or the other way around to make this work. In the meantime, I am using a \newpage right before the longtable.

Before posting, I looked at these related questions and did not find an answer in there: longtable misplaced noalign, Longtable breaks afterpage, https://tex.stackexchange.com/questions/147169/misplaced-noalign-when-using-afterpage-clearpage-with-either-onehafspacing, captionof in longtable gives Misplaced \noalign, Longtable alignment with caption, ! Misplaced \omit. Longtable Error and Caption Misalign Extra alignment tab has been changed to \cr.

• Put the longtable INSIDE the \afterpage, after the \clearpage. Dec 2, 2015 at 20:38
• Unfortunately, this is just a workaround. Currently, I am using a \newpage before the longtable which I find cleaner in my case. There, \afterpage contains \clearpage before and after a \begin{landscape} ... \end{landscape}. Dec 3, 2015 at 11:37
• Yes, same issue. It's a real pain in the butt. Oct 1, 2018 at 15:57

Funny, or not, I have the exact same issue.

Since the latex code is generated I can't manually change the code to "fit".

Here's my minimal example, which isn't as minimal as I want to.

\documentclass[10pt,a4paper]{article}
\usepackage{fancyhdr, lastpage}
\usepackage[]{geometry}
\usepackage{longtable}
\usepackage{afterpage}
\pagestyle{fancy}
\begin{document}