Skip to main content

Chapter 2: Writing a chapter

Chapter 2: Writing a chapter

TipAt a glance
  • You will build: nothing to run: this chapter is the house style, written as a page rather than a notebook
  • You need: Chapter 1 read, so the boxes below look familiar
  • Time to run: about 10 minutes to read
  • Level: for authors and translators

This chapter describes how a chapter in this template is put together, so that a new chapter looks and behaves like the others. It exists only in English. Open the Indonesian side of the site and you will find a short page in its place, saying so and linking here. That page is written by the template, not by an author.

NoteLearning objectives

By the end of this chapter you will be able to:

  • start a new chapter in either language with one command;
  • use the boxes the template draws from plain text;
  • back every number and fact with a cell output or a row of the source ledger;
  • keep a translation in step with the chapter it was translated from.

1. Files and the chapter list

A chapter is one file: en/<slug>.ipynb or en/<slug>.qmd, and the same in id/. The slug starts with two digits that set its place, such as 03-maps. A notebook suits a chapter with code to run; a .qmd page suits one that is only text, like this one.

Every chapter is listed once in tutorial.yml, with its title in each language. That list is the only place the order and titles are kept. Before every render, tools/prepare.py builds the menus, the sidebars and the chapter tables on the two home pages from it. If a chapter has no file in one language, it writes a placeholder page there.

To start a chapter:

python tools/new_chapter.py 03 maps "Making maps" --lang en

This writes en/03-maps.ipynb with the standard sections and adds the chapter to tutorial.yml. Use --lang id to start in Indonesian, and --qmd for a page without code.

2. The shape of a chapter

Each chapter follows the same order, so readers know where to look:

  1. Front matter: the title, and the glance fields that become the “At a glance” box at the top of the page: what the reader will build, what they need first, how long it takes to run, and the level.
  2. The first heading, matching the title.
  3. Learning objectives, as a short list.
  4. Before running, for any setup the chapter needs.
  5. Numbered sections, each a small step with its code, what to look at in the output, and a checkpoint.
  6. Troubleshooting, as a table of symptom, cause and fix.
  7. What comes next, one or two sentences linking to the next chapter.

3. Boxes from plain text

The boxes on these pages are drawn at render time from a bold lead-in at the start of a paragraph. The notebook itself keeps plain text, so it reads normally in Jupyter and on GitHub. Each lead-in has an English and an Indonesian form:

Lead-in (English) Lead-in (Indonesian) Box
**Learning objectives.** + a list **Tujuan pembelajaran.** note, titled
**At a glance** + a list **Sekilas** tip, titled (or use glance: in the front matter)
**Before running** **Sebelum menjalankan** important
**Slow cell** **Sel lambat** warning
**Look before moving on.** **Lihat dulu sebelum lanjut.** a light box after an output

Before running this page, nothing is needed. This paragraph shows the important box.

Slow cell: this paragraph shows the warning box. Use it on any cell that takes more than a minute or so, with the time it took when you ran it.

Look before moving on. This paragraph shows the light box. Put it after an output, and tell the reader what to check in it before going on.

Quarto’s own callout syntax (::: {.callout-note}, with five types, ledger L2) also works, but the lead-ins keep the notebook readable outside the website.

4. Measure it or cite it

Every number and fact in a chapter comes from one of two places:

  • A cell output in the same chapter. The reader can see it computed.
  • A row of the source ledger, ledger.md at the root of the repository. Each row is either MEASURED, naming the script or run that produced it, or CITED, linking to the primary source with the date it was read. Rows marked VERIFY are not used in chapters until they are confirmed.

In the text, write “ledger” and the row ID, as in ledger L1. The website turns that into a link to the row on the sources page, which tools/prepare.py builds from ledger.md. A fact that is neither measured nor cited is either written as unverified or left out.

Leave out promotional adjectives. Say what a tool does, and let the outputs show how well it does it.

5. Running and saving a notebook

A notebook chapter is committed with its outputs, because the website shows them and does not run any code. Before you commit one:

  • restart the kernel and run every cell from the top, so no output depends on something run earlier and then deleted;
  • keep data out of the repository, apart from small sample files in sample-data/;
  • keep figures small: tools/check_size.py warns when a notebook passes 5 MB and stops the build at 20 MB.

6. Translating a chapter

The language a chapter is written in first is its source; the other version is a translation. When a translation is finished, record which version of the source it was made from:

python tools/translations.py stamp id/03-maps.ipynb

This writes the source’s fingerprint into the translation’s front matter (translated-from:). When the source changes later, in text or code, python tools/translations.py lists the translation as stale, and its page on the website carries a notice that links to the source.

Copy the code cells unchanged. Translate the text, the comments that explain a step, and the labels of figures where it helps the reader. Keep numbers, file names and commands as they are.

Troubleshooting

Symptom Cause Fix
A new chapter does not appear in the menus It is not listed in tutorial.yml Add it, or start chapters with tools/new_chapter.py
A translated page shows an out-of-date notice The source chapter changed after the translation was stamped Update the translation, then stamp it again
The build stops at the size check A notebook is above 20 MB, usually because of large figures Lower the figure size or resolution and run the notebook again

What comes next

Replace the two example chapters with your own, and edit tutorial.yml to match.