# Syntax Guide¶

Note

This documentation utilized the Markedly Structured Text (MyST) syntax.

## Exercise Directive¶

An exercise directive can be included using the `exercise`

pattern. The directive is enumerated by default and can take in an optional title argument. The following options are also supported:

`label`

: textA unique identifier for your exercise that you can use to reference it with

`{ref}`

and`{numref}`

. Cannot contain spaces or special characters.`class`

: textValue of the exercise’s class attribute which can be used to add custom CSS or JavaScript.

`nonumber`

: flag (empty)Turns off exercise auto numbering.

**Example**

Recall that \(n!\) is read as “\(n\) factorial” and defined as \(n! = 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!\)
for any positive integer \(n\).

**MyST Syntax**

```
```{exercise}
:label: my-exercise
Recall that $n!$ is read as "$n$ factorial" and defined as
$n! = 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!$
for any positive integer $n$.
```
```

*Source:* QuantEcon

### Referencing Exercises¶

You can refer to an exercise using the `{ref}`

role like `{ref}`my-exercise` `

, which will display the title of the exercise directive. In the event that directive does not have a title, the title will default to “Exercise” like so: Exercise.

Enumerable directives can also be referenced through the `numref`

role like `{numref}`my-exercise` `

, which will display the number of the exercise directive. Referencing the above directive will display Exercise 1. Furthermore, `numref`

can take in three additional placeholders: *%s* and *{number}* which get replaced by the exercise number and *name* by the exercise title.1

## 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 unenumerable. The following options are also supported:

`label`

: textA unique identifier for your solution that you can use to reference it with

`{ref}`

. Cannot contain spaces or special characters.`class`

: textValue of the solution’s class attribute which can be used to add custom CSS or JavaScript.

Note

The title of the solution directive links directly to the referred directive.

**Example**

Here’s one solution.

```
def factorial(n):
k = 1
for i in range(n):
k = k * (i + 1)
return k
factorial(4)
```

**MyST Syntax**

```
````{solution} my-exercise
:label: my-solution
Here's one solution.
```{code-block} python
def factorial(n):
k = 1
for i in range(n):
k = k * (i + 1)
return k
factorial(4)
```
````
```

*Source:* QuantEcon

### Referencing Solutions¶

You can refer to a solution 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 \(n!\) Factorial. Click the toggle to see the supporting directives.

Exercise (\(n!\) Factorial)

Write a function `factorial`

such that `factorial(int n)`

returns \(n!\)
for any positive integer \(n\).

Here’s a solution in Java.

```
static int factorial(int n){
if (n == 0)
return 1;
else {
return(n * factorial(n-1));
}
}
```

If the title of the linked directive being reference does not exist, it will default to Solution to Exercise. Click the toggle to see the supporting directives.

Exercise

Write a function `factorial`

such that `factorial(int n)`

returns \(n!\)
for any positive integer \(n\).

Here’s a solution in Java.

```
static int factorial(int n){
if (n == 0)
return 1;
else {
return(n * factorial(n-1));
}
}
```

## How to Hide Directives¶

Directives can be hidden using the `dropdown`

class which is available through Sphinx Book Theme. For Sphinx projects, add `"sphinx_book_theme"`

to your `html_theme`

in the `conf.py`

to activate the theme in your Sphinx configuration

```
...
html_theme = "sphinx_book_theme"
...
```

Jupyter Book’s default theme is Sphinx Book Theme; therefore, Jupyter Book projects can utilize `dropdown`

without having to activate the theme in your Sphinx configuration.

To hide the directive, simply add `:class: dropdown`

as a directive option.

**Example**

Recall that \(n!\) is read as “\(n\) factorial” and defined as \(n! = 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!\)
for any positive integer \(n\).

**MyST Syntax**:

```
```{exercise}
:class: dropdown
Recall that $n!$ is read as "$n$ factorial" and defined as
$n! = 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!$
for any positive integer $n$.
```
```

- 1
If the exercise directive does not have a title, an

`invalid numfig format`

warning will be displayed during build if the user tries to use the*{name}*placeholder.