Misplaced \noalign error

 \documentclass[a4paper]{article}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[italian]{babel}

\begin{document}
\thispagestyle{empty}
\ensuremath
\begin{tabular}{|c|c|c|c|c|c|}
\hline
& Lunedì & Martedì & Mercoledì & Giovedì & Venerdì \\
\hline
8-9 & & & (8.30) P (CP) & & \\
\hline
9-10 & P (GD) & & P (CP) & & \\
\hline
10-11 & P (GD) & D (FC) & P (CP) & & D (FC) \\
\hline
11-12 & & D (FC) & F, S, D (MS) & & P (GD), D (FC) \\
\hline
12-13 & & D (FC) & F, S, D (MS) & & P (GD), D (FC) \\
\hline
13-14 & & & & & \\
\hline
14-15 & P (CP), F (PS) & & F (PS) & & F, S, D (MS) \\
\hline
15-16 & P (CP), F (PS) & & F (PS) & & F, S, D (MS) \\
\hline
16-17 & & D (RO) & & D (RO) & \\
\hline
17-18 & & D (RO) & & D (RO) & \\
\hline
\end{tabular}

\end{document}


I'm getting this error

! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=}\fi \hrule \@height \arrayrulewidth \futurelet...


I've been looking for similar questions for days, but none of the ones I've found solve my problem

• The culprit is \ensuremath. Why did you use it? Sep 28 '19 at 19:16
• Without \ensuremath, I get ! Missing $inserted. <inserted text>$
– ygh
Sep 28 '19 at 19:36
• With this very code??? Sep 28 '19 at 19:37
• @Bernard: I can confirm this. The error seems to be caused by the ì in combination with the \usepackage[latin1]{inputenc} and pdflatex. Commenting out said package (and of course \ensuremath) the code works perfectly fine (but yields and overfull box warning due to the table being too wide). Sep 28 '19 at 19:40
• It seems so. I found some question about "! Missing $inserted" solved with \ensuremath and so i put it in my code. It worked, I hope it is not really the cause of the new problem – ygh Sep 28 '19 at 19:41 1 Answer Just for the fun of it, let's see what happens (bullets denote a space in the macro name). 1. \ensuremath expands to \protect\ensuremath• 2. \protect is \relax in this context, so it disappears 3. \ensuremath• expands to \ifmmode\expandafter\@firstofone\else\expandafter \@ensuredmath\fi 4. Since we're not in math mode, \expandafter\@ensuredmath\fi remains 5. \expandafter removes the \fi and \@ensuredmath remains The definition of \@ensuredmath is % latex.ltx, line 4666: \long\def\@ensuredmath#1{$\relax#1$}  What's taken as argument of \@ensuredmath? No brace follows, the next token is \begin, we get $\relax\begin$ Now LaTeX happily proceeds, because there is a macro \$. A long series of errors follows, but if you skip them all, you'll see that LaTeX typesets

where the $symbol is clearly visible, generated by the \$ produced by \begin. No tabular environment is started, so \hline is completely out of place (the shown error is indeed Misplaced \noalign as you can check by a simple document such as \documentclass{article} \begin{document} \hline \end{document}  No, adding \ensuremath if an error Missing inserted pops up is generally the wrong thing to do. Take as general rule that \ensuremath should never be used in the document environment; it has its uses in command definitions.

The error Missing $inserted should be taken care of by looking where math mode is wrongly started or unfinished. If I remove the wrong \ensuremath, the example document compiles well when encoded as Latin-1. If I encode it as UTF-8, then the Missing$ inserted error pops up.

Solution: your document is UTF-8 encoded, so change \usepackage[latin1]{inputenc} into

\usepackage[utf8]{inputenc}
`

(or remove the call altogether if you're using an up-to-date TeX distribution).