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.

1 Answers1

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.
egreg
  • 1,121,712