2

If I type $\section{Second section}$ or

\begin{enumerate}
\item first bla bla 
\item second bla bla
$\item last bla bla$
\end{enumerate}

I get the error message ! Missing $ inserted... or ! LaTeX Error: Command \item invalid in math mode.

But if I compile the following code, I get the (correct) output, but let say, stringe output.

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\section{First section}
\lipsum[1]
\begin{table}
\section{Second section}
\caption{Just a table}
\end{table}
\begin{enumerate}
\item first bla bla
\item second bla bla
\begin{table}
\item last bla bla
\caption{Just a table}
\end{table}
\end{enumerate}
\section{Last section}
\lipsum[1]

\end{document}

enter image description here

Is this a bug? floating objects can go (any where) e.g. an \item goes before preceding items or worse within items of another enumerate.

Is it a feature? numbering still correct, and one can need this.

main question: why latex doesn't prevent \section, \item, ... commands from going inside a float environment and we know, conversely that \caption doesn't work outside floating environments. i.e. why there is no

\gdef\@infloaterr#1{%
   \relax
   \ifx\@captype\@undefined
   \else
 \@latex@error{Command \protect#1  inside float}
   \fi}

similar to \@inmatherr.

jarnosc
  • 4,266
touhami
  • 19,520
  • Hello, what in the world is $\item last bla bla$ supposed to do?! And the reason why \section can be placed inside floats is probably that disallowing it would cost a lot of resources, which were very precious back in the 90s. – yo' Aug 03 '15 at 07:53
  • Here to show goodness of (La)TeX if by arror you type $\item last bla bla$ rather than \item $last bla bla$ – touhami Aug 03 '15 at 07:56
  • A speed limit signal tells you that you shouldn't go faster than allowed, but has no way to prevent you from doing so. – egreg Aug 03 '15 at 07:56
  • @touhami Ah it seems I missed the fact that it should be wrong, sorry :D – yo' Aug 03 '15 at 07:57
  • There are many incorrect inputs that do not generate an error message. \section inside a float is an error it is just not trapped by the system (it would have taken an unreasonable amount of memory to do so at the point that latex was designed.) – David Carlisle Aug 03 '15 at 09:18
  • @DavidCarlisle you say memory, everything become black for me. what about \item you don't think same? – touhami Aug 03 '15 at 14:53
  • @DavidCarlisle can you list some of these many incorrect inputs? – touhami Aug 03 '15 at 14:56
  • @touhami how long a list do you want:-) \documentclass{article} \begin{document} \section[z]{\section{aa}} \fbox{\egroup\egroup aaa\bgroup\bgroup} \end{document} – David Carlisle Aug 03 '15 at 15:32
  • @DavidCarlisle well, your first comment is sufficient for me, however it seem almost same as yo's comment if one of you can make it answer i'll accept it. – touhami Aug 03 '15 at 17:40
  • @touhami the math/non-math distinction is built in to tex it is much easier to test for that than for every command to test for every place where it makes no sense. – David Carlisle Aug 03 '15 at 18:21

1 Answers1

5

There are many incorrect inputs that do not generate an error message. (\section[z]{\section{aa}} for example).

\section inside a float is an error it is just not trapped by the system (it would have taken an unreasonable amount of memory to do so at the point that latex was designed.)

The math/non-math distinction is built in to tex it is much easier to test for that than for every command to test for every place where it makes no sense.

Similarly \caption needs to know what type of caption to make (stored internally in \@captype) so the test there is much easier as the default definition is that it generates an error, \caption just locally works in contexts that enable captions.

David Carlisle
  • 757,742