template/book/syntax_exercises/009.md
2025-03-25 23:51:57 +01:00

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

  1. Open a new or existing Markdown file where you want to add a reference.
  2. Let's start with the simplest way to create a reference to another chapter. This is by directly linking it 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)
  1. 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. For example: Jump to the next section
[Jump to the next section](#referencing-an-equation)
  1. 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 using {ref} 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...
  1. 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`.
  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>`.
  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 the [] and calling the identifier within the <> as shown below. This will display: My favourite exercise was this one.
My favourite exercise was [this one](exercise_1).

Referencing an Equation

  1. 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)

  1. To refer to this equation we need to use {eq} and call the identifier within the `` to achieve: Refer to Equation {eq}eq:Newton.
Refer to Equation {eq}`eq:Newton`

Referencing a Table

  1. 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        |
  1. Refer to the table like {numref}Table {number} <tab-example> using {numref} and {number} as follows:
{numref}`Table {number} <tab-example>`

Referencing a Figure

  1. This can also be done for a figure. Let's add a figure as we learnt to do in Exercise 7, but this time adding an identifier using name:.
  ```{figure} figures/example.png
  ---
  width: 80%
  align: center
  name: fig-example
  ---
  Example figure.
  ```
  1. Refer to the figure in the format {numref}Figure {number} <fig-example> using {numref} as follows:
{numref}`Figure {number} <fig-example>`
  1. Finally, lets 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/).
  1. When you are ready, commit your changes to the repository by clicking on the green Commit changes button.

  2. Add a commit message.

  3. To see you changes, go to {octicon}play;1em Actions - The most recent workflow run overview.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 table Branches deployed and under Primary book at root (getting bored of waiting? There'll be exercising on doing this locally which prevents you from waiting).

  4. 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?