Skip To Article

There are two directives available to add exercises and solutions to your documents: (1) an exercise directive; and (2) a solution directive. The exercises are enumerated by default and can take in an optional title argument as well as be “gated” around Jupyter Notebook cells.

Exercise Directive

Example
MyST Syntax

Source: QuantEcon

The following options for exercise directives are supported:

exercise directive

Alias: exercise-start

Directive Argument (parsed)
No description
Directive Body (parsed)
No description
Options
class (string)

Annotate the exercise with a set of space-delimited class names.

label (string)

Label the exercise to be cross-referenced or explicitly linked to.

Alias: name
enumerated (boolean)

Turn on/off the numbering for the specific exercise

Alias: numbered
enumerator (string)

Explicitly set the exercise number

Alias: number
nonumber (boolean)

Legacy flag to disable numbering of exercises; equivalent to enumerated: false

hidden (boolean)
No description

Solution Directive

A solution directive can be included using the solution pattern. It takes in the label of the directive it wants to link to as a required argument. Unlike the exercise directive, the solution directive is not enumerable as it inherits numbering directly from the linked exercise. The argument for a solution is the label of the linked exercise, which is required.

Example
MyST Syntax

Source: QuantEcon

The following options for solution directives are supported:

solution directive

Alias: solution-start

Directive Argument (string, required)
No description
Directive Body (parsed)
No description
Options
class (string)

Annotate the solution with a set of space-delimited class names.

label (string)

Label the solution to be cross-referenced or explicitly linked to.

Alias: name
enumerated (boolean)

Turn on/off the numbering for the specific solution

Alias: numbered
enumerator (string)

Explicitly set the solution number

Alias: number
hidden (boolean)
No description

Referencing Exercises & Solutions

You can refer to an exercise using the standard link syntax:

Referencing Solutions

You can refer to a solution directly as well using a Markdown link or using the {ref} role like: {ref}`my-solution` the output of which depends on the attributes of the linked directive. If the linked directive is enumerable, the role will replace the solution reference with the linked directive type and its appropriate number like so: Solution to Exercise 1.

In the event that the directive being referenced is unenumerable, the reference will display its title: Solution to Exercise (n!n! Factorial).

If the title of the linked directive being reference does not exist, it will default to Solution to Exercise.

Alternative Gated Syntax

To be able to be viewed as Jupyter Notebooks (e.g. in JupyterLab MyST) code-cell directives must be at the root level of the document for them to be executed. This maintains direct compatibility with the jupyter notebook and enables tools like jupytext to convert between myst and ipynb files.

As a result executable code-cell directives cannot be nested inside of exercises or solution directives.

The solution to this is to use the gated syntax.

Basic Syntax

Example
MyST Syntax

This can also be completed for solutions with solution-start and solution-end directives. The solution-start and exercise-start directives have the same options as original directive.

Hiding Directive Content

To visually hide the content, simply add :class: dropdown as a directive option, similar to an admonition.

Example
MyST Syntax

Recall that n!n! is read as “nn factorial” and defined as n!=n×(n1)××2×1n! = n \times (n - 1) \times \cdots \times 2 \times 1.

There are functions to compute this in various modules, but let’s write our own version as an exercise.

In particular, write a function factorial such that factorial(n) returns n!n! for any positive integer nn.

Remove Directives

Any specific directive can be hidden by introducing the :hidden: option. For example, the following example will not be displayed

Footnotes
  1. If the exercise directive does not have a title, the label will be used instead.

MyST MarkdownMyST Markdown
Community-driven tools for the future of technical communication and publication, part of Jupyter.