[Tex/LaTex] How to get the width of \hfill

tex-core

In my LaTeX document, I have a first line with a given spacing obtained using \hfill, then a paragraph, and then a third line in which I would like to use the same spacing as in the first line.

The first line is something like:

a\hfill b\hfill c\hfill d

How do I use the same spacing between the elements of my third line as what \hfill does between the elements of my first line? The following example does not work, but shows the idea of what I am trying to achieve:

\newlength{\lengthofhfill}
a\hfill b\hfill c\hfill d\setlength{\lengthofhfill}{\widthof{\hfill}}

This is another paragraph.

e\hspace{\lengthofhfill}f\hspace{\lengthofhfill}g

I am guessing that there must be a variable that "knows" the length of an \hfill somewhere, I just cannot figure out what it is or how to use it.

As a side question: Is there any resource on these kind of low-level problems with TeX? I never know where to look to solve them, beside Google. I'd love to know a place in which I can see for myself roughly how \hfill is implemented, for instance, at a level in-between the LaTeX documentation and the source level.

Best Answer

There is no variable where the size used for \hfill is used.

Without taking \parindent into account, for simplicity, your first example is equivalent to saying

\hbox to\hsize{a\hfill b\hfill c\hfill d}

TeX measures the “natural width” of the box as if it had been \hbox{a{}b{}c{}d}, since the natural width of \hfill is zero. Then it computes the difference between the requested size and this natural width and divides the excess equally between the three spaces.

If Plain TeX with the standard settings is used, the width of \hbox{abcd} is 20.5556pt, while \hsize is 449.19939pt. Thus each space will be 149.73312pt wide. In its internal representation, TeX would have

\hbox(6.94444+0.0)x469.75499, glue set 149.73312fill
.\tenrm a
.\glue 0.0 plus 1.0fill
.\tenrm b
.\glue 0.0 plus 1.0fill
.\tenrm c
.\glue 0.0 plus 1.0fill
.\tenrm d

so the information can be seen, but it's not available at the programming level.

So if you have to use the information, you have to compute it with boxes:

\newdimen\usedhfill
\setbox0=\hbox{a{}b{}c{}d}
\usedhfill=\hsize
\advance\usedhfill by -\wd0
\divide\usedhfill by 3

Note. Why \hbox{a{}b{}c{}d} and not \hbox{abcd}? Because in the latter case kerning between letters would take place, which it doesn't when \hbox{a\hfill b\hfill c\hfill d} is built.

Related Question