| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \newif {Special if-macros, is-macros and loops <2025-03-01>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ---------------------------- |
| 6 |
\secc Classical \code{\\newif} |
| 7 |
The \`\newif` macro implements boolean value. |
| 8 |
It works as in plain \TeX. It means that |
| 9 |
after `\newif\ifxxx` you can use `\xxxtrue` or |
| 10 |
`\xxxfalse` to set the boolean value and use `\ifxxx true\else false\fi` |
| 11 |
to test this value. The default value is false. |
| 12 |
|
| 13 |
The macro \`\_newifi` enables to declare `\_ifxxx` and to use `\_xxxtrue` and |
| 14 |
`\_xxxfalse`. This means that it is usable for the internal namespace |
| 15 |
(`_`prefixed macros). |
| 16 |
\_cod ---------------------------- |
| 17 |
|
| 18 |
\_def\_newif #1{\_ea\_newifA \_string #1\_relax#1} |
| 19 |
\_ea\_def \_ea\_newifA \_string\if #1\_relax#2{% |
| 20 |
\_sdef{#1true}{\_let#2=\_iftrue}% |
| 21 |
\_sdef{#1false}{\_let#2=\_iffalse}% |
| 22 |
\_let#2=\_iffalse |
| 23 |
} |
| 24 |
\_def\_newifi #1{\_ea\_newifiA \string#1\_relax#1} |
| 25 |
\_ea\_def \_ea\_newifiA \_string\_if #1\_relax#2{% |
| 26 |
\_sdef{_#1true}{\_let#2=\_iftrue}% |
| 27 |
\_sdef{_#1false}{\_let#2=\_iffalse}% |
| 28 |
\_let#2=\_iffalse |
| 29 |
} |
| 30 |
\_public \newif ; |
| 31 |
|
| 32 |
\_doc ---------------------------- |
| 33 |
\`\afterfi` `{<what to do>}<ignored>\fi` closes condition by `\fi` and processes |
| 34 |
<what to do>. Usage: |
| 35 |
\begtt \catcode`<=13 |
| 36 |
\if<something> \afterfi{<result is true>} \else \afterfi{<result is false>} \fi |
| 37 |
\endtt |
| 38 |
\`\afterxfi` `{<what to do>}<ignored>\xfipoint` closes all opened |
| 39 |
`\if..\fi` conditionals until \`\xfipoint` is found and then proceses <what to do>. |
| 40 |
The <ignored> text must expand to nothing (or spaces only); arbitrary number |
| 41 |
of opened conditionals can be closed here. Example: |
| 42 |
\begtt |
| 43 |
\ifodd#1 \ifodd#2 \afterxfi{odd-odd} \else \afterxfi{odd-even} \fi |
| 44 |
\else \ifodd#2 \afterxfi{even-odd} \else \afterxfi{even-even} \fi |
| 45 |
\fi \xfipoint |
| 46 |
\endtt |
| 47 |
Nested `\if..\afterfi{\if..\afterfi{...}\fi}\fi` are possible. Another approach is mentioned |
| 48 |
in \ulink[http://petr.olsak.net/optex/optex-tricks.html\#fiinif]{OpTeX trick 0098} |
| 49 |
which also solves the `\fi` in `\if` problem. |
| 50 |
\_cod ---------------------------- |
| 51 |
|
| 52 |
\_long\_def \_afterfi#1#2\_fi{\_fi#1} |
| 53 |
\_long\_def \afterfi#1#2\fi{\_fi#1} |
| 54 |
\_let\_xfipoint=\_empty \_let\xfipoint=\_empty % \xfipoint alone does nothing |
| 55 |
\_long\_def \_afterxfi#1#2\_xfipoint{\_romannumeral#20 #1} |
| 56 |
\_long\_def \afterxfi#1#2\xfipoint{\_romannumeral#20 #1} |
| 57 |
|
| 58 |
\_doc ---------------------------- |
| 59 |
\secc Loops |
| 60 |
The \`\loop` `<codeA> \ifsomething <codeB>` \`\repeat` loops `<codeA><codeB>` |
| 61 |
until `\ifsomething` is false. Then `<codeB>` is not executed and loop is |
| 62 |
finished. This works like in plain \TeX, but implementation is somewhat |
| 63 |
better (you can use `\else` clause after the `\ifsomething`). |
| 64 |
There are public version `\loop...\repeat` and private version |
| 65 |
\`\_loop` ...\`\_repeat`. You cannot mix both versions in one loop. |
| 66 |
|
| 67 |
The `\loop` macro keeps its original plain \TeX/ meaning. It is not |
| 68 |
expandable and nested `\loop`s are possible only in a \TeX/ group. |
| 69 |
\_cod ---------------------------- |
| 70 |
|
| 71 |
\_long\_def \_loop #1\_repeat{\_def\_body{#1}\_iterate} |
| 72 |
\_long\_def \loop #1\repeat{\_def\_body{#1}\_iterate} |
| 73 |
\_let \_repeat=\_fi % this makes \loop...\if...\repeat skippable |
| 74 |
\_let \repeat=\_fi |
| 75 |
\_def \_iterate {\_body \_ea \_iterate \_fi} |
| 76 |
|
| 77 |
\_doc ----------------------------- |
| 78 |
\`\xloop` `<parameters>\do <codeA> \ifsomething <codeB> \repeat` or\nl |
| 79 |
\`\_xloop` `<parameters>\_do <codeA> \_ifsomething <codeB> \_repeat` |
| 80 |
behave analogically like \^`\loop`, but moreover: you can specify a rule of |
| 81 |
parameters scanning (like in `\def` primitive) and you can |
| 82 |
use these parameters (i.e. `#1`, `#2`, etc.) in the <codeA> or <codeB>. |
| 83 |
Each iteration of this loop reads next parameters from the input queue |
| 84 |
(just after `\repeat`) using the <parameters> rule. Example: |
| 85 |
\begtt |
| 86 |
\def\countparams{\tmpnum=0 |
| 87 |
\xloop ##1,\do \ifx\end##1\empty \else \incr\tmpnum \repeat} |
| 88 |
\def\list{A, B, C} |
| 89 |
\ea \countparams \list,\end, % \tmpnum = the number of comma separated parameters |
| 90 |
\endtt |
| 91 |
Second new feature: the \^`\xloop` macro is expandable, |
| 92 |
if <codeA> and <codeB> include only expandable macros. This is not the |
| 93 |
case of previous example but you can prefix `\tmpnum=0` and `\incr` by |
| 94 |
the `\immediateassignment` \LuaTeX/ primitive and you get expandable macro |
| 95 |
`\countparams`. |
| 96 |
\_cod ----------------------------- |
| 97 |
|
| 98 |
\_long\_def\xloop#1\do#2\repeat{\_immediateassignment\_long\_def\_body#1{#2\_ea\_body\_fi}\_body} |
| 99 |
\_long\_def\_xloop#1\_do#2\_repeat{\_immediateassignment\_long\_def\_body#1{#2\_ea\_body\_fi}\_body} |
| 100 |
|
| 101 |
\_doc ----------------------------- |
| 102 |
{\_let\_moremainpoints=\_relax |
| 103 |
\`\foreach` `<list>`\`\do` `{<what>}` |
| 104 |
repeats `<what>` for each element of the |
| 105 |
`<list>`. The `<what>` can include `#1` which is substituted by each |
| 106 |
element of the `<list>`. The macro is expandable. |
| 107 |
\nl |
| 108 |
\`\foreach` `<list>`\`\do` `<parameter-mask>{<what>}` |
| 109 |
reads parameters from <list> repeatedly and does <what> for each such reading. |
| 110 |
The parameters are declared by <parameter-mask>. Examples: |
| 111 |
\begtt |
| 112 |
\foreach (a,1)(b,2)(c,3)\do (#1,#2){#1=#2 } |
| 113 |
\foreach word1,word2,word3,\do #1,{Word is #1.} |
| 114 |
\foreach A=word1 B=word2 \do #1=#2 {"#1 is set as #2".} |
| 115 |
\endtt |
| 116 |
Note that `\foreach <list>\do {<what>}` is equivalent to `\foreach <list>\do #1{<what>}`. |
| 117 |
|
| 118 |
Recommendation: it is better to use private variants of \`\_foreach`. |
| 119 |
When the user writes `\load[tikz]` then `\foreach` macro is redefined in |
| 120 |
each TikZ environment. |
| 121 |
The private variants use \`\_do` separator instead `\do` separator |
| 122 |
and it isn't redefined.} |
| 123 |
\nl |
| 124 |
\`\foreachx` `\list` behaves like `\ea \foreach \list` but |
| 125 |
the `\list` is expanded later, when its data are |
| 126 |
actually needed. This is more effective becuase the macro needn't skip |
| 127 |
a potentially huge amount of data in order to scan `\do{<what>}`. |
| 128 |
\_cod ----------------------------- |
| 129 |
|
| 130 |
\_newcount\_frnum % the numeric variable used in \fornum |
| 131 |
\_def\_do{\_doundefined} % we need to ask \_ifx#1\_do ... |
| 132 |
|
| 133 |
\_def\_foreach{\_foreachx\_empty} \_def\foreach{\_foreachAx\_empty} |
| 134 |
\_long\_def\_foreachx #1\_do #2#{\_foreachAz{#1}{#2}} |
| 135 |
\_long\_def\_foreachAx #1\do #2#{\_foreachAz{#1}{#2}} \_let\foreachx=\_foreachAx |
| 136 |
\_long\_def\_foreachAz #1#2{\_isempty{#2}\_iftrue |
| 137 |
\_afterfi{\_foreachA{#1}{##1}}\_else\_afterfi{\_foreachA{#1}{#2}}\_fi} |
| 138 |
\_long\_def\_foreachA #1#2#3{\_putforstack |
| 139 |
\_immediateassignment \_long\_gdef\_fbody#2{\_testparam##1..\_iftrue #3\_ea\_fbody\_fi}% |
| 140 |
\_ea\_fbody #1#2\_finbody\_getforstack |
| 141 |
} |
| 142 |
\_long\_def\_testparam#1#2#3\_iftrue{\_ifx###1\_empty\_ea\_finbody\_else} |
| 143 |
\_long\_def\_finbody#1\_finbody{} |
| 144 |
|
| 145 |
\_doc ----------------------------- |
| 146 |
{\_let\_moremainpoints=\_relax |
| 147 |
\`\fornum` `<from>..<to>` \`\do` `{<what>}` or |
| 148 |
\`\fornumstep` `<num>: <from>..<to>` \`\do` `{<what>}` |
| 149 |
repeats `<what>` for each number from `<from>` to `<to>` (with step `<num>` |
| 150 |
or with step one). The `<what>` can include `#1` which is substituted by |
| 151 |
current number. The <from>, <to>, <step> parameters can be numeric expressions. |
| 152 |
The macro is expandable.\nl |
| 153 |
The test in the \`\_fornumB` says: if (<to> $\string<$ <current number> |
| 154 |
AND <step> is positive) or if (<to> $>$ <current number> AND <step> is |
| 155 |
negative) then close loop by `\_getforstack`. Sorry, the condition is |
| 156 |
written by somewhat cryptoid \TeX/ language.} |
| 157 |
\_cod ----------------------------- |
| 158 |
|
| 159 |
\_def\_fornum#1..#2\_do{\_fornumstep 1:#1..#2\_do} |
| 160 |
\_long\_def\_fornumstep#1:#2..#3\_do#4{\_putforstack |
| 161 |
\_immediateassigned{% |
| 162 |
\_gdef\_fbody##1{#4}% |
| 163 |
\_global\_frnum=\_numexpr#2\_relax |
| 164 |
}% |
| 165 |
\_ea\_fornumB\_ea{\_the\_numexpr#3\_ea}\_ea{\_the\_numexpr#1}% |
| 166 |
} |
| 167 |
\_def\_fornumB #1#2{\_ifnum#1\_ifnum#2>0<\_else>\_fi \_frnum \_getforstack |
| 168 |
\_else \_afterfi{\_ea\_fbody\_ea{\_the\_frnum}% |
| 169 |
\_immediateassignment\_global\_advance\_frnum by#2 |
| 170 |
\_fornumB{#1}{#2}}\_fi |
| 171 |
} |
| 172 |
\_def\fornum#1..#2\do{\_fornumstep 1:#1..#2\_do} |
| 173 |
\_def\fornumstep#1:#2..#3\do{\_fornumstep #1:#2..#3\_do} |
| 174 |
|
| 175 |
\_doc ---------------------------- |
| 176 |
The `\foreach` and `\fornum` macros can be nested and arbitrary combined. |
| 177 |
When they are nested then use `##1` for the variable of nested level, |
| 178 |
`####1` for the variable of second nested level etc. Example: |
| 179 |
\begtt |
| 180 |
\foreach ABC \do {\fornum 1..5 \do {letter:#1, number: ##1. }} |
| 181 |
\endtt |
| 182 |
Implementation note: |
| 183 |
we cannot use \TeX/-groups for nesting levels because we want to do the |
| 184 |
macros expandable. We must implement a special for-stack which saves the |
| 185 |
data needed by `\foreach` and `\fornum`. The \`\_putforstack` is used |
| 186 |
when `\for*` is initialized and \`\_getforstack` is used when the |
| 187 |
`\for*` macro ends. The \`\_forlevel` variable keeps the current nesting |
| 188 |
level. If it is zero, then we need not save nor restore any data. |
| 189 |
\_cod ---------------------------- |
| 190 |
|
| 191 |
\_newcount\_forlevel |
| 192 |
\_def\_putforstack{\_immediateassigned{% |
| 193 |
\_ifnum\_forlevel>0 |
| 194 |
\_sxdef{_frnum:\_the\_forlevel\_ea}{\_the\_frnum}% |
| 195 |
\_global\_slet{_fbody:\_the\_forlevel}{_fbody}% |
| 196 |
\_fi |
| 197 |
\_incr\_forlevel |
| 198 |
}} |
| 199 |
\_def\_getforstack{\_immediateassigned{% |
| 200 |
\_decr\_forlevel |
| 201 |
\_ifnum\_forlevel>0 |
| 202 |
\_global\_slet{_fbody}{_fbody:\_the\_forlevel}% |
| 203 |
\_global\_frnum=\_cs{_frnum:\_the\_forlevel}\_space |
| 204 |
\_fi |
| 205 |
}} |
| 206 |
\_ifx\_immediateassignment\_undefined % for compatibility with older LuaTeX |
| 207 |
\_let\_immediateassigned=\_useit \_let\_immediateassignment=\_empty |
| 208 |
\_fi |
| 209 |
|
| 210 |
\_doc ---------------------------- |
| 211 |
User can define own expandable \"foreach" macro by |
| 212 |
\`\foreachdef` `\macro <parameter-mask>{<what>}` |
| 213 |
which can be used by `\macro {<list>}`. The macro reads repeatedly parameters from <list> |
| 214 |
using <parameter-mask> and does <what> for each such reading. For example |
| 215 |
\begtt |
| 216 |
\foreachdef\mymacro #1,{[#1]} |
| 217 |
\mymacro{a,b,cd,efg,} |
| 218 |
\endtt |
| 219 |
expands to [a][b][cd][efg]. Such user defined macros are more effective during processing |
| 220 |
than `\foreach` itself because they need not to operate with the for-stack. |
| 221 |
\_cod ---------------------------- |
| 222 |
|
| 223 |
\_def\_foreachdef#1#2#{\_toks0{#2}% |
| 224 |
\_long\_edef#1##1{\_ea\_noexpand\_csname _body:\_csstring#1\_endcsname |
| 225 |
##1\_the\_toks0 \_noexpand\_finbody}% |
| 226 |
\_foreachdefA#1{#2}} |
| 227 |
\_long\_def\_foreachdefA#1#2#3{% |
| 228 |
\_long\_sdef{_body:\_csstring#1}#2{\_testparam##1..\_iftrue #3\_cs{_body:\_csstring#1\_ea}\_fi}} |
| 229 |
|
| 230 |
\_public \foreachdef ; |
| 231 |
|
| 232 |
\_doc ---------------------------- |
| 233 |
\secc Is-macros and selection of cases |
| 234 |
There are a collection of macros |
| 235 |
\^`\isempty`, \^`\istoksempty`, \^`\isequal`, \^`\ismacro`, |
| 236 |
\^`\isdefined`, \^`\isinlist`, \^`\isfile` and \^`\isfont` |
| 237 |
with common syntax: |
| 238 |
\begtt \catcode`\<=13 |
| 239 |
\issomething <params> \iftrue <codeA> \else <codeB> \fi |
| 240 |
or |
| 241 |
\issomething <params> \iffalse <codeB> \else <codeA> \fi |
| 242 |
\endtt |
| 243 |
The `\else` part is optional. The `<codeA>` is processed if |
| 244 |
`\issomething<params>` generates true condition. The `<codeB>` |
| 245 |
is processed if `\issomething<params>` generates false condition. |
| 246 |
|
| 247 |
The `\iftrue` or `\iffalse` is an integral part of this syntax |
| 248 |
because we need to keep skippable nested `\if` conditions. |
| 249 |
|
| 250 |
Implementation note: |
| 251 |
we read this `\iftrue` or `\iffalse` into unseparated parameter and repeat |
| 252 |
it because we need to remove an optional space before this command. |
| 253 |
|
| 254 |
\medskip\noindent |
| 255 |
\`\isempty` `{<text>}\iftrue` |
| 256 |
is true if the `<text>` is empty. This macro is expandable.\nl |
| 257 |
\`\istoksempty` `<tokens variable>\iftrue` |
| 258 |
is true if the `<tokens variable>` is empty. It is expandable. |
| 259 |
\_cod ---------------------------- |
| 260 |
|
| 261 |
\_long\_def \_isempty #1#2{\_if\_relax\_detokenize{#1}\_relax \_else \_ea\_unless \_fi#2} |
| 262 |
\_def \_istoksempty #1#2{\_ea\_isempty\_ea{\_the#1}#2} |
| 263 |
\_public \isempty \istoksempty ; |
| 264 |
|
| 265 |
\_doc ---------------------------- |
| 266 |
\`\isequal` `{<textA>}{<textB>}\iftrue` |
| 267 |
is true if the <textA> and <textB> are |
| 268 |
equal, only from strings point of view, category codes are ignored. |
| 269 |
The macro is expandable. |
| 270 |
\_cod ---------------------------- |
| 271 |
|
| 272 |
\_long\_def\_isequal#1#2#3{\_directlua{% |
| 273 |
if "\_luaescapestring{\_detokenize{#1}}"=="\_luaescapestring{\_detokenize{#2}}" |
| 274 |
then else tex.print("\_nbb unless") end}#3} |
| 275 |
\_public \isequal ; |
| 276 |
|
| 277 |
\_doc ---------------------------- |
| 278 |
\`\ismacro` `\macro{text}\iftrue` is true if macro is defined as {<text>}. |
| 279 |
Category codes are ignored in this testing. The macro is expandable. |
| 280 |
\_cod ---------------------------- |
| 281 |
|
| 282 |
\_long\_def\_ismacro#1{\_ea\_isequal\_ea{#1}} |
| 283 |
\_public \ismacro ; |
| 284 |
|
| 285 |
\_doc ---------------------------- |
| 286 |
\`\isdefined` `{<csname>}\iftrue` is true if `\<csname>` is defined. |
| 287 |
The macro is expandable. |
| 288 |
\_cod ---------------------------- |
| 289 |
|
| 290 |
\_def\_isdefined #1#2{\_ifcsname #1\_endcsname \_else \_ea\_unless \_fi #2} |
| 291 |
\_public \isdefined ; |
| 292 |
|
| 293 |
\_doc ---------------------------- |
| 294 |
\`\isinlist` `\list{<text>}\iftrue` is true if the |
| 295 |
`<text>` is included the macro body of the `\list`. |
| 296 |
The category codes are relevant here. The macro is expandable. |
| 297 |
\_cod ---------------------------- |
| 298 |
|
| 299 |
\_long\_def\_isinlist#1#2#3{% |
| 300 |
\_immediateassignment\_long\_def\_isinlistA##1#2##2\_end/_% |
| 301 |
{\_if\_relax\_detokenize{##2}\_relax \_ea\_unless\_fi#3}% |
| 302 |
\_ea\_isinlistA#1\_endlistsep#2\_end/_% |
| 303 |
} |
| 304 |
\_public \isinlist ; |
| 305 |
|
| 306 |
\_doc ----------------------------- |
| 307 |
\`\isfile` `{<filename>}\iftrue` is true if the file <filename> exists and are |
| 308 |
readable by \TeX. |
| 309 |
\_cod ----------------------------- |
| 310 |
|
| 311 |
\_newread \_testin |
| 312 |
\_def\_isfile #1#2{% |
| 313 |
\_openin\_testin ={#1}\_relax |
| 314 |
\_ifeof\_testin \_ea\_unless |
| 315 |
\_else \_closein\_testin |
| 316 |
\_fi #2% |
| 317 |
} |
| 318 |
\_public \isfile ; |
| 319 |
|
| 320 |
\_doc ----------------------------- |
| 321 |
\`\isfont` `{<fontname or [fontfile]>}\iftrue` |
| 322 |
is true if a given font exists. The result of this testing |
| 323 |
is saved to the \`\_ifexistfam`. |
| 324 |
\_cod ----------------------------- |
| 325 |
|
| 326 |
\_newifi \_ifexistfam |
| 327 |
\_def\_isfont#1#2{% |
| 328 |
\_begingroup |
| 329 |
\_suppressfontnotfounderror=1 |
| 330 |
\_font\_testfont={#1}\_relax |
| 331 |
\_ifx\_testfont\_nullfont \_def\_tmp{\_existfamfalse \_unless} |
| 332 |
\_else \_def\_tmp{\_existfamtrue}\_fi |
| 333 |
\_ea \_endgroup \_tmp #2% |
| 334 |
} |
| 335 |
\_public \isfont ; |
| 336 |
|
| 337 |
\_doc ---------------------------- |
| 338 |
The macro \`\isnextchar` `<char>{<codeA>}{<codeB>}` |
| 339 |
has a different syntax than all other is-macros. |
| 340 |
It executes `<codeA>` if a next character is equal to <char>. |
| 341 |
Else the `<codeB>` is executed. The macro is expandable. |
| 342 |
It cheks the next character <space> too. If you want to check |
| 343 |
a next non-space character, use |
| 344 |
\^`\nospaceafter``{\isnextchar <char>{<codeA>}{<codeB>}}`. |
| 345 |
\_cod ---------------------------- |
| 346 |
|
| 347 |
\_long\_def\_isnextchar#1#2#3{\_immediateassignment |
| 348 |
\_def\_isnextcharA{\_isnextcharB{#1}{#2}{#3}}% |
| 349 |
\_immediateassignment\_futurelet \_next \_isnextcharA |
| 350 |
} |
| 351 |
\_long\_def\_isnextcharB#1{\_ifx\_next#1\_ea\_ignoresecond\_else\_ea\_usesecond\_fi} |
| 352 |
|
| 353 |
\_public \isnextchar ; |
| 354 |
|
| 355 |
\_doc ---------------------------- |
| 356 |
\`\casesof` `<token> <list of cases>` implements something similar to |
| 357 |
the `switch` command known from C language. It is expandable macro. The |
| 358 |
<list of cases> is a list of arbitrary number of pairs in the format |
| 359 |
`<token>`\,`{<what to do>}` which must be finalized by the pair |
| 360 |
`\_finc {<what to do else>}`. The optional spaces after <token>s |
| 361 |
and between listed cases are ignored. The usage of \^`\casesof` looks like: |
| 362 |
\begtt \catcode`<=13 |
| 363 |
\casesof <token> |
| 364 |
<token-1> {<what to do if token=token-1>} |
| 365 |
<token-2> {<what to do if token=token-2>} |
| 366 |
... |
| 367 |
<token-n> {<what to do if token=token-n>} |
| 368 |
\_finc {<what to do in other cases>} |
| 369 |
\endtt |
| 370 |
The meaning of tokens are compared by `\ifx` primitive. |
| 371 |
The parts `<what to do>` can be finalized by a macro which can read more |
| 372 |
data from the input stream as its parameters. |
| 373 |
\_cod ---------------------------- |
| 374 |
|
| 375 |
\_long\_def \_casesof #1#2#3{\_ifx #2\_finc \_ea\_ignoresecond \_else \_ea\_usesecond \_fi |
| 376 |
{#3}{\_ifx#1#2\_ea\_ignoresecond \_else \_ea\_usesecond \_fi {\_finc{#3}}{\_casesof#1}}% |
| 377 |
} |
| 378 |
\_long\_def \_finc #1#2\_finc#3{#1} |
| 379 |
|
| 380 |
\_public \casesof ; |
| 381 |
|
| 382 |
\_doc ----------------------------- |
| 383 |
\`\qcasesof` `{<string>} <list of cases>` behaves like \^`\casesof` but it |
| 384 |
compares phrases with the given <string> using \^`\isequal`. |
| 385 |
The <list of cases> includes pairs |
| 386 |
`{<phrases>} {<what to do if string=phrase>}` finalized by a |
| 387 |
pair `\_finc {<what to do else>}`. The `<phrases>` is a single |
| 388 |
phrase or phrases separated by `|` which means \"or". For example |
| 389 |
the pair `{ab|cde|f} {<code>}` runs <code> if the given <string> is `ab` or `cde` or~`f`. |
| 390 |
The usage of `\qcasesof` can be found in |
| 391 |
\ulink[http://petr.olsak.net/optex/optex-tricks.html\#thedimen]{OpTeX trick 0132}. |
| 392 |
\_cod ----------------------------- |
| 393 |
|
| 394 |
\_long\_def \_qcasesof #1#2#3{\_ifx\_finc#2\_ea\_ignoresecond \_else \_ea\_usesecond \_fi |
| 395 |
{#3}{\_qcasesofA{#1}#2|\_qcasesofA|{\_finc{#3}}{\_qcasesof{#1}}}% |
| 396 |
} |
| 397 |
\_long\_def\_qcasesofA#1#2|{\_ifx\_qcasesofA#2\_ea\_usesecond \_else |
| 398 |
\_isequal{#1}{#2}\_iftrue \_qcasesofB \_fi \_afterfi{\_qcasesofA{#1}}\_fi |
| 399 |
} |
| 400 |
\_long\_def\_qcasesofB #1\_qcasesofA|#2#3{\_fi\_fi#2} |
| 401 |
|
| 402 |
\_public \qcasesof ; |
| 403 |
|
| 404 |
\_doc ----------------------------- |
| 405 |
\`\xcasesof` `<list of pairs>` extends the features of the macro \^`\casesof`. |
| 406 |
Each pair from the `<list of pairs>` is in the format |
| 407 |
`{<if statement>}{<what to do>}`, only the last pair must have the different |
| 408 |
format: `\_finc {<what to do else>}`. The `<if statement>` can be arbitrary |
| 409 |
primitive `\if*` condition (optionally prefixed by `\unless`) and it must be closed |
| 410 |
in its expansion. It means that `{\ifnum\mycount>0}` is bad, `{\ifnum\mycount>0 }` |
| 411 |
is correct. Optional spaces between parameters are ignored. Example: |
| 412 |
\begtt |
| 413 |
\message {The \tmpnum has \xcasesof |
| 414 |
{\ifnum\tmpnum>0 } {positive} |
| 415 |
{\ifnum\tmpnum=0 } {zero} |
| 416 |
\_finc {negative} value} |
| 417 |
\endtt |
| 418 |
The \^`\xcasesof` macro works with principle: first true condition wins, |
| 419 |
next conditions are not evaluated. |
| 420 |
\_cod ----------------------------- |
| 421 |
|
| 422 |
\_def \_xcasesof {\_nospacefuturelet\_next\_xcasesofA} |
| 423 |
\_def \_xcasesofA {\_ifx\_next\_finc \_ea\_usesecond \_else \_ea \_xcasesofB \_fi} |
| 424 |
\_long\_def \_xcasesofB #1#2{% |
| 425 |
#1\_ea\_ignoresecond\_else \_ea\_usesecond\_fi {\_finc{#2}}{\_xcasesof}% |
| 426 |
} |
| 427 |
\_public \xcasesof ; |
| 428 |
|
| 429 |
\_endcode |
| 430 |
|
| 431 |
2025-03-01 \foreachx introduced. |
| 432 |
2024-11-24 \foreach bug fixed: \foreach {xx}\do {...} |
| 433 |
2024-11-12 \isinlist, \isfile allow space before \iftrue/\iffalse |
| 434 |
2024-11-10 \xloop implemented. |
| 435 |
2024-09-10 \afterxfi implemented. |
| 436 |
2024-02-19 \qcasesof reimplemented, it supports | for more phrases. |
| 437 |
2023-12-07 \_testparam define \long (big fixed). |
| 438 |
2023-10-17 \qcasesof introduced, \xcasesof reimplemented. |
| 439 |
2023-01-16 \isnextchar created expandable. |
| 440 |
2022-12-02 \xcasesof: its first parameter is \long too. |
| 441 |
2022-11-29 renamed to \casesof, \xcasesof. |
| 442 |
2022-11-26 \casesby, \casesbyif introduced. |
| 443 |
2022-05-04 \isinlist created expandable. |
| 444 |
2022-03-30 \afterfi defined as \long. |
| 445 |
2021-08-02 more robust \fornum: \fi moved by \afterfi |
| 446 |
2021-02-03 public version of \loop and \foreach are \long |
| 447 |
2020-05-22 \foreach, \fornum: all settings are global, independent on TeX group |
| 448 |
2020-05-06 \isnextchar: \let\tmp=#1 -> \let\tmp= #1 (bug fix, #1 should be space) |
| 449 |
2020-05-02 \newif bug fix |
| 450 |
2020-04-15 \fornumstep 3: 1..12 instead \fornum 1..12\step 3 |
| 451 |
2020-04-15 \fornum, \foreach can be nested without groups |
| 452 |
2020-04-01 implemented |