Discussion:
LaTeX error with longtable
(too old to reply)
jel
2009-03-05 21:51:08 UTC
Permalink
I'm having difficulty with the simplest longtable. Here's an example:

\documentclass[12pt,letterpaper]{article}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{dcolumn}

\begin{document}

\newcolumntype{G}{>{\raggedright\columncolor[gray]{0.95}}m{2in}}
\newcolumntype{H}{>{\centering}m{1in}}

\begin{longtable}{GHGH}
\caption{My Caption} \\ \hline
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C}
& \multicolumn{1}{c}{D} \endhead \hline
a & b & c & d \\ \hline
a & b & c & d \\ \hline
\end{longtable}

\end{document}

When I run this through LaTeX I get the following error:

! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \penalty \@M \futurelet \@***@token
\LT@@h...
l.14 a & b & c & d \\ \hline

I have 4 columns in the table so I need 3 alignment characters (&) per
row which I have. I haven't missed the "\\" at the end of each row.
I'm sure I'm missing something very simple but I can't figure it out.
Please educate me.

John
Heiko Oberdiek
2009-03-05 22:29:55 UTC
Permalink
Post by jel
\documentclass[12pt,letterpaper]{article}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{dcolumn}
\begin{document}
\newcolumntype{G}{>{\raggedright\columncolor[gray]{0.95}}m{2in}}
\newcolumntype{H}{>{\centering}m{1in}}
\begin{longtable}{GHGH}
\caption{My Caption} \\ \hline
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C}
& \multicolumn{1}{c}{D} \endhead \hline
a & b & c & d \\ \hline
a & b & c & d \\ \hline
\end{longtable}
\end{document}
! Misplaced \noalign.
\hline ->\noalign
l.14 a & b & c & d \\ \hline
The \\ only ends a line inside the cell, because of it's type "m"
similar to "p". Use \tabularnewline:
a & b & c & d \tabularnewline \hline
Or read
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=tabcellalign
How to alter the alignment of tabular cells

Yours sincerely
Heiko <***@uni-freiburg.de>
Enrico Gregorio
2009-03-05 22:38:04 UTC
Permalink
Post by jel
\documentclass[12pt,letterpaper]{article}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{dcolumn}
\begin{document}
\newcolumntype{G}{>{\raggedright\columncolor[gray]{0.95}}m{2in}}
\newcolumntype{H}{>{\centering}m{1in}}
\begin{longtable}{GHGH}
\caption{My Caption} \\ \hline
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C}
& \multicolumn{1}{c}{D} \endhead \hline
a & b & c & d \\ \hline
a & b & c & d \\ \hline
\end{longtable}
\end{document}
! Misplaced \noalign.
\hline ->\noalign
l.14 a & b & c & d \\ \hline
I have 4 columns in the table so I need 3 alignment characters (&) per
row which I have. I haven't missed the "\\" at the end of each row.
I'm sure I'm missing something very simple but I can't figure it out.
Please educate me.
It's a known feature: the command \centering redefines \\ as
"break the line here", so you can't use it any more to end the
table rows. Either use \tabularnewline instead of \\ to end the
table rows or say

\newcolumntype{H}{>{\centering\arraybackslash}m{1in}}

since \arraybackslash restores the original meaning of \\ in a
tabular environment (and in longtable as well).

In this case you'll use \newline to end lines in the table cells.
The same applies to \raggedright and \raggedleft.

Ciao
Enrico

Loading...