Stencila documents can contain code as part of the document narrative, even when that code is not executable. This is useful for tutorials, technical writing, API documentation, method descriptions, and any document that needs to show source code or code fragments.
Use backticks for short code fragments inside paragraphs:
A code fragment: `2 * pi * r^2`.
A code fragment: 2 * pi * r^2.
You can also add a language tag when you want to make the language explicit:
With language: `6 * 7`{python}.
Use fenced code blocks for longer static code listings:
```python
# Some python code
a = 3
```
A code block can also omit the language:
Code blocks can also be empty or contain only whitespace when needed:
You can add the demo keyword to a fenced code block when you want Stencila to parse the content of the block and render the resulting document content below it.
This is useful in documentation when you want readers to see both the source and the rendered result.
```smd demo
> [!note]
> This is rendered from the code block above.
```
> [!note]
> This is rendered from the code block above.
Note
This is rendered from the code block above.
Unlike exec, the demo keyword does not execute code. Instead, it treats the code block content as document source, parses it, and renders the parsed result underneath the block.
Use static code when you want to:
For related authoring features, see:
For the formal Stencila Schema definitions behind these features, see: