![]() |
Control flow | ![]() |
Besides sequences of instructions, which can be achieved using the concat primitive, and the mechanism of macro expansion, the TeXmacs style-sheet language provides a few other primitives for affecting the control flow: if, case, while and for-each. These primitives are available from the Source\(\rightarrow\)Control flow menu. However, we have to warn the user that the conditional constructs are quite fragile: they only apply to inline content and the accessibility of macro arguments should not too much depend on the conditions.
The most important primitive if, which can be entered using ⌥⌃?, allows for basic conditional typesetting:
<assign|appendix| <macro|title|body|<document|<\compound| <if|<long-document>|chapter-appendix|section-appendix> | title | body > >>> |
In this example, appendix is a block environment consisting of a title and a body, and which is rendered as a chapter for long documents and as a section for short ones. (This is a simplified example: the predicate long-document and the macros chapter-appendix and section-appendix are not part of the standard styles, whose actual implementation of appendices in section-base relies on a similar test on sectional-short-style.) Notice that the following implementation would have been incorrect, since the if primitive currently only works for inline content:
<assign|appendix| <macro|title|body|<document|<\if| <long-document> | <chapter-appendix|title|body> | <section-appendix|title|body> > >>> |
The if primitive may also be used in order to implement optional arguments:
<assign|hey| <macro|first|second|<\if| <equal|second|?> | Hey first, you look lonely today… | Hey first and second, you form a nice couple! > >> |
However, TeXmacs is not clever enough to detect which arguments are optional and which arguments are accessible (i.e. which arguments can be edited by the user). Therefore, you will have to manually give this information using the drd-props primitive. The case, while and for-each primitives are explained in more detail in the corresponding section on the TeXmacs primitives.