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}
70 % This file implements an |\ifthenelse| command for \LaTeXe.
71 % The algorithm used is compatible with that used in the \LaTeX~2.09
72 % |ifthen| style option. It has been recoded, making the resulting
73 % definitions somewhat more compact and efficient.
76 % \changes{v1.0a}{1993/10/15}{New implementation}
77 % \changes{v1.0b}{1993/12/17}{Upgrade to LaTeX2e}
78 % \changes{v1.0e}{1994/02/11}{Improve documentation}
79 % \changes{v1.0f}{1994/03/02}{Remove need for dtx file}
80 % \changes{v1.0g}{1994/03/14}{Modify for the new ltxdoc.cls}
81 % \changes{v1.0k}{1995/04/25}{Fix `driver' docstrip guards.}
82 % \changes{v1.0n}{1997/11/03}{Documentation fix.}
83 % \changes{v1.0o}{1998/08/17}{Documentation fix.}
85 % \section{Introduction}
87 % \DescribeMacro{\ifthenelse}
89 % \meta{test}|}{|\meta{then clause}|}{|\meta{else clause}|}|
91 % Evaluates \meta{test} as a boolean function, and then executes
92 % either \meta{then clause} or \meta{else clause}.
94 % \meta{test} is a boolean expression using the infix connectives,
95 % |\and|, |\or|, the unary |\not| and parentheses |\( \)|.
97 % As an alternative notation |\AND|, |\OR| and |\NOT| can be
98 % used. This is safer since it can't be misinterpreted
99 % when appearing inside a \TeX-conditional in which |\or| has a
102 % The atomic propositions are:\\
103 % \meta{number} |<| \meta{number} \\
104 % \meta{number} |=| \meta{number} \\
105 % \meta{number} |>| \meta{number} \\
106 % |\isodd{| \meta{number} |}|\\
107 % |\isundefined{| \meta{command name} |}|\\
108 % |\equal{|\meta{string}|}{|\meta{string}|}|\\
109 % |\lengthtest{|\meta{dimen}|<|\meta{dimen}|}|\\
110 % |\lengthtest{|\meta{dimen}|=|\meta{dimen}|}|\\
111 % |\lengthtest{|\meta{dimen}|>|\meta{dimen}|}|\\
112 % |\boolean{|\meta{name}|}|
114 % The \meta{string}s tested by |\equal| may be any sequence of commands
115 % that expand to a list of tokens. If these expansions are equal, then
116 % the proposition is true.
118 % |\isodd| is true if the \meta{number} is odd, and false otherwise
119 % (even if the argument is not a number).
121 % |\isundefined{\cmd}| is true if |\cmd| is not defined.
123 % |\boolean{xyz}| returns the truth value contained in the primitive
124 % \TeX\ |\if|, |\ifxyz|. This is usually used with boolean flags
125 % created with |\newboolean| and |\provideboolean| described below.
126 % It can also be used with the names of |\newif| created tokens, and
127 % primitive \TeX\ |\if| constructs, for example |\boolean{true}|
128 % (|\iftrue|), |\boolean{mmode}| (|\ifmmode|) etc.
132 % |\newboolean|\marg{name}\DescribeMacro{\newboolean}\ and\
133 % |\provideboolean|\marg{name}\DescribeMacro{\provideboolean}\
134 % are provided so the user can easily create new boolean flags.
135 % As for |\newcommand|, |\newboolean| generates an error if the
136 % command name is not new. |\provideboolean| silently does nothing in
139 % The boolean flags may be set with:\\
140 % |\setboolean|\marg{name}\marg{value}\DescribeMacro{\setboolean}\\
141 % \meta{value} may be either |true| or |false| (any CaSe).
143 % Note that there is no precedence between |\and| and |\or|.
144 % The proposition is evaluated in a left right manner. |\not| only
145 % applies to the immediately following proposition. (This is consistent
146 % with Lamport's |ifthen.sty|.) In this style, though the test is
147 % `lazily' evaluated, so for instance if the first proposition in an
148 % |\or| is true, the second one is skipped. (On the second pass---the
149 % first pass in an |\edef| expands clauses in all propositions.)
151 % Apart from the addition of the extra atomic propositions |\isodd|,
152 % |\boolean|, |\lengthtest| and |\isundefined|,
153 % the only known incompatibility is that
154 % in this package the expression|\not\not|\meta{P} is equivalent to
156 % However in the original style it was equivalent to |\not|\meta{P}.
157 % This is intentional (bug fix:-).
159 % \DescribeMacro{\whiledo}
160 % The command |\whiledo| is also defined (copied directly from
161 % the \LaTeX2.09 definition).
163 % |\whiledo{|\meta{test}|}{|\meta{while clause}|}|
165 % With \meta{test} as above, repeatedly executes \meta{while clause}
166 % while the test remains true.
170 % \section{The Implementation}
176 % \begin{macro}{\TE@throw}
177 % In order to support the syntax of |ifthen.sty|, which allows access
178 % to the primitive \TeX\ syntax for a numeric test, rather than a |{}|
179 % delimited argument form, it is most convenient to work `within' an
180 % |\ifnum|. |\ift@throw| `throws' you out of the current |\ifnum| so
181 % that you can (eg) start an |\ifdim| for the length tests.
183 \def\TE@throw{\@ne=\@ne\noexpand\fi}
187 % \begin{macro}{\boolean}
188 % A non-standard extension to |ifthen|, supporting boolean was
189 % previously available, this is a simpler implementation.
192 \TE@throw\expandafter\noexpand\csname if#1\endcsname#2}
196 % \begin{macro}{\TE@length}
197 % Testing lengths. |#1| is the test. The extra argument gobbles spaces.
199 \def\TE@length#1#2{\TE@throw\noexpand\ifdim#1#2}
203 % \begin{macro}{\TE@odd}
204 % \begin{macro}{\TE@@odd}
205 % Testing odd/even. This is true if |#1| is an odd number, and false
206 % otherwise (even if |#1| is not a number at all).
208 % It is hard to make this completely reliable. Here I have erred on the
209 % side of safety. This should not generate a \TeX\ error if given any
210 % robust commands as its argument. However it returns true on any
211 % argument that \emph{starts} with an odd number |11xx| which is bad,
212 % and it can not deal with \TeX's count registers, although \LaTeX\
213 % counters work (via |\value|).
214 % \changes{v1.0b}{1993/12/17}{Improve \cs{isodd}.}
215 % \changes{v1.0c}{1994/01/20}{Improve \cs{isodd} again.}
218 \TE@throw\noexpand\TE@@odd#1\noexpand\@nil\noexpand\ifodd\count@#2}
221 % |\TE@@odd| is not expanded on the first pass.
223 \def\TE@@odd#1#2\@nil{%
225 \count@\if-#1-0\else0\expandafter#1\fi#2\relax\@nnil}
230 % \begin{macro}{\TE@repl}
231 % |\TE@repl| replaces the single token |#1| by |#2|. (Not within |{}|
232 % groups.) It is used to replace |\or| by |\TE@or| without the need to
233 % redefine |\or|. Earlier versions just |\let\or\TE@or| but this has a
234 % bad effect on the expansion of commands which use the primitive
235 % |\or| internally, eg |\alph|, and so caused surprising results if
236 % these commands were used inside |\equal|.
237 % \changes{v1.0h}{1994/05/14}{macro added}
240 \long\def\@tempc##1#1##2{%
241 \def\@tempa{##2}\def\@tempb{\@tempc}%
243 \toks@\expandafter{\the\toks@##1}%
246 \toks@\expandafter{\the\toks@##1#2}%
250 \expandafter\toks@\expandafter{\expandafter}%
251 \expandafter\@tempc\the\toks@#1\@tempc}
256 % \begin{macro}{\ifthenelse}
257 % The remaining macros in this file are derived from the ones in
258 % |ifthen.sty| but recoded and simplified. The main simplification is
259 % that the original style (and the |\boolean| extensions) expressed
260 % logical values always in terms of |\ifnum|. As |\fi| is `untyped' this
261 % is not necessary, so for example the length tests can return values
262 % via |\ifdim|, the trailing |\fi| will not complain, even though it was
263 % `expecting' an |\ifnum|. Also the system of passing information via
264 % macros expanding to |T| or |F| has been completely replaced by a
265 % simpler system using |\iftrue|, which furthermore allows lazy
266 % evaluation on the second pass.
268 \long\def\ifthenelse#1{%
270 % \changes{v1.0h}{1994/05/14}{Use \cs{TE@repl}}
278 % \changes{v1.1c}{2001/05/25}{Added \cs{AND}, \cs{OR} and \cs{NOT} as
279 % boolean operators as alternative to the lower case versions
281 % Support alternate names for the boolean operators (strictly
282 % speaking only |\OR| would be necessary).
288 % The original |ifthen.sty| processed everything inside a box
289 % assignment, to catch any extra spaces before they appeared in the
290 % output. Instead I have added extra arguments to the commands so they
291 % each remove any following space.
293 % Set up the user level names |\not| etc.
294 % \changes{v1.0c}{1994/01/20}{Modify \cs{protect} and \cs{value}}
295 % \changes{v1.0j}{1994/11/15}{Modify \cs{protect} add \cs{@setref}}
296 % \changes{v1.0l}{1996/03/22}
297 % {Use \cs{begingroup} not \cs{bgroup} for latex/2105}
298 % \changes{v1.1a}{1999/01/07}{\cs{isundefined} added for /2824}
301 \let\protect\@unexpandable@protect
302 \def\@setref##1##2##3{%
303 \ifx##1\relax\z@\else\expandafter##2##1\fi}%
304 \def\value##1{\the\csname c@##1\endcsname}%
305 \let\equal\TE@equal \let\(\TE@lparen \let\)\TE@rparen
306 \let\isodd\TE@odd \let\lengthtest\TE@length
307 \let\isundefined\TE@undef
309 % For the first pass, in a group, make various tokens non-expandable.
311 % It is unfortunate that in order to remain compatible with |ifthen|
312 % syntax, it is necessary to have a two pass system. The first pass
313 % inside an |\edef| `exposes' the |\if|\ldots\ |\fi| tokens, so the
314 % correct clauses may be skipped on the second pass. This means that the
315 % whole |\ifthenelse| command does not work by expansion, and so
316 % possibly has only limited usefulness for macro code writers.
317 % The main problem with the |ifthen:| syntax is that (unique for \LaTeX)
318 % it does not uses a brace delimited argument form, and exposes the
319 % primitive \TeX\ syntax for \meta{number}. Pretty much the only way of
320 % parsing |1 > 2 \or 2 < 1| is to actually evaluate the primitive
321 % |\ifnum|s. A syntax such as:\\%
322 % |\or{\numtest{1<2}}{\lengthtest{1pt<1in}}|\\
323 % could easily be evaluated in a one pass way, operating directly via
324 % expansion, and leaving no extra tokens in the token stream.
326 % Still, on with the code\ldots\ make |\@tempa| and |\@tempb| tokens
327 % non-expandable on the first pass.
328 % \changes{v1.0l}{1996/03/22}
329 % {Use \cs{begingroup} not \{ for latex/2105}
332 \let\@tempa\relax\let\@tempb\relax
333 \xdef\@gtempa{\expandafter\TE@eval\the\toks@\TE@endeval}%
336 % Now outside the group, execute |\@gtempa| which causes all the
337 % |\if|s etc., to be evaluated, the final truth value is contained in
338 % the |\newif| token |\ifTE@val|. Finally this is tested and either the
339 % first or second following argument is chosen accordingly.
340 % \changes{v1.0d}{1994/01/24}
341 % {Use \cs{@firstoftwo} not \cs{@leftmark}.}
344 \expandafter\endgroup\ifTE@val
345 \expandafter\@firstoftwo
347 \expandafter\@secondoftwo
352 % \begin{macro}{\TE@eval}
353 % Initialise a term. (Expanded on the first pass).
355 \def\TE@eval{\noexpand\TE@negatefalse\noexpand\iftrue\noexpand\ifnum}
359 % \begin{macro}{\ifTE@val}
360 % \begin{macro}{\ifTE@negate}
361 % Two |\newif|s the first holds the current truth value of the
362 % expression. The second is a temporary flag which is true if we need to
363 % negate the current proposition.
371 % \begin{macro}{\TE@endeval}
372 % Finalise a term. (Expanded on the first pass).
374 \def\TE@endeval{\relax
375 \noexpand\TE@setvaltrue\noexpand
377 \noexpand\TE@setvalfalse\noexpand
379 \noexpand\TE@negatefalse\noexpand
384 % \begin{macro}{\TE@setvaltrue}
385 % \begin{macro}{\TE@setvalfalse}
386 % Set the |\ifTE@val| to true or false depending on the value of the
387 % current proposition, and the negate flag. (Not expanded on the first
391 \ifTE@negate\TE@valfalse\else\TE@valtrue\fi}
392 \def\TE@setvalfalse{\let\ifTE@val\ifTE@negate}
397 % \begin{macro}{\TE@or}
398 % The internal version of |\or|. Ends the current term.
399 % If true skip the remaining terms.
401 \def\TE@or{\TE@endeval\noexpand\ifTE@val\noexpand\else\noexpand\ifnum}
405 % \begin{macro}{\TE@and}
406 % The internal version of |\and|. If false skip the remaining terms.
408 \def\TE@and{\TE@endeval\noexpand\ifTE@val\noexpand\ifnum}
412 % \begin{macro}{\TE@neg}
413 % \begin{macro}{\TE@negswitch}
414 % |\not|. Throw the current context, set a negate flag, then restart
415 % the |\ifnum|. |\TE@negswitch| is not expanded on the first pass.
417 \def\TE@neg{\TE@throw\noexpand\TE@negswitch\noexpand\ifnum}
418 \def\TE@negswitch{\ifTE@negate\TE@negatefalse\else\TE@negatetrue\fi}
423 % \begin{macro}{\TE@lparen}
424 % |\(|. Throw the current context, then restart a term inside a group.
426 \def\TE@lparen#1{\TE@throw\begingroup\TE@eval#1}
430 % \begin{macro}{\TE@rparen}
431 % |\)| end the current term, and the local group started by |\(|, but
432 % pass on the boolean value in |\if\@val T|. The |\noexpand| stops the
433 % |\expandafter| from expanding on the first pass.
437 \noexpand\expandafter\endgroup\noexpand\ifTE@val#1}
441 % \begin{macro}{\TE@equal}
442 % |\equal| greatly simplified from the original. |\def| may be used
443 % rather than |\edef| as the whole thing is expanded anyway in the
444 % first pass. The boolean can be directly encoded with the |\ifx|,
445 % there is no need to start an equivalent |\ifnum|.
446 % \changes{v1.0h}{1994/05/14}{make long}
448 \long\def\TE@equal#1#2#3{\TE@throw
449 \def\@tempa{#1}\def\@tempb{#2}%
450 \noexpand\ifx\@tempa\@tempb#3}
454 % \begin{macro}{\setboolean}
455 % |\setboolean| takes |true| or |false|, as |#2|, and sets |#1|
457 % \changes{v1.0i}{1994/05/27}{New style error commands}
459 \def\setboolean#1#2{%
460 \lowercase{\def\@tempa{#2}}%
461 \@ifundefined{@tempswa\@tempa}%
462 {\PackageError{ifthen}%
463 {You can only set a boolean to `true' or `false'}\@ehc}%
464 {\@ifundefined{#1\@tempa}%
465 {\PackageError{ifthen}{Boolean #1 undefined}\@ehc}%
466 {\csname#1\@tempa\endcsname}}}
470 % \begin{macro}{\newboolean}
471 % \changes{v1.0m}{1996/08/02}{Add \cs{@ifdefinable} test for latex/2220}
472 % Define a new `boolean'.
475 \expandafter\@ifdefinable\csname if#1\endcsname{%
476 \expandafter\newif\csname if#1\endcsname}}
480 % \begin{macro}{\provideboolean}
481 % \changes{v1.0m}{1996/08/02}{Macro added for latex/2220}
482 % Define a new `boolean' if it is not already defined.
484 \def\provideboolean#1{%
485 \@ifundefined{if#1}{%
486 \expandafter\newif\csname if#1\endcsname}\relax}
490 % \begin{macro}{\whiledo}
491 % |\whiledo| copied directly from the original.\\
492 % |\whiledo{|\meta{test}|}{|\meta{body}|}|\\
493 % repeatedly evaluates \meta{body} until \meta{test} is true.
495 \long\def\whiledo#1#2{%
498 \@whilesw\if@whiledo\fi
500 \ifthenelse{#1}\@whiledotrue\@whiledofalse}}%
506 % \begin{macro}{\TE@undef}
507 % \changes{v1.1a}{1999/01/07}{Macro added for /2824}
508 % test if csname is defined. |\ifx| test.
511 \TE@throw\noexpand\ifx\noexpand\@undefined\noexpand#1#2}
515 % \begin{macro}{\if@whiledo}
516 % Internal switch for |\whiledo|.