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
Solution to Exercise 1
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\).
Solution to \(n!\) Factorial
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\).
Solution to Exercise
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$.
```
Custom CSS or JavaScript¶
Custom JavaScript scripts and CSS rules will allow you to add additional functionality or customize how elements are displayed. If you’d like to include custom CSS or JavaScript scripts in Jupyter Book, simply add any files ending in .css
or .js
under a _static
folder. Any files under this folder will be automatically copied into the built book.
In Sphinx, this can be achieved by specifying the path of your _static
folder and including CSS/JavaScript files by using the options html_css_files
and html_js_files
in conf.py
:
# conf.py
html_static_path = ["_static"]
# CSS files
html_css_files = ["custom.css",]
# JS files
html_js_files = ["custom.js",]
For example, to include the following CSS content which changes the default color of an exercise directive named “orange” under docs/_static/custom.css
:
/* docs/_static/custom.css */
:root {
--background-color: rgba(253, 126, 20, .3);
--border-color: #fd7e14;
}
div.orange {
background-color: var(--background-color);
border-color: var(--border-color);
}
div.orange p.admonition-title {
background-color: var(--background-color);
}
Add html_static_path
and html_css_files
under conf.py
:
# conf.py
html_static_path = ['../_static']
html_css_files = ['custom.css']
These steps will change the default color of the exercise directive named “orange” displayed below
```{exercise}
:class: orange
This is an example of how to introduce custom CSS.
```
This is an example of how to introduce custom CSS.
- 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.