There's a couple of things I want to be able to do that are quite similar and basically involve keeping back a macro expansion until the last minute.
Possibly the simplest example is that I have a family of macros that expand out to variations of "object in X", but sometimes I override the default. So whilst \cobj
expands to "object in C", \sobj
expands to "set" and \gobj
expands to "group". Where this causes trouble is when I want to talk about an object. Generically, I should type "an \cobj" but in the specific cases I may need to change to "a \sobj". The difficulty is that one reason for using these macros is so that I can change my mind as to which ones get special names and which don't. But it (partially) defeats the purpose if I have to go back through rewriting all the 'an's as 'a's. So what I'd like to do is define a macro '\a' which checks the next character for a vowel and produces 'an' or 'a' appropriately (modulo the odd special case!).
The difficulty with this is that it isn't followed by a character, it's followed by a macro. So I need to expand that macro to find out what character is the eventual first one. If it were a simple macro, say \def\cobj{object in C}
then I could just do \def\a{\noexpand\@a}
and \def\@a{\@ifnextchar{a}{an}{\@ifnextchar{e}{an}...}
, but \cobj
is quite a complicated macro and there's several levels to be expanded before one actually gets to the final characters. In particular, \cobj
takes an optional argument.
So what I'd like is to be able to expand \a
at the last possible minute. Sort of like \noexpand
, but that only does a noexpand for one hop rather than 'end - 1' hops.
Anyone have any ideas as to how best to proceed? To give an indication of the level of answer that would be acceptable, I know about \expandafter
and I'm not afraid to use it!
Edit: Here's another example that is a little more complicated. In a similar situation to the above, I have \dcat
for "the category D". On a few rare occasions, I want to "eat" the "the", so I want a \nothe
command that I can put beforehand: \nothe\dcat
which eats the first word following it. It's simple enough to write a command that gobbles the next word in the input stream, but I want the next word in the output to disappear.
\protected\def\nothe{\expandafter\notheii\romannumeral0}
\def\notheii the {}
Here TeX will expand until it finds something telling it that no further digits follow (if necessary, swallowing a space). On the assumption that "he " will be produced at the same time as "t", we can then swallow the whole thing. So this will work even if\dcat
is also a\protected\def
. – user9588 Jan 7 '12 at 14:06