%% This is part of OpTeX project, see http://petr.olsak.net/optex \_codedecl \newif {Special if-macros, is-macros and loops <2020-02-27>} % preloaded in format \_doc ---------------------------- The `\newif` macro works like in plain \TeX. It means that after `\newif\ifxxx` you can use `\xxxtrue` or `\xxxfalse` to set the boolean value and use `\ifxxx true\else false\fi` to test this value. The defalut value is false. The macro `\_newifi` enables to declare `\_ifxxx` and to use `\_xxxtrue` and `\_xxxfalse`. This means that it is usable for _prefixed macros. \_cod ---------------------------- \_def\_newif #1{\_ea\_newifA \_string #1\_relax#1} \_ea\_def \_ea\_newifA \_string\if #1\_relax#2{% \_sdef{#1true}{\_let#2=\_iftrue}% \_sdef{#1false}{\_let#2=\_iffalse}% \_let#2=\_iffalse } \_def\_newifi #1{\_ea\_newifiA \string#1\_relax#1} \_ea\_def \_ea\_newifiA \string\_if #1\_relax#2{% \_sdef{_#1true}{\_let#2=\_iftrue}% \_sdef{_#1false}{\_let#2=\_iffalse}% \_let#2=\_iffalse } \_doc ---------------------------- The `\loop \ifsomething \repeat` loops ` ` until `\ifsomething` is false. Then `` is not executed and loop is finished. This works like in plain \TeX, but implementation is soewhat better (you can use `\else` clause after the `\ifsomething`). There are public version `\loop...\repeat` and private version `\_loop...\_repeat`. You canot mix both versions in one loop. \_cod ---------------------------- \_def \_loop #1\_repeat{\_def\_body{#1}\_iterate} \_def \loop #1\repeat{\_def\_body{#1}\_iterate} \_let \_repeat=\_fi % this makes \loop...\if...\repeat skippable \_let \repeat=\_fi \_def \_iterate {\_body \_ea \_iterate \_fi} \_doc ---------------------------- The macro `\isnextchar {}{}` executes `` if next character is equal to . Else the `` is executed. The macro is not expandable. \_cod ---------------------------- \_long\_def\_isnextchar#1#2#3{\_begingroup\_toks0={\_endgroup#2}\_toks1={\_endgroup#3}% \_let\_tmp=#1\_futurelet\_next\_isnextcharA } \_def\_isnextcharA{\_the\_toks\_ifx\_tmp\_next0\_else1\_fi\_space} \_doc ---------------------------- The macro `\isempty{}\iftrue \else \fi` executes if is empty and if it is non-empty. You can use `\isempty{}\iffalse \else \fi` too. This macro is expandable. There are a collection of macros with commnon syntax: `\issomething\iftrue` or `\issomething\iffalse`. The `\iftrue` or `\iffalse` is a part of this syntax because we need to keep skippable nested `\if` conditions. We read this `\iftrue` or `\iffalse` into unseparated parameter and repeat it because we need to remove an optional space before this command. \_cod ---------------------------- \_def \_isempty #1#2{\_ea\_ifx\_ea\_relax\_detokenize{#1}\_relax \_else \_ea\_unless \_fi #2} \_def \_isnoempty #1#2{\_ea\_ifx\_ea\_relax\_detokenize{#1}\_relax \_ea\_unless \_fi #2} \_def \_istoksempty #1{\_ea\_isempty\_ea{\_the#1}} \_doc ---------------------------- `\isequal{}{}\iftrue` is true if the and are equal, only from strings point of view, category codes are ignored. The macro is expandable. \_cod ---------------------------- \_def\_isequal#1#2#3{\_directlua{% if "\_luaescapestring{\_detokenize{#1}}"=="\_luaescapestring{\_detokenize{#2}}" then else tex.print("\_nbb unless") end}#3} \_doc ---------------------------- `\ismacro\macro{text}\iftrue` is true if macro is defined as {}. Category codes are ignored in this testing. The macro is expandable. \_cod ---------------------------- \_def\_ismacro#1{\_ea\_isequal\_ea{#1}} \_doc ---------------------------- `\isinlist\list{}\iftrue` is true if the `` is included the macro body of the `\list`. The catogory code are relevant here. THe macro is not expandable. \_cod ---------------------------- \_def\_isdefined #1#2{\_ifcsname #1\_endcsname \_else \_ea\_unless \_fi #2} \_doc ---------------------------- `\isdefined{}\iftrue` is true if `\` is defined. \_cod ---------------------------- \_long\_def\_isinlist#1#2{\_begingroup \_long\_def\_tmp##1#2##2\_end/_{\_endgroup\_isnoempty{##2}}% \_ea\_tmp#1\_endlistsep#2\_end/_% } \_doc ---------------------------- `\isfile{}\iftrue` is true if the file exists and are readable by \TeX. \_cod \_fin ---------------------- \_newread \_testin \_def\_isfile #1{% \_openin\_testin ={#1}\_relax \_ifeof\_testin \_ea\_unless \_else \_closein\_testin \_fi } \_public \newif \isnextchar \isempty \isequal \ismacro \isdefined \isinlist \isfile ; \_endcode