7

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]

Sigur
  • 37,330
  • 1
    There are several of these questions, but with more specific contexts than yours: http://tex.stackexchange.com/questions/95236/as-first-character-in-table-row http://tex.stackexchange.com/questions/34466/error-with-table http://tex.stackexchange.com/questions/144694/not-working?lq=1 (not an exhaustive list). Voted, for generality. :-) – Paul Gessler Jun 30 '14 at 20:34
  • @PaulGessler, I know about the problem inside arrays and tables. But since my problem is with a simple line, I decided to ask. Thanks. – Sigur Jun 30 '14 at 20:56

1 Answers1

12

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

  • Ow, how I didn't realize that! Usually I use \\[xx] inside arrays but I did never think it as a possible optional argument for \\ when using in text mode. Thanks. – Sigur Jun 30 '14 at 20:59
  • @Sigur It was the use in arrays that clued me in to the nature of your problem. \linebreak on the other hand, I had to do additional research, but figured it was of a similar nature. – Steven B. Segletes Jun 30 '14 at 21:45
  • +1: Just had the problem in tikz. \newline did not work in tikz and I ended up using \\\relax. – Dr. Manuel Kuehner Mar 26 '21 at 15:57