[tex-eplain] (was optional arguments)

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Tue Feb 26 10:41:29 CET 2008


Hi,

having programmed in a lot of languages, having (just) used TeX
for many years, TeX's macro language is still a book of seven seals
for me.
Would anybody please be so kind to recommend some reading on that
topic. Unfortunately Knuth's TeXbook is quite short about things
like \futurelet, \expandafter, depth of evaluation and so on.
Trying to learn macros a bit, I have come up with the following
variant of an indexing macro (since I'm soooo lazy)

\catcode`\@=\active
\def @{\futurelet\testchar\oneortwo}
\def \oneortwo{%
  \ifx[\testchar
    \let\next\twoargs
  \else
    \let\next\onearg
  \fi
  \next
}
\def\twoargs[#1]#2|{\centerline{#1 / #2}}
\def\onearg#1|{\centerline{#1}}
@onearg|
@[second]first|
\bye

or if the optional parameter is to follow the mandatory parameter:
(I have learnt that from Oleg just right now

\catcode`\@=\active
\newtoks\myreffirstarg
\def @#1|{\myreffirstarg={#1}\futurelet\testchar \myrefoneortwo}
\def \myrefoneortwo{%
    \ifx[\testchar
      \expandafter\mtwo\expandafter{\the\myreffirstarg}%
    \else
      \expandafter\mone\expandafter{\the\myreffirstarg}%
    \fi}
\def\mtwo#1[#2]{\centerline{#1 / #2}}
\def\mone#1{\centerline{one: #1}}
@onearg|
@first|[second]
\bye


It looks much easier than Eplain's \@getoptionalarg,
so where is the bug or limitation of my variant?

Many thanks for your comments,
Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany


More information about the tex-eplain mailing list