Follow us on TwitterFork us on GitHub
MyST Markdown

Code and code-blocks

You can include code in your documents using the standard markup syntax of ```language, where language is the programing language for highlighting.

```python import matplotlib.pyplot as plt plt.plot([1, 2, 3], [1, 2, 3], 'go-', label='line 1', linewidth=2) ```

#Code blocks

The above code is not a directive, it is just standard markdown syntax, which cannot add a caption or label. To caption or label blocks of code use the code-block directive.

```{code-block} python :name: my-program :caption: Creating a TensorMesh using SimPEG from discretize import TensorMesh hx = [(1, 40)] hy = [(1, 40)] mesh = discretize.TensorMesh([hx, hy]) ``` In the [](#my-program), we create a mesh for simulation using [SimPEG](https://discretize.simpeg.xyz/).

#Numbering and Highlighting

To add numbers and emphasis to lines, we are following the sphinx code-block directive. You can use linenos which is a flag, with no value, and emphasize-lines with a comma-seperated list of line numbers to emphasize.

1
2
3
4
```{code-block}
:linenos:
:emphasize-lines: 2,3
...

Emphasize lines inside of a code block.

You can also set the start number using the lineno-start directive, and all emphasized lines will be relative to that number.

#code-block reference

linenos (no value)

Show line numbers for the code block

lineno-start (number)

Set the first line number of the code block. If present, linenos option is also automatically activated.

Default line numbering starts at 1.

emphasize-lines (string)

Emphasize lines of the code block, for example, 1, 2 highlights the first and second lines.

The line number counting starts at lineno-start, which is by default 1.

caption (string)

Add a caption to the code block.

name (string)

The target label for the code-block, can be used by ref and numref roles.