[Tex/LaTex] Linebreak followed by bracket

line-breaking

I tried to find a topic here on this (possible problem) but I didn't.

I reported it at TeXstudio SF but Tim Hoffmann pointed me that it is not a problem of editor. After tested with nano and pdflatex I'm posting here.

The minimal code produces errors. OK, we can solve it using \\{}.

But is this a problem with TeXlive 2014 or there are some good reasons to have that behavior.

\documentclass{report}
\begin{document}
test\\ [foo]

test\linebreak [foo]

test\newline [foo]
\end{document}

! Missing number, treated as zero.
f l.3 test\ [foo]

Best Answer

Both \\ and \linebreak take an optional argument, and so you need to end the expansion with \relax, so that the bracket is taken as text, and not as an optional argument. The optional argument for \\, if used, is expected to be a length dimension indicating the additional vertical space to provide. And so [foo] does not qualify, thus generating an error.

For \linebreak, the optional argument is a number between 0 and 4. According to Lamport, "a larger value of [num] more strongly encourages or discourages the line break." The default is equivalent to 4. Thus, [foo] likewise breaks the expansion of \linebreak.

\newline, on the other hand, does not take an optional argument, and so [foo] following it works fine.

\documentclass{report}
\begin{document}
test\\\relax [foo]

test\linebreak\relax [foo]

test\newline [foo]
\end{document}

enter image description here

For example, test\\[12pt][foo]will add a larger gap after test and before [foo]:

\documentclass{report}
\begin{document}
test\\[12pt][foo]

test\linebreak\relax [foo]

test\newline [foo]
\end{document}

enter image description here