[texhax] Conditional statement

Disgusted of Tunbridge Wells P.Taylor at Rhul.Ac.Uk
Mon Oct 23 23:50:56 CEST 2006



Andreas Ess wrote:

> Hi,
> I'm trying to do some conditional statements with TeX...
> 
> Given the following statement
> 
> \def\arbeitE#1#2{
>    \author #2// & \title #1//\\ \hline
> }
> 
> , which takes arbeitE, of the following form
> \arbeitE{A;B;C}{D;E;F;G}
> 
> and then passes the two arguments on to the \author and \title commands, 
> I want to execute the entire statement (\author ... \hline) only if the 
> value of E equals a certain character sequence, and otherwise, do 
> nothing... The things I don't see:
> 
> - How can I extract E from argument #2

Pass #2 to a delimited-parameter macro, as in

	\def \parse #1;#2;#3;#4\sentinel {\def \secondoffour {#2}}
	\def \arbeitE#1#2%
		{%
     			\parse #1\sentinel \if<something> \author #2// & \title #1//\\ \hline \fi
		}


> - How can I do the actual comparison?

That obviously requires an appropriate definition for \ifsomething.
We should start by defining a control sequence against which
your pattern can be matched :

	\def \target {an arbitrary character sequence, to be detected}
	
Then we need to examine the set of TeX \ifs to see which one can compare
two macros, returning true IIF they have the same expansion...  \ifx
is the closest to this, see p.~210 for a more precise definition.

Thus \if<something> turns out to be

	\ifx \target \secondoffour

which leads to the final coding :

	\def \parse #1;#2;#3;#4\sentinel {\def \secondoffour {#2}}
	\def \arbeitE#1#2%
		{%
     			\parse #1\sentinel \ifx \target \secondoffour \author #2// & \title #1//\\ \hline \fi
		}

A fully worked demo. follows --

\toks 0 = {\author #2// & \title #1//\\ \hline}
\def \parse #1;#2;#3;#4\sentinel {\def \secondoffour {#2}}
\def \arbeitE#1#2%
     {%
             \parse #2\sentinel
             \ifx \target \secondoffour
                  \message {\the \toks 0 }%
             \else
                  \message {[\secondoffour] NOT \the \toks 0}%
             \fi
     }

\def \target {e}
\arbeitE {a;b;c}{d;e;f;g}

\def \target {not e}
\arbeitE {a;b;c}{d;e;f;g}

\def \target {complex but allowed}
\arbeitE {a;b;c}{d;complex but allowed;f;g}
\end

Philip TAYLOR


More information about the texhax mailing list