% \iffalse meta-comment
%
% Copyright (C) 1993 by LaTeX3 project.  All rights reserved.
% For additional copyright information see further down in this file.
% 
% This file is part of the LaTeX2e system (PRELIMINARY TEST RELEASE)
% ------------------------------------------------------------------
% 
%  This system 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.
% 
% 
% IMPORTANT NOTICE:
% 
% For error reports in case of UNCHANGED versions see readme files.
% 
% Please do not request updates from us directly.  Distribution is
% done through Mail-Servers and TeX organizations.
% 
% You are not allowed to change this file.
% 
% You are allowed to distribute this file under the condition that
% it is distributed together with all files mentioned in 00readme.l2e.
% 
% If you receive only some of these files from someone, complain!
% 
% You are NOT ALLOWED to distribute this file alone.  You are NOT
% ALLOWED to take money for the distribution or use of either this
% file or a changed version, except for a nominal charge for copying
% etc.
% \fi
% \iffalse
%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     author_1        = "Leslie Lamport",
%%%     author_2        = "David Carlisle",
%%%     version         = "0.1a",
%%%     date            = "12 December 1993,
%%%     time            = "17:51:47 GMT,
%%%     filename        = "ifthen.dtx",
%%%     address_2       = "Computer Science Department
%%%                        Manchester University
%%%                        Oxford Road
%%%                        Manchester
%%%                        England
%%%                        M13 9PL",
%%%     email_1         = "carlisle@cs.man.ac.uk (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "LaTeX, Conditional commands",
%%%     supported       = "yes",
%%%     docstring       = "
%%%     
%%%     The \ifthenelse and \whiledo control commands for LaTeX2e.
%%%
%%%     Copyright (C) 1991 by Leslie Lamport
%%%                   1993 by David Carlisle
%%%     ",
%%%  }
%%% ====================================================================
% \fi
%
\def\filename{ifthen}
\def\fileversion{v0.1b}
\def\filedate{1993/12/17}
\def\docdate{1993/12/17}
%
% \title{The \texttt{\filename} package\thanks{This file
%        has version number \fileversion, last
%        revised \filedate, documentation dated \docdate.}}
% \author{David Carlisle}
% \date{\docdate}
%
% \maketitle
%
% \begin{abstract}
% This file implements an |\ifthenelse| command for \LaTeXe.
% The algorithm used is compatible with that used in the \LaTeX~2.09
% |ifthen| style option. It has been recoded, making the resulting
% definitions somewhat more compact and efficient. 
% \end{abstract}
%
% \changes{v1.0b}{1993/12/17}{Upgrade to LaTeX2e}
%
% |\ifthenelse{|^^A
%       \meta{test}|}{|\meta{then clause}|}{|\meta{else clause}|}|
% 
% Evaluates \meta{test} as a boolean function, and then executes
% either \meta{then clause} or \meta{else clause}.
%
% \meta{test} is a boolean expression using the infix connectives,
% |\and|, |\or|, the unary |\not| and parentheses |\( \)|.
%
% The atomic  propositions are:\\
% \meta{number} |<| \meta{number} \\
% \meta{number} |=| \meta{number} \\
% \meta{number} |>| \meta{number} \\
% |\isodd{| \meta{number} |}|\\
% |\equal{|\meta{string}|}{|\meta{string}|}|\\
% |\lengthtest{|\meta{dimen}|<|\meta{dimen}|}|\\
% |\lengthtest{|\meta{dimen}|=|\meta{dimen}|}|\\
% |\lengthtest{|\meta{dimen}|>|\meta{dimen}|}|\\
% |\boolean{|\meta{name}|}|
%
% The \meta{string}s tested by |\equal| may be any sequence of commands
% that expand to a list of tokens. If these expansions are equal, then
% the proposition is true. 
%
% |\isodd| is true if the \meta{number} is odd.
%
% |\boolean{xyz}| returns the truth value contained in the primitive
% \TeX\ |\if|, |\ifxyz|. This is usually used with |\newif| created
% tokens, but can also be used with |\boolean{true}| (|\iftrue|),
% |\boolean{mmode}| (|\ifmmode|) etc. The commands:\\
%  |\newboolean{|\meta{name}|}| and\\
%  |\setboolean{|\meta{name}|}{|\meta{value}|}|\\
% are provided so the user can easily create and set these flags.
% \meta{value} may be either |true| or |false| (any CaSe).
%
% Note that there is no precedence between |\and| and |\or|.
% The proposition is evaluated in a left right manner. |\not| only
% applies to the immediately following proposition. (This is consistent
% with Lamport's |ifthen.sty|. In this style, though the test is
% `lazily' evaluated, so for instance if the first proposition in an
% |\or| is true, the second one is skipped. (On the second pass---The
% first pass in an |\edef| expands clauses in all propositions).
%
% Apart from the extra atomic propositions |\isodd| |\boolean| and
% |\lengthtest| the only known incompatibility is that in this style,
% |\not\not|\meta{P} is equivalent to \meta{P}.
% However in the original style it was equivalent to |\not|\meta{P}.
% This is intentional (bug fix:-).
%
% The command |\whiledo| is also defined (copied directly from
% |ifthen.sty|). 
%
% |\whiledo{|\meta{test}|}{|\meta{while clause}|}|
%
% With \meta{test} as above, repeatedly executes \meta{while clause}
% while the test remains true.
%
% In order to support the syntax of |ifthen.sty|, which allows access
% to the primitive \TeX\ syntax for a numeric test, rather than a |{}|
% delimited argument form, it is most convenient to work `within' an
% |\ifnum|. |\ift@throw| `throws' you out of the current |\ifnum| so
% that you can (eg) start an |\ifdim| for the length tests.
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ifthen}[\filedate\space\fileversion\space
                           LaTeX2e package]
\typeout{Package: `ifthen'
   \fileversion\space <\filedate> (DPC)}
\typeout{English documentation \@spaces<\docdate> (DPC)}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\TE@throw{\@ne=\@ne\noexpand\fi}
%    \end{macrocode}
%
% The non-standard extension to |ifthen|, |ifthen???| supporting
% boolean was previously available, this is a simpler implementation.
%    \begin{macrocode}
\def\boolean#1#2{%
  \TE@throw\expandafter\noexpand\csname if#1\endcsname#2}
%    \end{macrocode}
%
% Testing lengths. |#1| is the test. The extra argument gobbles spaces.
%    \begin{macrocode}
\def\TE@length#1#2{\TE@throw\noexpand\ifdim#1#2}
%    \end{macrocode}
%
% Testing odd/even. With special support built in, so that
% |\isodd{\pageref{foo}}| works. (as long as |\thepage| expands to
% something that looks like a number).
%    \begin{macrocode}
\def\TE@odd#1#2{%
  \TE@throw\noexpand\TE@@odd\noexpand#1\relax\noexpand\ifodd\@tempa#2}
%    \end{macrocode}
%
% |\TE@@odd| is not expanded on the first pass
%    \begin{macrocode}
\def\TE@@odd#1#2\relax{%
  \ifx\pageref#1%
    \@ifundefined{r@#2}%
      {\@warning{no page ref, using 0}\let\@tempa\z@}%
      {\def\@tempa{\expandafter\expandafter\expandafter
                \@cdr\csname r@#2\endcsname\@nil}}%
  \else
    \def\@tempa{#1#2}%
  \fi}
%    \end{macrocode}
%
% The remaining macros in this file are derived from the ones in
% |ifthen.sty| but recoded and simplified. The main simplification is
% that the original style (and |ifthen???|) expressed logical
% values always in terms of |\ifnum|. As |\fi| is `untyped' this is
% not necessary, so for example the length tests can return values via
% |\ifdim|, the trailing |\fi| will not complain, even though it was
% `expecting' an |\ifnum|. Also the system of passing information via
% macros expanding to |T| or |F| has been completely replaced by a
% simpler system using |\iftrue|, which furthermore allows lazy
% evaluation on the second pass.
%    \begin{macrocode}
\long\def\ifthenelse#1{%
%    \end{macrocode}
% The original |ifthen.sty| processed everything inside a box
% assignment, to catch any extra spaces before they appeared in the
% output. Instead I have added extra arguments to the commands so they
% each remove any following space.
%
% Set up the user level names |\not| etc.
%    \begin{macrocode}
    \bgroup\let\protect\noexpand
        \let\or\TE@or \let\and\TE@and \let\not\TE@neg 
        \let\equal\TE@equal \let\(\TE@lparen \let\)\TE@rparen
        \let\isodd\TE@odd \let\lengthtest\TE@length
%    \end{macrocode}
% For the first pass, in a group, make various tokens non-expandable.
%
% It is unfortunate that in order to remain compatible with |ifthen|
% syntax, it is necessary to have a two pass system. The first pass
% inside an |\edef| `exposes' the |\if|\ldots\ |\fi| tokens, so the
% corect clauses may be skipped on the second pass. This means that the
% whole |\ifthenelse| command does not work by expansion, and so
% possibly has only limited usefulness for macro code writers.
% The main problem with the |ifthen:| syntax is that (unique for \LaTeX)
% it does not uses a brace delimited argument form, and exposes the
% primitive \TeX\ syntax for \meta{number}. Pretty much the only way of
% parsing |1 > 2 \or 2 < 1| is to actually evaluate the primitive
% |\ifnum|s. A syntax such as:\\%
% |\or{\numtest{1<2}}{\lengthtest{1pt<1in}}|\\
%  could easily be evaluated in a one pass way, operating directly via
%  expansion, and leaving no extra tokens in the token stream.
%
% Still, on with the code\ldots\ make |\@tempa| and |\@tempb| tokens
% non-expandable on the first pass.
%    \begin{macrocode}
        {\let\@tempa\relax\let\@tempb\relax
        \xdef\@gtempa{\TE@eval#1\TE@endeval}}%
%    \end{macrocode}
% Now outside the group, execute |\@gtempa| which causes all the
% |\if|s etc., to be evaluated, the final truth value is contained in
% the |\newif| token |\ifTE@val|. Finally this is tested and either the
% first or second following argument is chosen accordingly.
%    \begin{macrocode}
        \@gtempa
        \expandafter\egroup\ifTE@val
          \expandafter\@leftmark
        \else
          \expandafter\@rightmark
        \fi}
%    \end{macrocode}
% 
% Initialise a term. (Expanded on the first pass).
%    \begin{macrocode}
\def\TE@eval{\noexpand\TE@negatefalse\noexpand\iftrue\noexpand\ifnum}
%    \end{macrocode}
% 
% Two |\newif|s the first holds the current truth value of the
% expression. The second is a temporary flag which is true if we need to
% negate the current proposition.
%    \begin{macrocode}
\newif\ifTE@val
\newif\ifTE@negate
%    \end{macrocode}
%
% Finalise a term. (Expanded on the first pass).
%    \begin{macrocode}
\def\TE@endeval{\relax
  \noexpand\TE@setvaltrue\noexpand\else\noexpand\TE@setvalfalse\noexpand\fi
  \noexpand\TE@negatefalse\noexpand\fi}
%    \end{macrocode}
%
% Set the |\ifTE@val| to true or false depending on the value of the
% current proposition, and the negate flag. (Not expanded on the first
% pass.) 
%    \begin{macrocode}
\def\TE@setvaltrue{%
  \ifTE@negate\TE@valfalse\else\TE@valtrue\fi}
\def\TE@setvalfalse{\let\ifTE@val\ifTE@negate}
%    \end{macrocode}
%
% The internal version of |\or|. Ends the current term.
% If true skip the remaining terms.
%    \begin{macrocode}
\def\TE@or{\TE@endeval\noexpand\ifTE@val\noexpand\else\noexpand\ifnum}
%    \end{macrocode}
% 
% The internal version of |\and|. If false skip the remaining terms.
%    \begin{macrocode}
\def\TE@and{\TE@endeval\noexpand\ifTE@val\noexpand\ifnum}
%    \end{macrocode}
% 
% |\not|. Throw the current context, set a negate flag, then restart
% the |\ifnum|. |\TE@negateswitch| is not expanded on the first pass.
%    \begin{macrocode}
\def\TE@neg{\TE@throw\noexpand\TE@negswitch\noexpand\ifnum}
\def\TE@negswitch{\ifTE@negate\TE@negatefalse\else\TE@negatetrue\fi}
%    \end{macrocode}
% 
% |\(|. Throw the current context, then restart a term inside a group.
%    \begin{macrocode}
\def\TE@lparen#1{\TE@throw\begingroup\TE@eval#1}
%    \end{macrocode}
% 
% |\)| end the current term, and the local group started by |\(|, but
% pass on the boolean value in |\if\@val T|. The |\noexpand| stops the
% |\expandafter| from expanding on the first pass.
%    \begin{macrocode}
\def\TE@rparen#1{%
  \TE@endeval
  \noexpand\expandafter\endgroup\noexpand\ifTE@val#1}
%    \end{macrocode}
% 
% |\equal| greatly simplified from the original. |\def| may be used
% rather than |\edef| as the whole thing is expanded anyway in the
% first pass. The boolean can be directly encoded with the |\ifx|,
% there is no need to start an equivalent |\ifnum|.
%    \begin{macrocode}
\def\TE@equal#1#2#3{\TE@throw
      \def\@tempa{#1}\def\@tempb{#2}%
      \noexpand\ifx\@tempa\@tempb#3}
%    \end{macrocode}
% 
% |\setboolean| takes |true| or |false|, as |#2|, and sets |#1|
% accordingly. 
%    \begin{macrocode}
\def\setboolean#1#2{%
  \lowercase{\def\@tempa{#2}}%
  \@ifundefined{@tempswa\@tempa}%
    {\@latexerr{You can only set a boolean to `true' or `false'.}\@ehc}
    {\@ifundefined{#1\@tempa}%
      {\@latexerr{Boolean #1 undefined.}\@ehc}
      {\csname#1\@tempa\endcsname}}}
%    \end{macrocode}
%
% Oh I do love those |\outer| macros.
%    \begin{macrocode}
\def\newboolean#1{%
  \csname newif\expandafter\endcsname\csname if#1\endcsname}
%    \end{macrocode}
% 
% |\whiledo| copied directly from the original.
%    \begin{macrocode}
\long\def\whiledo#1#2{%
  \ifthenelse{#1}{\@whiledotrue \@whilesw\if@whiledo\fi
     {#2\ifthenelse{#1}{\@whiledotrue}{\@whiledofalse}}}{}}
%    \end{macrocode}
% 
%    \begin{macrocode}
\newif\if@whiledo
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<*driver>
%    \end{macrocode}
%
%    \begin{macrocode}
\documentclass{ltxdoc}

\begin{document}

\DocInput{ifthen.dtx}

\end{document}
%    \end{macrocode}

%    \begin{macrocode}
%</driver>
%    \end{macrocode}

