SelfscribeDownloadOpen the app

Your own notation

Invent a shorthand and let it become a diagram or a formula.

Document · Advanced · uses markup, diagram, maths

Edit it in the playgroundRead the file

What it shows

Invent a short notation, and let rewrite rules turn it into a diagram or maths.

What you learn

A rewrite block, a template for each kind of line, numbered slots, first and last lines, a different slot mark for LaTeX.

Try this

  • Add a line "bed onions 2x2 at 1,-1" to the plan, and a fifth bed appears under the herbs.
examples/own-notation.self · 99 lines
~~~ selfscribe
# Lesson: Invent a short notation, and let rewrite rules turn it into a diagram or maths.
# You learn: a rewrite block, a template for each kind of line, numbered slots, first and last lines, a different slot mark for LaTeX.
# Try this: add a line "bed onions 2x2 at 1,-1" to the plan, and a fifth bed appears under the herbs.
# summary: Invent a shorthand and let it become a diagram or a formula.
# category: syntax
# tags: document, advanced, ideas
# uses: markup, diagram, maths
use markdown
set title Your own notation

# ---- A ```chain block: "bins: Three-bay bins" names a step, "bins > heap" moves on to the next ----
# Each indented line is a template: the left side is what you type, the right side what the
# flow plugin reads. _1, _2 and _3 carry the words over. `first` writes a line before the others.
rewrite block ``` chain ... ``` -> flow
  line   _1: _2              ->  node _1 "_2" :service
  line   _1 > _2             ->  _1 -> _2 moving
  line   _1 > _2 after _3    ->  _1 -> _2 "_3" moving
  first                      ->  set direction=LR

# TikZ and LaTeX use _ themselves, so the next templates fill in @1, @2 … instead.
set slot @

# ---- A ```plan block: "bed beans 3x2 at 1.5,5" places a bed, "path gate to compost" a path --
# `first` adds the styles every bed, the pond and the shed are drawn with.
rewrite block ``` plan ... ``` -> tikz
  line   bed @ @x@ at @,@    ->  \node[bed, minimum width=@2cm, minimum height=@3cm] (@1) at (@4,@5) {\textbf{@1}\\ \small @2 m by @3 m};
  line   pond @ at @,@       ->  \node[pond, minimum size=@1cm] (pond) at (@2,@3) {pond};
  line   shed @x@ at @,@     ->  \node[shed, minimum width=@1cm, minimum height=@2cm] (shed) at (@3,@4) {shed};
  line   gate at @,@         ->  \node[gate] (gate) at (@1,@2) {gate};
  line   compost at @,@      ->  \node[gate] (compost) at (@1,@2) {compost};
  line   path @ to @         ->  \draw[walk] (@1) -- (@2);
  first                      ->  \tikzset{bed/.style={draw=green!50!black, fill=green!60!black, fill opacity=0.18, text opacity=1, rounded corners=6pt, thick, align=center},pond/.style={circle, draw=cyan!60!black, fill=cyan!70!black, fill opacity=0.2, text opacity=1, thick}, shed/.style={draw=brown!70!black, fill=brown, fill opacity=0.25, text opacity=1, thick}, gate/.style={draw=orange!80!black, fill=orange, fill opacity=0.22, text opacity=1, rounded corners=3pt, inner sep=5pt},walk/.style={dashed, very thick, draw=brown!60}}

# ---- A ```yield block: "beans: 14 kg from 6 m2 is 2.3" becomes a line of maths -----------
# `first` and `last` wrap the lines in an aligned group, so the equals signs line up.
rewrite block ``` yield ... ``` -> math
  line   @: @ kg from @ m2 is @   ->  y_{\text{@1}} &= \frac{@2\ \text{kg}}{@3\ \text{m}^2} = @4\ \text{kg/m}^2 \\
  first                           ->  \begin{aligned}
  last                            ->  \end{aligned}
set slot _
~~~
# Hollins Lane community garden

This year's plan, drawn from nine lines of our own shorthand.

``` plan
bed beans 3x2 at 1.5,5
bed potatoes 3x2 at 5.5,5
bed squash 3x2 at 9.5,5
bed herbs 2x2 at 1,1.5
pond 2 at 5.5,1.5
shed 2.4x1.8 at 9.5,1.5
gate at -1.2,3.2
compost at 12.4,3.2
path gate to compost
```

The beds are three metres by two. The path runs from the gate on Hollins Lane to the compost bays at the back, and it is wide enough for a wheelbarrow.

## Where the compost goes

Peelings go out in the caddies on Saturday mornings. Nothing is bought in.

``` chain
caddy: Kitchen caddies
bins: Three-bay bins
heap: Rotted heap
beds: The beds in March
caddy > bins
bins > heap after six months
heap > beds
```

## What each bed gave last year

``` yield
beans: 14 kg from 6 m2 is 2.3
potatoes: 31 kg from 6 m2 is 5.2
squash: 22 kg from 6 m2 is 3.7
herbs: 3.5 kg from 4 m2 is 0.9
```

Potatoes did best again, so they keep the middle bed. The beans and the squash swap ends, so neither grows in the same soil two years running.

## How it's made

- Each block is written in a few words of this document's own, such as `bed beans 3x2 at 1.5,5` or `bins > heap`. Switch to Source to see the short lines beside the drawings.
- A `rewrite` rule in the rules block turns each line into the long form a plugin reads: TikZ for the plan, the flow plugin's lines for the compost, LaTeX for the yields. `first` and `last` add lines before and after.
- TikZ and LaTeX use `_` for their own purposes, so `set slot @` makes `@` the slot for those rules. A line no template matches reaches the plugin exactly as written.

## Try it yourself

1. Add `bed onions 2x2 at 1,-1` to the plan. A fifth bed appears under the herbs.
2. Add `beds > caddy after the harvest` to the compost. The chain loops back to the start.
3. Add `onions: 9 kg from 4 m2 is 2.3` to the yields. A new line of maths lines up under the others.
4. In the chain's rule, change `set direction=LR` to `set direction=TB`. The compost runs top to bottom.

Next: *How Selfscribe works* shows the other kinds of rule; *Changelog* gives each kind of line its own colour.