[tex-eplain] optional arguments

Russell Lyons rdlyons at indiana.edu
Sat Aug 29 05:46:01 CEST 2009


Hi, Oleg.

Thanks very much for your quick and clear reply, including solution.

Now permit me another question: is there a simple way to define a macro
so that the optional argument is second, not first, as is usually the
case? What about optional arguments that can come first or last, as in
\sidx itself? As you may gather, I'd like to define a macro that acts as
\sidx, but which I will modify a little, but I figure it would be
useful to know how to do this in general, as well.

Thanks,
Russ

On Sat, 29 Aug 2009, Oleg Katsitadze wrote:

> On Wed, Aug 26, 2009 at 06:17:17PM -0400, Russell Lyons wrote:
>> -------------
>>
>> \let\nobibtex = t
>> \let\noarrow = t
>> \input eplain
>> \enablehyperlinks[idxexact]
>> \catcode`@=\letter
>> \def\residx{\@getoptionalarg\finresidx}
>> \def\finresidx#1{\ifx\@optionalarg\empty{\sidx{#1}}
>>             \else{\sidx{#1}[\@optionalarg]}\fi}
>>
>> And a result later. \residx[sub]{result}
>>
>> \vfill\eject
>>
>> \readindexfile{i}
>>
>> \bye
>>
>> -------------
>>
>> However, what is produced in the index is the entry "result" with the
>> subentry "optionalarg ". (Note it also has an extra space.)
>>
>> What am I doing wrong?
>
> You are not expanding \@optionalarg.  Since \sidx doesn't expand the
> optional argument, it gets written to the .idx file as is.  But if
> \sidx would have tried to expand it, I suspect you would get infinite
> recursion, because inside \sidx, \@optionalarg would be defined as a
> call to a single macro, \@optionalarg.
>
> Anyway, to do what you want you need the \edef trick (below), as
> suggested by Karl.  We pass #1 and the expansion of \@optionalarg
> through toks registers to avoid any further expansion.  And we don't
> use \toks1 because by convention assignments to \toks1 should be
> \global, to avoid save stack overflow.
>
> HTH,
> Oleg
>
> \def\finresidx#1{%
>  \ifx\@optionalarg\empty
>    \sidx{#1}%
>  \else
>    \toks0={#1}%
>    \toks2=\expandafter{\@optionalarg}%
>    \edef\temp{\noexpand\sidx{\the\toks0}[\the\toks2]}%
>    \temp
>  \fi
> }
>
> Also note that this can be simpler, although slightly less efficient:
>
> \def\finresidx#1{%
>  \toks0={#1}%
>  \toks2=\expandafter{\@optionalarg}%
>  \edef\temp{\noexpand\sidx{\the\toks0}[\the\toks2]}%
>  \temp
> }
>


More information about the tex-eplain mailing list