4

What are the actual definitions of \newline, \linebreak and \break (and also \par)? Is the latter a TeX primitive? I can't find the relevant documentation anywhere regarding their precise function, other than general discussions, so perhaps someone can point me in that direction too.

This question originates from finding that \newline does not work as I had expected in a \raggedleft environment. I found this useful answer, which explains what is going on by declaring that \newline boils down to \hfil\break, which I can see is true upon experimentation but cannot find documentation for.

2 Answers 2

8

TL;DR

Simple strategy: never use \newline unless you have precise control of the environment.

Simple strategy: never use \break in a LaTeX document environment.

Longer answer

This is not going to be simple, sorry.

The definition of \newline at begin document is

\@normalcr\relax

and we see

% latex.ltx, line 7444:
\protected\def\@normalcr{%
  \let \reserved@e \relax
  \let \reserved@f \relax
  \@ifstar{\let \reserved@e \vadjust \let \reserved@f \nobreak
             \@xnewline}%
  \@xnewline}

% latex.ltx, line 7454:
\def\@xnewline{\@ifnextchar[% ] bracket matching
                  \@newline
                 {\@gnewline\relax}}

% latex.ltx, line 7459:
\def\@gnewline #1{%
  \ifvmode
    \@nolnerr
  \else
    \unskip \reserved@e {\reserved@f#1}\nobreak \hfil \break
  \fi}

The reason for the complications is that the “standard” definition of \newline is the same as for \\, which can be followed by * and an optional argument.

So, no, \newline is not really \hfil\break.

However, \newline remains the same inside flushleft, flushright or center, whereas \\ changes. Why doesn't \newline work inside flushright and center?

Let's consider

\documentclass{article}

\begin{document}

\noindent X\dotfill X

\begin{flushright}
X \newline Y
\end{flushright}

\begin{center}
X \newline Y
\end{center}

\end{document}

enter image description here

The first line inside flushright has \hfil glue at the left and also at the right of X, so the net effect is to center the X. Inside center it's even worse, because now there is again \hfil glue before X, but two globs of \hfil glue after it. Thus X gets placed with twice as much space after than before.

Simple strategy: never use \newline unless you have precise control of the environment.

To the contrary, \\ inside flushright and center gets a different definition so as to countermand the infinitely stretchable glues at either side.

What is \break? It's a macro inherited from plain TeX and used inside the kernel code for brevity.

Simple strategy: never use \break in a LaTeX document environment. It's possible to use it in definitions (if you know what it does).

The preferred command is \linebreak for an important reason: \linebreak checks the mode it is found in, whereas \break doesn't. Both eventually issue \penalty-10000 that forces a break: a line break if found in horizontal mode, a page break in vertical mode.

If you inadvertently issue \break between paragraphs, you get a page break. To the contrary, \linebreak between paragraphs will just tell you that

! LaTeX Error: There's no line here to end.
3
  • 1
    This looks excellent. It will take me a while to digest it, but will then upvote it. Commented Mar 26 at 23:14
  • Just to add some definitions which I did not know. Taken from ctan.org/pkg/source2e?lang=en or tug.ctan.org/macros/latex/base/ltspace.dtx : Commented Apr 3 at 8:30
  • The nolnerr is defined \gdef\@nolnerr{% 239 \@latex@error{There’s no line here to end}\@eha} where @eha is an error help message 'Your command was ignored...' It seems nolnerr is only generated by \newline and \\ in vertical mode. Also, relevant for \newline[<#1>] constructions, \def\@newline[#1]{\let \reserved@e \vadjust \@gnewline {\@vspace@calcify{#1}}} where \def\@vspace@calcify#1{\setlength\sp@ce@skip{#1}\vskip\sp@ce@skip} provides a vskip (making sure its a length by running the argument through \setlength). Commented Apr 3 at 8:30
0

After working through the TeX book and actually bothering to be more rigorous TeX-wise, I would like to add my own answer to complement the excellent answer above.

First, the meaning of any macro expanded once can be done using \meaning<your macro> in the LaTeX document and compiling. Alternatively, the source code for latex2e or latex3 is available at the latex-project. For example, the newline macros are defined here or with comments (for LaTeX2e) here.

Second, the plain TeX \break expands to either \linebreak or \pagebreak and gives a maximum negative demerit (-10000), ensuring that the TeX engine places a line break or new page at that point. This might mean that the hbox the line lives in is underfull, and so one should not use it on its own. Instead \hfil \break is better in plain TeX, which gives infinite (a 'small' infinity) glue to fill out the rest of the line. But on its own \hfil \break has a slight defect if the line is overfull - a null line will be introduced, as in

\hbox{aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
    aa aa aa aa aa} \hfil\break aa aa aa

To remedy this, the documentation linked to above tells us that the newline definition was modified to include \nobreak \hfil \break, to stop the overfull box automatically generating a line break. Finally, the prefix \unskip is added to remove any blank spaces at the end of the line, which is probably useful. Thus we want \newline to be the same as \unskip \nobreak \hfil \break - which it essentially is. The complicated definitions as seen in the other answer are explained below and are essentially to simultaneously treat \\ and its variants.

Third, \par is the same as leaving a blank line in plain TeX.

Now, I move onto unwrapping the complicated definitions of \newline and \\. The above answer is excellent. However, in the following I want to illustrate it in more concrete terms.

First, it is useful to note the following expansions (assuming plain LaTeX):

\newline --> \@normalcr\relax
\\       --> \@normalcr

Now, \@normalcr comes in a starred and unstarred variety. Its expansions are illustrated in the following diagrams, where I use the shorthand \e = \reserved@e and \f = \reserved@f:

Expansion of @normalcr

Expansion of @xnewline

Expansion of @gnewline

Hence, assuming we are not in vertical mode, in which case we will get a @nolnerr ("there is no line here to end") error, one has

\@normalcr        -->  \unskip \relax{\relax \relax} \nobreak\hfil\break
\@normalcr[1pt]   -->  \unskip \vadjust{\relax \vskip 1pt} \nobreak\hfil\break
\@normalcr*       -->  \unskip \vadjust{\nobreak \relax} \nobreak\hfil\break
\@normalcr*[1pt]  -->  \unskip \vadjust{\nobreak \vskip 1pt} \nobreak\hfil\break

It follows that \newline does not have a starred version or argument with [] delimiters, so only the following expansions are possible:

\newline   --> \unskip \relax{\relax \relax} \nobreak\hfil\break\relax
\\         --> \unskip \relax{\relax \relax} \nobreak\hfil\break
\\[1pt]    --> \unskip \vadjust{\relax \vskip 1pt} \nobreak\hfil\break
\\*        --> \unskip \vadjust{\nobreak \relax} \nobreak\hfil\break
\\*[1pt]   --> \unskip \vadjust{\nobreak \vskip 1pt} \nobreak\hfil\break

In the above, \unskip removes any existing spaces at the end of the line, \nobreak \hfil adds glue to fill out the rest of the line without breaking, then \break gives a maximum penalty (10000) to ensure a line break.

One should be a bit careful about the expansions of \\ and its cousins, because many environments redefine \\ as the answer above notes.

From the above, I conclude that a good intuition is that \newline behaves more or less like \hfil \break (adding infinite glue to the right and then breaking), and is also identical to the bare-bones version of \\ (the difference is a technicality - one has a final \relax).

The other variations include either a \vadjust{\vskip x} (unstarred version) or \vadjust{\nobreak \vskip x} (starred version) just before the line break. I have a question with two good answers here explaining the point of these. The short story is that the unstarred case introduces some vertical space before the next line, which is allowed to be swallowed by a page break, whereas the starred version generally prevents a page break between the line and the vertical space (i.e. it will move the line to the next page). The latter is not quite true at the beginning of a paragraph - see the linked question immediately above.

You must log in to answer this question.

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