Formulas
A formula computes a value inside the text. It is written between <% and
%>:
Area: <% 3 * 4 %>
Area: 12
In the editor, a line shows the result of its formulas. Press Enter on the line to see the formulas themselves; Escape leaves it unchanged.
Write a formula
- Select a line and press Alt+Enter: an empty line appears below.
- Press Enter to edit it, and type
Area:. - Press Ctrl+E: a small field opens for the formula.
- Type
3 * 4and press Enter. The formula shows as a grey box, a chip, in the text. - Press Ctrl+Enter. The line shows Area: 12.
In a rich text line, as here, each formula is a
chip: point at it to see the formula, click it to change it. In a Markdown,
Text or Table line, Ctrl+E writes the brackets of a formula, <% %>,
with the cursor between them, around the selected text if there is any.
Names
A formula can give a value a name, and later formulas can use it. A formula that only gives names shows nothing. Several names in one formula are separated by commas:
<% L = 6, q = 12.5 %>
Span L = <% L %> m, load q = <% q %> kN/m.
The midspan moment is **<% q * L ^ 2 / 8 %> kNm**.
Span L = 6 m, load q = 12.5 kN/m.
The midspan moment is **56.25 kNm**.
A name is known on its own line and on every line below it, as in a
calculation on paper. Change L = 6 to L = 8 and press Ctrl+Enter:
every line below that uses L follows.
A name that is used before it is given, or misspelled, shows nothing: it is not an error. If a value is missing from a line, check the name, and check that it is given on a line above.
A formula can span several lines. # starts a note that runs to the end of
the line:
<%
L = 6, # span in m
q = 12.5 # load in kN/m
%>
Numbers
| Formula | Result |
|---|---|
7 + 3 * 2 | 13 |
(7 + 3) * 2 | 20 |
10 / 4 | 2.5 |
2 ^ 3 | 8 |
math.sqrt 16 | 4 |
math.round 2.567 | 3 |
math.round (2.567 * 100) / 100 | 2.57 |
math.max (3, 8) | 8 |
math.round rounds to a whole number. To keep two decimals, multiply by
100, round, and divide by 100, as in the table. The
reference of math
lists all the functions of math.
Text and choices
A formula can also give text, between double quotes, and choose between two
values with condition ? "if true" ; "if false":
<% section = "IPE 300", u = 0.85 %>
The section is <% section %>, utilization <% u * 100 %> %: **<% u <= 1 ? "adequate" ; "not adequate" %>**.
The section is IPE 300, utilization 85 %: **adequate**.
The comparisons are <, <=, >, >=, == (equal) and != (not
equal).
A formula stays on its line
A formula must close on the line where it opens. If you leave a line with a
<% that would only close at a %> on a line below, the editor keeps you on
the line and says why:
The “<%” on line 2 has no closing “%>”, so it would run into line 4: correct it, or press Escape to discard the changes.
The same goes for a %> you add below a <% left open higher up. A <% with
no %> anywhere below it is not a formula: it stays plain text.
A line that is only a formula
A line that holds one formula and nothing else can be a Formula cell: select the line and press =, as in a spreadsheet, and type the expression without the brackets. See Cell types.
When a formula is wrong
A formula that cannot be computed shows [[Undefined]], highlighted, and
the rest of the document still works:
Result: <% "a" - 1 %>
Result: [[Undefined]]
Here, the formula subtracts a number from a word. Edit the line and fix the formula. Point at the highlighted value to read the reason, or see Errors.
Learn more
A formula is a Texl tag, and the language inside it is called Swan. Writing documents with Texl explains how tags work in a document, The Swan language describes the language (lists, functions, namespaces), and the built-in reference lists every function.