In short
Two chapters of a physiology textbook with an index at the back. Words are marked in the text, and the book finds their page numbers by itself, so none are typed by hand.
What you'll build
Part IV of Principles of Human Physiology, a textbook on a 210 × 277 mm page: chapter 14, The heart as a pump, and chapter 15, Vessels and blood pressure, open on rectos under a red band and set their clinical notes and captions in an outer channel. The part closes with an index of about ninety entries and thirty sub-entries in two columns, the way a medical textbook sets one: lower-case entries, letter heads in the accent, sub-entries one em in, the defining page in bold, see and see also in italic. No page number in it was typed. Each term is marked in the chapter where the text explains it, and the index reads the pages those marks landed on, folios 297 to 304.
This recipe answers
- How do I build a back-of-book index whose page numbers change by themselves when the text moves?
The short answer
// The chapters mark each term where the text discusses it:
// the :index[stroke volume]{main} prints the words, files them, bold page
// mitral:index{term="heart!valves!mitral"} prints nothing, files the word before it
// ## Venous return :index{term="venous return" range="start" main} … range="end"
// :index{term="inotropy" see="contractility"} a cross-reference, no page
// The last chapter is `# Index {style="index"}` and `:::index`. buildBundle hands it every
// chapter's marks with the pages they landed on, and lays the book out again until those
// pages stop moving.
const indexStyle = {
id: 'index', numbered: false, toc: false, // not counted: no chapter 16
layout: { layoutType: 'double', gutterWidth: mm(6) }, // two columns in this section only
advancedDesign: opener(INDEX_BAND), // the chapters' band, shallower and with no number
header: runningHeads(t({ en: 'Index', es: 'Índice analítico' })),
};
const index = {
fontFamily: SERIF, fontSize: pt(8.6), lineHeight: pt(11.4),
indent: em(1), turnoverIndent: em(2), // sub-entries step in 1 em; wrapped lines hang 2 em
// English: Chicago's short ranges (301–3; 298–300 keeps the digit that changes). Spanish
// writes both numbers in full and joins them with a hyphen (301-303).
rangeFormat: t({ en: 'chicago', es: 'full' }), rangeSeparator: t({ en: '–', es: '-' }),
main: { bold: true }, // the defining page in bold
see: { italic: true }, // See / See also, Véase / Véase también by the document's locale
groups: { ...label, fontSize: pt(9.5), letterSpacing: pt(0), color: col('accent') },
};
Marks in the chapters, and an index chapter set in two columns
Ingredients
- Features
- Back-of-book indexBooks built chapter by chapterHeading stylesDesigned openersHeading attributesNumbered headingsRunning heads and foliosHeads by page roleMirrored marginsMargin column for floatsMargin notesSide captionsUnnumbered chaptersFloated boxesCallout boxesNumbered captionsPDF export
- Also uses
- Column and a halfCitations that place figuresFigure and Table in your languageFull-width chapter bandParagraph stylesFonts embedded in the PDFCustom resource typesFigures and tables as resourcesRoman front matterSection geometryRunning heads per sectionSuperscripts and subscripts
- Type
- Literata, Libre Franklin (SIL OFL 1.1)
- Assets
- None: every picture is drawn in code
Method
#1 · Mark the passage, not the word
// The chapters mark each term where the text discusses it:
// the :index[stroke volume]{main} prints the words, files them, bold page
// mitral:index{term="heart!valves!mitral"} prints nothing, files the word before it
// ## Venous return :index{term="venous return" range="start" main} … range="end"
// :index{term="inotropy" see="contractility"} a cross-reference, no page
// The last chapter is `# Index {style="index"}` and `:::index`. buildBundle hands it every
// chapter's marks with the pages they landed on, and lays the book out again until those
// pages stop moving.
const indexStyle = {
id: 'index', numbered: false, toc: false, // not counted: no chapter 16
layout: { layoutType: 'double', gutterWidth: mm(6) }, // two columns in this section only
advancedDesign: opener(INDEX_BAND), // the chapters' band, shallower and with no number
header: runningHeads(t({ en: 'Index', es: 'Índice analítico' })),
};
const index = {
fontFamily: SERIF, fontSize: pt(8.6), lineHeight: pt(11.4),
indent: em(1), turnoverIndent: em(2), // sub-entries step in 1 em; wrapped lines hang 2 em
// English: Chicago's short ranges (301–3; 298–300 keeps the digit that changes). Spanish
// writes both numbers in full and joins them with a hyphen (301-303).
rangeFormat: t({ en: 'chicago', es: 'full' }), rangeSeparator: t({ en: '–', es: '-' }),
main: { bold: true }, // the defining page in bold
see: { italic: true }, // See / See also, Véase / Véase también by the document's locale
groups: { ...label, fontSize: pt(9.5), letterSpacing: pt(0), color: col('accent') },
};
An indexer marks the places where a term is explained, not every place the word appears. The two chapters use stroke volume twelve times, and the index sends the reader to two places: page 298, where the term is defined, and 299–300, where the text explains what regulates it. A visible mark, :index[cardiac output]{main}, prints its words and files them. An invisible one, mitral:index{term="heart!valves!mitral"}, prints nothing and takes the page of the word before it, which is how the passage on the four valves files mitral, tricuspid, aortic and pulmonary under heart without rewording the sentence. The ! separates levels, so heart gets three: valves, then each valve. A mark also works in a heading (the ranges of 14.1 and 15.6 start there) and in a callout (heart failure is defined in the side note on page 300).
#2 · Main pages, ranges and cross-references
// The chapters mark each term where the text discusses it:
// the :index[stroke volume]{main} prints the words, files them, bold page
// mitral:index{term="heart!valves!mitral"} prints nothing, files the word before it
// ## Venous return :index{term="venous return" range="start" main} … range="end"
// :index{term="inotropy" see="contractility"} a cross-reference, no page
// The last chapter is `# Index {style="index"}` and `:::index`. buildBundle hands it every
// chapter's marks with the pages they landed on, and lays the book out again until those
// pages stop moving.
const indexStyle = {
id: 'index', numbered: false, toc: false, // not counted: no chapter 16
layout: { layoutType: 'double', gutterWidth: mm(6) }, // two columns in this section only
advancedDesign: opener(INDEX_BAND), // the chapters' band, shallower and with no number
header: runningHeads(t({ en: 'Index', es: 'Índice analítico' })),
};
const index = {
fontFamily: SERIF, fontSize: pt(8.6), lineHeight: pt(11.4),
indent: em(1), turnoverIndent: em(2), // sub-entries step in 1 em; wrapped lines hang 2 em
// English: Chicago's short ranges (301–3; 298–300 keeps the digit that changes). Spanish
// writes both numbers in full and joins them with a hyphen (301-303).
rangeFormat: t({ en: 'chicago', es: 'full' }), rangeSeparator: t({ en: '–', es: '-' }),
main: { bold: true }, // the defining page in bold
see: { italic: true }, // See / See also, Véase / Véase también by the document's locale
groups: { ...label, fontSize: pt(9.5), letterSpacing: pt(0), color: col('accent') },
};
main sets the page that defines a term in bold, so a student looking up preload goes to page 299 first. range="start" and range="end", with the same term, bound a discussion that runs over a page break: stroke volume, regulation, 299–300, baroreceptor reflex, 303–4. A main on the start makes the whole range bold. A page marked again inside its own range folds into it, and a main there makes the range bold. see stands in for the page numbers (inotropy. See contractility), and seealso follows them (heart failure, 300. See also ejection fraction). A target with levels is written with ! and printed with a colon: See blood pressure: mean arterial.
#3 · An index chapter in two columns
// The chapters mark each term where the text discusses it:
// the :index[stroke volume]{main} prints the words, files them, bold page
// mitral:index{term="heart!valves!mitral"} prints nothing, files the word before it
// ## Venous return :index{term="venous return" range="start" main} … range="end"
// :index{term="inotropy" see="contractility"} a cross-reference, no page
// The last chapter is `# Index {style="index"}` and `:::index`. buildBundle hands it every
// chapter's marks with the pages they landed on, and lays the book out again until those
// pages stop moving.
const indexStyle = {
id: 'index', numbered: false, toc: false, // not counted: no chapter 16
layout: { layoutType: 'double', gutterWidth: mm(6) }, // two columns in this section only
advancedDesign: opener(INDEX_BAND), // the chapters' band, shallower and with no number
header: runningHeads(t({ en: 'Index', es: 'Índice analítico' })),
};
const index = {
fontFamily: SERIF, fontSize: pt(8.6), lineHeight: pt(11.4),
indent: em(1), turnoverIndent: em(2), // sub-entries step in 1 em; wrapped lines hang 2 em
// English: Chicago's short ranges (301–3; 298–300 keeps the digit that changes). Spanish
// writes both numbers in full and joins them with a hyphen (301-303).
rangeFormat: t({ en: 'chicago', es: 'full' }), rangeSeparator: t({ en: '–', es: '-' }),
main: { bold: true }, // the defining page in bold
see: { italic: true }, // See / See also, Véase / Véase también by the document's locale
groups: { ...label, fontSize: pt(9.5), letterSpacing: pt(0), color: col('accent') },
};
The index is the last Markdown document of the book, # Index {style="index"} followed by :::index. The heading style swaps the column and a half of the chapters for two columns 6 mm apart, only in its own section, and gives the index its own opener and running head. The index is set in the text face at 8.6/11.4 pt, one em per sub-level, and a turnover line hangs two ems, so a wrapped line never lines up with a sub-entry. rangeFormat: 'chicago' drops the digits a range repeats (303–4, but 299–300). The Spanish edition writes ranges in full with a hyphen (303-304), the usual practice in Spanish books.
#4 · Why the numbers follow the text
const text = chapters.map((chapter) => chapter.markdown).join('\n');
await loadSvg('pv-loop.svg', pvLoop());
await prepareFonts(text, config, kitFonts(FONTS));
const docs = await withLoadedFonts(() => buildBundle({ chapters, config, resources }),
{ ...kitFonts(FONTS), text });
showPages(docs, { title: t({ en: 'Principles of Human Physiology',
es: 'Principios de fisiología humana' }) });
offerPdf(() => renderToPdf(docs, { fontProvider: fontsourceProvider, resourceBytes: imageBytes }),
`${RECIPE}.pdf`);
buildBundle lays out the chapters in order and finds that the third one holds :::index. After the first pass it collects every mark of every chapter with the page it landed on, and hands that list to the index chapter as its outline. Then it lays the book out again, up to three passes, until no page number moves. Rewrite a paragraph of chapter 14, change the trim or the body size, and the index reprints with the new pages. In the PDF each page number links to its page.
#5 · Openers and running heads
function opener(band = BAND, kicker = '') {
const onBand = { color: col('paper'), align: 'left', overflow: 'wrap' }; // wrap: never '…'
const inBand = band - MARGIN.top - 8; // the title's box: it stands on a line 8 mm above the foot
return { enabled: true, minHeight: mm(inBand + 8 + 31), slot: { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('accent') },
placement: { ...at('bleed', 'top-left', 0, 0), size: { height: mm(band) } } },
{ kind: 'text', id: 'kicker', content: kicker, ...label, ...onBand, fontSize: pt(8.5),
placement: at('container', 'top-left', 0, -6) },
{ kind: 'text', id: 'title', content: '{titleText}', ...onBand, fontFamily: SANS,
fontWeight: 800, fontSize: pt(30), lineHeight: 1.05, // a multiple, never pt()
verticalAlign: 'bottom', // one line or two, the title sits on the same line
placement: { ...at('container', 'top-left', 0, 0),
size: { width: mm(170), height: mm(inBand) } } },
{ kind: 'text', id: 'lead', content: '{attr.lead}', fontFamily: SERIF, italic: true,
fontSize: pt(10.5), lineHeight: 1.4, color: col('ink'), align: 'left', overflow: 'wrap',
inlineMarks: true, // *See* in the index's lead
placement: { ...at('container', 'top-left', 0, inBand + 15),
size: { width: mm(118) } } },
] } };
}
The chapters and the index share one opener: a band from the trim to 62 mm (46 mm for the index), the title set on its foot by a bottom-aligned box, so a two-line title and a one-line title end on the same line. The kicker prints {chapterNumber}, which the heading attribute {startAt=14} sets to 14, and page.pageNumbering.startAt: 297 places the part where it would sit in the book. The index style is numbered: false, so it does not become chapter 16.
The whole recipe
// ═══ Postext Cookbook · Nº 072 · A textbook index that follows the text ═══════════════
// https://postext.dev/en/cookbook/back-of-book-index
// Code: MIT · Text: original (CC BY 4.0) · Drawing: generated in code (CC BY 4.0)
// Fonts: Literata, Libre Franklin (SIL OFL 1.1) · Needs postext ≥ 1.28.0
// Two chapters of a physiology textbook and the index that closes them, laid out by
// buildBundle as one book: the terms are marked where the text discusses them, and the
// index chapter prints them with the pages they land on.
import {
buildBundle, prepareFonts, withLoadedFonts, renderPageToCanvas, registerResourceImage,
defaultResourceTypes,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'back-of-book-index';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // every colour in the config links to one of these ids
ink: '#1f1a1c', // text: a warm near-black
accent: '#9e1b32', // the one accent (7.4:1 on paper): bands, letter heads, numbers, labels
tint: '#f7ebe9', // the loop in Figure 14.1
rule: '#d9c6c3', // hairlines
muted: '#6b5f61', // running heads, notes, the colophon
paper: '#ffffff', // type on the bands
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
// The engine's defaults link to 'main-color': point it at the accent, so nothing prints blue.
{ id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// A textbook trim, mirrored, in mm. The body is a column and a half: text in the main column,
// figures' captions and clinical notes in the outer channel.
const TRIM = { width: 210, height: 277 };
const MARGIN = { top: 24, bottom: 22, inner: 20, outer: 14 };
const LEAD = 13.6; // body leading in pt: the baseline grid
const [SERIF, SANS] = ['Literata', 'Libre Franklin'];
const [BAND, INDEX_BAND] = [62, 46]; // mm from the trim to the foot of the opener bands
const HEAD_Y = 13; // mm from the top trim to the running heads
const at = (to, edge, x, y) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const label = { fontFamily: SANS, fontSize: pt(7.5), fontWeight: 700, letterSpacing: pt(1.3),
textTransform: 'uppercase' };
// #region answer: marks in the chapters, and an index chapter set in two columns
// The chapters mark each term where the text discusses it:
// the :index[stroke volume]{main} prints the words, files them, bold page
// mitral:index{term="heart!valves!mitral"} prints nothing, files the word before it
// ## Venous return :index{term="venous return" range="start" main} … range="end"
The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)
Variations
#Write every range in full
Some house styles print both numbers of a range in full.
- rangeFormat: t({ en: 'chicago', es: 'full' }), rangeSeparator: t({ en: '–', es: '-' }),
+ rangeFormat: 'full', rangeSeparator: t({ en: '–', es: '-' }),#An index without letter heads
A short index often runs its groups together, separated only by a blank line.
- groups: { ...label, fontSize: pt(9.5), letterSpacing: pt(0), color: col('accent') },
+ groups: { enabled: false },Pitfalls
- The capture of this recipe does not report
indexSeeUnknown: a cross-reference whose target is spelled differently from the entry (Reynolds number against number, Reynolds) prints without complaint. Read every See in the finished index against its target. - A sub-entry list that breaks across columns does not repeat its main heading as a heart (cont.) line; an entry with no page of its own always stays with its first sub-entry, so a heading never ends a column alone.
Credits
- Recipe
- Ignacio Ferro
- Text
- Original prose, CC BY 4.0
- Fonts
- Literata (SIL OFL 1.1) · Libre Franklin (SIL OFL 1.1)
Edit this write-up ↗ (opens in a new tab)Recipe folder on GitHub ↗ (opens in a new tab)


