[tex-eplain] optional arguments

Adam Fenn acwfenn at hotmail.com
Mon Feb 25 14:32:46 CET 2008


Thanks John and Oleg for sorting out my optional argument problem (but why the brackets after
\cmd at arg{} ?).
 
> \makeatletter
> \def\cmd#1{\def\cmd at arg{#1}\@getoptionalarg\finishcmd}
> \def\finishcmd{%
> \ifx\@optionalarg\empty
> % No optional argument present.
> \cmd at arg{}
> \else
> % One was present.
> {\@optionalarg} {\it\cmd at arg}
> \fi
> }
> \resetatcatcode
> \end
>
 
However, this was preparatory to my real problem. I'm trying to write a specific indexing macro
with an optional argument. My attempt below is supposed to typeset two entries in the index, 
one under architects and one under the name of the architect with an optional argument for his location.
 
It has a number of failings. Firstly, it only typesets the last occurrence of \architect. Secondly, it 
puts them in the wrong alphabetical order and whilst I understand why this happens I don't know 
how to rectify it and thirdly it typesets too much when the optional argument isn't present. All in all 
a triumph in the art of macro writing. Any (further) help would be greatly appreciated.
 
Adam.
 
\makeatletter
\def\architect#1{\def\architect at arg{#1}\@getoptionalarg\finisharchitect}
\def\finisharchitect{%
\let\name=\architect at arg{}
\let\location=\@optionalarg
      \ifx\@optionalarg\empty
           % No optional argument present.
           \sidx{\name, {\it architect}}
           \else
           % One was present.
           \sidx{architects!\name, {\it\location}}
           \sidx{\name, {\it architect, \location}}
       \fi
 }
\resetatcatcode
 
\architect{Smith, John}[of London]
_________________________________________________________________
Telly addicts unite!
http://www.searchgamesbox.com/tvtown.shtml


More information about the tex-eplain mailing list