diff options
Diffstat (limited to 'doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex')
-rw-r--r-- | doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex | 212 |
1 files changed, 100 insertions, 112 deletions
diff --git a/doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex b/doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex index dfff4ac72..288521bdd 100644 --- a/doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex +++ b/doc/context/sources/general/manuals/lowlevel/lowlevel-conditionals.tex @@ -8,9 +8,9 @@ \pushoverloadmode -\startsection[title=Preamble] +\startsectionlevel[title=Preamble] -\startsubsection[title=Introduction] +\startsectionlevel[title=Introduction] You seldom need the low level conditionals because there are quite some so called support macros available in \CONTEXT . For instance, when you want to compare two @@ -53,9 +53,9 @@ The others are often wrapped into support macros that are more convenient. In due time I might add more examples and explanations. Also, maybe some more tests will show up as part of the \LUAMETATEX\ project. -\stopsubsection +\stopsectionlevel -\startsubsection[title={Number and dimensions}] +\startsectionlevel[title={Number and dimensions}] Numbers and dimensions are basic data types in \TEX. When you enter one, a number is just that but a dimension gets a unit. Compare: @@ -289,13 +289,13 @@ assuming that the fraction is within the maximum permitted) so these numbers the are the same. Anyway, this is not different in other programming languages and just something you need to be aware of. -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title={\TEX\ primitives}] +\startsectionlevel[title={\TEX\ primitives}] -\startsubsection[title={\tex{if}}] +\startsectionlevel[title={\tex{if}}] I seldom use this one. Internally \TEX\ stores (and thinks) in terms of tokens. If you see for instance \type {\def} or \type {\dimen} or \type {\hbox} these all @@ -330,9 +330,9 @@ We get: \inlinebuffer . % protected macros -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcat}}] +\startsectionlevel[title={\tex{ifcat}}] In \TEX\ characters (in the input) get interpreted according to their so called catcodes. The most common are letters (alphabetic) and and other (symbols) but @@ -377,9 +377,9 @@ You can use \type {\noexpand} to prevent expansion: We get: \inlinebuffer, so who still thinks that \TEX\ is easy to understand for a novice user? -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifnum}}] +\startsectionlevel[title={\tex{ifnum}}] This condition compares its argument with another one, separated by an \type {<}, \type {=} or \type {>} character. @@ -410,9 +410,9 @@ case the dimension is in scaled points. Of course this equal treatment of a dimension and number is only true when the dimension is a register or box property. -\stopsubsection +\stopsectionlevel -\startsection[title={\tex{ifdim}}] +\startsectionlevel[title={\tex{ifdim}}] This condition compares one dimension with another one, separated by an \type {<}, \type {=} or \type {>} sign. @@ -430,9 +430,9 @@ This condition compares one dimension with another one, separated by an \type {< While when comparing numbers a dimension is a valid quantity but here you cannot mix them: something with a unit is expected. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifodd}}] +\startsectionlevel[title={\tex{ifodd}}] This one can come in handy, although in \CONTEXT\ it is only used in checking for an odd of even page number. @@ -454,9 +454,9 @@ too, which is then interpreted as representing scaled points. Here we get: \getbuffer \stoplines -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifvmode}}] +\startsectionlevel[title={\tex{ifvmode}}] This is a rather trivial check. It takes no arguments and just is true when we're in vertical mode. Here is an example: @@ -470,9 +470,9 @@ in vertical mode. Here is an example: We're always in horizontal mode and issuing a \type {\par} inside a horizontal box doesn't change that, so we get: \ruledhbox{\inlinebuffer}. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifhmode}}] +\startsectionlevel[title={\tex{ifhmode}}] As with \type {\ifvmode} this one has no argument and just tells if we're in vertical mode. @@ -494,9 +494,9 @@ content (or command) is done more than once: \ruledhbox{\inlinebuffer} \stoplinecorrection -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifmmode}}] +\startsectionlevel[title={\tex{ifmmode}}] Math is something very \TEX\ so naturally you can check if you're in math mode. here is an example of using this test: @@ -507,9 +507,9 @@ here is an example of using this test: Of course in reality macros that do such things are more advanced than this one. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifinner}}] +\startsectionlevel[title={\tex{ifinner}}] \startbuffer \def\ShowMode @@ -549,9 +549,9 @@ By the way, moving the \type {\ifinner} test outside the branches (to the top of the macro) won't work because once the word \type {inner} is typeset we're no longer in vertical mode, if we were at all. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifvoid}}] +\startsectionlevel[title={\tex{ifvoid}}] A box is one of the basic concepts in \TEX. In order to understand this primitive we present four cases: @@ -601,15 +601,15 @@ Setting a dimension of a void voix (empty) box doesn't make it less void: \getbuffer \stoplines -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifhbox}}] +\startsectionlevel[title={\tex{ifhbox}}] This test takes a box number and gives true when it is an hbox. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifvbox}}] +\startsectionlevel[title={\tex{ifvbox}}] This test takes a box number and gives true when it is an vbox. Both a \type {\vbox} and \type {\vtop} are vboxes, the difference is in the height and depth @@ -627,9 +627,9 @@ And in a \type {\vtop} the first line takes control: but, once wrapped, both internally are just vlists. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifx}}] +\startsectionlevel[title={\tex{ifx}}] This test is actually used a lot in \CONTEXT: it compares two token(list)s: @@ -651,29 +651,29 @@ that get compared, like in: \edef\TempA{...}\edef\TempB{...}\ifx\TempA\TempB ...\else ...\fi \stoptyping -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifeof}}] +\startsectionlevel[title={\tex{ifeof}}] This test checks if a the pointer in a given input channel has reached its end. It is also true when the file is not present. The argument is a number which relates to the \type {\openin} primitive that is used to open files for reading. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{iftrue}}] +\startsectionlevel[title={\tex{iftrue}}] It does what it says: always true. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{iffalse}}] +\startsectionlevel[title={\tex{iffalse}}] It does what it says: always false. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcase}}] +\startsectionlevel[title={\tex{ifcase}}] The general layout of an \type {\ifcase} tests is as follows: @@ -693,13 +693,13 @@ The general layout of an \type {\ifcase} tests is as follows: As in other places a number is a sequence of signs followed by one of more digits -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title={\ETEX\ primitives}] +\startsectionlevel[title={\ETEX\ primitives}] -\startsubsection[title={\tex{ifdefined}}] +\startsectionlevel[title={\tex{ifdefined}}] This primitive was introduced for checking the existence of a macro (or primitive) and with good reason. Say that you want to know if \type {\MyMacro} is defined? One @@ -736,9 +736,9 @@ In order to catch the last problem there is the option to test directly: This (or course) results in: \inlinebuffer, but the macro is still sort of defined (with no meaning). The next section shows how to get around this. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcsname}}] +\startsectionlevel[title={\tex{ifcsname}}] A macro is often defined using a ready made name, as in: @@ -795,9 +795,9 @@ during this test, and in \LUAMETATEX\ that is default. This means that tests can be made quite robust as it is pretty safe to assume that names that make sense are constructed from regular characters and not boxes, font switches, etc. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{iffontchar}}] +\startsectionlevel[title={\tex{iffontchar}}] This test was also part of the \ETEX\ extensions and it can be used to see if a font has a character. @@ -824,9 +824,9 @@ In the perspective of \LUAMETATEX\ I considered also supporting \type {\fontid} but it got a bit messy due to the fact that this primitive expands in a different way so this extension was rejected. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{unless}}] +\startsectionlevel[title={\tex{unless}}] You can negate the results of a test by using the \type {\unless} prefix, so for instance you can replace: @@ -847,41 +847,41 @@ by: \fi \stoptyping -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title={\LUATEX\ primitives}] +\startsectionlevel[title={\LUATEX\ primitives}] -\startsubsection[title={\tex{ifincsname}}] +\startsectionlevel[title={\tex{ifincsname}}] As it had no real practical usage uit might get dropped in \LUAMETATEX, so it will not be discussed here. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifprimitive}}] +\startsectionlevel[title={\tex{ifprimitive}}] As it had no real practical usage due to limitations, this one is not available in \LUAMETATEX\ so it will not be discussed here. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifabsnum}}] +\startsectionlevel[title={\tex{ifabsnum}}] This test is inherited from \PDFTEX\ and behaves like \type {\ifnum} but first turns a negative number into a positive one. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifabsdim}}] +\startsectionlevel[title={\tex{ifabsdim}}] This test is inherited from \PDFTEX\ and behaves like \type {\ifdim} but first turns a negative dimension into a positive one. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcondition}}] +\startsectionlevel[title={\tex{ifcondition}}] This is not really a test but in order to unstand that you need to know how \TEX\ internally deals with tests. @@ -1061,13 +1061,13 @@ permits more complex arguments, like: Another trick is that we use an integer division (the \type {:}) which is an operator supported by \LUAMETATEX . -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title={\LUAMETATEX\ primitives}] +\startsectionlevel[title={\LUAMETATEX\ primitives}] -\startsubsection[title={\tex{ifcmpnum}}] +\startsectionlevel[title={\tex{ifcmpnum}}] This one is part of s set of three tests that all are a variant of a \type {\ifcase} test. A simple example of the first test is this: @@ -1080,9 +1080,9 @@ The test scans for two numbers, which of course can be registers or expressions, and sets the case value to 0, 1 or 2, which means that you then use the normal \type {\or} and \type {\else} primitives for follow up on the test. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifchknum}}] +\startsectionlevel[title={\tex{ifchknum}}] This test scans a number and when it's okay sets the case value to 1, and otherwise to 2. So you can do the next: @@ -1097,9 +1097,9 @@ recovery token, although in fact we just use the fast scanner mode that comes with the \type {\ifcase}: because the result is 1 or 2, we never see invalid tokens. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifnumval}}] +\startsectionlevel[title={\tex{ifnumval}}] A sort of combination of the previous two is \type {\ifnumval} which checks a number but also if it's less, equal or more than zero: @@ -1113,29 +1113,29 @@ You can decide to ignore the bad number or do something that makes more sense. Often the to be checked value will be the content of a macro or an argument like \type {#1}. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcmpdim}}] +\startsectionlevel[title={\tex{ifcmpdim}}] This test is like \type {\ifcmpnum} but for dimensions. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifchkdim}}] +\startsectionlevel[title={\tex{ifchkdim}}] This test is like \type {\ifchknum} but for dimensions. The last checked value is available as \type {\lastchknum}. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifdimval}}] +\startsectionlevel[title={\tex{ifdimval}}] This test is like \type {\ifnumval} but for dimensions. The last checked value is available as \type {\lastchkdim} -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{iftok}}] +\startsectionlevel[title={\tex{iftok}}] Although this test is still experimental it can be used. What happens is that two to be compared \quote {things} get scanned for. For each we first gobble @@ -1195,49 +1195,49 @@ Case one and four mixed: The last case is more a catch: it will issue an error when no number is given. Eventually that might become a bit more clever (depending on our needs.) -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifcstok}}] +\startsectionlevel[title={\tex{ifcstok}}] There is a subtle difference between this one and \type {iftok}: spaces and \type {\relax} tokens are skipped but nothing gets expanded. So, when we arrive at the to be compared \quote {things} we look at what is there, as|-|is. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{iffrozen}}] +\startsectionlevel[title={\tex{iffrozen}}] {\em This is an experimental test.} Commands can be defined with the \type {\frozen} prefix and this test can be used to check if that has been the case. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifprotected}}] +\startsectionlevel[title={\tex{ifprotected}}] Commands can be defined with the \type {\protected} prefix (or in \CONTEXT, for historic reasons, with \type {\unexpanded}) and this test can be used to check if that has been the case. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifusercmd}}] +\startsectionlevel[title={\tex{ifusercmd}}] {\em This is an experimental test.} It can be used to see if the command is defined at the user level or is a build in one. This one might evolve. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{ifarguments}}] +\startsectionlevel[title={\tex{ifarguments}}] This conditional can be used to check how many arguments were matched. It only makes sense when used with macros defined with the \type {\tolerant} prefix and|/|or when the sentinel \type {\ignorearguments} after the arguments is used. More details can be found in the lowlevel macros manual. -\stopsubsection +\stopsectionlevel -\startsubsection[title={\tex{orelse}}] +\startsectionlevel[title={\tex{orelse}}] This it not really a test primitive but it does act that way. Say that we have this: @@ -1297,13 +1297,13 @@ This permits: where, of course, the quitting normally is the result of some intermediate extra test. But let me play safe here: beware of side effects. -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title={For the brave}] +\startsectionlevel[title={For the brave}] -\startsubsection[title={Full expansion}] +\startsectionlevel[title={Full expansion}] If you don't understand the following code, don't worry. There is seldom much reason to go this complex but obscure \TEX\ code attracts some users so \unknown @@ -1361,9 +1361,9 @@ different: they contain the assignments and the test for the character is actually done when constructing the content of the \type {\edef}, but for the current font. So, basically that test is now useless. -\stopsubsection +\stopsectionlevel -\startsubsection[title={User defined if's}] +\startsectionlevel[title={User defined if's}] There is a \type {\newif} macro that defines three other macros: @@ -1411,11 +1411,11 @@ This one is cheaper on the hash and doesn't need the two extra macros per test. The price is the use of \type {\ifconditional}, which is {\em not} to confused with \type {\ifcondition} (it has bitten me already a few times). -\stopsubsection +\stopsectionlevel -\stopsection +\stopsectionlevel -\startsection[title=Relaxing] +\startsectionlevel[title=Relaxing] When \TEX\ scans for a number or dimension it has to check tokens one by one. On the case of a number, the scanning stops when there is no digit, in the case of a @@ -1532,19 +1532,7 @@ it acts as relax, but otherwise it just is ignored and disappears. \typebuffer[c] \getbuffer[c,b] -\stopsection - -\startsubject[title=Colofon] - -\starttabulate -\NC Author \NC Hans Hagen \NC \NR -\NC \CONTEXT \NC \contextversion \NC \NR -\NC \LUAMETATEX \NC \texengineversion \NC \NR -\NC Support \NC www.pragma-ade.com \NC \NR -\NC \NC contextgarden.net \NC \NR -\stoptabulate - -\stopsubject +\stopsectionlevel \popoverloadmode |