Skip to main content
Recipe number 19

Cookbook · Chapter 5 · Book structure

Parts in colour from one attribute

A pocket field guide where each :::part sets one palette entry, and its divider, painted verso, thumb tabs, field marks and contents row print in that colour.

pp. 2–3 of 10

  • Trim 150 × 200 mm
  • 1 column
  • Alegreya 10.5/14.5
  • Barlow Condensed
  • Zilla Slab
  • 10 pages
  • Level
  • Postext 1.57.1
  • Laid out in 70 ms
  • 245 lines of code

In short

A pocket guide to estuary birds in two parts, one mud brown and one reed green. It shows how one colour setting per part tints its opening page, page tabs, page numbers and contents line.

What you'll build

Birds of the Estuary is a pocket field guide in two parts, one per habitat. The mudflats print in mud brown and the reedbeds in reed green, and each colour is written once, in the :::part that opens its habitat. Each divider is a full-bleed field with a 150 pt Roman numeral and the habitat's species listed in cream. The back of the leaf is painted to match, with the part's tab in reverse at the fore-edge. On the species pages the thumb tab, the folio, the kicker, the rule under the Latin name, the bold field marks, the bullets and the plate label take the part's colour. The contents open under a strip of the estuary and give each part a band in its colour, with its divider's page number. The cover, the strip and the four plates are paintings generated with diffusion models, loaded as JPEG bitmaps; a part's palette does not reach pictures, so their colours are their own.

This recipe answers

  • How do I divide a book into parts or sections, each with its own colour and divider page?
  • How do I hide running heads on openers and blank pages, or paint a blank verso in the part colour?
  • How do I add a table of contents that updates itself (leaders, page numbers, authors, part rows)?
  • How do I colour key terms (bold or italic) in the body or inside boxes?

The short answer

script.js · lines 53–86in full code
// In the Markdown:  :::part{number="I" title="The \\ Mudflats" palette="band=#8c5e24"}
// Every colour below that is linked to 'band' takes #8c5e24 until the next part.
const parts = { // passed to the config as `parts`
  // A divider opens on a recto by default. The break after it goes to the next recto too, so
  // the back of the leaf stays blank and versoDesign paints it (gotcha: verso-design-breakafter).
  breakAfter: { parity: 'odd' },
  margins: { top: mm(125) }, // the fence's text starts low, under the title
  design: { elements: [ // the divider: its container is the whole trim
    box('field', col('band'), { ...at('top-left', 0, 0, 'bleed'), ...fill }),
    text('part', t({ en: 'Part', es: 'Parte' }), { ...label, fontSize: pt(10),
      letterSpacing: pt(2.4), color: col('paper') },
    at('top-left', MARGIN.inner, MARGIN.top)), // a recto: the inner margin is on the left
    // Roman for the parts, Arabic for the species. {numberRoman} re-formats number="I" (or
    // "1"); the species design prints {number}, from numberingTemplate '{1}'.
    text('numeral', '{numberRoman}', { ...display, fontSize: pt(150), lineHeight: 0.9,
      color: col('paper') }, below('part', 0)),
    // The \\ in the title breaks the line here; the contents and the heads get one line.
    text('title', '{titleText}', { ...display, fontSize: pt(46), lineHeight: 0.98,
      color: col('paper') }, below('numeral', 2, { width: mm(MEASURE) })),
    rule('rule', col('paper'), 1, below('title', 7, { width: mm(14) })),
  ] },
  // The back of the leaf: the same band, edge to edge, the part's tab in reverse at the
  // fore-edge (a verso's is on the left) and its name at the foot.
  versoDesign: { elements: [
    box('field', col('band'), { ...at('top-left', 0, 0, 'bleed'), ...fill }),
    text('tab', '{partNumber}', { ...label, fontSize: pt(9), color: col('band'), align: 'center',
      box: { backgroundColor: col('paper') } }, { ...at('top-left', 0, TAB.y), size: TAB.size }),
    text('name', '{partTitle}', { ...label, fontSize: pt(9), letterSpacing: pt(2.4),
      color: col('paper') }, at('bottom-left', MARGIN.outer, -MARGIN.bottom)),
  ] },
  // The fence's list of species, in the paper colour on the band.
  bodyStyle: { fontSize: pt(11), color: col('paper'), textAlign: 'left', numberColor: col('paper'),
    orderedLists: { fontFamily: 'Barlow Condensed', separator: '', gap: mm(4) } },
};

A divider, its painted verso and its list, all in the part's own 'band'

Ingredients

Type
Alegreya, Zilla Slab, Barlow Condensed (SIL OFL 1.1)
Assets
  • cover-1152.jpg
  • curlew-1200.jpg
  • redshank-1200.jpg
  • reedling-1200.jpg
  • strip-1536.jpg
  • warbler-1200.jpg
  • The cover: a curlew on the estuary mud, reeds in front (Generated With Diffusion Models, original)
  • The contents strip: a curlew and a redshank on the mud, the reedbed beyond (Generated With Diffusion Models, original)
  • Plate 1: Eurasian curlew (Generated With Diffusion Models, original)
  • Plate 2: common redshank (Generated With Diffusion Models, original)
  • Plate 3: bearded reedling (Generated With Diffusion Models, original)
  • Plate 4: common reed warbler (Generated With Diffusion Models, original)

Method

#1 · One palette entry for the parts to override

script.js · lines 16–29in full code
const palette = {
  ink: '#1f2624', // text: a green-tinted near-black
  band: '#3c4b4f', // the house slate, before any part; each :::part brings its own
  paper: '#f6f3ea', // the page, and the type set on a band
  rule: '#d5d1c4', // hairlines
  muted: '#61675f', // running heads, Latin names, the colophon
};
// The paletteId is the link a part's palette="band=#…" follows, in text and designs alike.
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 ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];

A part overrides palette entries by id, so everything that should change with the habitat links to one entry, band. Its own value, a slate, prints only outside the parts (the cover's kicker and the title of the note under the contents), and no other colour in the config has the same hex. col() gives every colour its paletteId, and that id is what a part's override follows, in the text styles and in the design elements alike.

#2 · A divider, its verso and its list

The code for this step is the short answer above. A :::part opens a divider page on a recto, and parts.design lays it out over the whole trim. The Markdown between its fences starts 125 mm from the top edge, under the title: a few lines on the habitat in the habitat paragraph style, then its list of species in bodyStyle. versoDesign paints the back of the leaf (the field, and the tab in reverse) only when that page is blank, and breakAfter: { parity: 'odd' } keeps it blank by sending the first species on to the next recto. {numberRoman} prints the part's number="I" as a Roman numeral ("1" would print the same I), while the species are numbered 1 to 4 in Arabic figures. The \\ in the title breaks the line on the divider only; the contents and the running heads set the title on one line.

#3 · The text's accents follow the part

script.js · lines 90–108in full code
const bodyText = {
  fontFamily: 'Alegreya', fontSize: pt(10.5), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('band'), // the field marks: **Bill**, **Voice.**
  italicColor: col('ink'), referenceColor: col('ink'),
  firstLineIndent: mm(4), indentAfterHeading: false,
  // Tighter than the 0.6–2 defaults. runtMinCharacters counts word spaces, not letters:
  // 45 are about 20 letters of Alegreya (the default 20, about 9); a shorter last line is a runt.
  minWordSpacing: 0.75, maxWordSpacing: 1.7, runtMinCharacters: 45,
};
const unorderedLists = { bulletChar: '•', color: col('band'), // the field marks' bullets
  marginTop: pt(0), marginBottom: pt(0) };
// The plates are numbered with the species, and their label is in the part's colour.
const resourceTypes = [{ id: 'plate', numberingTemplate: '{n}', resetOn: 'never',
  counterFormat: 'decimal', ...t({
    en: { name: 'Plate', namePlural: 'Plates', shortLabel: 'Pl.', captionPrefix: 'Plate' },
    es: { name: 'Lámina', namePlural: 'Láminas', shortLabel: 'Lám.', captionPrefix: 'Lámina' },
  }) }];
const captionStyle = { fontSize: pt(8.5), labelColor: col('band'), descriptionItalic: true,
  gap: mm(1.8) };

From one :::part to the next, every text colour equal to band's own value takes the part's value (the :::part container). Here those are the bold field marks, the bullets and the plate labels. Body text and italics stay in ink. The plates are a resource type of their own, labelled Plate and never reset, so they count 1 to 4 in step with the species.

#4 · The species opener

script.js · lines 112–123in full code
const species = { enabled: true, slot: { elements: [
  text('kicker', '{number} · {attr.status}', { ...label, fontSize: pt(8.5),
    letterSpacing: pt(1.7), color: col('band') }, at('top-left', 0, 1, 'container')),
  // 'cm' is a unit symbol: it keeps its lower case, so the size is not set in capitals.
  text('size', '{attr.size}', { ...label, textTransform: 'none', fontSize: pt(8.5),
    letterSpacing: pt(0.5), color: col('muted') }, at('top-right', 0, 1, 'container')),
  text('name', '{titleText}', { ...display, fontSize: pt(22), lineHeight: 1.05,
    color: col('ink') }, below('kicker', 1.5, { width: 'fill' })),
  text('latin', '{attr.latin}', { fontFamily: 'Alegreya', italic: true, fontSize: pt(11.5),
    color: col('ink') }, below('name', 0.8)),
  rule('rule', col('band'), 0.75, below('latin', 2, { width: mm(MEASURE) })),
] } };

The species opener prints three of the heading's attributes: status in the kicker after the number, size at the right of the kicker and latin under the name. The kicker and the rule under the Latin name link to band, so one design serves both habitats. The heading sets no page break of its own: each species starts after a :::pagebreak, or after the break that follows the part, so its page counts as a body page and prints the running heads. The size is set without the label's capitals, because cm is a unit symbol.

#5 · A band per part in the contents

script.js · lines 127–145in full code
const contents = {
  levels: [{ level: 1, fontFamily: 'Zilla Slab', fontSize: pt(12), fontWeight: 600,
    numberFontFamily: 'Barlow Condensed', numberFontWeight: 600, numberColor: col('muted'),
    numberWidth: mm(5), numberGap: mm(3), marginTop: pt(4) }],
  pageNumber: { fontFamily: 'Barlow Condensed', fontSize: pt(10), fontWeight: 600, width: mm(7) },
  leader: { char: '. ' },
  subtitle: { enabled: true, attr: 'latin', fontFamily: 'Alegreya', fontSize: pt(9.5),
    color: col('muted') }, // the Latin name, in italic by default
  // A part row lays out this design with the part's number, title, page and palette.
  parts: { height: mm(8.5), marginTop: pt(LEAD), design: { elements: [
    box('row', col('band'), { ...at('top-left', 0, 0, 'container'), ...fill }),
    text('part', t({ en: 'Part {number}', es: 'Parte {number}' }), { ...label, fontSize: pt(8),
      letterSpacing: pt(1.6), color: col('paper') }, at('left', 3, 0, 'container')),
    text('title', '{titleText}', { ...display, fontSize: pt(12), color: col('paper') },
      at('left', 20, 0, 'container')),
    text('page', '{pageNumber}', { ...label, fontSize: pt(10), color: col('paper'),
      align: 'right' }, at('right', -2.5, 0, 'container')),
  ] } },
};

:::toc gives every :::part a row and lays toc.parts.design out in it with that part's number, title, divider page and palette (table of contents), so the same box prints brown for the mudflats and green for the reedbeds. Each species row under a band shows its number, a dotted leader and its page, with the Latin name from the latin attribute on the line below.

#6 · The part in the running heads and the tab

script.js · lines 149–162in full code
const head = (id, content, parity, placement, style = {}) => ({ ...text(id, content, { ...label,
  fontSize: pt(8), letterSpacing: pt(1.4), color: col('muted'), overflow: 'clip', ...style },
placement), parity, pages: 'body' }); // dividers are 'part' pages, their versos 'blank'
const folio = { fontSize: pt(9), fontWeight: 700, color: col('band') };
const tab = (parity, edge) => head(`tab-${parity}`, '{partNumber}', parity,
  { ...at(edge, 0, TAB.y), size: TAB.size },
  { fontSize: pt(9), color: col('paper'), align: 'center', box: { backgroundColor: col('band') } });
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', at('top-left', MARGIN.outer, HEAD.y), folio),
  head('verso-title', '{title}', 'even', at('top-left', MARGIN.outer + HEAD.gap, HEAD.y)),
  head('recto-title', '{partTitle}', 'odd', at('top-right', -(MARGIN.outer + HEAD.gap), HEAD.y)),
  head('recto-folio', '{pageNumber}', 'odd', at('top-right', -MARGIN.outer, HEAD.y), folio),
  tab('even', 'top-left'), tab('odd', 'top-right'), // on the fore-edge, left on a verso
] };

With pages: 'body' the heads print on body pages only. The dividers are part pages and their painted versos blank pages; the cover and the contents each open with a page-wide heading, which makes them openers. The verso head prints {title}, the title in the front matter, so the book’s name stays in the Markdown. The recto head prints {partTitle} on one line, and the tab at the fore-edge prints {partNumber} on a box whose background links to band.

The whole recipe

Sandbox
// ═══ Postext Cookbook · Nº 019 · Parts in colour from one attribute ═══════════════════════
// https://postext.dev/en/cookbook/parts-in-colour
// Code: MIT · Text: original (CC BY 4.0) · Pictures: diffusion models
// Fonts: Alegreya, Zilla Slab, Barlow Condensed (SIL OFL 1.1) · Needs postext ≥ 1.57.0
// A pocket field guide to two habitats. Each :::part names its own 'band' colour, and every
// colour linked to 'band' takes it: the divider and its verso, the tab, the field marks.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'parts-in-colour';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: 'band' is the entry the parts override; the others keep their value
const palette = {
  ink: '#1f2624', // text: a green-tinted near-black
  band: '#3c4b4f', // the house slate, before any part; each :::part brings its own
  paper: '#f6f3ea', // the page, and the type set on a band
  rule: '#d5d1c4', // hairlines
  muted: '#61675f', // running heads, Latin names, the colophon
};
// The paletteId is the link a part's palette="band=#…" follows, in text and designs alike.
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 ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
// #endregion
const TRIM = { width: 150, height: 200 }; // a pocket guide
const MARGIN = { top: 22, bottom: 20, inner: 19, outer: 21 }; // mirrored; room for a thumb
const MEASURE = TRIM.width - MARGIN.inner - MARGIN.outer; // 110 mm, about 70 characters
const LEAD = 14.5; // body leading in pt: the baseline grid
const STRIP = 44; // the contents' picture strip, from the top edge (mm)
const HEAD = { y: 12, gap: 8 }; // running heads from the top edge; folio to title (mm)
const TAB = { y: 26, size: { width: mm(8), height: mm(24) } }; // the thumb tab, at the fore-edge
const TITLE_DROP = 6; // mm from the foot of the contents strip to the title's box
const BOOK = t({ en: 'Birds of the Estuary', es: 'Aves del estuario' });
const label = { fontFamily: 'Barlow Condensed', fontWeight: 600, textTransform: 'uppercase' };
const display = { fontFamily: 'Zilla Slab', fontWeight: 700 };
const at = (edge, x, y, to = 'page') => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const below = (id, y, size) => ({ ...at('below', 0, y, `#${id}`), ...(size && { size }) });
const fill = { size: { width: 'fill', height: 'fill' } };
const text = (id, content, style, placement) => ({ kind: 'text', id, content, overflow: 'wrap',
  align: 'left', ...style, placement });
const box = (id, color, placement) => ({ kind: 'box', id, style: { backgroundColor: color },
  placement });

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

#Add a third habitat

A fence before the first saltmarsh species adds a third colour, with its divider, painted verso and contents row, and the config stays as it is. Drop the note under the contents as well, because the third band and its species need the space.

+:::part{number="III" title="The \\ Saltmarsh" palette="band=#6b4a7a"}
+:::
+
+# Common Shelduck {latin="Tadorna tadorna" size="58–67 cm" status="Resident"}

#Let the species face the divider

Without the break to the next recto, the first species starts on the back of the divider, so no blank verso is left for versoDesign to paint.

-  breakAfter: { parity: 'odd' },

Pitfalls

Pitfall

parts.versoDesign needs parts.breakAfter parity 'odd'

The back of a part divider is painted only when parts.breakAfter { enabled: true, parity: 'odd' } leaves that verso blank. Part divider pages →

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 →

  • A part recolours the text by comparing values: any text colour equal to band's own value switches with the part, even one linked to another entry or to none. Keep that hex for band alone.
  • The tab prints {partNumber}, which is empty outside a part, so a body page before the first :::part gets an empty slate tab. Keep every body page inside a part.
  • Pictures and inline swatches keep the colours written in them. The plates here take their habitat's colours from HABITAT in the code, so a new colour in a :::part has to go there too.

Credits

Text
Original prose, CC BY 4.0
Images
  • The cover: a curlew on the estuary mud, reeds in front · Generated With Diffusion Models · original
  • The contents strip: a curlew and a redshank on the mud, the reedbed beyond · Generated With Diffusion Models · original
  • Plate 1: Eurasian curlew · Generated With Diffusion Models · original
  • Plate 2: common redshank · Generated With Diffusion Models · original
  • Plate 3: bearded reedling · Generated With Diffusion Models · original
  • Plate 4: common reed warbler · Generated With Diffusion Models · original
Fonts
Alegreya (SIL OFL 1.1) · Zilla Slab (SIL OFL 1.1) · Barlow Condensed (SIL OFL 1.1)
Sandbox