Code blocks
When you want to drop in some code - a snippet of Python, a shell command, a chunk of JSON - put it in a code block. EtherPK highlights it in the right colours for the language and keeps it tidy as you type.
The same three backticks also give you maths and diagrams, which are drawn rather than highlighted - jump to Blocks that draw instead of highlighting if that is what you are after.
Making a code block
Type three backticks ``` and press Enter. EtherPK finishes the block for you: it adds
the closing backticks below and drops your cursor on the empty line between them. Once you are
out of the block it shows as a tidy shaded panel, with the backticks dimmed back so the code
stands out - click back in any time and they brighten for editing.
To get colours, give it a language: type it right after the backticks before you press Enter -
```python , ```ts , ```sql . A new block has no language by default (so no colours
until you add one); you can set a default language for the graph in its settings.
You can also type / and choose Code block from the menu.
Need backticks inside your code (say you are writing about markdown)? Open the block with four backticks instead of three. EtherPK matches whatever you use, so the closing fence balances and a three-backtick line sits happily inside.
Choosing the language
The word straight after the opening backticks is the language. Change it any time and the highlighting follows. A wide range of languages and markups are supported; if EtherPK does not recognise one, the code simply shows as plain text on the panel - nothing breaks.
Two words are the exception, and do something else entirely - see Blocks that draw instead of highlighting below.
You can set a default language for new blocks in a graph's settings (the cog on the graphs list). Leave it blank and new blocks start with no language until you pick one.
Blocks that draw instead of highlighting
Two words after the backticks are not languages at all. Instead of colouring the text, they draw it: the block turns into a diagram or a set of equations once your cursor leaves it.
| Type this | You get |
|---|---|
```math |
Typeset maths (KaTeX) |
```mermaid |
A diagram (Mermaid) |
Everything else after the backticks is treated as a language and highlighted as code.
Maths
Open a block with ```math and write LaTeX inside it:
```math
\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}
```
Move your cursor out and the block becomes the typeset equation. Click it - anywhere on it, or on either fence - and you get the LaTeX straight back to edit. Nothing is stored but the text you typed, so the block is still ordinary markdown in your file.
A few things worth knowing:
- It must be spelled
math.latexandtexare not recognised, and a block opened with either is highlighted as code rather than drawn. - A mistake in your LaTeX will not lose your work. The part KaTeX cannot read shows in red, in place, and the rest still renders. Click in and fix it.
- Backticks only. A block opened with tildes (
~~~) is left as plain text.
Maths inside a sentence
For a formula in the middle of a line, wrap it in single dollar signs instead:
The area of a circle of radius $r$ is $\pi r^2$.
The same click-to-edit rule applies: the formula becomes editable text whenever your cursor touches it.
- There is no
$ ... $. Double dollars are left as plain text - a block of maths is a```mathblock, and that is the only way to write one. - A lone
$is just a dollar sign. Prices are safe: "it costs $5" stays exactly as typed, because a$with no closing partner on the same line is never treated as maths. - The pair has to close on the same line. Maths never runs across a line break.
- Write
\$if you want a literal dollar sign next to something that would otherwise pair up.
Diagrams
```mermaid works the same way - write Mermaid syntax inside and
the block draws itself once your cursor leaves. If the diagram cannot be drawn at all, the block
says so rather than disappearing, and your text is untouched.
Code blocks inside a list
Code blocks work inside outliner bullets too. The block lines up neatly one space to the right of the bullet, and everything inside stays aligned there - the opening and closing fences sit in the same column so it all looks balanced. You cannot accidentally push the code further left than its fence, so a block in a list stays valid.
Maths and diagram blocks behave exactly the same way - the bullet stays visible and the block sits to the right of it:
- Gaussian integral
```math
\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}
```
The keys, briefly
- Enter inside a block - a new line of code, kept aligned. (Outside a block, Enter makes the next bullet as usual.)
- Tab / Shift+Tab inside a block - indent or outdent your code, but never past the left edge of the block.
- Cmd/Ctrl+Enter inside a block - step out, onto a fresh line below the block.
A note on the editor keys generally
While we are talking about keys, two outliner behaviours changed alongside code blocks:
- Enter on an empty bullet now makes a new bullet below it (it used to step you back out a level). To go back a level, use Shift+Tab.
- Cmd/Ctrl+Enter breaks you out of the list entirely, onto a plain line at the left margin.
- Shift+Enter adds a soft line within the current bullet, lined up under its text.
You can also choose how far each level indents - 2 or 4 spaces - in a graph's settings.