template/book/syntax_exercises/010.md
2025-03-31 22:26:53 +02:00

2.3 KiB

Exercise 10: Create tables

In this exercise, we will explore how to create and format tables in your TeachBook using Markdown and reStructuredText.

::::::{topic} Exercise objective Can you correctly format tables within your book? ::::::

Tables help organize and present structured data clearly. JupyterBook supports Markdown tables and reStructuredText list-tables.

Writing Tables with Markdown

  1. Open a new or existing Markdown file where you want to insert a table.
  2. Add a simple table using the | separator and --- for column alignment. Try the following example:
| Column 1 | Column 2 | Column 3 |
|---|---|---|
| Data 1  | Data 2  | Data 3  |
| Data 4  | Data 5  | Data 6  |

Creating Tables with reStructuredText

  1. You can also create tables using the list-table directive. The advantage of this method is that it allows referencing (refer to Exercise 11). Use the code below to try this out.
```{list-table} Sample Data Table
:header-rows: 1
* - Category
  - Value 1
  - Value 2
* - Item A
  - 10
  - 20
* - Item B
  - 15
  - 30
```
  1. To test your understanding, try and create a new table from scratch with at least three columns, ensuring proper alignment and formatting, using both methods described above.

  2. When you are ready, commit your changes to the repository by clicking on the green Commit changes button.

  3. Add a commit message.

  4. To see your 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).

  5. 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 create a basic table using Markdown? 
- When would you choose to use a `list-table` instead of a Markdown table?
- What syntax is required to define a `list-table`?  
- How do you specify column headers in a `list-table`?