Exercise 12: Use admonitions#
In this exercise, you will learn how to use admonitions to enhance the readability and structure of your TeachBook content. Admonitions are special highlighted blocks that are useful for tips, warnings, notes, and more.
Tip
Admonitions help to guide readers’ attention and make your book more accessible. Full documentation is available here for the default MyST syntax in your book and here for the TeachBooks’ extension which allows custom colours.
Using standard admonitions#
Open a new or existing Markdown file.
Start by adding a simple
note
admonition. This type uses a fixed title (“Note”):
```{note}
This is a note. It always has the title "Note".
```
Try adding the following fixed-title admonitions. These cannot have custom titles:
```{tip}
This is a helpful tip!
```
```{warning}
Be careful when editing this file.
```
```{attention}
Important: Save your work frequently.
```
Other fixed-title admonitions are listed below. Try use these in your code using the same format used above.
caution
important
seealso
error
hint
danger
Custom titles & styles for admonitions#
Some admonitions allow you to specify your own title. Try the
admonition
directive:
```{admonition} Did you know?
You can customise the title of this block using the `admonition` directive.
```
Similarly, you can customise titles for
dropdown
admonitions:
```{dropdown} Click to reveal more info
This content is hidden until the user clicks the title.
```
Dropdowns are great for optional or expandable content. Try nesting content inside a dropdown block::
```{dropdown} See the steps
1. Open your file.
2. Write some content.
3. Preview the changes.
```
Check your understanding with the summary table below.
Summary: Fixed vs. Custom Title Admonitions
Here’s a quick overview:
Fixed Title Admonitions |
Custom Title Admonitions |
---|---|
|
|
For these admonitions that allow custom titles (
admonition
,dropdown
), you can also apply a custom style using the:class:
option. This can be used to change the appearance (e.g. colour or layout) of the block. Try the following examples:
```{admonition} Pro tip!
:class: tip
You can apply styles using the `:class:` option.
```
```{dropdown} Save your work
:class: warning
Don't forget to save your changes regularly to avoid data loss!
```
You can apply the
:class: dropdown
to anyadmonition
, even if it is not originally a dropdown block. This will allow the admonition to be collapsible, making it useful for sections where you’d like to hide additional content until the reader clicks to expand it. Test this out:
```{admonition} Extra Info
:class: dropdown
This content will be hidden until the reader clicks to reveal it.
```
The table below summarizes the classes that can be used together with the
admonition
anddropdown
blocks. Try customize admonitions yourself, using different classes from the table.
Class name |
Effect |
---|---|
|
Gives the block a green background (styled like a helpful tip) |
|
Orange background with a warning-style border |
|
Yellow caution-style background |
|
Red background indicating errors or important warnings |
|
Makes the block collapsible (content is hidden until clicked) |
|
Blue background, often used for related info or references |
|
Dark blue or purple background for emphasis |
|
Subtle grey note block |
You can also use multiple classes at once by separating them with spaces. For example,
:class: important dropdown
will give you a collapsible, important-looking block. Try mixing and matching these, using the example below as a base:
```{admonition} Important: Check this out!
:class: important dropdown
This content is both highlighted as important and collapsible.
```
Custom colours for admonitions#
It is also possible to use specific colours to customize your admonitions, using our extension ‘Sphinx-Named-Colours’. This allow you to use default ‘CSS’ colours or your own custom colours. To do so, it is necessary to add the extension as shown below. Note that in this template book, this is already done for you, let’s check that!
Step A: Add to requirements.txt
Ensure that the package is included in your project’s requirements.txt
(in the root of your repository) to make it available:
.
.
.
sphinx-named-colors
.
.
.
Step B: Enable in _config.yml
In your _config.yml
file (in the book/
subdirectory of your repository), ensure that the extension is added to the list of Sphinx extra extensions so that it’s included in the building of your book:
sphinx:
extra_extensions:
.
.
.
- sphinx_named_colors
.
.
.
Now we can experiment with custom-coloured admonitions! Go ahead and look at the available CSS Named Colours, and pick a desired colour.
Once you have chosen a colour, coloured admonitions can be generated in two ways, depending on how you’d like to structure your content. The first method is by adding the name-colour via
:class:
to an existingadmonition
ordropdown
type block, similar to what was done above. This can be done as follows:
```{admonition} Custom Colour Example 1
:class: darkred
This is a custom coloured admonition using `:class: darkred`.
```
The second method of adding coloured admonitions is by directly creating an admonition based on a colour. This method can be used if you’d rather not use the generic type and want a minimal syntax. In this case, the title is optional, and if omitted, the title bar will not appear. Try this out with the code below:
```{darkgreen} Optional Title with custom coluor example 2
This is a green-styled custom admonition.
```
For more details, see the documentation of this extension in our manual. This also describes how to add your own custom colours!
When you are ready, commit your changes to the repository by clicking on the green
Commit changes
button.Add a commit message.
To see your changes, go to
Actions
- The most recent workflow runoverview.md / the commit message of the commit you just made
- Wait for it to finish - In the summary, click on the link of your book shown in the tableBranches deployed
and underPrimary book at root
(getting bored of waiting? There’ll be exercising on doing this locally which prevents you from waiting).Do you see your change? If you don’t see it click
CTRL
+F5
/Control
+F5
to refresh the page.
Check your understanding
Before moving on, make sure you understand:
Which admonitions allow custom titles, and which don’t?
What is the purpose of a dropdown admonition?
When might you use a
note
vs. anadmonition
?In which ways can you further customize
admonition
blocks?