1 % \iffalse meta-comment
4 % The LaTeX3 Project and any individual authors listed elsewhere
7 % This file is part of the LaTeX base system.
8 % -------------------------------------------
10 % It may be distributed and/or modified under the
11 % conditions of the LaTeX Project Public License, either version 1.3c
12 % of this license or (at your option) any later version.
13 % The latest version of this license is in
14 % http://www.latex-project.org/lppl.txt
15 % and version 1.3c or later is part of all distributions of LaTeX
16 % version 2005/12/01 or later.
18 % This file has the LPPL maintenance status "maintained".
20 % The list of all files belonging to the LaTeX base distribution is
21 % given in the file `manifest.txt'. See also `legal.txt' for additional
24 % The list of derived (unpacked) files belonging to the distribution
25 % and covered by LPPL is defined by the unpacking scripts (with
26 % extension .ins) which are part of the distribution.
32 %% Copyright (C) 1991 by Leslie Lamport
33 %% Copyright (C) 1994-2001 LaTeX3 project, David Carlisle
34 %% all rights reserved.
38 \ProvidesFile{ifthen.dtx}
40 %<package>\NeedsTeXFormat{LaTeX2e}[1994/12/01]
41 %<package>\ProvidesPackage{ifthen}
42 %<driver>\ProvidesFile{ifthen.drv}
44 % \ProvidesFile{ifthen.dtx}
45 [2014/09/29 v1.1c Standard LaTeX ifthen package (DPC)]
49 \documentclass{ltxdoc}
56 % \GetFileInfo{ifthen.dtx}
58 % \title{The \textsf{ifthen} package\thanks{This file
59 % has version number \fileversion, last
60 % revised \filedate.}}
61 % \author{David Carlisle}
63 % \MaintainedByLaTeXTeam{latex}
69 % This file implements an |\ifthenelse| command for \LaTeXe.
70 % The algorithm used is compatible with that used in the \LaTeX~2.09
71 % |ifthen| style option. It has been recoded, making the resulting
72 % definitions somewhat more compact and efficient.
75 % \changes{v1.0a}{1993/10/15}{New implementation}
76 % \changes{v1.0b}{1993/12/17}{Upgrade to LaTeX2e}
77 % \changes{v1.0e}{1994/02/11}{Improve documentation}
78 % \changes{v1.0f}{1994/03/02}{Remove need for dtx file}
79 % \changes{v1.0g}{1994/03/14}{Modify for the new ltxdoc.cls}
80 % \changes{v1.0k}{1995/04/25}{Fix `driver' docstrip guards.}
81 % \changes{v1.0n}{1997/11/03}{Documentation fix.}
82 % \changes{v1.0o}{1998/08/17}{Documentation fix.}
84 % \section{Introduction}
86 % \DescribeMacro{\ifthenelse}
88 % \meta{test}|}{|\meta{then clause}|}{|\meta{else clause}|}|
90 % Evaluates \meta{test} as a boolean function, and then executes
91 % either \meta{then clause} or \meta{else clause}.
93 % \meta{test} is a boolean expression using the infix connectives,
94 % |\and|, |\or|, the unary |\not| and parentheses |\( \)|.
96 % As an alternative notation |\AND|, |\OR| and |\NOT| can be
97 % used. This is safer since it can't be misinterpreted
98 % when appearing inside a \TeX-conditional in which |\or| has a
101 % The atomic propositions are:\\
102 % \meta{number} |<| \meta{number} \\
103 % \meta{number} |=| \meta{number} \\
104 % \meta{number} |>| \meta{number} \\
105 % |\isodd{| \meta{number} |}|\\
106 % |\isundefined{| \meta{command name} |}|\\
107 % |\equal{|\meta{string}|}{|\meta{string}|}|\\
108 % |\lengthtest{|\meta{dimen}|<|\meta{dimen}|}|\\
109 % |\lengthtest{|\meta{dimen}|=|\meta{dimen}|}|\\
110 % |\lengthtest{|\meta{dimen}|>|\meta{dimen}|}|\\
111 % |\boolean{|\meta{name}|}|
113 % The \meta{string}s tested by |\equal| may be any sequence of commands
114 % that expand to a list of tokens. If these expansions are equal, then
115 % the proposition is true.
117 % |\isodd| is true if the \meta{number} is odd, and false otherwise
118 % (even if the argument is not a number).
120 % |\isundefined{\cmd}| is true if |\cmd| is not defined.
122 % |\boolean{xyz}| returns the truth value contained in the primitive
123 % \TeX\ |\if|, |\ifxyz|. This is usually used with boolean flags
124 % created with |\newboolean| and |\provideboolean| described below.
125 % It can also be used with the names of |\newif| created tokens, and
126 % primitive \TeX\ |\if| constructs, for example |\boolean{true}|
127 % (|\iftrue|), |\boolean{mmode}| (|\ifmmode|) etc.
131 % |\newboolean|\marg{name}\DescribeMacro{\newboolean}\ and\
132 % |\provideboolean|\marg{name}\DescribeMacro{\provideboolean}\
133 % are provided so the user can easily create new boolean flags.
134 % As for |\newcommand|, |\newboolean| generates an error if the
135 % command name is not new. |\provideboolean| silently does nothing in
138 % The boolean flags may be set with:\\
139 % |\setboolean|\marg{name}\marg{value}\DescribeMacro{\setboolean}\\
140 % \meta{value} may be either |true| or |false| (any CaSe).
142 % Note that there is no precedence between |\and| and |\or|.
143 % The proposition is evaluated in a left right manner. |\not| only
144 % applies to the immediately following proposition. (This is consistent
145 % with Lamport's |ifthen.sty|.) In this style, though the test is
146 % `lazily' evaluated, so for instance if the first proposition in an
147 % |\or| is true, the second one is skipped. (On the second pass---the
148 % first pass in an |\edef| expands clauses in all propositions.)
150 % Apart from the addition of the extra atomic propositions |\isodd|,
151 % |\boolean|, |\lengthtest| and |\isundefined|,
152 % the only known incompatibility is that
153 % in this package the expression|\not\not|\meta{P} is equivalent to
155 % However in the original style it was equivalent to |\not|\meta{P}.
156 % This is intentional (bug fix:-).
158 % \DescribeMacro{\whiledo}
159 % The command |\whiledo| is also defined (copied directly from
160 % the \LaTeX2.09 definition).
162 % |\whiledo{|\meta{test}|}{|\meta{while clause}|}|
164 % With \meta{test} as above, repeatedly executes \meta{while clause}
165 % while the test remains true.
169 % \section{The Implementation}
175 % \begin{macro}{\TE@throw}
176 % In order to support the syntax of |ifthen.sty|, which allows access
177 % to the primitive \TeX\ syntax for a numeric test, rather than a |{}|
178 % delimited argument form, it is most convenient to work `within' an
179 % |\ifnum|. |\ift@throw| `throws' you out of the current |\ifnum| so
180 % that you can (eg) start an |\ifdim| for the length tests.
182 \def\TE@throw{\@ne=\@ne\noexpand\fi}
186 % \begin{macro}{\boolean}
187 % A non-standard extension to |ifthen|, supporting boolean was
188 % previously available, this is a simpler implementation.
191 \TE@throw\expandafter\noexpand\csname if#1\endcsname#2}
195 % \begin{macro}{\TE@length}
196 % Testing lengths. |#1| is the test. The extra argument gobbles spaces.
198 \def\TE@length#1#2{\TE@throw\noexpand\ifdim#1#2}
202 % \begin{macro}{\TE@odd}
203 % \begin{macro}{\TE@@odd}
204 % Testing odd/even. This is true if |#1| is an odd number, and false
205 % otherwise (even if |#1| is not a number at all).
207 % It is hard to make this completely reliable. Here I have erred on the
208 % side of safety. This should not generate a \TeX\ error if given any
209 % robust commands as its argument. However it returns true on any
210 % argument that \emph{starts} with an odd number |11xx| which is bad,
211 % and it can not deal with \TeX's count registers, although \LaTeX\
212 % counters work (via |\value|).
213 % \changes{v1.0b}{1993/12/17}{Improve \cs{isodd}.}
214 % \changes{v1.0c}{1994/01/20}{Improve \cs{isodd} again.}
217 \TE@throw\noexpand\TE@@odd#1\noexpand\@nil\noexpand\ifodd\count@#2}
220 % |\TE@@odd| is not expanded on the first pass.
222 \def\TE@@odd#1#2\@nil{%
224 \count@\if-#1-0\else0\expandafter#1\fi#2\relax\@nnil}
229 % \begin{macro}{\TE@repl}
230 % |\TE@repl| replaces the single token |#1| by |#2|. (Not within |{}|
231 % groups.) It is used to replace |\or| by |\TE@or| without the need to
232 % redefine |\or|. Earlier versions just |\let\or\TE@or| but this has a
233 % bad effect on the expansion of commands which use the primitive
234 % |\or| internally, eg |\alph|, and so caused surprising results if
235 % these commands were used inside |\equal|.
236 % \changes{v1.0h}{1994/05/14}{macro added}
239 \long\def\@tempc##1#1##2{%
240 \def\@tempa{##2}\def\@tempb{\@tempc}%
242 \toks@\expandafter{\the\toks@##1}%
245 \toks@\expandafter{\the\toks@##1#2}%
249 \expandafter\toks@\expandafter{\expandafter}%
250 \expandafter\@tempc\the\toks@#1\@tempc}
255 % \begin{macro}{\ifthenelse}
256 % The remaining macros in this file are derived from the ones in
257 % |ifthen.sty| but recoded and simplified. The main simplification is
258 % that the original style (and the |\boolean| extensions) expressed
259 % logical values always in terms of |\ifnum|. As |\fi| is `untyped' this
260 % is not necessary, so for example the length tests can return values
261 % via |\ifdim|, the trailing |\fi| will not complain, even though it was
262 % `expecting' an |\ifnum|. Also the system of passing information via
263 % macros expanding to |T| or |F| has been completely replaced by a
264 % simpler system using |\iftrue|, which furthermore allows lazy
265 % evaluation on the second pass.
267 \long\def\ifthenelse#1{%
269 % \changes{v1.0h}{1994/05/14}{Use \cs{TE@repl}}
277 % \changes{v1.1c}{2001/05/25}{Added \cs{AND}, \cs{OR} and \cs{NOT} as
278 % boolean operators as alternative to the lower case versions
280 % Support alternate names for the boolean operators (strictly
281 % speaking only |\OR| would be necessary).
287 % The original |ifthen.sty| processed everything inside a box
288 % assignment, to catch any extra spaces before they appeared in the
289 % output. Instead I have added extra arguments to the commands so they
290 % each remove any following space.
292 % Set up the user level names |\not| etc.
293 % \changes{v1.0c}{1994/01/20}{Modify \cs{protect} and \cs{value}}
294 % \changes{v1.0j}{1994/11/15}{Modify \cs{protect} add \cs{@setref}}
295 % \changes{v1.0l}{1996/03/22}
296 % {Use \cs{begingroup} not \cs{bgroup} for latex/2105}
297 % \changes{v1.1a}{1999/01/07}{\cs{isundefined} added for /2824}
300 \let\protect\@unexpandable@protect
301 \def\@setref##1##2##3{%
302 \ifx##1\relax\z@\else\expandafter##2##1\fi}%
303 \def\value##1{\the\csname c@##1\endcsname}%
304 \let\equal\TE@equal \let\(\TE@lparen \let\)\TE@rparen
305 \let\isodd\TE@odd \let\lengthtest\TE@length
306 \let\isundefined\TE@undef
308 % For the first pass, in a group, make various tokens non-expandable.
310 % It is unfortunate that in order to remain compatible with |ifthen|
311 % syntax, it is necessary to have a two pass system. The first pass
312 % inside an |\edef| `exposes' the |\if|\ldots\ |\fi| tokens, so the
313 % correct clauses may be skipped on the second pass. This means that the
314 % whole |\ifthenelse| command does not work by expansion, and so
315 % possibly has only limited usefulness for macro code writers.
316 % The main problem with the |ifthen:| syntax is that (unique for \LaTeX)
317 % it does not uses a brace delimited argument form, and exposes the
318 % primitive \TeX\ syntax for \meta{number}. Pretty much the only way of
319 % parsing |1 > 2 \or 2 < 1| is to actually evaluate the primitive
320 % |\ifnum|s. A syntax such as:\\%
321 % |\or{\numtest{1<2}}{\lengthtest{1pt<1in}}|\\
322 % could easily be evaluated in a one pass way, operating directly via
323 % expansion, and leaving no extra tokens in the token stream.
325 % Still, on with the code\ldots\ make |\@tempa| and |\@tempb| tokens
326 % non-expandable on the first pass.
327 % \changes{v1.0l}{1996/03/22}
328 % {Use \cs{begingroup} not \{ for latex/2105}
331 \let\@tempa\relax\let\@tempb\relax
332 \xdef\@gtempa{\expandafter\TE@eval\the\toks@\TE@endeval}%
335 % Now outside the group, execute |\@gtempa| which causes all the
336 % |\if|s etc., to be evaluated, the final truth value is contained in
337 % the |\newif| token |\ifTE@val|. Finally this is tested and either the
338 % first or second following argument is chosen accordingly.
339 % \changes{v1.0d}{1994/01/24}
340 % {Use \cs{@firstoftwo} not \cs{@leftmark}.}
343 \expandafter\endgroup\ifTE@val
344 \expandafter\@firstoftwo
346 \expandafter\@secondoftwo
351 % \begin{macro}{\TE@eval}
352 % Initialise a term. (Expanded on the first pass).
354 \def\TE@eval{\noexpand\TE@negatefalse\noexpand\iftrue\noexpand\ifnum}
358 % \begin{macro}{\ifTE@val}
359 % \begin{macro}{\ifTE@negate}
360 % Two |\newif|s the first holds the current truth value of the
361 % expression. The second is a temporary flag which is true if we need to
362 % negate the current proposition.
370 % \begin{macro}{\TE@endeval}
371 % Finalise a term. (Expanded on the first pass).
373 \def\TE@endeval{\relax
374 \noexpand\TE@setvaltrue\noexpand
376 \noexpand\TE@setvalfalse\noexpand
378 \noexpand\TE@negatefalse\noexpand
383 % \begin{macro}{\TE@setvaltrue}
384 % \begin{macro}{\TE@setvalfalse}
385 % Set the |\ifTE@val| to true or false depending on the value of the
386 % current proposition, and the negate flag. (Not expanded on the first
390 \ifTE@negate\TE@valfalse\else\TE@valtrue\fi}
391 \def\TE@setvalfalse{\let\ifTE@val\ifTE@negate}
396 % \begin{macro}{\TE@or}
397 % The internal version of |\or|. Ends the current term.
398 % If true skip the remaining terms.
400 \def\TE@or{\TE@endeval\noexpand\ifTE@val\noexpand\else\noexpand\ifnum}
404 % \begin{macro}{\TE@and}
405 % The internal version of |\and|. If false skip the remaining terms.
407 \def\TE@and{\TE@endeval\noexpand\ifTE@val\noexpand\ifnum}
411 % \begin{macro}{\TE@neg}
412 % \begin{macro}{\TE@negswitch}
413 % |\not|. Throw the current context, set a negate flag, then restart
414 % the |\ifnum|. |\TE@negswitch| is not expanded on the first pass.
416 \def\TE@neg{\TE@throw\noexpand\TE@negswitch\noexpand\ifnum}
417 \def\TE@negswitch{\ifTE@negate\TE@negatefalse\else\TE@negatetrue\fi}
422 % \begin{macro}{\TE@lparen}
423 % |\(|. Throw the current context, then restart a term inside a group.
425 \def\TE@lparen#1{\TE@throw\begingroup\TE@eval#1}
429 % \begin{macro}{\TE@rparen}
430 % |\)| end the current term, and the local group started by |\(|, but
431 % pass on the boolean value in |\if\@val T|. The |\noexpand| stops the
432 % |\expandafter| from expanding on the first pass.
436 \noexpand\expandafter\endgroup\noexpand\ifTE@val#1}
440 % \begin{macro}{\TE@equal}
441 % |\equal| greatly simplified from the original. |\def| may be used
442 % rather than |\edef| as the whole thing is expanded anyway in the
443 % first pass. The boolean can be directly encoded with the |\ifx|,
444 % there is no need to start an equivalent |\ifnum|.
445 % \changes{v1.0h}{1994/05/14}{make long}
447 \long\def\TE@equal#1#2#3{\TE@throw
448 \def\@tempa{#1}\def\@tempb{#2}%
449 \noexpand\ifx\@tempa\@tempb#3}
453 % \begin{macro}{\setboolean}
454 % |\setboolean| takes |true| or |false|, as |#2|, and sets |#1|
456 % \changes{v1.0i}{1994/05/27}{New style error commands}
458 \def\setboolean#1#2{%
459 \lowercase{\def\@tempa{#2}}%
460 \@ifundefined{@tempswa\@tempa}%
461 {\PackageError{ifthen}%
462 {You can only set a boolean to `true' or `false'}\@ehc}%
463 {\@ifundefined{#1\@tempa}%
464 {\PackageError{ifthen}{Boolean #1 undefined}\@ehc}%
465 {\csname#1\@tempa\endcsname}}}
469 % \begin{macro}{\newboolean}
470 % \changes{v1.0m}{1996/08/02}{Add \cs{@ifdefinable} test for latex/2220}
471 % Define a new `boolean'.
474 \expandafter\@ifdefinable\csname if#1\endcsname{%
475 \expandafter\newif\csname if#1\endcsname}}
479 % \begin{macro}{\provideboolean}
480 % \changes{v1.0m}{1996/08/02}{Macro added for latex/2220}
481 % Define a new `boolean' if it is not already defined.
483 \def\provideboolean#1{%
484 \@ifundefined{if#1}{%
485 \expandafter\newif\csname if#1\endcsname}\relax}
489 % \begin{macro}{\whiledo}
490 % |\whiledo| copied directly from the original.\\
491 % |\whiledo{|\meta{test}|}{|\meta{body}|}|\\
492 % repeatedly evaluates \meta{body} until \meta{test} is true.
494 \long\def\whiledo#1#2{%
497 \@whilesw\if@whiledo\fi
499 \ifthenelse{#1}\@whiledotrue\@whiledofalse}}%
505 % \begin{macro}{\TE@undef}
506 % \changes{v1.1a}{1999/01/07}{Macro added for /2824}
507 % test if csname is defined. |\ifx| test.
510 \TE@throw\noexpand\ifx\noexpand\@undefined\noexpand#1#2}
514 % \begin{macro}{\if@whiledo}
515 % Internal switch for |\whiledo|.