Re: Uppercasing the first character of an expanded macro arg?
- From: Dan <luecking@xxxxxxxx>
- Date: Fri, 8 Feb 2008 14:48:44 -0800 (PST)
On Feb 4, 3:27 am, Hans Meine <me...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Alan Ristow wrote:
[...]
Nevertheless, I wanted to hear the answer from some TeX gurus whether and
why \lowercase and \uppercase are not accompanied by \capitalize.
Wouldn't the expansion triggered by \expandafter in
\expandafter\capitalize\prettyref{eq:foo}
expand \prettyref{eq:foo} as far as possible?
No. According to the TeXbook: the second token following \expandafter
is expanded (meaning: replaced with its replacement text) and then
the
first token following expandafter is processed.
\expandafter\capitalize\prettyref{eq:foo}
produces
\capitalize \@prettyref eq:foo:
You can trigger more expansion with
\expandafter\expandafter\expandafter\capitalize\prettyref{eq:foo}
which produces
\expandafter\capitalize\@prettyref eq:foo:
which then produces
\capitalize \expandafter \ifx \csname pr@eq\endcsname \relax
\PackageWarning {prettyref}{Reference format eq\space undefined}%
\ref {eq:foo}\else \csname pr@eq\endcsname {eq:foo}\fi
which would attempt to run \capitalize on \expandafter. If you define
\capitalize to mean \expandafter\capitalizea you can expand away that
\expandafter but now you encounter \ifx, etc., etc.
There are cases where expansion proceeds as far as possible:
- in the replacement text of \edef or \xdef
- inside \csname ...\endcsname
- inside \write constructs
- inside \special{...}
There are also cases where expansion proceeds until some
unexpandable token appears, mainly in numeric assignments.
An trick recently discussed in c.t.t was \romannumeral 0\stuff
This will expand \stuff as far as necessary looking for more
digits. If it finds digits it continues expanding. When finally an
unexpandable nondigit appears, it produces the lowercase
roman numeral of the number. If no nonzero digits appear,
it produces nothing (except the tokens of the expansion
that remain).
Thus
\expandafter\capitalize\romannumeral 0\prettyref{eq:foo}
if like an infinity of \expandafters, but here is what it actually
produces:
\capitalize \protect \textup {(\ref {eq:foo})}\fi
and it tries to capitalize \protect (=\relax). This is
where arbitrary numbers of \expandafter's would stop,
because \relax is unexpandable.
This whole idea is doomed from the start (for
anything except the most simple macros).
Can adding more \expandafter really help? (Yes, I tried, though being
sceptical, but up to 13 made no difference.)
The number has to a power of two minus one, and it will
expand the number of times equal to the power.
So 1, 3, 7, 15, 31, etc. will expand 1, 2, 3, 4, 5, etc. times
This is because in
\expandafter\expandafter\expandafter\x\y
the first expands the third, which expands \y, then
the second remains to expand the first token of the
expansion of \y. To expand three levels, you have to
interleave 4 \expandafter among the 3 (7 total), then
8 among the 7, etc.
Dan
.
- Follow-Ups:
- Re: Uppercasing the first character of an expanded macro arg?
- From: David Roderick
- Re: Uppercasing the first character of an expanded macro arg?
- References:
- Uppercasing the first character of an expanded macro arg?
- From: Hans Meine
- Re: Uppercasing the first character of an expanded macro arg?
- From: Alan Ristow
- Re: Uppercasing the first character of an expanded macro arg?
- From: Hans Meine
- Uppercasing the first character of an expanded macro arg?
- Prev by Date: Re: better alternative to repeatindex.sty?
- Next by Date: Re: Preventing linebreaks in some cases...
- Previous by thread: Re: Uppercasing the first character of an expanded macro arg?
- Next by thread: Re: Uppercasing the first character of an expanded macro arg?
- Index(es):
Relevant Pages
|