Skip to content
Varlig Guide
Varlig home

Writing a calculation line

This page covers the basics that every calc block relies on: how a line is read, how to leave notes beside your sums, how to name values and reuse them, and how to make a line depend on a condition. Start here if you are new to calculations in Varlig, and come back to it when a name or a comment doesn’t behave the way you expect.

One expression per line

Calculations live in a calc block inside a note. Type a ```calc fence, or press ⌘Y to insert one. Each line in the block is one calculation, and its answer appears beside the line.

calc
£3.40 * 5 gives £17.00
45 min + 1 hour 20 min gives 2 hours 5 min
1.5 litres in ml gives 1,500 mL

Blank lines and comment lines have no answer, so you can space a block out and explain it as you go.

Varlig reads the whole line as maths first, including everyday phrases such as £45 with 20% VAT. If the line doesn’t work that way, it sets aside the words it doesn’t recognise, so 5 apples + 3 oranges still comes to 8. That’s handy for quick jottings, but a description can collide with a phrase or a unit name, so when you want to say what a line is for, use a comment or a label, described next. How Varlig reads a phrase has the details.

Comments

A comment is text the calculator ignores. There are three ways to write one:

Form Example What is ignored
Hash comment rent * 12 # per year or # Budget Everything from # to the end of the line
Slash comment rent * 12 // per year Everything from // to the end of the line
Quoted note "flat whites" £3.40 * 5 Anything inside double quotes, anywhere on a line
calc
# Weekly shop
milk = £1.45
bread = £1.20 // sourdough
eggs = £2.30 "free range"
milk + bread + eggs # without the extras gives £4.95

A line that starts with # is a comment from start to finish, which makes it a handy heading inside a block.

Labels

A label is a few words followed by a colon and a space at the start of a line. It describes the answer without being part of the maths:

calc
rent = £1,250
bills = £180
Rent and bills: rent + bills gives £1,430.00
Café bill: £12.50 + £4 gives £16.50

Labels can use letters (including accented ones), spaces and underscores, but not digits.

A label does not create a name. After Rent and bills: rent + bills, there is no value called Rent and bills. Use = when you want to reuse a result. Also avoid putting a label in front of an assignment: Housing: rent = £1,250 shows the amount, but rent is not stored.

Assignment

= gives a value a name. The line also shows the value, so you can check it as you go.

calc
price = £85 gives £85.00
price + 20% gives £102.00

To update a name you have already defined, use +=, -=, *= or /=:

calc
holiday fund = £400
holiday fund += £150 gives £550.00
holiday fund -= £85 gives £465.00
servings = 4
servings *= 3 gives 12
flour = 750 g
flour /= 3 gives 250 g

If the right-hand side fails, the name holds the error, and every line that uses it shows the same error until you assign the name again:

calc
guests = 0
per head = £120 / guests gives Error: divide by zero
per head + £5 gives Error: divide by zero

Names

Names can be several words long, and can contain accented and non-Latin letters, digits, underscores and emoji:

calc
flat white = £3.40
flat white * 5 gives £17.00
crème brûlée = £6.50
crème brûlée * 2 gives £13.00
🍎 price = £0.45
🍎 price * 6 gives £2.70
q1 sales = £12,400
q2 sales = £14,100
q1 sales + q2 sales gives £26,500.00
bedroom_width = 3.2 m
bedroom_width * 2 gives 6.4 m

A name can include words that Varlig uses elsewhere, such as to, in or needed, and it can borrow the name of a built-in function such as floor. Wherever you use it as a value, Varlig reads your value:

calc
time to work = 25 min
time to work * 10 gives 250 min
paint needed = 12.44 L
paint needed to 1 dp gives 12.4 L
floor = 12
floor * 2 gives 24 m²
floor(3.7) gives 3

Followed by brackets, as in floor(3.7), the function still does its usual job.

A name can even end in a number word, so a price reads the way it’s written on the menu:

calc
table for four = £96
table for four / 4 gives £24.00

Names are case-sensitive: eggs and Eggs are two different values.

If you want a name available in every note, such as your hourly rate or a tax rate, add it to the global definitions in Settings rather than repeating it in each note.

Names that clash with built-in words

A name you define takes priority over anything else spelled the same way, such as a unit, a number word or a constant. Everyday names still leave your quantities alone. A number written straight before a plural name or a unit counts that thing, and a percentage before tip or tax names the tip or tax, so values called hours, nights or tip don’t change these lines:

calc
rate = £14 / hour
hours = 7.5
rate * 7.5 hours gives £105.00
nights = 14
£118/night * 14 nights gives £1,652.00
tip = $12.69
$84.60 + 18% tip gives $99.83

Hold the pointer over the word to see which reading Varlig used. On rate * 7.5 hours it says “Counts hours here, not the variable hours = 7.5.” Anywhere else on a line, the name is your value, as in split £90 among people.

In a few cases your definition does change what a line means:

  • A single letter always means your value, so after m = 3, 2 m is 6 rather than two metres.
  • A number before a singular name multiplies it, so after width = 3, 2 width is 6. The hint suggests writing 2 * width to make that clear.
  • Number words and constants are replaced too: after one = 7, one times two is 14.

Two habits avoid most surprises:

  • Don’t use unit symbols on their own as names, such as m, s, h or in, or reuse constants and number words such as pi, one or i.
  • Prefer descriptive names like hours worked, day rate or run distance.

Document order

Each line sees the names defined above it. Names also carry forward into later calc blocks in the same note, so you can set up your inputs once and use them further down the page.

calc
price = £20
quantity = 3
cost = price * quantity gives £60.00
price = £25
price * quantity gives £75.00
cost gives £60.00

Redefining price doesn’t change the earlier cost line: cost was worked out with the price at that point. To change the result of an earlier calculation, edit the line where its input is defined.

The answer above

prev is the answer on the nearest line above, and ans means the same unless you’ve named a value ans. They suit a quick follow-up sum:

calc
£1,250 * 12 gives £15,000.00
prev / 52 gives £288.46
ans + £20 gives £308.46

To add up a group of lines, write sum or total on a line of its own. See Adding up the lines above.

Scope reset

A line containing only --- starts a fresh scope. Names, custom units and your own functions defined above it are no longer visible below it. This is useful for comparing two scenarios in one note without their values leaking into each other.

calc
# Option A: the flat in town
rent = £1,250
travel = £60
rent + travel gives £1,310.00
---
# Option B: the flat further out
rent = £980
travel = £210
rent + travel gives £1,190.00

Below the ---, travel has to be defined again. If you forget, the line that uses it has nothing to refer to.

Conditionals

if … then … else … picks one of two values based on a comparison. You can use it on its own or assign the result to a name:

calc
basket = £34.50
delivery = if basket >= £40 then £0 else £3.99 gives £3.99
basket + delivery gives £38.49

Chain else if for more than two outcomes:

calc
basket = £34.50
if basket < £20 then £4.99 else if basket < £40 then £2.99 else £0 gives £2.99

Only the chosen branch is worked out, so the other branch may contain something that would fail:

calc
guests = 0
if guests > 0 then £120 / guests else £0 gives £0.00

Postfix if and unless

You can also put the condition after the value. value if condition shows the value when the condition holds, and value unless condition shows it when the condition does not. In either case the line shows nothing otherwise.

calc
basket = £34.50
£3.99 if basket < £40 gives £3.99
£3.99 unless basket >= £40 gives £3.99

Assigning inside a conditional

Both branches can assign to the same name:

calc
basket = £34.50
if basket >= £40 then shipping = £0 else shipping = £3.99 gives £3.99
shipping gives £3.99

With only a then branch, the assignment happens when the condition holds and the name keeps its old value otherwise:

calc
basket = £34.50
points = 0
if basket > £30 then points = 50 gives 50
points gives 50

Comparisons such as >= and the && and || operators are covered in Operators, comparisons and conditionals.

Putting it together

Here is a small freelance invoice, written the way you might keep it in a note:

calc
# March invoice
day rate = £420
days worked = 6
travel = £86.40 // train fares
fees = day rate * days worked gives £2,520.00
subtotal = fees + travel gives £2,606.40
vat registered = true
vat = if vat registered then subtotal * 20% else £0 gives £521.28
Total due: subtotal + vat gives £3,127.68

Every input has a name, so next month you only change days worked and travel, and the rest of the invoice follows. The comments and label keep the block readable, and the conditional means the same note works whether or not you charge VAT.