Home | Download | Help | Contribute | Plug-ins | About | Contact | Search
The style-sheet language
Manual | Top | Previous | Next

1.Environment primitives

The current environment both defines all style parameters which affect the typesetting process and all additional macros provided by the user and the current style. The primitives in this section are used to access and modify environment variables.

<assign|var|val>
(variable mutation)

This primitive sets the environment variable named var (string value) to the value of the val expression. This primitive is used to make non-scoped changes to the environment, like defining markup or increasing counters.

This primitive affects the evaluation process through value, provides, and macro definitions and the typesetting process through special typesetter variables.

Example 1. Enabling page breaking by style.

The page-medium is used to enable page breaking. Since only the initial environment value for this variable is effective, this assignation must occur in a style file, not within a document.

<assign|page-medium|paper>

Example 2. Setting the chapter counter.

The following snippet will cause the immediately following chapter to be number 3. This is useful to get the the numbering right in book style when working with projects and include.

<assign|chapter-nr|2>

The operand must be a literal string and is interpreted as a file name. The content of this file is typeset in place of the include tag, which must be placed in block context.

<with|var-1|val-1||var-n|val-n|body>
(variable scope)

This primitive temporarily sets the environment variables var-1 until var-n (in this order) to the evaluated values of val-1 until val-n and typesets body in this modified environment. All non-scoped change done with assign to var-1 until var-n within body are reverted at the end of the with.

This primitive is used extensively in style files to modify the typesetter environment. For example to locally set the text font, the paragraph style, or the mode for mathematics.

<value|var>
(variable value)

This primitive evaluates the current value of the environment variable var (literal string). This is useful to display counters and generally to implement environment-sensitive behavior.

This primitive is used extensively in style files to modify the typesetter environment. For example to locally set the text font, the paragraph style, or the mode for mathematics.

<provides|var>
(definition predicate)

This predicate evaluates to true if the environment variable var (string value) is defined, and to false otherwise.

That is useful for modular markup, like the session environments, to fall back to a default appearance when a required package is not used in the document.

2.Macro primitives

Macros can be used to define new tags and to build procedural abstractions in style files.

Older versions of TeXmacs used to make a distinction between macros (all children accessible) and functions (no accessible child). In modern TeXmacs there are only macros: the accessibility of children is determined heuristically and can be controlled with drd-props.

<macro|var-1||var-n|body>
(macro of fixed arity)

This primitive returns a macro (the TeXmacs analogue of a λ-expression) with n arguments, named after the literal strings var-1 until var-n.

New tags are defined by storing macros in the environment. Most of the time, macros are stored without scope with assign, but it is sometimes useful to redefine a tag locally within the scope of a with. For example, itemized and enumerated environment redefine item locally.

Example 3. Definition of the abbr tag

<assign|abbr|<macro|x|<group|x>>>

Storing a macro in the environment defines a tag whose arity is fixed to the number of arguments taken by the macro.

<arg|var|index-1||index-n>
(retrieve macro arguments)

This primitive is used to retrieve the arguments of a macro within its body. For instance, <arg|var> expands the content of the macro argument with name arg (literal string). Of course, this argument must be defined by a macro containing the arg tag.

This tag is similar to value, but differs in important ways:

When more than one arguments are specified, <arg|var|index-1||index-n> expands to a subtree of the argument var. The value of the named argument must be a compound tree (not a string). The operands var until index-n must all evaluate to positive integers and give the path to the subtree of the macro argument.

<xmacro|var|body>
(macro with a variable arity)

This primitive returns a macro (the TeXmacs analogue of a λ-expression) capable of taking any number of arguments. The arguments are stored in the macro variable with name var (a literal string) during the evaluation of the body. The i-th individual argument can then be accessed using <arg|var|i>.

<map-args|foo|root|var>

<map-args|foo|root|var|first>

<map-args|foo|root|var|first|last>
(map a tag on subtrees of an argument)

This primitive evaluates to a tree whose root is labeled by root and whose children are the result of applying the macro foo to the children of the macro argument with name var.

By default, the macro foo is applied to all children. If first has been specified, then we rather start at the i-th child of var, where i is the result of evaluating first. If last has been specified to, then we stop at the j-th child of var (the j-th child not being included), where j is the result of evaluating last. In this last case, the arity of the returned tree is therefore j - i.

Stated otherwise, map-args applies foo to all subtrees of the macro argument var (or a range of subtrees if first and last are specified) and collect the result in a tree with label root. In addition, the second argument to foo gives its position of the first argument in the expansion of var.

The map-args is analogue to the Scheme function map. Since TeXmacs use labeled trees, the label of the mapping list must also be specified.

Example 4. Comma-separated lists.

The comma-separated tag has any arity (though it does not make much sense with arity zero) and typeset its operands interspersed with commas.

<assign|comma-extra|<macro|x|, x>>

< assign | comma-separated | < xmacro | args |

<\concat|

<arg|args|0>

|

<map-args|comma-extra|concat|args|1>

>

> >

<eval-args|var>
(macro with a variable arity)

This primitive evaluates to the tree with the same label as the expansion of the argument var and whose subtrees are the result of the evaluation of the subtrees of the expansion of var.

<compound|foo|arg-1||arg-n>
(expand an unnamed macro)

This primitive is useful to expand macros which are the result of a computation: it applies the macro which is the result of the evaluation of foo to the arguments arg-1 until arg-n. The compound primitive is useful in call-back and lambda programming idioms, where a higher-level macro is given a macro as an operand, which it may later apply under certain conditions or with operands which are not known the client code.

Actually, in the current implementation, foo may either evaluate to a macro or to a literal string which gives the name of a macro. However, we discourage users to rely on the second case.

Example 5. Lambda programming with macros.

In the code below, <filter|pred|t> expects a macro pred and a tuple t on input and returns a tuple containing the elements of t for which pred evaluates to true.

< assign | filter | < macro | pred | t |

<\if|

<equal|<length|t>|0>

|

<tuple>

|

<\merge|

<\if|

<compound|pred|<look-up|t|0>>

|

<tuple|<look-up|t|0>>

|

<tuple>

>

|

<filter|pred|<range|t|1|<length|t>>>

>

>

> >

As an application, we may define a macro <evens|t>, which expects t to be a tuple containing integers, and which returns the tuple of integers in t which are divisible by 2.

<assign|evens|<macro|t|<filter|<macro|x|<equal|<mod|x|2>|0>>|t>>>

<drd-props|var|prop-1|val-1||prop-n|val-n>
(set D.R.D. properties of a tag)

The arity and children accessibility of tags defined by macros are determined heuristically by default. The drd-props primitive overrides this default for the environment variable (usually a macro) with name var. The currently supported property-value pairs are:

(arity, n)
Sets the arity to the given fixed value n (literal integer).
(accessible, all)
Make it impossible to deactivate the tag with normal editor actions. Inaccessible children become effectively uneditable.
(accessible, none)
Make it impossible to position the caret within the tag when it is active, so children can only be edited when the tag is inactive.

<get-label|expression>
(label of an expression)

Returns the label of the tree obtained when evaluating expression.

<get-arity|expression>
(arity of an expression)

Returns the label of the tree obtained when evaluating expression.

3.Flow control primitives

<if|condition|if-body>

<if|condition|if-body|else-body>
(conditional markup)

This primitive can be used to typeset if-body only if the condition is satisfied. If the optional else-body is specified, then it is typeset if and only if the condition fails.

Remark 6. It should be noticed that the use of conditional markup can be a bit tricky due to the fact that the accessability of arguments cannot necessarily be checked beforehand. For instance, in the macro definition

<macro|x|<if|<visibility-flag>|x>>

the macro argument x is accessible if and only if <visibility-flag> evaluates to true. This condition cannot necessarily be checked a priori. For certain editing operations, like searches or spell checking, the incorrect determination of the accessability may lead to the positioning of the cursor at unaccessible places, or to the ignorance of certain markup. In the future, we plan to improve this aspect of the editor, but it is better to avoid conditional markup whenever another solution can be found.

Remark 7. The conditional constructs are only fully implemented for inline markup. In the case when you need conditional markup for block structures you currently have to write macros for the if-case and the else-case and use the compound tag. For instance:

<assign|cold|<macro|x|<with|color|blue|x>>>

<assign|hot|<macro|x|<with|color|red|x>>>

<assign|adaptive|<macro|x|<compound|<if|<summer>|hot|cold>|x>>>

<case|cond-1|body-1||cond-n|body-n>

<case|cond-1|body-1||cond-n|body-n|else-body>
(case distinction)

These commands are respectively equivalent to

<if|cond-1|body-1|<if|cond-n|body-n>>

<if|cond-1|body-1|<if|cond-n|body-n|else-body>>

<while|condition|body>
(repeated evaluation)

This construct maybe used in order to repeatly execute a given body while a given condition is satisfied. For instance, when declaring

< assign | count | < macro | from | to | < with | i | from |

<\concat|

<while|<less|i|to>|i, <assign|i|<plus|i|1>>>

|

to

>

> > >

the code <count|1|50> produces

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

4.Evaluation control primitives

This section describes several primitives for controlling the way expressions in the style-sheet language are evaluated. The primitives are analoguous to the Scheme primitives eval, quote, quasiquote, etc., although the TeXmacs conventions are slightly different than those used by conventional functional languages like Scheme.

<eval|expr>
(force evaluation)

Typeset the result of the evaluation of expr. This primitive is usually combined with a tag like quote or quasiquote for delaying the evaluation.

<quote|expr>
(delayed evaluation)

Evaluation of the expression <quote|expr> yields expr itself. This kind of delayed evaluation may be useful in combination with the eval primitive which forces evaluation.

<quasiquote|expr>
(delay evaluation and substitution)

This tag is a variant of the quote tag, which returns the expression expr in which all subexpressions of the form <unquote|subexpr> have been replaced by the evaluations of subexpr. For instance,

<assign|hello|<quasiquote|<macro|name|<unquote|<localize|Hello>> name.>>>

may be used to define a macro hello whose value is localized for the current language. In a French document, the declaration would typically be equivalent to

<assign|hello|<macro|name|Bonjour name.>>

Notice however that it is usually better not to use the quasiquote primitive for such applications. When defining

<assign|hello|<macro|name|<localize|Hello> name.>>

the typesetting of <hello|Name> would naturally adapt itself to the current language, while the above version would always use the language at the moment of the definition of the macro. Nevertheless, the first form does have the advantage that the localization of the word “Hello” only has to be computed once, when the macro is defined. Therefore, the quasiquote primitive may sometimes be used in order to improve performance.

<unquote|subexpr>
(mark substitutable subexpressions)

This tag is used in combination with quasiquote and quasi in order to mark the subexpressions which need to be evaluated.

<unquote*|subexprs>
(unquote splicing)

This tag is similar to unquote, except that the argument subexprs now evaluates to a list of subexpressions, which are inserted into the arguments of the parent node. For instance, consider the macro

< assign | fun | < xmacro | x |

<\quasi|

<tree|dup|<unquote*|<quote-arg|x>>|<unquote*|<quote-arg|x>>>

>

> >

Then <fun|a|b|c> is typeset as

<quasi|expr>
(substitution)

This tag is a shortcut for <eval|<quasiquote|expr>>. This primitive is often used in the TeXmacs style files in order to write macros which define sets of other macros. For instance, the macro

<assign|new-theorem|<macro|name|text|<quasi|<assign|<unquote|name>|<macro|body|<surround|<no-indent><strong|<unquote|text>. >|<right-flush>|body>>>>>>

may be used in order to define new theorem-like environments.

<quote-value|var>
(retrieve a value but don't evaluate)

When retrieving an environment variable var, one is usually interested in its typesetted value, as given by <value|var>. In some cases, it may be useful to access the real, non-typesetted value. This can be done with <quote-value|var>.

<quote-arg|var|index-1||index-n>
(retrieve an argument but don't evaluate)

When retrieving (a subexpression of) a macro argument var, one is usually interested in its typesetted value, as given by <arg|var|index-1||index-n>. In some cases, it may be useful to access the real, non-typesetted value. This can be done with <quote-arg|var|index-1||index-n>.

5.Functional operators

Funcational operators are used for computational purposes during the typesetting phase, such as increasing counters, localizing strings like “theorem” and so on. A fundamental set of basic functional operators are built-in primitives. New functional operators can easily be added using the extern primitive. Functional operators operate on five main types of arguments: strings, numbers, lengths, booleans and tuples. Some operators are overloaded, so that they can be used for several types.

5.1.Operations on text

<length|expr>
(length of a string)

If expr is a string, the length of the string is returned. For instance, <length|Hello> evaluates to 5.

<range|expr|start|end>
(extract a substring)

Return the substring of expr starting at position start and ending at position end (not included). For instance, <range|hottentottententententoonstelling|9|15> evaluates to tenten.

<merge|expr-1||expr-n>
(concatenate strings)

This primitive may be used to concatenate several strings expr-1 until expr-n. For instance, <merge|Hello|World> produces HelloWorld.

<number|number|render-as>
(alternative rendering of numbers)

Renders a number in a specified way. Supported values for render-as are

roman
Lower case Roman: <number|18|roman> ⟶ xviii.
Roman
Upper case Roman: <number|18|Roman> ⟶ XVIII.
alpha
Lower case letters: <number|18|alpha> ⟶ r.
Alpha
Upper case letters: <number|18|Alpha> ⟶ R.

<date>

<date|format>

<date|format|language>
(obtain the current date)

Returns the current date in a specified format (which defaults to a standard language-specific format when empty) and a specified language (which defaults to English). The format is similar to the one used by the Unix date command. For instance, <date> evaluates to “January 1, 2009”, <date||french> to “1 January 2009” and <date|%d %B om %k:%M|dutch> to “1 January om 16:40”.

<translate|what|from|into>
(translation of strings)

Returns the translation of a string what of the language from into the language into, using the built-in TeXmacs dictionaries. The languages should be specified in lowercase letters. For instance, <translate|File|english|french> yields “Fichier”.

The list of currently available languages can be checked in the DocumentLanguage menu. The built-in TeXmacs dictionaries can be found in

$TEXMACS_PATH/languages/natural/dic

When attempting to use a non-existing dictionary, the program may quit. For most purposes, it is more convenient to use the localize macro, which converts a string from English into the current language.

5.2.Arithmetic operations

<plus|expr-1||expr-n>

<minus|expr-1||expr-n>
(addition and subtraction)

Add or subtract numbers or lengths. For instance, <plus|1|2.3|5> yields 8.3 and <plus|1cm|5mm> produces . In the case of subtractions, the last argument is subtracted from the sum of the preceding arguments. For instance, <minus|1> produces -1 and <minus|1|2|3|4> yields 2.

<times|expr-1||expr-n>
(multiplication)

Multiply two numbers expr-1 until expr-n. One of the arguments is also allowed to be a length, in which case a length is returned. For instance, <times|3|3> evaluates to 9 and <times|3|2cm> to .

<over|expr-1||expr-n>
(division)

Divide the product of all but the last argument by the last argument. For instance, <over|1|2|3|4|5|6|7> evaluates to 102.857, <over|3spc|7> to , and <over|1cm|1pt> to 28.4528.

<div|expr-1|expr-2>

<mod|expr-1|expr-2>
(division with remainder)

Compute the result of the division of an integer expr-1 by an integer expr-2, or its remainder. For instance, <div|18|7>=2 and <mod|18|7>=4.

<equal|expr-1|expr-2>

<unequal|expr-1|expr-2>

<less|expr-1|expr-2>

<lesseq|expr-1|expr-2>

<greater|expr-1|expr-2>

<greatereq|expr-1|expr-2>
(comparing numbers or lengths)

Return the result of the comparison between two numbers or lengths. For instance, <less|123|45> yields false and <less|123mm|45cm> yields true.

5.3.Boolean operations

<or|expr-1||expr-n>

<and|expr-1||expr-n>

Returns the result of the boolean or/and on the expressions expr-1 until expr-n. For instance, <or|false|<equal|1|1>|false> yields true.

<xor|expr-1|expr-2>

Returns the exclusive or of two expressions expr-1 and expr-2, i.e. <xor|true|true> yields false.

<not|expr>

Returns the negation of expr.

5.4.Operations on tuples

<tuple|expr-1||expr-n>
(construct a tuple)

Forms a tuple from the expressions expr-1 until expr-n.

<is-tuple|expr>
(tuple predicate)

Tests whether a given expression expr evaluates to a tuple.

<length|expr>
(length of a tuple)

If expr is a tuple, then we return its arity. For instance, <length|<tuple|hop|hola>> evaluates to 2.

<look-up|tuple|which>
(access an entry in a tuple)

Returns the element with index which in tuple. For instance, <look-up|<tuple|a|b|c>|1> yields b.

<range|