# What is the difference between 'fil' and 'fill'?

TeX provides the commands \vfil and \vfill (and their corresponding horizontal versions \hfil and \hfill).

These commands are actually defined as:

\vskip 0cm plus 1fil
\vskip 0cm plus 1fill


where 'fil' and 'fill' (and in fact filll) are units of infinite glue with increasing orders of infinity. Here's a quote from the TeXBook (p. 71):

TeX actually recognizes several kinds of infinity, some of which are “more infinite” than others. You can say both \vfil and \vfill; the second is stronger than the first. In other words, if no other infinite stretchability is present, \vfil will expand to fill the remaining space; but if both \vfil and \vfill are present simultaneously, the \vfill effectively prevents \vfil from stretching. You can think of it as if \vfil has one mile of stretchability, while \vfill has a trillion miles.

This much I know, at least in theory, and I also know that I can use the fil and fill versions of the spacing commands with different effects.

What I'm less clear on, is why the commands work the way they do, and what the whole concept of "infinite glue" actually means.

So can someone help to elucidate the Knuth quote a bit?

-
A pedantic point: these commands are not defined in terms of \vskip rather they are TeX primitives that are "essentially equivalent" to those \vskips. I'm not sure whether there is any significant difference, and why Knuth chose to implement these as primitives instead of macros. Perhaps I should ask a separate question.... – Lev Bishop Jun 17 '11 at 16:23
@Lev Bishop: because they are primitives, the underlying data structure can be shared by all instances of each of these predefined skips easily. This saved a considerable amount of memory in the original TeX implementations. It could have been made to work without the primitives, but that would have been slower (and less elegant on the pascal side). – Taco Hoekwater Jun 17 '11 at 16:34
Think ordinal arithmetic. – Willie Wong Jun 17 '11 at 16:35
@Willie: −1. Ordinal arithmetic has some odd properties not shared by TeX's glue calculations, non-commutativity perhaps being the biggest. Subtraction isn't entirely obvious, either. – Harald Hanche-Olsen Jun 18 '11 at 9:26
I had a hard time deciding which out of egreg's and Yiannis' answers to accept. Yiannis drew the picture, and egreg wrote the thousand words :-). Thanks to all who answered. – Alan Munn Jun 18 '11 at 13:59

Let's look at the simplest case

\hbox to \hsize{<horizontal material>}


Among the <horizontal material> there will be also glue, implicit (that is, space tokens) or explicit (\hskip commands).

TeX maintains two four dimensional vectors in order to compute the glue ratio, say v for the stretching and w for the shrinking. A glue such as

\hskip 4pt plus 2pt minus 1pt


contributes 2pt to the first component of v and 1pt to the first component of w. A glue such as

\hskip 6pt plus 1fil minus 0.5fill


contributes 1 to the second component of v and 0.5 to the third component of w. At the end we'll have

v=(finite, first order infinite, second order infinite, third order infinite)

and similarly for w, where the components are the sum of all contributions. TeX also maintains the sum of the natural widths of characters, boxes and glues in the <horizontal material>.

When TeX has finished gathering the material for the \hbox, it compares the natural width to the desired box width (in our example to \hsize) and decides what to do. If the natural width is equal to the desired width, it typesets the box. Otherwise it decides that it has to stretch or shrink the glue. In the former case it looks at v and in the latter to w.

Let's look at the stretching case (the other is similar). If v is zero, there' little to do: there's no glue or the glues all cancel with each other: the box will be underfull.

Otherwise one entry in v will be different from zero; TeX will choose the rightmost non-zero component. This is the order of infinity that wins (it may be the "finite" component). The excess space to fill is then distributed proportionally among the glues that contributed that order of infinity.

Let's look at some examples

\hbox spread 3cm{A\hskip 4pt plus 2pt minus 1pt
B\hskip 4pt plus 1fil minus 1pt
C\hskip 4pt plus 2fil minus 1pt
D}


The box must stretch by 3cm (it's a convenient syntax for doing experiments of this kind), so we have to compute v=(2pt,3,0,0). The first-order infinity wins, so the excess space will be divided adding 1cm between B and C, and 2cm between C and D; between A and B there will be a 4pt wide space (no stretching). The result is ( denotes the resulting space)

A<4pt>B<4pt+1cm>C<4pt+2cm>D

Let's see with

\hbox spread 3cm{A\hskip 4pt plus 2pt minus 1pt
B\hskip 4pt plus 1fill minus 1pt
C\hskip 4pt plus 2fil minus 1pt
D}


Here v=(2pt,2,1,0), so the second-order infinity wins and the 3cm wide space will go between B and C:

A<4pt>B<4pt+3cm>C<4pt>D

Third order infinities are rarely used, but they are there for emergency cases when one has to cancel second order infinities.

The coefficient before fil(ll) should be a decimal number less than 16384 in absolute value (there must be one). The minimum non-zero value is 2^(-16)=0.000015, so saying 0.000014filll is equivalent to say 0filll (and useless, of course).

TeX has some primitives equivalent to glue specifications:

\hfil = \hskip 0pt plus 1fil minus 0pt
\hfilneg = \hskip 0pt plus -1fil minus 0pt
\hss = \hskip 0pt plus 1fil minus 1fil
\hfill = \hskip 0pt plus 1fill minus 0pt

The same algorithm is used for shrinking, but no glue will be stretched to become less than its natural width, while all glues may be used for stretching (possibly resulting in an underfull box). The same holds for vertical boxes.

-
What does Knuth mean by first order infinite and second order infinite, is there a mathematical explanation or are they convenient methods for the tuples. – Yiannis Lazarides Jun 17 '11 at 16:51
@Yiannis: the method uses those vectors, as it would be impractical to use real lengths. The "one mile" versus "one trillion mile" is just a way of thinking to it practically. – egreg Jun 17 '11 at 17:03

As the quote given by you already states: fill overwrites fil. Think of fil like x for x to infinity, then fill is x2 (or maybe xx?).

The way I see it, the idea behind it is that normal formatting macros should use fil which will stretch infinite to e.g. center material. The infinite stretch factor simply fills out the remaining space. They only work if the existing space is predefined, e.g. using \vbox to ...{...} or \hbox to ...{...}. Multiple fils share the remaining space which can be used to center material. If required the user or package author can add a fill stretch manually which overwrites the normal fils. This way the normal formatting macros can still be used in this cases.

-
What – no mention of filll? filll is to fill what fill is to fil: a whole other level of infinity. Think of filll as a level of infinity to use for emergencies only, when you need something that is more stretchable than any amount of fill. I have found it handy on a couple of occasions. – Harald Hanche-Olsen Jun 17 '11 at 16:39
Even the tiniest non-zero amount of fill is infinitely more than even the maximum amount of fil, but TeX does not actually need arithmetic to decide that. It simply remembers an 'order' for glue stretching / shrinking levels, where normal units like pt, cm, in etc. are order 0, fil is order 1, fill is order 2, and filll is order 3. When decisions have to be made, higher orders always trump all lower order's values. – Taco Hoekwater Jun 17 '11 at 16:52
Interesting aside: LaTeX seems to want an order 0.5 infinity (more infinite than cm, less infinite than fil) which it fakes with 0.0001fil. See definition of \raggedbottom. – Lev Bishop Jun 17 '11 at 19:00

One addition to what Martin Scharrer already said: fil has an additive nature:

\hbox to \hsize{\hfil x\hfil\hfil x\hfil}


All the \hfils will stretch to the same amount and this means there will be twice as much space between the two x than there will be to the right and left of them.

-

Agreed, the TeXbook is a bit cryptic when it comes to explain these macros and the keword fil. First it refers to glue which is a misnomer for springiness as it can expand and shrink.

Consider the following diagram, which you can obtain with the minimal that follows:

\def\abox#1#2{
\vbox{\hrule
\hbox
{\vrule
\hskip#1
\vbox{\vskip#1\relax
#2%
\vskip#1}%
\hskip#1
\vrule}
\hrule}}

\def\testbox#1{\abox{0.2cm}{\hbox{#1}}}

\hskip 2cm\abox{0.4cm}{\hbox{
\vbox to 4cm{\vfil\testbox A}
\vrule\ \vbox to 4cm{\testbox B\vfil}
\vrule\ \vbox to 4cm{\vfil \testbox C \vfil}
\vrule\ \vbox to 4cm{\vfil \testbox D \vfil\vfil}
\vrule\ \vbox to 4cm{\vfil \testbox E \vfill}}}

\bye


fil is actually a measure and one of the few keywords of TeX, some other being "plus" "height", "pt" etc., i.e., keywords and not macros. In the original Pascal program these measures can result in infinities and I guess that is why Knuth keeps on referring to them as infinite glues.

Think of them as springs, even if they are called "glue", study the above diagram and code and hopefully they will become clearer.

-
“Glue” may be a misnomer, but I think Knuth stuck with it because it turned out to be immensely popular among early users, much more so than any alternative. I am pretty sure this is explained in the good book somewhere. – Harald Hanche-Olsen Jun 17 '11 at 16:42
@Harald Yes Knuth said so himself in many occassions and he mentions this in the TeXbook as well. – Yiannis Lazarides Jun 17 '11 at 16:45
Are you missing a \def from the beginning? – morbusg Jun 17 '11 at 16:56
@morbusg Oops! you right missed it on the cut and paste! – Yiannis Lazarides Jun 17 '11 at 16:58