Code blocks
Fenced code blocks
Three backticks, with the language after the opening fence:
```python
def collatz(n: int) -> int:
return n // 2 if n % 2 == 0 else 3 * n + 1
```def collatz(n: int) -> int:
return n // 2 if n % 2 == 0 else 3 * n + 1Highlighting adapts to the reader's light or dark theme. All mainstream
languages are recognized by their usual identifiers (python, js, ts,
rust, cpp, haskell, r, julia, latex, sql, bash, json,
yaml, ...). An unknown or missing language renders as plain text – it
never errors.
Tildes (~~~) work as an alternative fence. To show a code block inside a
code block (as this guide does), use more backticks for the outer fence than
the inner one.
Titles
Add a filename or caption after the language with a colon:
```python:collatz.py
def collatz(n: int) -> int:
return n // 2 if n % 2 == 0 else 3 * n + 1
```def collatz(n: int) -> int:
return n // 2 if n % 2 == 0 else 3 * n + 1Inline code
Backticks within a sentence: `f(x)` renders as f(x). Nothing inside
is interpreted – no math, no emphasis, no directives – which also makes
inline code the way to show syntax literally.
If the snippet itself contains a backtick, use two backticks as the
delimiter: ``a `quoted` word``.
Use inline code for identifiers, file names, commands, and short expressions
in prose. For mathematical notation, prefer math:
$O(n \log n)$ reads better than O(n log n).
What code blocks are not
- A code block is display-only; there is no execution or evaluation.
- For LaTeX/TikZ that should render as a diagram, use a
TikZ block; a
```latexblock just shows highlighted source. - To group alternative versions of a snippet (say, the same algorithm in two languages), use tabs.