[Tex/LaTex] How to fix misplaced \noalign on afterpage with \clearpage and too high longtable head

afterpagelongtable

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 \\
    \hline \endfirsthead
    \caption[]{\textit{continued from the previous page}} \\
    \toprule
    Col 1 \\
    \hline \endhead
    \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.

Best Answer

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}
\setlength{\headsep}{0.1mm}
\begin{document}
\newgeometry{total={210mm,297mm}, head=56pt, top=10mm, left=10mm, right=10mm, bottom=58pt, includeheadfoot}
\afterpage{\clearpage\lfoot{Page}}

\begin{tabular}{l}
TABLE1\tabularnewline[210mm]
\end{tabular}

\begin{longtable}{ l}
TABLE2\\[20mm]
\endhead
B\tabularnewline
\end{longtable}
\end{document}
Related Question