[Tex/LaTex] The LaTeX \obeylines command not working for the entire document if document contains a table

line-breakingmiktexoutput

I'm using MikTeX 9.6.0.5 on windows 8.1. The following displays all the four lines in a new line, as expected:

\documentclass{article}
\begin{document}
\begin{obeylines}
This is first line.
And the second line.
Third line.
Fourth line.
\end{obeylines}
\end{document}

But if I insert a table between second and third line as:

\documentclass{article}
\begin{document}
\begin{obeylines}
This is first line.
And the second line.
\begin{tabular}{|l|l|l|}
  \hline
  A11&A12\\ \hline
  A21&A22\\
  \hline
\end{tabular}
Third line.
Fourth line.
\end{obeylines}
\end{document}

I get the error:

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

According to a response and comments by Ian Thompson here we should be able to obeylines for the entire document.

Best Answer

\obeylines defines the end of line to be the definition of \par at the point that the \endlines command is used. so here it is \endgraf essentially. You can not have that primitive before \hline as it has to be the first thing on the row.

Using \obeylines for a whole document only really makes sense if you are typesetting some original plain text document. Mixing \obeylines with tex markup is almost certain to fail somewhere.

Related Question