In short
A small edition of a famous science lecture about a candle. It shows how to collect the notes at the end, on a page of their own in two columns, instead of at the bottom of each page.
What you'll build
Lecture I of Michael Faraday's The Chemical History of a Candle, set as a small reader's edition on a 156 × 234 mm page. The lecture opens on a field of soot with a lit candle standing on its lower edge, a photograph generated with diffusion models whose black matches the soot, and the text runs in one justified column of Libre Bodoni. Where a book would put footnotes, small raised numbers in dark orange send the reader to the end of the lecture. The notes follow on a page of their own, under a band of soot with the candle snuffed out, in two ragged columns of 8.2 pt type with each bold number hanging in the indent. Three of the six are William Crookes's, from the 1908 impression of his 1861 edition; the other three are new and signed Ed. Both columns end on the same line, and the colophon closes the second.
This recipe answers
- How do I do footnotes?
- How do I set a bibliography or glossary (hanging indent, smaller type)?
- How do I write superscripts, subscripts and chemical formulas without full maths?
The short answer
// Use: buildDocument({ markdown: endnotes(markdown) }, config()), with a 'note' paragraph style.
// [^label] in the text → **^n^**, numbered by first citation; bold, so it takes bodyText.boldColor.
// The [^label]: definitions (one line each) print where the first stood, a 'note' paragraph each:
// '**n** text', never '1. text', which would open a numbered list (gotcha: digit-period-list).
function endnotes(markdown, style = 'note') {
const notes = new Map(); // label → text
const HOLE = '\u0000'; // marks where a definition stood: the first becomes the notes
const text = markdown.replace(/^\[\^([^\]\s]+)\]:[ \t]*(.+)\n?/gm,
(_, label, note) => { notes.set(label, note.trim()); return HOLE; });
const cited = []; // labels in order of first citation
const number = (label) => {
if (!notes.has(label)) throw new Error(`The note [^${label}] has no definition`);
if (!cited.includes(label)) cited.push(label);
return cited.indexOf(label) + 1;
};
// Markers side by side share one superscript, [^a][^b] → **^1,2^**, never 12 raised (note 12).
// A word joiner (U+2060, no width) opens each one: after an italic, '*Royal George*' and the
// bold's ** would make '***', which the parser reads as a bold run, and print the asterisks.
const marked = text.replace(/(?:\[\^[^\]\s]+\])+/g, (run) => {
const numbers = [...run.matchAll(/\[\^([^\]\s]+)\]/g)].map(([, label]) => number(label));
return `**^${numbers.join(',')}^**`;
});
const unused = [...notes.keys()].filter((label) => !cited.includes(label));
if (unused.length) console.warn(`Notes never cited: ${unused.join(', ')}`);
const entries = cited.map((label, i) => `**${i + 1}** ${notes.get(label)}`);
const section = `:::paragraphs{style="${style}"}\n${entries.join('\n\n')}\n:::\n`;
return marked.replace(HOLE, () => section).replaceAll(HOLE, ''); // () =>: '$&' stays text
}
Markdown footnotes become raised markers and a Notes section
Ingredients
- Features
- Superscripts and subscriptsBibliographies and glossariesSection geometryHeading stylesColumn balancingColumn ruleParagraph stylesEscapes and literal charactersBold, italic and their coloursCitations that place figuresNumbered captionsCustom resource typesCaption styleDesigned openersPictures in page designsHeading attributesRunning heads and foliosMirrored marginsSemantic colour palette
- Type
- Libre Bodoni, Besley, Archivo Narrow (SIL OFL 1.1)
- Assets
candle-640.jpgsnuffed-600.jpg- The lit candle on the lecture’s opener, a photograph (Generated With Diffusion Models, original)
- The snuffed candle on the notes’ band, a photograph (Generated With Diffusion Models, original)
- Figure 1, the flame as seen and in section, drawn in code in the page’s palette (Ignacio Ferro, CC BY 4.0)
Method
#1 · Turn footnotes into endnotes before the build
The preprocessor is the short answer above. Postext sets [^1] notes itself (footnotes), at the foot of the citing column or, with placement: 'chapterEnd', after the chapter's last block. Either way they stay in the chapter's column, in the text face, under a short rule. This edition gives the notes a heading, a band and a page in two columns, and a heading style does that only for a heading written in the Markdown. So the pen rewrites the Markdown before it calls buildDocument, and the engine never sees a [^1]. It numbers the markers in the order the text first cites them and sets all the definitions, in that order, where the first of them stands, under ## Notes. Each note starts with **1**, since 1. would open a numbered list. A word joiner (U+2060) opens each marker; without it, the ** of the marker after *Royal George* would meet the italic's closing asterisk as ***, and the asterisks would print.
#2 · Keep bold for the markers
const markers = { boldColor: col('ember'), // **^n^**: a superscript at 58 % of the text size
referenceBold: false }; // [Fig. 1] follows the bold colour, set roman
The preprocessor writes each marker as **^n^**. The carets set the number at 58 % of the text size, raised a third of it (inline formatting), and the bold gives it bodyText.boldColor, which is free because Faraday never uses bold. A 5.8 pt figure needs a contrast of 4.5:1 against the paper, and the flame reaches only 2.6:1, so the markers take the darker ember. The reference [Fig. 1] on page 17 prints in the same colour, in roman, because referenceColor defaults to the bold colour and referenceBold: false drops the bold.
#3 · Set the notes smaller, with the number in the hang
const NOTE = 8.2; // pt: the notes' size, about four fifths of the text
// The hang is a bold number and an en space, measured in the notes' face once the fonts are in:
// a bold 5 matches 2, 3 and 6 within 0.02 em (the 4 is 0.05 em wider, the 1 0.14 em narrower).
function hang(label = '5') { // from ten notes on, pass the widest label: hang('10')
const ctx = new OffscreenCanvas(1, 1).getContext('2d');
const width = (w, s) => { ctx.font = `${w} 100px "${TEXT}"`; return ctx.measureText(s).width; };
return em((width(700, label) + width(400, ' ')) / 100);
}
// Ragged, as justifying would stretch the en space after each number.
const noteStyles = () => [{ id: 'note', fontSize: pt(NOTE), lineHeight: pt(NOTE * 1.3),
textAlign: 'left', hangingIndent: hang() },
// 22 pt above the colophon is copy-fitted: its last line and the first column's share a line.
{ id: 'colophon', fontFamily: LABEL, fontSize: pt(7), lineHeight: pt(9.2), color: col('muted'),
textAlign: 'left', firstLineIndent: pt(0), marginTop: pt(22) },
];
The notes are 8.2 pt on 10.7 pt, about four fifths of the text size. The pen measures the style's hangingIndent in Libre Bodoni once the fonts are loaded: a bold 5 and an en space come to 1.12 em, so the first word of each note lines up with its turnover lines (paragraph styles). With the number out in the hang, the notes need no space between them. They are set ragged because justification would stretch that en space, and a justified 55 mm column of 8.2 pt type runs loose. Tildes set subscripts the way carets set superscripts, as in C~25~H~52~ in note 4 and H~2~O in note 5.
#4 · Give the notes a page in two columns
const BAND = 82; // mm from the trim's top: level with the foot of Figure 1 across the spread
// The band sets the reserve too, but the first grid line clear of it lies 3 mm under the soot:
const BAND_GAP = 5; // mm more of minHeight moves the notes down a line
const notesSection = { id: 'notes', // an opener page: the drop folio, no running heads
breakBefore: { enabled: true, parity: 'any' }, span: 'page', // the next page, either side
layout: { layoutType: 'double', gutterWidth: mm(6), // two columns of about 40 characters
columnRule: { enabled: true, color: col('rule') } }, // and a hairline between them
advancedDesign: { enabled: true, minHeight: mm(BAND - TOP + BAND_GAP), slot: { elements: [
soot(BAND), art('snuffed', 32, 64, BAND, 12), series,
text('kicker', '{attr.kicker}', LABEL, 9.5, 'flame', at('container', 'top-left', 0, 16),
caps(9.5)),
text('title', '{titleText}', DISPLAY, 52, 'wax', below('kicker', 0.5, 80), display),
text('intro', '{attr.intro}', LABEL, 8.4, 'rule', below('title', 2.5, 92),
{ lineHeight: 1.3 }),
] } } };
The heading ## Notes {style="notes" …} opens a section whose pages take the style's layout and design. parity: 'any' starts it on the next page, odd or even, and span: 'page' runs the band across both columns, so it reaches the trim and the second column starts under it (heading styles). The band sets the reserve, but the first grid line clear of it lies only 3 mm under the soot, so BAND_GAP adds 5 mm to minHeight and the notes start a line lower. The hairline between the columns is the columnRule of the style's own layout, so only the pages of the notes draw it. Column balancing is on by default and cuts the closing page so that both columns end on the same line (column balancing).
#5 · Open the lecture on a field of soot
const FIELD = 112; // mm from the trim's top
// The field reaches below the heading, so it sets the reserve (gotcha: opener-reserves-anchored);
// the H1's default bottom margin (0.5 em of 18 pt) rides on it: the text starts a grid line lower.
const opener = { enabled: true,
slot: { elements: [soot(FIELD), art('candle', 40, 100, FIELD, 10), series,
text('kicker', '{attr.kicker}', LABEL, 9.5, 'flame', below('series', 17, 60), caps(9.5)),
text('title', '{titleText}', DISPLAY, 48, 'wax', below('kicker', 1.5, 96), display),
// 86 mm wide, so the subtitle breaks after a dash, never before one
text('subtitle', '{attr.subtitle}', DISPLAY, 11.5, 'rule', below('title', 3.5, 86),
{ fontWeight: 500, italic: true, lineHeight: 1.3 }),
text('byline', '{attr.byline}', LABEL, 7.6, 'rule', below('subtitle', 7, 84), caps(7.6)),
] } };
The field is a box anchored to the page, and the candle, an image element holding a JPEG, stands on its lower edge. The lecture has one column, and a design kept in its column still paints where its elements are anchored, so the field runs from the trim across the top and side margins. The kicker, subtitle and byline come from the heading line, # A Candle {kicker="Lecture I" subtitle="…" byline="…"}, so Lecture II would need no new code. The field reaches below the heading, so it sets the reserve. The heading's default bottom margin of 0.5 em is added to it, and the text starts one grid line below the first line clear of the soot. The subtitle is 86 mm wide so that it breaks after a dash, never before one.
#6 · Use the flame on soot and the ember on paper
const palette = {
ink: '#1f1c19', paper: '#fffdf8', // text and the soot of the fields; a warm white page
flame: '#e08a1e', ember: '#a9560c', // kickers on soot (6:1); markers and numbers (5.1:1)
wax: '#f6efe1', rule: '#d4c6ad', // type on soot; hairlines and small type on soot (10:1)
muted: '#6d6356', blue: '#4f7cae', // running heads, colophon (5.8:1); a flame's blue foot
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the ember, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.ember })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
The flame orange has a contrast of 6:1 on soot but only 2.6:1 on the paper. It colours the kickers on the dark bands and Figure 1; everything small printed in orange on the paper (markers, note numbers, the figure's label) takes the darker ember, at 5.1:1. main-color points at the ember, so a default that the config does not restate follows the design instead of printing blue.
The whole recipe
// ═══ Postext Cookbook · Nº 020 · Endnotes on a page of their own, in two columns ═════
// https://postext.dev/en/cookbook/endnotes-instead-of-footnotes
// Code: MIT · Text: Faraday, ed. Crookes (PD) · Notes, figure: CC BY 4.0 · Photos: diffusion models
// Fonts: Libre Bodoni, Besley, Archivo Narrow (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'endnotes-instead-of-footnotes';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: soot, wax and a flame; the ember is the flame dark enough for small type
const palette = {
ink: '#1f1c19', paper: '#fffdf8', // text and the soot of the fields; a warm white page
flame: '#e08a1e', ember: '#a9560c', // kickers on soot (6:1); markers and numbers (5.1:1)
wax: '#f6efe1', rule: '#d4c6ad', // type on soot; hairlines and small type on soot (10:1)
muted: '#6d6356', blue: '#4f7cae', // running heads, colophon (5.8:1); a flame's blue foot
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the ember, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.ember })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const TEXT = 'Libre Bodoni', DISPLAY = 'Besley', LABEL = 'Archivo Narrow'; // text; titles; labels
const TOP = 22, INNER = 18, OUTER = 22; // mm: margins, and a 116 mm measure of 74 characters
// #region answer: Markdown footnotes become raised markers and a Notes section
// Use: buildDocument({ markdown: endnotes(markdown) }, config()), with a 'note' paragraph style.
// [^label] in the text → **^n^**, numbered by first citation; bold, so it takes bodyText.boldColor.
// The [^label]: definitions (one line each) print where the first stood, a 'note' paragraph each:
// '**n** text', never '1. text', which would open a numbered list (gotcha: digit-period-list).
function endnotes(markdown, style = 'note') {
const notes = new Map(); // label → text
const HOLE = '\u0000'; // marks where a definition stood: the first becomes the notes
const text = markdown.replace(/^\[\^([^\]\s]+)\]:[ \t]*(.+)\n?/gm,
(_, label, note) => { notes.set(label, note.trim()); return HOLE; });
const cited = []; // labels in order of first citation
const number = (label) => {
if (!notes.has(label)) throw new Error(`The note [^${label}] has no definition`);
if (!cited.includes(label)) cited.push(label);
return cited.indexOf(label) + 1;
};
// Markers side by side share one superscript, [^a][^b] → **^1,2^**, never 12 raised (note 12).
// A word joiner (U+2060, no width) opens each one: after an italic, '*Royal George*' and the
// bold's ** would make '***', which the parser reads as a bold run, and print the asterisks.
const marked = text.replace(/(?:\[\^[^\]\s]+\])+/g, (run) => {
const numbers = [...run.matchAll(/\[\^([^\]\s]+)\]/g)].map(([, label]) => number(label));
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
#Let Postext set the notes
Drop endnotes() and the ## Notes heading, put a blank line between the definitions (the engine reads a definition up to the next blank line) and add footnotes: { placement: 'chapterEnd' } to the config. The engine numbers the markers and sets the notes at the end of the lecture, after the colophon, in its one column; Footnotes at the foot of the column that cites them styles notes the engine sets.
#Mark the notes in ink
If the text uses bold for its own emphasis, print the markers in ink, since boldColor colours the markers and every bold word alike.
-const markers = { boldColor: col('ember'), // **^n^**: a superscript at 58 % of the text size
+const markers = { boldColor: col('ink'), // **^n^**: a superscript at 58 % of the text size#Let the note columns run unlevelled
Switch off the balancing of the closing page, and the first column fills to the foot before the second begins.
- headings: { fontFamily: DISPLAY, color: col('ink'), levels: [
+ headings: { fontFamily: DISPLAY, color: col('ink'), balancing: { trailing: false }, levels: [Pitfalls
Pitfall
'1998. ' or '- ' at a paragraph start opens a list
A paragraph that starts with a number, a period and a space, or with a hyphen and a space, becomes a list item. Put a word joiner (U+2060) before the number, and write dialogue with an em dash. Escapes and literal characters →
Pitfall
A 'top' float goes to the page after its citation unless it may head the citing page
By default a float never goes above its own reference, so a page-wide 'top' float cited on page N opens page N+1. Set placement.citingPage: true on the resource or its type's defaultPlacement (or layout.floatsAtCitingPage for the whole book, since postext 1.25) and it heads page N instead, the text above the reference moving down under it, when it fits within layout.maxTopFraction and the citing line stays on that page. It never heads a chapter's first page or a page after a forced break: there it still opens the next page, and 'auto' or 'bottom' can take the foot of the citing page. Figure placement →
Pitfall
An opener reserves height down to its lowest page-anchored element
An advanced-design opener reserves the height of its lowest element, and page- or bleed-anchored elements below the heading count too, so decoration at the foot of the page pushes the text to the next page. Give such an element reserve: false (since postext 1.5): it is painted where it is anchored and reserves nothing. Elements that follow the band itself (anchored to its foot, or 'fill' tall) never set its height. Designed openers →
Pitfall
No <marker> or filters in SVG art (raster fallback)
An SVG figure stays vector in the PDF only without <marker>, filters and masks; otherwise it falls back to a raster, and deeply nested filters can blank it in Chrome. Draw arrowheads as paths. Figures and tables as resources →
Pitfall
Page 1 is a recto: plan pages with physical numbers
Page 1 is a right-hand page and page 2 the first verso, so plan spreads with physical page numbers: an opener on an even page faces the odd page after it. Page and column breaks →
Credits
- Recipe
- Ignacio Ferro
- Text
- The Chemical History of a Candle, Lecture I (abridged), and William Crookes’s notes to it, from the Chatto & Windus impression of 1908 · Michael Faraday; William Crookes · public domain
- The notes signed Ed., the colophon and the drawings · Ignacio Ferro · CC BY 4.0
- Images
- The lit candle on the lecture’s opener, a photograph · Generated With Diffusion Models · original
- The snuffed candle on the notes’ band, a photograph · Generated With Diffusion Models · original
- Figure 1, the flame as seen and in section, drawn in code in the page’s palette · Ignacio Ferro · CC BY 4.0
- Fonts
- Libre Bodoni (SIL OFL 1.1) · Besley (SIL OFL 1.1) · Archivo Narrow (SIL OFL 1.1)
Edit this write-up ↗ (opens in a new tab)Recipe folder on GitHub ↗ (opens in a new tab)


