I am trying to define a macro with flexible arguments: 3 mandatory and 2 optional ones - like this:
\macro{<enter code here>}[arg2] arg3 = arg4 | arg5;
So args 2 and 5 are optional, as is the |.
I got quiet close to solving this with ideas found here and here.
Now I am stuck somewhere in TeX's stomach. Maybe someone can shed some light on the problem - I am a total newbie and I think, there must be plenty of possibilities. I have tried a lot but am not familiar enough with \expandafter, \futurelet, \xdef & friends. I am stuck here:
\makeatother
\def\@pop#1{}
\def\@split#1;{\def\@@tmp{#1|}\expandafter\@@split\@@tmp\@end@token}
\def\@@split#1|{\@ifnextchar\@end@token{{#1}{}\@pop}{\@@@split{#1}}}
\def\@@@split#1#2|{{#1}{#2}\@pop}
\def\display#1{\@ifnextchar[{\@display{#1}}{\@display{#1}[1]}}
\def\@display#1[#2]#3={\def\@tmp{\@@display{#1}{#2}{#3}}\expandafter\@tmp\@split}
\def\@@display#1#2#3#4#5{*#1*#2*#3*#4*#5*}
\makeatletter
\display{aaa}[bbb] AAA = BBB | CCC;
the output is supposed to look like this:
*aaa*bbb*AAA*BBB*CCC*
but instead I get
/tmp/problem.tex:56: Missing control sequence inserted.
<inserted text>
\inaccessible
l.42 \display{aaa} AAA = BBB;
...am using a href="http://gummi.midnightcoding.org/"gummi/a ...good stuff IMHO!

[...], the key is to use\@ifnextchar, which you already knew. For the second optional argument, as the;has to be there it is best to grab everything then re-parse the last argument looking for a|. By using two of these and an appropriate end point you can avoid needing to test for|explicitly. The method is to use#4|#5|#6\@nil(or similar), with#6as a dummy argument that is thrown away. – Joseph Wright Jan 23 '11 at 13:54latex.ltx. – Joseph Wright Jan 23 '11 at 14:51