summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ifthenx/ifthenx.sty
blob: 147984fe7e2ee45f5f09867775381fbc12e5063a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
% $Id: ifthenx.sty 790 2012-04-01 10:59:41Z Geoffrey $
%
% ifthenx.sty Copyright (C) 2012 Geoffrey Jones 2012/04/01 (v0.1a)
% <geoffrey stop jones snailshell uqconnect stop edu stop au>
%
% Package ifthenx.sty -- this little package extends David Carlisle's
% ifthen package, adding a few extra tests. These include:
%     \isinteger
%     \ispositiveinteger
%     \isrealnumber           (synonym: \isnumber)
%     \ispositiverealnumber   (synonym: \ispositivenumber)
%     \classloaded
%     \packageloaded
%     \fileexists
%
% Example:
%   \ifthenelse{\ispositiveinteger{\foo}\AND\fileexists{\foo.log}}
%     { commands to execute if true  }
%     { commands to execute if false }
%
%   The production rules for numbers are quite straightforward:
%     \ispositiveinteger    (string of one or more digits)
%     \isinteger            (ditto, with optional leading minus sign)
%     \isrealnumber         (ditto, with optional decimal point)
%     \ispositiverealnumber (ditto, with no leading minus sign)
%
%   Note that well-formed numbers are considered negative if they
%   possess a leading minus sign. Accordingly, while 0, 0.0, -0 and
%   -0.0 are all well-formed numbers, only 0 and 0.0 test positive.
%   Consider this a feature.
%
% Motivation:
%   While nowadays the ifthen package is sometimes deprecated (e.g., see the
%   March 2011 stackexchange.com conversation on this topic), it has several
%   useful attributes that make it attractive in certain situations.
%   Setting aside its downsides (particularly, the care needed when using it
%   inside fragile contexts), positives include:
%   1. it provides a simple mechanism for assembling complex logical
%      expressions, i.e., those employing \AND, \OR, \NOT and \( ... \)
%   2. its syntax, while always subject to preference, may be somewhat
%      easier for LaTeX novices to read, write and understand. (To my
%      eye, it presents a more approachable and certainly more uniform
%      grammar than etoolbox's syntax. E.g., compare
%        \ifboolexpr{ test {\ifnumcomp{\value{mycounter}}{>}{3}}}...
%      with
%        \ifthenelse{\value{mycounter} > 3}...
%      )
%   3. string expansion semantics (cf., etoolbox that does not)
%   4. it is relatively lightweight compared to, say, the might of etoolbox
%      (although note that we use etoolbox's \patchcmd if already loaded).
%
% Required Packages:
%   ifthen.sty (that's all)
%
%   However, note that ifthenx uses \patchcmd if etoolbox.sty is already
%   loaded.
%
% Related Packages:
%   xifthen.sty provides an alternative set of ifthen package extension
%   tests, several of which are based around capabilities drawn from
%   the calc package.
%
%   xifthen and ifthenx are compatible, but only if xifthen is loaded
%   before ifthenx (xifthen doesn't patch \ifthenelse but rewrites its
%   own modified version instead). If loaded in the recommended order,
%   users can ``mix and match'' the tests.
%   For example:
%       \newcommand*\foo{3}
%       \newcommand*\baz{98}
%       \ifthenelse{
%         \isnumber{\foo}                   % ifthenx pkg command
%         \AND\isodd{\foo}                  % ifthen pkg command
%         \AND\cnttest{\foo + \baz}{>}{100} % xifthen pkg command
%       }{true}{false}
%     evaluates true.
%
% Version: v0.1a *** release for comments ***
%    The author would welcome bug reports, comments, suggestions,
%    extensions and so forth at this early stage.
%
% Acknowledgements:
%   Gracious thanks David Carlisle for the ifthen package and for
%   teaching me a new technique for run-time patching macro code.
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, either version 1.3 of this license or (at
% your option) any later version. The latest version of this license is in
% http://www.latex-project.org/lppl.txt and version 1.3 or later is part of
% all distributions of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Original Author of this work is Geoffrey Jones.
% The Current Maintainer of this work is Geoffrey Jones.
% ==========================================================================
\NeedsTeXFormat{LaTeX2e}[1994/12/01]

\ProvidesPackage{ifthenx}
  [2012/04/01 (v0.1a) ifthen package extensions (GJ)]

\RequirePackage{ifthen}

\def\itx@patchpayload{%
  \let\isinteger\itx@isinteger
  \let\ispositiveinteger\itx@ispositiveinteger
  \let\isrealnumber\itx@isrealnumber%   <-- synonyms (a)
  \let\isnumber\itx@isrealnumber%       <-- synonyms (a)
  \let\ispositiverealnumber\itx@ispositiverealnumber%   <-- synonyms (b)
  \let\ispositivenumber\itx@ispositiverealnumber%       <-- synonyms (b)
  \let\classloaded\itx@classloaded
  \let\packageloaded\itx@packageloaded
  \let\fileexists\itx@fileexists
}

\newif\ifitx@patched
\itx@patchedfalse

\@ifpackageloaded{etoolbox}{
  % compile-time patch
  \patchcmd{\ifthenelse}
    {\TE@undef}{\TE@undef\itx@patchpayload}
    {\itx@patchedtrue}{}
}{}

\ifitx@patched\else
  % run-time patch
  \let\itx@ifthenelse\ifthenelse
  \long\def\ifthenelse{\expandafter\itx@@patch\itx@ifthenelse}
  \def\itx@@patch#1\TE@undef{#1\TE@undef\itx@patchpayload}
  \itx@patchedtrue % quite unnecessary but at least complete
\fi

% Pattern: all the tests that follow share a similar structure.
% Each entry command sets up conditions then executes a helper
% macro. The helper writes `!' or `?' to a temporary macro,
% respectively to signify test success/true or failure/false.
% This is then expanded and responded to by the mainline command.
% Note that, where appropriate (the number tests), we ensure that
% any `!' contained in arguments won't interrupt this pattern.

%------------------------------------------------------------------
% \ifthenelse{\packageloaded{<pkgname>}}{<true part>}{<false part>}
% \ifthenelse{\classloaded{<clsname>}}{<true part>}{<false part>}
%------------------------------------------------------------------
\def\itx@packageloaded#1#2{%
  \TE@neg\TE@throw\noexpand\itx@@packageloaded#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@packageloaded#1\@nil{%
  \def\@tempa{\if\csname ver@#1.sty\endcsname\relax!\else?\fi}}

\def\itx@classloaded#1#2{%
  \TE@neg\TE@throw\noexpand\itx@@classloaded#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@classloaded#1\@nil{%
  \def\@tempa{\if\csname ver@#1.cls\endcsname\relax!\else?\fi}}

%------------------------------------------------------------------
% \ifthenelse{\fileexists{<file.ext>}}{<true part>}{<false part>}
%
% Normal TeX file search conventions apply, e.g., if file `x.tex'
% exists in the TEXMF file structure or a local path, then
%     \ifthenelse{\fileexists{x}}{<true part>}{<false part>}
% will branch along the <true part> path.
%
% \input@path will be searched if the file isn't found in the
% installation TEXMF tree. This can be configured; for example,
%     \newcommand*\input@path{{C:/Temp/}{C:/tmp/}}
% Note that each search path must reside within its own group,
% and that each must end with a file path separator '/'.
%
% Also note that this test trims leading (but not trailing) space
% characters from its argument before producing its result. Like
% LaTeX's native \IfFileExists, this test returns false if passed
% an empty (zero length) filename and, rather absurdly, true
% if the filename expands to \relax.
%
% While our \filexists test (mostly) follows \IfFileExists semantics,
% we resist the urge to emulate the following rather rare and thus
% far unreported bug...
%
%   LaTeX2e \IfFileExists bug. Under the following conditions,
%     1. \input@path has been defined, and
%     2. filename includes one or more leading spaces, and
%     3. file does not exist in TEXMF or in \input@path
%   then, once for every path specified in \input@path,
%   \IfFileExists places filename on the token stream.
%
%   Example:
%     \newcommand\input@path{{C:/Temp/}{D:/tmp/}}
%     \IfFileExists{ xx}{yes}{no}
%   produces:
%     xx xx no
%------------------------------------------------------------------
\def\itx@fileexists#1#2{%
  \TE@throw\openin\@inputcheck=#1 % local endgroup implicitly closes the file
  \noexpand\itx@@fileexists#1\noexpand\@nil%
  \noexpand\@tempa\noexpand\if!\@tempb#2}

\def\itx@@fileexists#1\@nil{%
  \def\@tempa{%
    \edef\@tempc{% zap leading spaces in #1
      \detokenize\expandafter{\romannumeral-`X\expandafter\noexpand#1}}%
    \if\relax\@tempc\relax% catch empty #1
      \def\@tempb{?}%
    \else
      \ifeof\@inputcheck%
        \ifx\input@path\@undefined%
          \def\@tempb{?}%
        \else%
         \@iffileonpath{\@tempc}{\def\@tempb{!}}{\def\@tempb{?}}%
        \fi%
      \else%
        \def\@tempb{!}%
      \fi%
    \fi%
  }%
}

%----------------------------------------------------------------------------
% \ifthenelse{<numtest>{...}}{<true part>}{<false part>}, where <numtest> is:
%   \ispositiveinteger    (string of one or more digits)
%   \isinteger            (ditto, with optional leading minus sign)
%   \isrealnumber         (ditto, with optional decimal point)
%   \ispositiverealnumber (ditto, with no leading minus sign)
%
% NB, 0, 0.0, etc consigned positive, -0, -0.0, etc negative by these tests.
%----------------------------------------------------------------------------
\def\itx@gobbleleadingminus#1{\if-#1\else#1\fi}

\def\itx@gobblefirstdecimalpoint#1{%
  \expandafter\itx@@gobblefirstdecimalpoint%
  \romannumeral-`X#1\@empty.\@empty\relax}

\def\itx@@gobblefirstdecimalpoint#1.#2\@empty#3\relax{#1#2}

\def\itx@ispositiveinteger#1#2{%
  \TE@throw\noexpand\in@{!}{#1}%
  \noexpand\itx@@ispositiveinteger#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@ispositiveinteger#1\@nil{%
  \def\@tempa{\ifin@?\else\ifnum9<1#1!\else?\fi\fi}}

\def\itx@isinteger#1#2{%
  \TE@throw\noexpand\in@{!}{#1}%
  \noexpand\itx@@isinteger#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@isinteger#1\@nil{%
  \expandafter\itx@@ispositiveinteger%
    \expandafter{\expandafter\itx@gobbleleadingminus#1}\@nil}

\def\itx@ispositiverealnumber#1#2{%
  \TE@throw\noexpand\in@{!}{#1}%
  \noexpand\itx@@ispositiverealnumber#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@ispositiverealnumber#1\@nil{%
  \expandafter\itx@@ispositiveinteger%
    \expandafter{\expandafter\itx@gobblefirstdecimalpoint%
      \expandafter{#1}}\@nil}

\def\itx@isrealnumber#1#2{%
  \TE@throw\noexpand\in@{!}{#1}%
  \noexpand\itx@@isrealnumber#1\noexpand\@nil%
  \noexpand\if!\@tempa#2}

\def\itx@@isrealnumber#1\@nil{%
  \expandafter\itx@@ispositiveinteger%
    \expandafter{\expandafter\itx@gobblefirstdecimalpoint%
      \expandafter{\expandafter\itx@gobbleleadingminus%
        \expandafter{#1}}}\@nil}

%
%============================ end ifthenx.sty ============================