8

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.

4
  • Have you tried just using \obeylines rather than an environment? I still believe that \obeylines is designed as a local effect and would not expect it to work across an entire document. Jun 5, 2015 at 6:33
  • 2
    It will compile if you make sure that the \hline are not on new lines. (You should also put the \end{tabular} on the same line if you want to avoid spurious vertical lines). But the active end-of-line chars will certainly bite you in various places. \obeylines is not something I would use for a large portion of a document. Jun 5, 2015 at 7:59
  • @UlrikeFischer, your suggestion worked. But I think as you, Christian and David have stated using obeylines with the entire document is almost certain to fail somehwere.
    – nam
    Jun 6, 2015 at 1:57
  • @ChristianLindig, just using \obeylines worked only in combination with the Ulrike's suggestion.
    – nam
    Jun 6, 2015 at 2:00

2 Answers 2

11

\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.

2
  • Based on David's opinion and others it seems in my case using obeylines for the entire document is not a good idea. I'm copying the content from an MS Word document to MikTeX and want to preserve the line breaks as in MS Word without putting \\ or \newline at the end of the lines. Is there another way to obey the lines globally in the entire document?
    – nam
    Jun 6, 2015 at 2:10
  • @nam that seems a very strange thing to do, the line breaking in Word is not very good, why preserve it if moving the document to TeX, why not leave the document as Word? If you really want to do it If you are doing the whole document with no tex markup then \obeylines might make sense as a first draft but use {\obeylines.... } to limit the scope, and each time you edit a section with tex markup, close off the obeylines for that section. Jun 6, 2015 at 9:12
1

When using obeylines append % to every line where we do not want to have the said effect.

In this case we can add % at the end of every line in the tabular element.

This will fix the error and make the table format correctly.

\begin{tabular}{|l|l|l|} %
  \hline                 %
  A11&A12\\ \hline       %
  A21&A22\\              %
  \hline                 %
\end{tabular}            %

More info in this TeX post.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .