The LaTeX2e source fails to emphasize that \linebreak
and \nolinebreak
deals with horizontal list output, while \pagebreak
and \nopagebreak
deals with vertical list output. It's just mentioned to be analogous.
Here is the code for \linebreak
, from latex.ltx
:
\def\linebreak{\@testopt{\@no@lnbk-}4}
\def\nolinebreak{\@testopt\@no@lnbk4}
\def\@no@lnbk #1[#2]{%
\ifvmode
\@nolnerr
\else
\@tempskipa\lastskip
\unskip
\penalty #1\@getpen{#2}%
\ifdim\@tempskipa>\z@
\hskip\@tempskipa
\ignorespaces
\fi
\fi}
From this we see that both \linebreak
and \nolinebreak
calls \@no@lnbk
internally. Calling \linebreak
results in the first argument of \@no@lnbk
to be a negative -
, while \nolinebreak
doesn't supply a first argument (or leaves it empty). In fact, \nolinebreak
is equivalent to \linebreak[4]
.
Within \@no@lnbk
, a test is made to ensure you're not in v
ertical mode
(\ifvmode
). If you are in vertical mode, \linebreak
and \nolinebreak
will result in an error (\@nolnerr
or "There's no line here to end."). The following example replicates this failure:
\documentclass{article}
\begin{document}
Lorem ipsum
\linebreak
\end{document}
If you are not in vertical mode, the last
skip
is stored, undone (\unskip
) and a \penalty
is inserted. Penalties can increase or decrease the "badness" of what is being set on the page. Inserting a negative penalty (when you're using \linebreak
) reduces the badness and therefore encourages a breaking point, while a positive penalty (when using \nolinebreak
) increases the badness and therefore discourages a breaking point.
Once the penalty has been inserted, the original skip is replaced (and subsequent spaces inserted in the code is ignored (\ignorespaces
). The penalty has to be inserted before the skip to avoid visually-odd breaking points.
\linebreak
is equivalent to \linebreak[4]
, while
\linebreak[0]
inserts \penalty -0
(not necessary);
\linebreak[1]
inserts \penalty -\@lowpenalty
(\penalty -51
);
\linebreak[2]
inserts \penalty -\@medpenalty
(\penalty -151
);
\linebreak[3]
inserts \penalty -\@highpenalty
(\penalty -301
); and
\linebreak[4]
inserts \penalty -\@M
(\penalty -10000
)
\nolinebreak
inserts \penalty \@M
(\penalty 10000
)