GNU TeXmacsUser manualReferenceAboutEnglishยทDeutsch
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|\(\cdots\)|var-n|body>
(macro of fixed arity)

This primitive returns a macro (the TeXmacs analogue of a \(\lambda\)-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 1. Definition of the abbr tag

<assign|abbr|<macro|x|<rigid|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|\(\cdots\)|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 var (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 argument is specified, <arg|var|index-1|\(\cdots\)|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 index-1 until index-n are evaluated; they must yield non-negative integers (counting from zero) and give the path to the subtree of the macro argument. The name var itself is not evaluated.

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

This primitive returns a macro (the TeXmacs analogue of a \(\lambda\)-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.

The arguments foo, root and var are literal strings. By default, the macro foo is applied to all children. As a special case, when foo is identity, the children are copied without applying any macro. 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 too, 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 collects the result in a tree with label root. In addition, the second argument to foo gives the 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 2. Comma-separated lists.

The comma-separated tag has any arity (though it does not make much sense with arity zero) and typesets 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>
(evaluate the arguments of a compound argument)

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. The value of the macro argument var must be a compound tree.

<compound|foo|arg-1|\(\cdots\)|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 to 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 3. 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|\(\cdots\)|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. Its arguments are not evaluated. The main supported property-value pairs are:

(arity, \(n\))

Sets the arity to the given fixed value \(n\) (literal integer). Variable arities may be specified with the values <tuple|repeat|a|b>, <tuple|repeat*|a|b> and <tuple|options|a|b>.

(accessible, \(i\))

, (hidden, \(i\)), (unaccessible, \(i\))Make the \(i\)-th child always accessible, hidden (it may require unfolding in order to be accessed) or inaccessible (only accessible in source mode). The value may also be all, for all children, or none, which makes all children inaccessible.

(border, \(v\))

Specifies whether the cursor may be positioned at the outer and inner borders of the tag (\(v\) is one of yes, inner, outer or no).

(name, \(s\))

Sets the name under which the tag is presented to the user.

(syntax, \(t\))

Sets the syntax used when parsing mathematical formulas which contain the tag.

(with-like, \(v\))

Declares the tag to behave like with (yes or no).

(locals, \(t\))

Sets local environment variables for all children.

(normal-writability, \(i\))

, (disable-writability, \(i\)), (enable-writability, \(i\))Control whether the \(i\)-th child (or all children) can be edited.

(returns, \(\operatorname{type}\))

, (parameter, \(\operatorname{type}\)), (macro-parameter, \(\operatorname{type}\))Declare the type of the value returned by the tag, or declare the variable to be a style parameter of a given type.

(\(\operatorname{type}\), \(i\))

Declare the type of the \(i\)-th child (or of all children), where \(\operatorname{type}\) is one of the D.R.D. types, such as regular, boolean, integer, length, color or url.

The complete list can be found in edit_env_rep::exec_drd_props (

Typeset/Env/env_exec.cpp

). See also the section on drd-props in the chapter on the data relation descriptor.

<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 arity of the tree obtained when evaluating expression.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".