[Tex/LaTex] \midrule before \endhead in longtable environment with booktabs package

booktabsheader-footerlongtable

I am trying to use longtable with repeating headers on each page. For example:

Column 1 & Column 2 & Column 3 \endhead \midrule

results in an ugly multi-page table. The horizontal rule separating the headers from the data appears only on the first page.

In an attempt to fix it:

Column 1 & Column 2 & Column 3 \midrule \endhead

I'd get the following error upon compile:

! Misplaced \noalign.
\midrule ->\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global \@...
l.6 ...) & Column 2 & Column 3 \midrule
                                                   \endhead

How do I make the horizontal rule also appear on every page?

Here's a screenshot of the part of the manual where \endhead is described:
alt text

Best Answer

It seems that you simply forgot the double \\

\documentclass{article}
\usepackage{geometry}
\geometry{paperwidth=5cm,paperheight=5cm}%a very small page
\usepackage{longtable,booktabs}
\begin{document}
    \pagestyle{empty}
    \begin{longtable}{ll}
        \caption{My longtable} \\   \toprule
        col 1 &col 2  \\ \midrule
        \endfirsthead
        \toprule
        col 1 & col 2 \\ \midrule
        \endhead
        \hline
        \multicolumn{2}{c}{Continued}\\   \bottomrule
        \endfoot
        \bottomrule
        \endlastfoot
        1 & 2\\   1 & 2\\   1 & 2\\   1 & 2\\   1 & 2\\   1 & 2\\   1 & 2\\   1 & 2\\   
    \end{longtable}

\end{document}

alt text