This trick of catching until the first open brace can be used in many situations.
As other posters said, it allows to catch an optional argument expandably if it is not the last. It is possible in principle to get this to be fully robust with respect to nesting, but not implemented yet.
It can be used to parse the body of a definition provided by the user, to change it to fit your purposes while keeping a natural syntax. (More on parsing def below.)
I've used this trick primarily to parse a token list expandably without losing any brace. For instance,
to expandably uppercase or lowercase a given string (see this answer)
to fully expand a token list expandably (almost as well as the luatex primitive \expanded
(see this answer)
to expand tokens selectively, or in the reverse order
to write a primitive macro expander (i.e. take a file, and expand user-defined macros)
so basically any situation where you need to be careful with braces, but cannot use \futurelet
.
This trick only works if there is only one character with catcode 1 (begin group character). Also, we need to be able to put a sentinel at the end of the token list that we are manipulating: otherwise, in the absence of opening brace, we would get a runaway argument.
On parsing a definition: say that you want to give the user an easy way of defining a macro which possibly takes arguments, and always produces a boxed math result. Say that you also want the parameter text to be arbitrary. Either you let the user do everything, or you parse the definition using the trick you ask about.
%non-user-friendly
\def\foo_#1^#2#3#4{\fbox{$\sum_{#1}^{#2} \frac{#3}{#4}$}}
%more user friendly (perhaps)
\boxeddef\foo_#1^#2#3#4{\sum_{#1}^{#2} \frac{#3}{#4}}
To do that:
\documentclass{article}
\makeatletter
\def\boxeddef#1#2#{\boxeddef@aux{#1}{#2}}
\def\boxeddef@aux#1#2#3{\def#1#2{\fbox{$#3$}}}
\makeatother
\begin{document}
%\def\foo#1#2#3#4{\fbox{$\sum_{#1}^{#2} \frac{#3}{#4}$}}
\boxeddef\foo_#1^#2#3#4{\sum_{#1}^{#2} \frac{#3}{#4}}
\[
\foo_{a}^{b}{C}{D}
\]
\end{document}
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.
Best Answer
The
\TeX
macro is defined inplain.tex
asor in LaTeX (
ltlogos.dtx
) as(the difference is that LaTeX makes
\TeX
a robust macro, and adds the spacefactor correctionÂ\@
).You can use
\show\TeX
to see its definition in plain TeX, or in a recent LaTeX distribution,\ShowCommand\TeX
:This macro is hand-tuned to Computer Modern, so if you change fonts you might need to adapt the kerning.