# What is glue stretching?

I had a question about dynamic vspaces (Dynamic vspace depending on text height) and there was a hint I should use a stretchable glue.

I would be glad, if someone can explain it and give me a simple example to understand how it works.

-
There's a very good paper by Nelson Beebe –  egreg Jul 26 '12 at 10:05
I sympathize. Google is totally useless for questions about plain TeX; you turn up many copies of the LaTeX manual, but not a single document even listing the TeX primitives! I didn't know about TeX by Topic until I came here. There is always the TeXbook, which is not free online, but still worth it. –  Ryan Reich Jul 26 '12 at 19:20

In TeX any glue is composed by a fixed part, a stretchable part and a shrinkable part. Each of these parts can be zero.

The general form to express a glue is: <fixed part> plus <stretchable part> minus <shrinkable part>. Each of these parts can be expressed in any of TeX units (mm, cm, pt, em, etc.). For example 2cm plus 2mm minus 1mm.

When composing a box which contains glues, TeX uses first their "natural dimensions" which is the fixed part (2cm in the above example). If the resulting box is underfull, then TeX expands all glue which has a non-zero shrinkable part, up to the amount specified in that glue. In our example, the glue can stretch 2mm at maximum. If the box contains several glues with different stretchability, each one is stretched proportionally to the given stretchability. If the box is still underfull after stretching all glue to its maximum, a warning about "Underfull box" is issued.

Analogously, if the box is overfull, TeX tries to reduce the space by shrinking that glue. So, in our example, the final inserted glue can vary between 1.9cm and 2.2cm, depending on the size of the box which contains that glue.

It is very frequent that the "outer box" is the page, and the glue is vertical space between paragraphs or environments. LaTeX sets up things so that these spaces have always stretchable and shrinkable parts, so that TeX can adjust those glues and have tha last line of the page adjusted at its bottom.

UPDATE: Infinite stretchability

The plus part in the glue can specify the value "infinite", through one of the following keywords: fil, fill or filll. Each of these infinites is "greater" than the preceding one. This deserves a detailed explanation.

If all glues in a box have finite stretchability, except one which has fil stretchability, then the whole box has infinite stretchability (it can never be underfull), and all the stretching required to adapt the box to a given size happens at that infinite glue. This is very useful, for example, to "push" the first and last line in a vertical box to the top and bottom, respectively, by inserting \vskip 0pt 1fil in between. In case of horizontal glue, this can serve to "push" the last words in a horizontal word against the right border of the box.

If there are two glues with infinite stretchability of the same order (i.e: both have 1fil or both have 1fill or both have 1filll, then the required stretching is equally divided among these two glues. This is useful to center content, by putting one infinite glue at the beginning and another at the end of the box.

If one of the infinites is of greater order than the other (for example, one is 1fil while the other is 1fill) the higher order infinite "wins" and gets all the stretch, and the other glue is not expanded at all.

Note that there is a 1 in front of fil. This factor can be changed to any other number, and then TeX distributes the stretching proportionally to that factor. So, if we have a glue of 1fill at the beginning of a box, and another glue of 2fill at the end, when the box is stretched, the second glue expands twice, with the result of the other content being "centered" in the first third of the box.

TeX provides some shortcuts for using these infinitely stretchable glues. You can type \vfil as equivalent of \vskip 0pt plus 1fil and analogously for \vfill, \hfil (for horizontal glue) and \hfill.

Finally, the infinite stretchability can be also negative. In this case it cancels the value of other positive infinitely stretchable glue in the same box. This strange glue is useful to implement some kind of "conditional stretching", conditioned to a break happeninng or not between the positive and negative fills. If the break happens, the positive fill is expanded and the negative one discarded. If the break does not happen, both fills are canceled one with each other, and there is no glue expansion. \vfilneg is the shorthand for \vskip 0pt -1fil.

UPDATE The following MWE showcases different examples of both horizontal and vertical stretchable glue:

\documentclass{article}
\begin{document}
\def\showhbox#1#2{\fboxsep=0pt\fbox{\hbox to #1{#2}}}
\long\def\showvbox#1#2{\parbox{3cm}{\fbox{\vbox to #1{#2}}}}

1. \showhbox{3cm}{Foo bar}  space betwen words will be stretched beyond limits\par
2. \showhbox{3cm}{Foo\hfil bar}   Same result, but without warning\par
3. \showhbox{3cm}{Foo bar\hfil}   Flush left\par
4. \showhbox{3cm}{\hfil Foo bar}  Flush right\par
5. \showhbox{3cm}{\hfil Foo bar\hfil}  centered\par
6. \showhbox{3cm}{\hfil Foo \hfil bar\hfil}  Evenly spaced\par
7. \showhbox{3cm}{\hfil Foo\hfil\hfil} "Centered" at first third\par
8. \showhbox{3cm}{\hfil Foo\hskip 0pt plus 2fil} Equivalent to the above\par
9. \showhbox{3cm}{\hfill Foo\hfil} The first fill "wins" the second fil, so flush right\par

\vskip 5mm

\showvbox{2cm}{Both\par at top}
\showvbox{2cm}{Top\vfil bottom}
\showvbox{2cm}{\vfil Both\par centered\vfil}
\showvbox{2cm}{\vskip 0pt plus 2fil 4 times more\par above than below\vskip 0pt plus 0.5fil}
\end{document}


Which produces the following output:

UPDATE infinite shrinkability

We can use the special keywords fil, fill and filll in the minus part of the glue, but, what would it mean? One could think that the minimum possible final value for the glue is zero, but in fact TeX can continue shrinking it to negative values. These negative values will cause overlap between the boxes separated by the negative glue. When one fil is specified for the minus part, the glue can be made arbitrarily negative.

This is useful for overlapping effects, such that the ones achieved by \rlap or \llap (which indeed are defined in terms of infinitely shrinkable glue, inside a \hbox of zero width).

TeX provides the shortcut \hss as an equivalent of \hskip 0pt plus 1fil minus 1fil. This is thus an infinitely stretchable and shrinkable glue, useful for special effects. For example, the macro \centerline is defined as a \hbox of the width of a line, with the content {\hss#1\hss}. The use of \hss instead of \hfill allows the content of the line to be larger than the line width, and in this case those contents will "spill" over the margins, the same amount in each margin, thanks of the two \hss.

The following MWE showcases some examples of \hss and its vertical counterpart \vss:

\documentclass{article}
\begin{document}
\def\showhbox#1#2{\fboxsep=0pt\fbox{\hbox to #1{#2}}}
\long\def\showvbox#1#2{\parbox{3cm}{\fbox{\vbox to #1{#2}}}}

Following boxes are all 2cm wide, no matter their contents:

1. \showhbox{2cm}{Foobar too long}  ~~Overfull box warning\par
2. \showhbox{2cm}{\hss Foobar too long}  ~~hss shrinks beyond zero, spilling to left\par
3. \showhbox{2cm}{Foobar too long\hss}  ~~hss shrinks beyond zero, spilling to rigt\par
4. \showhbox{2cm}{\hss Foobar too long\hss}  ~~Spills to left and right the same amount\par

\vskip 5mm
Following boxes are all 1cm tall, no matter their contents:
\vskip 5mm

\showvbox{1cm}{This is\par too much\par text\par and it spills out}
\showvbox{1cm}{\vss This is\par too much\par text\par and it spills out}
\showvbox{1cm}{\vss This is\par too much\par text\par and it spills out\vss}
\end{document}


Result:

-
Could you add a bit about fil fill and filll infinite glue? Yours is a good answer on the finite part so I don't really want to give a second answer but the question should have some discussion of infinite stretch as that is perhaps the most common form. –  David Carlisle Jul 26 '12 at 11:58
@JLDiaz -- actually, the stretchable part of the glue will stretch beyond the specified value, although if the stretch is really excessive, tex will complain. the shrinkable part is absolute. and regarding your addition about fil and \vfilneg, you may as well mention \vss and \hss; they're very useful for things like \rlap and friends. –  barbara beeton Jul 26 '12 at 14:30
thanks for adding the fil stuff (+1) –  David Carlisle Jul 26 '12 at 15:12
Could you add an MWE that illustrates this. –  user1189687 Jul 26 '12 at 16:31
@barbarabeeton Thank you for your commments, I updated the answer to include your suggestions. –  JLDiaz Jul 27 '12 at 8:26