Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

\ifcase, AND and OR

342 views
Skip to first unread message

Jonathan Fine

unread,
May 3, 1999, 3:00:00 AM5/3/99
to
Recently, Paul Abrahams asked if anyone had any real use for the \if
primitive. This command, like many of the other \if... primitives,
expands tokens until it finds the arguments it needs. (In fact, they
all do, except for \ifx. However some, such as \ifvmode, need no
arguments. See TeXbook, p209-10.)

Recently, I found myself wanting to code a 'lock' on a file, that would
warn against its use after a particular date. Suppose we want the file
to expire at the end of May of next year.

First check, have we gone past year 2000. Next check, are we in year
2000. If so, check if we have gone past May.

Here is the code I wrote to do this. The \ifcase expands until it finds
the number it is looking for. There are some space tokens in the code
below. They are all there for a reason. Similar code can be used to
produce AND and OR statements.

% Test to see if we are past May 2000.
\ifcase
\ifnum \year >2000 1 \fi % past year 2000, so failure
\ifnum \year =2000 % if in year 2000 ...
\ifnum \month >5 1 \fi % ... have we gone past May?
\fi
0 % Still here? All is OK. Supply default value.
% we've not yet reached June 2000
\else
% we're past May 2000
\fi

Now suppose we have two booleans conditions, \ifA and \ifB, and we wish
to test to see if they are both true or both false.

The code
\if
\ifA 0\else 1\fi % produce first character for \if
\ifA 0\else 1\fi % produce second character for \if
% booleans agreee
\else
% booleans disagree
\fi
will do the job for us. There are no space tokens in this code. This
is important. The code above work even if, say, \ifA is replaced by
\ifnum \this <\that
and so forth.

If both \ifA and \ifB are \newif commands, then we could use the
admittedly peculiar code
\ifx \ifA \ifB
to solve the problem, so long as this fragment did not itself appear in
conditional text to be skipped.

I find \ifcase, and particularly \ifcase ... \else ... \fi , to be one
of my favorite \if... commands. Sometimes I call it \ifzero.

Jonathan Fine, Cambridge, UK
Active TeX and the DOT syntax
http://www.active-tex.demon.co.uk/

MicroPress, Inc.

unread,
May 5, 1999, 3:00:00 AM5/5/99
to
Here is a good one:
can one implement via a TeX macro "real" \AND and \OR expressions?
I guess, one would want to allow something like

\IF (\year >2000 \AND \month > 5)
\crush

(probably doable, but was this done?)

Donald Arseneau

unread,
May 5, 1999, 3:00:00 AM5/5/99
to
In article <372fb322....@news.panix.com>, sup...@micropress-inc.com (MicroPress, Inc.) writes...

>Here is a good one:
>can one implement via a TeX macro "real" \AND and \OR expressions?
>I guess, one would want to allow something like
>
> \IF (\year >2000 \AND \month > 5)
> \crush

Much easier would be:

\if \and{\year >2000}{\month > 5}


Donald Arseneau as...@triumf.ca

Jonathan Fine

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
I'm a bit of an efficiency freak, and macros as below are unlikely to be
efficient. For this reason, I do not think they are a good idea.

All the same, the problem of control structures is an important one.

Jonathan Fine


Donald Arseneau <as...@reg.triumf.ca> writes

Jonathan Fine, Cambridge, UK

Donald Arseneau

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
In article <2r146NAD...@active-tex.demon.co.uk>, Jonathan Fine <fi...@active-tex.demon.co.uk> writes...

>I'm a bit of an efficiency freak,

Well, so am I.

>and macros as below are unlikely to be
>efficient. For this reason, I do not think they are a good idea.

Nonsense. They would be the mose efficient possible; essentially
expanding directly to something like what you posted.


>>\if \and{\year >2000}{\month > 5}

\def\and#1#2{\ifnum#1\else A\fi\ifnum#2\else B\fi CC}

Donald Arseneau as...@triumf.ca

mjdo...@my-dejanews.com

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
In article <372fb322....@news.panix.com>,

sup...@micropress-inc.com (MicroPress, Inc.) wrote:
> Here is a good one:
> can one implement via a TeX macro "real" \AND and \OR expressions?
> I guess, one would want to allow something like
>
> \IF (\year >2000 \AND \month > 5)
> \crush

The following code, more or less, is found in the breqn package
(ftp.ams.org:/pub/tex/breqn.tgz).

========================================================================
\def\@True{00}
\def\@False{01}

% \@Not takes one argument which is expected to be something like \@True
% or \@False.

\def\@Not#1{0\ifcase#11 \or\expandafter 1\else \expandafter 0\fi}

% \@And and \@Or take two (and only two) arguments.

\def\@And#1#2{0\ifcase#1#2 \expandafter 0\else \expandafter 1\fi}
\def\@Or#1#2{0\ifnum#1#2<101 \expandafter 0\else \expandafter 1\fi}
========================================================================

Example usage:

\let\eq@group\@True
\let\GRP@top\@False

\def\equation@top{%
\if\@And{\eq@group}{\@Not\GRP@top}%
\penalty\intereqpenalty \vskip\intereqskip
\else
...
\fi
}

A compound "and" test could be done pretty readily:

\if\several@And{\eq@group\GRP@top\another@boolean ...}

with \several@And defined as

\def\several@And#1{%
0\ifnum#1=\z@ \expandafter 0\else \expandafter 1\fi}

but there is a subtle pitfall in that the number of arguments is limited
by the size of TeX integers. And I don't believe I ever got a
satisfactory implementation of a companion \several@Or test.

Michael Downes <m...@ams.org>


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---

MicroPress, Inc.

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
This is the prefix form. The challenge was the infix form.

David Carlisle

unread,
May 11, 1999, 3:00:00 AM5/11/99
to MicroPress, Inc.

> This is the prefix form. The challenge was the infix form.

ifthen.sty does infix, but is not expandable. It's probably hard and/or
pointless to try to do infix just via expansion.

David

Robin Fairbairns

unread,
May 11, 1999, 3:00:00 AM5/11/99
to

indeed, given the hash tex itself makes of infix notation in maths,
it's hard to imagine any rational person wanting to build infix *on
top of* tex...
--
Robin Fairbairns, Cambridge

mjdo...@my-dejanews.com

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
In article <373852ce...@news.panix.com>,

sup...@micropress-inc.com (MicroPress, Inc.) wrote:
> This is the prefix form. The challenge was the infix form.

I'm sorry, I thought you wanted a serious solution. TeX macro expansion
is intrinsically a prefix operation, and hence no real TeXnician would
bother with the infix approach---unless their goal was to provide
syntactic sugar for non-experts, as in the LaTeX ifthen package.

By the way, e-tex provides extended arithmetic capabilities, with parens for
grouping subexpressions and so on, so something similar could probably be
done for compound conditional expressions. But that would be done by
extending TeX itself, not by programming in TeX's macro language.

Victor Eijkhout

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
Here is my attempt at programming ands and ors. Look at the example at
the end for the syntax. The example uses the repeat macro from ctan.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
%
% Logic Programming macros, version 0.9, unreleased
% Copyright Victor Eijkhout 1999
% file name: LGC_prog.tex
%
% Author:
% Victor Eijkhout
% Department of Computer Science
% University of Tennessee, Knoxville TN 37996
%
% vic...@eijkhout.net
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% For a copy of the GNU General Public License, write to the
% Free Software Foundation, Inc.,
% 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA,
% or find it on the net, for instance at
% http://www.gnu.org/copyleft/gpl.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
%
%%
%% Prevent multiple loading of this file
%%
\expandafter\ifx\csname LGCprogversion\endcsname\relax
\def\LGCprogversion{0.9}
\message{Loading Logic Programming macros, version \LGCprogversion}%
\else \endinput \fi

\def\LGCgobble#1\LGCend\LGCend{} \def\LGCend{LGCend}
\def\LGCyieldtrue{\expandafter\iftrue\LGCgobble}
\def\LGCyieldfalse{\expandafter\iffalse\LGCgobble}
%
% or
\def\LGCor#1{00\fi \LGCorx#1\LGCend\LGCend}
\def\LGCorx#1#2{\ifx#1\LGCend \LGCordefault
\else \csname#1\expandafter\endcsname \fi
#2\expandafter\LGCyieldtrue
\else \expandafter\LGCorx \fi}
\def\LGCordefault#1\fi#2\fi{\fi\csname iffalse\expandafter\endcsname}
%
% and
\def\LGCand#1{00\fi \LGCandx#1\LGCend\LGCend}
\def\LGCandx#1#2{\ifx#1\LGCend \LGCanddefault
\else \csname#1\expandafter\endcsname \fi #2\expandafter\LGCandx
\else \expandafter\LGCyieldfalse \fi}
\def\LGCanddefault#1\fi#2\fi{\fi\csname iftrue\expandafter\endcsname}
%
% not
% ultra-cool macro by David Kastrup
\def\ifnot#1{#1\else\expandafter\expandafter\fi\iffalse\iftrue\fi}

\endinput

%\tracingmacros=2
\input repeat
\def\ifeven#1{\ifnot{\ifodd#1}}
\repeat \for{x} \to{7} \do {
\message{\number\x}
\if\LGCor{{ifnum}{\x=1 }{ifnum}{\x=3 }{ifnum}{\x=5 }}
\message{Yes135}\else \message{No135}\fi
\if\LGCand{{ifnum}{\x>1 }{ifnum}{\x<5 }{ifeven}{\x}}
\message{Yes24}\else \message{No24}\fi
}
\end

--
Victor Eijkhout
http://www.eijkhout.net/


Peter Schmitt

unread,
May 12, 1999, 3:00:00 AM5/12/99
to Jonathan Fine
On Mon, 10 May 1999, Jonathan Fine wrote:

> I'm a bit of an efficiency freak,

Indeed? I wonder:

> and macros as below are unlikely to be
> efficient. For this reason, I do not think they are a good idea.

[]


> Jonathan Fine, Cambridge, UK
> Active TeX and the DOT syntax
> http://www.active-tex.demon.co.uk/
>

Can it be _efficient_ to use ActiveTeX other than in an emergency?
:-)

Peter


MicroPress, Inc.

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
I'm sorry; no disrespect was intended. I was trying to offer a problem

which looked interesting, not solicit a solution for anything we need.

On Wed, 12 May 1999 13:12:48 GMT, mjdo...@my-dejanews.com wrote:

>In article <373852ce...@news.panix.com>,
> sup...@micropress-inc.com (MicroPress, Inc.) wrote:
>> This is the prefix form. The challenge was the infix form.
>
>I'm sorry, I thought you wanted a serious solution. TeX macro expansion
>is intrinsically a prefix operation, and hence no real TeXnician would

Yes, of course. The assumption is that the "\IF" operator would be a
(prefix) macro itself which would do all the parsing; this has to be
doable.

>bother with the infix approach---unless their goal was to provide
>syntactic sugar for non-experts, as in the LaTeX ifthen package.

>
>By the way, e-tex provides extended arithmetic capabilities, with parens for
>grouping subexpressions and so on, so something similar could probably be
>done for compound conditional expressions. But that would be done by
>extending TeX itself, not by programming in TeX's macro language.

Anything can be done with TeX extentions.

Jonathan Fine

unread,
May 13, 1999, 3:00:00 AM5/13/99
to
I wrote that I'm a bit of an efficiency freak.

Peter Schmitt <sch...@ap.univie.ac.at> writes


>Can it be _efficient_ to use ActiveTeX other than in an emergency?
>:-)

This is a deep and thoughtful and very good question.

For a long time - indeed too long a time - the very question of
efficiency kept me from taking the vital all-active step.

I took the step because the advantages it offered were so great. Or to
put in other terms, you never can tell when an emergency might arise.
For example, if your client wants hanging punctuation, you'll be really
glad that secretly your punctuation characters were already active.

Because of the extra processing involved in Active TeX, I decided that I
had to take care with efficiency, so as to compensate. This is in part
why I became something of an efficiency freak.

With today's computers, we can well afford the extra processing overhead
involved with Active TeX, and it is my view that if you want to do
anything special, Active TeX is tbe best way to go. Compared to LaTeX,
Active TeX is about as efficient as LaTeX is, compared to plain.

However, if you want to use Active TeX only for emergencies, and use
plain or LaTeX for the rest, then my soon-to-be-released style file is
just the job.

0 new messages