[OS X TeX] Defining an if-like macro

J. McKenzie Alexander jalex at lse.ac.uk
Thu Feb 6 16:14:27 EST 2014


Hello,

I’ve written a simple package which permits conditional inclusion of material with more sophisticated logical constraints on what gets included than can easily be generated with \newif conditionals.  But it misbehaves in one context, and I was wondering if there was some clever way around it…

Let me first describe the package (in case anyone is interested), before I mention the misbehaviour.

The package is available here, in case anyone wants to try it (bug reports appreciated!):

	https://www.dropbox.com/s/l1vmwlsfui6sbxt/ifkeys.sty

Here’s how it works… You call a macro called \SetKeys with a comma-separated list of key names (spaces permitted). Those keys are true, all other keys are false.

Another macro, \ifkeys, is defined, which takes an expression in *disjunctive normal form* as its argument. It then checks whether that expression is satisfied and, if so, evaluates to \iftrue, otherwise \iffalse.  The expression uses commas to separate each disjunct, and && to indicate conjunction of key values within each disjunct. And ~ is allowed to specify negation of key values, too. (Since every expression can be written in disjunctive normal form, this allows all logical constraints on keys to be specified, even if at some expense of length.)

Here’s an example of how it can be used (the number of spaces used in keys doesn’t matter):

\SetKeys{ key one, key two, key three, a really long key name }

\ifkeys{ key one && key two }
This is where you put some additional material to be conditionally included exactly when ``key one'' and ``key two'' are both set.
\fi

\ifkeys{ key one && ~key three }
This is where you put some additional material to be conditionally included exactly when ``key one'' is set and ``key three'' is not.
\else
You can also use an \verb+\else+ clause to insert material in
cases where the specified condition is not satisfied.
\fi

\ifkeys{a really long key name}
Keys can have long names with spaces, as well!
\fi 

\ifkeys{ key one && ~key two, ~key three, ~key four}
Keys which are not mentioned in \verb+\SetKeys+ are assumed to be false, so this will appear.
\fi 


Ok — now for the problem… The \ifkeys macro cannot be nested with itself or other conditionals, because TeX has a special mechanism for skipping past material at high speeds once it encounters an \iffalse. (It generates the “Extra \fi” error message.) I suspect that there’s no easy / elegant solution for this, but I wanted to ask just in case.

Many thanks,

Jason

--
Prof. J. McKenzie Alexander
Department of Philosophy, Logic and Scientific Method
London School of Economics
Houghton Street, London WC2A 2AE







Please access the attached hyperlink for an important electronic communications disclaimer: http://lse.ac.uk/emailDisclaimer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://email.esm.psu.edu/pipermail/macosx-tex/attachments/20140206/f3d91a29/attachment.html>


More information about the MacOSX-TeX mailing list