0

I am studying boxes and glue in LaTeX and I try such a sample:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

\begin{document}
Algebra began with\\ computations similar to those of arithmetic, with letters standing for numbers.

Algebra began with\linebreak computations similar to those of arithmetic, with letters standing for numbers.

% kind of imitation of above with boxes:
\hbox to \textwidth{Algebra began with\hfill}

\hbox to \textwidth{Algebra began with}
\end{document}

Am I right that in case of \\ TeX puts a horizontal filler (\hfill or \hfil) to the end of line so that there is no an underfull hbox or it works somehow else? And in case of \linebreak, TeX just has an hbox of \textwidth width and TeX stretches all the spaces to fill all the line (it would be an underfull hbox). So does TeX processes this way?

1
  • Actually, TeX uses \cr (carriage return). \linebreak was not defined, and \\ was used for \backslash. Nov 12, 2023 at 16:16

2 Answers 2

3

You are asking as if asking about tex's linebreaking behaviour but \\ is a latex defined macro that does (after some checks for optional arguments}

\unskip \penalty10000 \hfil \penalty-10000

So the filling glue to avoid an underfull hbox is explicitly added by the command.

Conversely \linebreak (after checking for optional arguments) just does

\penalty-10000

(actually \linebreak does a bit more to normalise white space around the break but that's probably not relevant here)

3

The definition of \\ depends on the context.

In your case, LaTeX will eventually do \nobreak\hfil\break. Now you can check that \nobreak is essentially \penalty10000 which prohibits a line break and \break is \penalty-10000 which forces a line break.

Why the first one? Because otherwise \hfil could be taken as a line break point (and disappear). Instead it remains and a line break is forced after it.

To the contrary, \linebreak will eventually do \break, requiring TeX to have a line break there when the paragraph is typeset.

Note that TeX always takes an entire paragraph into consideration, not a single line at a time like most word processors do. Compare the two following examples.

\documentclass{article}

\begin{document}

This is some text that has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

\end{document}

enter image description here

In the first case TeX has no other choice than spreading the interword spaces in the top line, resulting in an underfull box. To the contrary, in the second paragraph the spreading can be distributed across two lines. You can check that if you remove \linebreak, the line would be broken just after the “and” that follows “HERE”.

However, if we try with

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

that has one more “goes on with nonsense words” we'd get

enter image description here

because the paragraph would be underfull anyway and concentrating the stretching in a single line makes the badness being minimum. But if we help TeX with nonzero \emergencystretch

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.{\emergencystretch=12em\par}

we get

enter image description here

where the stretching is again distributed in the lines before the forced break.

3
  • TeX takes in consider the entire paragraph because this way it can organize the text in the best way? So before printing the paragraph, TeX takes the whole paragraph and tries to redistribute line breaks and spaces to achieve the best look of the text? It's like a bird's-eye view instead of seeing just one line.
    – Vladimir
    Nov 12, 2023 at 11:56
  • and by adding non-zero \emergencystretch we make our lines more flexible\stretchable so that TeX now can redistribute all the spaces (according to the value of \emergencystretch) in the paragraph and there is no such an underfull line like it was before?
    – Vladimir
    Nov 12, 2023 at 12:00
  • 1
    @Vladimir Yes, that's right under all respects except that the third example is still a bit underfull.
    – egreg
    Nov 12, 2023 at 13:23

You must log in to answer this question.

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