5.5 KiB
Exercise 9: Cross-referencing content
In this exercise, you will learn how to create references to other parts of your book, such as chapters, sections, equations, tables and figures, as well as links.
::::::{topic} Exercise objective Can you create internal references to other sections, equations, tables, figures, and links? ::::::
Referencing elements properly ensures that readers can navigate your book more easily. More details can be found in the [JupyterBook documentation](https://jupyterbook.org/en/stable/content/references.html).
Referencing a Chapter or Section
- Open a new or existing Markdown file where you want to add a reference.
- Let's start with the simplest way to create a reference to another chapter. Try this out by directly linking the chapter through the file path as shown in the following syntax.
[Text to display](path/to/file.md)
For example, if you want to link to Exercise 7, you can write:
[Go to Exercise 7](../exercises/007.md)
- If we want to refer to a section within the same file, we can use local refencing by directly inputting the name of the required section. Use the example below to test this out. This code gives: Jump to the next section
[Jump to the next section](#referencing-an-equation)
- Try out another way of referencing by using a target header. Navigate to the chapter or section you want to reference and assign it an identifier within brackets as shown in the example below. This could be once again in the same markdown file, or a different file in the same repository.
(exercise_1)=
# Exercise 1: First file edit
Let's start with the most basic edit: add some text to a file and see that the website is updated...
- Now, let's explore three different ways to reference this section or chapter! For the first method, we use the standard title text by calling the identifier as follows. This will display: My favourite exercise was {ref}
exercise_1.
My favourite exercise was {ref}`exercise_1`.
- For the second method, we want to refer to the section using a custom title text. Try this out by placing the text that you want to be displayed within `` and calling the identifier within <>.
This will display: My favourite exercise was {ref}
the first one <exercise_1>.
My favourite exercise was {ref}`the first one <exercise_1>`.
- The last method uses markdown syntax to also display a custom title text. Refer to the desired section once again, this time by putting the custom text within [] and calling the identifier within <> as shown below. This will display: My favourite exercise was this one.
My favourite exercise was [this one](exercise_1).
Referencing an Equation
- Now we will test our knowledge using an equation. Similar to the code below, create an equation using
and identify it using a label within the brackets.
$$ F = m \cdot a$$ (eq:Newton)
F = m \cdot a (eq:Newton)
- Refer to this equation by using
{eq}and calling the identifier within the `` to achieve: Refer to Equation {eq}eq:Newton.
Refer to Equation {eq}`eq:Newton`
Referencing a Table
- Similar methods can also be used to refer to a table. Start by creating a table by copying the code below. The identifier is defined using
:name:.
```{table} Example Table
:name: tab-example
| Column 1 | Column 2 |
|----------|----------|
| A | B |
```
:name: tab-example
| Column 1 | Column 2 |
|----------|----------|
| A | B |
- Refer to the table like {numref}
Table {number} <tab-example>using{numref}and{number}as follows:
{numref}`Table {number} <tab-example>`
Referencing a Figure
- This can also be done for a figure. Let's add a figure as we learnt to do in Exercise 8, but this time adding an identifier using
name:.
```{figure} figures/example.png
---
width: 80%
align: center
name: fig-example
---
Example figure.
```
- Refer to the figure in the format {numref}
Figure {number} <fig-example>using{numref}as follows:
{numref}`Figure {number} <fig-example>`
Referencing a Link
- Finally, let's reference a link, using the following syntax:
[Text to display](url)
For example, you can use the code below to display: Here is the link to the template.
Here is the [link to the template](https://teachbooks.github.io/template/).
-
When you are ready, commit your changes to the repository by clicking on the green
Commit changesbutton. -
Add a commit message.
-
To see you changes, go to {octicon}
play;1emActions- 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 deployedand 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+F5to refresh the page.
:class: note
Before moving on to the next exercise, make sure you understand the following:
- How do you reference another chapter or section?
- How do you create a reference to an equation, table or figure in your book?
- How do you reference a link?