Strings and values

The content of any variable can be interpreted as a literal text string, as a numerical value, or as a variable name.

The distinction is subtle but important. For example when compared as values, variables containing 24pt and 2pc are considered equal. But when compared as strings they are not.

When you assign to a variable using the string attribute of the <set/> command, the variable is given a literal copy of the specified string. But a value attribute is first converted into a single integer value before being assigned.

For example the commands:

<set var="MyStr" string="2.5pc" />
<set var="MyVal" value="2.5pc" />

will assign 2.5pc to {MyStr} and 300 to {MyVal}. Note that 300 is the internal TopLeaf representation for 2.5 picas.

In general, whole numbers and measures (in terms of pt, pc, in, cm etc.) are best handled as values. Anything else (including numbers with a decimal point) is best left as a string.

The value attribute of the <set/> command has a limited math function, so that:

<set var="Double" value="{MyVal}*2" />

will assign {Double} the value 600. And in fact:

<set var="Double" value="{MyStr}*2" />

will do exactly the same thing, as the variable {MyStr} is expanded to 2.5pc before the command is executed. Note however that an error will result if the expanded value string cannot be resolved into a valid value (so the above command would fail if {MyStr} contained "Hi there!").

The allowed operations in a value expression are:

  • A + B [addition]

  • A - B [subtraction]

  • A * B [multiplication]

  • A / B [division]

  • A % B [modulus (integer remainder after division)]

  • ( expression ) [grouping]

In the absence of parentheses, multiplication and division are performed prior to addition and subtraction, so that 1+2*3+4 evaluates to 11, while (1+2)*(3+4) gives 21. Spaces are not required but may be added for readability, for example 1 + 2*3 + 4.

Note that decimal numbers are permitted in value expressions, but the resulting value will be converted to an integer before being assigned. Thus "100*1.75" gives the value 175, while "100+1.75" gives 102. If you need to save such a value, keep it as a string until just before you use it.