44

What is the difference between \nobreak and \nolinebreak?

I noticed that a lot of solutions in TeX.SE use the former, but I thought the latter is LaTeX while the former is plain TeX. And LaTeX macros are often more fleshed-out versions of the more rudimentary TeX equivalents. Which is better for which situations?

2 Answers 2

54

\nobreak is defined in plain TeX (and given the same definition in LaTeX) as

\penalty \@M

which is \penalty 10000 which is an infinitely bad place to break (a line if in horizontal mode or a page if in vertical mode).

\nolinebreak Is a LaTeX command that tests that it is in horizontal mode, and gives an error message if not, and then can give one of 5 different penalty values giving different strengths of hint \nolinebreak[0] to ... \nolinebreak[4] 4 is the default and gives \@M the same as \nobreak. The actual penalties inserted for values 0–4 are set by the class file.

The values are [0] is penalty 0, 4 is penalty 10000 and 1–3 are set (in article class) by

\@lowpenalty   51
\@medpenalty  151
\@highpenalty 301

There is also some code to fix white space so that

a \nolinebreak[0] b
a\nolinebreak[0] b
a \nolinebreak[0]b

each get exactly one word space between a and b if no break happens.

So in the usual case the two commands do the same thing. The LaTeX one takes more time and because of the optional argument testing it used to be fragile (it is robust in LaTeX releases since 2019). So since most LaTeX2e class files use quite low level code you usually see \nobreak or even just \penalty\@M in class files but in documents it is better to use the LaTeX form usually for the extra error checking and the interface to standard levels of penalty hint set by the class.

2
  • Very useful, thanks. Isn't a small update needed in the last paragraph? The command seems to be robust nowadays, or am I mistaken?
    – frougon
    Commented Jul 21, 2022 at 7:15
  • 1
    @frougon yes, almost everything is robust these days, unless some package re-instates a fragile version. Apparently most machines have more than 640K of memory available, and we can afford the extra space. Commented Jul 21, 2022 at 7:25
33

The \nobreak command is not documented in the LaTeX manual nor in the "LaTeX Companion". Its meaning is just \penalty10000 which makes it useful both in horizontal and vertical mode, but this ubiquity makes it a bit dangerous.

The meaning of \nolinebreak is "look if there's an optional argument and, if not, call the inner macro \@no@lnbk with argument 4", which means doing

\ifvmode
  \@nolnerr
\else
  \@tempskipa\lastskip
  \unskip
  \penalty\@M
  \ifdim\@tempskipa >\z@
    \hskip\@tempskipa\ignorespaces
  \fi
\fi

which checks whether we are in horizontal mode and does a bit of juggling: if \nolinebreak is preceded by a space (a normal interword space or an explicit \hspace), it is removed, the penalty is issued and the space is reinserted.

A bad habit (of which I admit to be guilty) is to use the shorter command when it's sure that it will do the right thing. For instance,

$a=\nobreak b$

is faster than $a=\nolinebreak b$ because there's no check to be performed and no space to be shifted.

One can also find \nobreak just after a \par, meaning that a vertical space after \par\nobreak will never be used for a page break.

Of course it's better to use \par\nopagebreak in this case. But, alas, bad habits prevail. Use \nopagebreak.

(I promise I'll be more LaTeX savvy in the future.)

2
  • 12
    +1 (even though I like my answer better, you need the rep:-) I am sure the community will hold you to that promise. Commented Jan 17, 2013 at 13:33
  • 1
    @DavidCarlisle Yeah, let's report user egreg to the TeX police for every offense spotted! (just kidding) Commented Jan 18, 2013 at 2:39

You must log in to answer this question.

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