Kurz gesagt
Ein kurzes Handbuch zur Auslegung von Solarmodulen und Akkus für eine Hütte. Verweist Kapitel 3 auf eine Tabelle in Kapitel 1 oder Kapitel 1 voraus auf einen Abschnitt in Kapitel 2, stehen dort die richtige Nummer und Seite, als Links.
Was Sie setzen
Ein kurzes technisches Handbuch, Power for a Cabin, gesetzt in IBM Plex auf einer Seite von 178 × 233 mm: ein Umschlag mit im Code gezeichneten Sonnenbahnen, eine Inhaltsseite und drei Kapitel unter einem nachtblauen Band. Das Buch legt eine kleine Solaranlage aus, also stützt sich jedes Kapitel auf die anderen. Kapitel 3 zitiert die Lasttabelle aus Kapitel 1 und das Sonnenstundendiagramm aus Kapitel 2; Kapitel 1 verweist den Leser voraus auf Abschnitt 2.3 und gibt dessen Seite an. Der Autor schreibt @tbl:loads und @sec:array-size, wie pandoc-crossref sie erwartet, in vier getrennten Markdown-Dateien. buildBundle setzt sie als ein Buch, sodass jeder Verweis die Nummer ausgibt, die das Ziel in seinem eigenen Kapitel erhalten hat, die Seite, auf der es steht, und einen Link darauf.
Dieses Rezept beantwortet
- Wie verweise ich im Stil von pandoc-crossref auf eine Abbildung, eine Tabelle oder einen Abschnitt in einem anderen Kapitel und halte die Nummern richtig?
- Wie verweise ich auf einen Abschnitt und die Seite, auf der er steht, und halte beides richtig, wenn sich das Buch ändert?
Die kurze Antwort
// buildBundle lays the chapters out in order. Each chapter whose text names a target it
// does not hold (`@sec:array-size` in chapter 1, `@tbl:loads` in chapter 3) is laid out
// with the outline of the whole book, so the reference prints "section 2.3" and its page,
// and links to it. Headings carry their ids in the Markdown, `## Sizing the array
// {#sec:array-size}`; figures and tables are resources whose ids are what follows the @.
const book = () => buildBundle({ chapters, config: config(), resources });
const crossRefs = {
chapter: t({ en: 'chapter {n}', es: 'capítulo {n}' }), // @sec:array → "chapter 2"
section: t({ en: 'section {n}', es: 'apartado {n}' }), // @sec:losses → "section 2.2"
page: t({ en: 'p. {n}', es: 'pág. {n}' }), // :ref{id="sec:losses" style=page} → "p. 7"
};
// Figures and tables count per chapter ({h1}.{n}) and carry on from one document to the
// next. A reference prints the type's shortLabel: @fig:sun → "Fig. 2.1", @Tbl:loads (capital
// T, at the start of a sentence) → "Table 1.1", [-@tbl:loads] → the bare "1.1".
const resourceTypes = defaultResourceTypes(LANG).map((type) => ({ ...type,
shortLabel: type.id === 'table' ? t({ en: 'table', es: 'tabla' })
: t({ en: 'fig.', es: 'figura' }),
...(type.id === 'table' && { captionStyle: { position: 'above' } }) }));
Zutaten
- Funktionen
- QuerverweiseNummerierte BildunterschriftenKapitel für Kapitel gebaute BücherNummerierte ÜberschriftenInhaltsverzeichnisGestaltete AuftakteÜberschriftenattributeÜberschriftenformateKapitel ohne NummerKolumnentitel und SeitenzahlenUmschläge, Titelseiten und ImpressumBilder in SeitengestaltungenGespiegelte RänderSemantische FarbpaletteAbsatzformatePDF-Export
- Nutzt außerdem
- Zitate in einem ZitierstilVerweise, die Abbildungen platzierenAbbildung und Tabelle in Ihrer SpracheKopfzeilen nach SeitenrolleIm PDF eingebettete SchriftenEigene RessourcentypenAbbildungen und Tabellen als RessourcenKolumnentitel je AbschnittTabellen aus Daten
- Schrift
- IBM Plex Serif, IBM Plex Sans Condensed, IBM Plex Mono (SIL OFL 1.1)
- Dateien
- Keine: Jedes Bild wird im Code gezeichnet
Zubereitung
#1 · Jedes Ziel einmal benennen, für das ganze Buch
Der Code steht in der kurzen Antwort oben. Überschriften erhalten einen Pandoc-Bezeichner mit seinem Präfix, ## Sizing the array {#sec:array-size}. Abbildungen und Tabellen sind im Skript deklarierte Ressourcen, und ihre IDs werden genauso geschrieben, fig:sun und tbl:loads, sodass der Text @fig:sun lautet, ob die Abbildung zwei Absätze entfernt ist oder zwei Kapitel zurückliegt. Eine ID muss im ganzen Buch eindeutig sein, nicht nur in ihrem Kapitel.
#2 · Die Kapitel als ein Buch setzen
// buildBundle lays the chapters out in order. Each chapter whose text names a target it
// does not hold (`@sec:array-size` in chapter 1, `@tbl:loads` in chapter 3) is laid out
// with the outline of the whole book, so the reference prints "section 2.3" and its page,
// and links to it. Headings carry their ids in the Markdown, `## Sizing the array
// {#sec:array-size}`; figures and tables are resources whose ids are what follows the @.
const book = () => buildBundle({ chapters, config: config(), resources });
const crossRefs = {
chapter: t({ en: 'chapter {n}', es: 'capítulo {n}' }), // @sec:array → "chapter 2"
section: t({ en: 'section {n}', es: 'apartado {n}' }), // @sec:losses → "section 2.2"
page: t({ en: 'p. {n}', es: 'pág. {n}' }), // :ref{id="sec:losses" style=page} → "p. 7"
};
// Figures and tables count per chapter ({h1}.{n}) and carry on from one document to the
// next. A reference prints the type's shortLabel: @fig:sun → "Fig. 2.1", @Tbl:loads (capital
// T, at the start of a sentence) → "Table 1.1", [-@tbl:loads] → the bare "1.1".
const resourceTypes = defaultResourceTypes(LANG).map((type) => ({ ...type,
shortLabel: type.id === 'table' ? t({ en: 'table', es: 'tabla' })
: t({ en: 'fig.', es: 'figura' }),
...(type.id === 'table' && { captionStyle: { position: 'above' } }) }));
buildBundle führt die Zähler für Abbildungen und Tabellen von Kapitel zu Kapitel fort, sodass die erste Tabelle von Kapitel 3 die 3.1 ist und @tbl:loads in Kapitel 3 weiterhin table 1.1 ausgibt. Ein Kapitel, dessen Text auf eine Überschrift verweist, die es nicht enthält, wird mit der Gliederung des ganzen Buchs gesetzt und erneut gesetzt (höchstens drei Runden), bis sich die Seiten dieser Gliederung nicht mehr verschieben. So kann Kapitel 1 section 2.1 … p. 6 ausgeben, bevor Kapitel 2 auf Papier existiert.
#3 · Die Wörter wählen, die ein Verweis druckt
crossRefs legt chapter, section und p. fest (im Spanischen capítulo, apartado und pág.). Ein Verweis auf eine Abbildung oder Tabelle gibt das shortLabel ihres Typs aus, hier kleingeschrieben, wie pandoc-crossref sie setzt, fig. 2.1 und table 1.1. Ein Großbuchstabe im Präfix, @Tbl:loads oder @Sec:array, schreibt die Bezeichnung am Satzanfang groß, und [-@tbl:losses] gibt die bloße Nummer aus für tables 2.1 and 3.1.
#4 · Voraus auf Abschnitte verweisen, zurück auf Abbildungen
Eine Abbildung oder Tabelle wird dort nummeriert und platziert, wo sie zum ersten Mal zitiert wird. @fig:soc in Kapitel 1 machte sie zu Abbildung 1.2 und setzte sie in Kapitel 1. Deshalb verweist Kapitel 1 voraus auf @sec:autonomy, und die Abbildungen und Tabellen werden zuerst in ihrem eigenen Kapitel zitiert; jedes spätere Kapitel verweist frei auf sie zurück. Eine Seitenangabe geht mit dem Abschnitt, :ref{id="sec:sun-hours" style=page}: Ein Verweis auf eine Ressource gibt ihre Bezeichnung und ihre Nummer aus, nie ihre Seite.
#5 · Auftaktseiten und Umschlag kommen aus demselben Band
const BAND = 52; // mm from the trim's top
const opener = { enabled: true, minHeight: mm(54), slot: { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('night') },
placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: mm(BAND + 3) } } },
{ kind: 'text', id: 'kicker', content: t({ en: 'Chapter', es: 'Capítulo' }), ...caps(8.5),
color: col('sun'), placement: at('container', 'top-left', 0, 2) },
{ kind: 'text', id: 'title', content: '{titleText}', fontFamily: COND, fontWeight: 600,
fontSize: pt(30), lineHeight: 1.05, color: col('paper'), align: 'left', overflow: 'wrap',
placement: { ...at('#kicker', 'below', 0, 3), size: { width: mm(MEASURE - 26) } } },
{ kind: 'text', id: 'number', content: '{chapterNumber}', fontFamily: COND, fontWeight: 600,
fontSize: pt(84), lineHeight: 1, color: col('sun'), align: 'right',
placement: { ...at('container', 'top-right', 0, -6), size: { width: mm(40) } } },
{ kind: 'text', id: 'lead', content: '{attr.lead}', fontFamily: SERIF, italic: true,
fontSize: pt(11), lineHeight: 1.38, color: col('ink'), align: 'left', overflow: 'wrap',
placement: { ...at('container', 'top-left', 0, BAND - MARGIN.top + 8),
size: { width: mm(MEASURE - 10) } } },
] } };
// The contents page wears the same band, with the book's subtitle as its kicker.
const contentsOpener = { ...opener, minHeight: mm(BAND - MARGIN.top + 4), slot: { elements:
opener.slot.elements.filter((e) => ['band', 'kicker', 'title'].includes(e.id))
.map((e) => (e.id === 'kicker' ? { ...e, content: '{subtitle}' } : e)) } };
Das Band reicht in jedem Kapitel und auf der Inhaltsseite, die dieselben Elemente wiederverwenden, 55 mm von der Oberkante des Endformats herab; die Kapitelnummer ist {chapterNumber}, der Vorspann ein Attribut der Überschrift.
const COVER_BAND = 168; // mm
const cover = { enabled: true, slot: { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('night') },
placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: mm(COVER_BAND) } } },
{ kind: 'image', id: 'art', resourceId: 'cover',
placement: { ...at('bleed', 'top-left'), size: { width: 'fill' } } },
// Stacked upwards from the subtitle, so a title of one line or two keeps its distance.
{ kind: 'text', id: 'subtitle', content: '{subtitle}', fontFamily: SERIF, italic: true,
fontSize: pt(14), color: col('tint'), align: 'left',
placement: at('page', 'top-left', MARGIN.inner, COVER_BAND - 22) },
{ kind: 'text', id: 'title', content: '{titleText}', fontFamily: COND, fontWeight: 600,
fontSize: pt(52), lineHeight: 1, color: col('paper'), align: 'left', overflow: 'wrap',
placement: { ...at('#subtitle', 'above', 0, -4), size: { width: mm(140) } } },
{ kind: 'text', id: 'kicker', content: '{attr.kicker}', ...caps(8.5), color: col('sun'),
placement: at('#title', 'above', 0, -4) },
{ kind: 'text', id: 'author', content: '{author}', fontFamily: COND, fontWeight: 600,
fontSize: pt(13), color: col('ink'), align: 'left',
placement: at('page', 'top-left', MARGIN.inner, COVER_BAND + 14) },
{ kind: 'text', id: 'edition', content: '{attr.edition}', ...caps(7.5), color: col('muted'),
placement: at('#author', 'below', 0, 2.5) },
] } };
Das ganze Rezept
// ═══ Postext Cookbook · Nº 094 · A technical book whose references cross chapters ═══
// https://postext.dev/en/cookbook/technical-book-crossref-chapters
// Code: MIT · Text: original (CC BY 4.0) · Charts: generated in code (CC BY 4.0)
// Fonts: IBM Plex Serif, Sans Condensed and Mono (SIL OFL 1.1) · Needs postext ≥ 1.28.0
// A small handbook in four Markdown documents. The text writes @fig:sun, @tbl:loads and
// @sec:array-size the way pandoc-crossref reads them, and buildBundle resolves each one to
// the right number, title or page wherever in the book its target lies.
import {
buildBundle, prepareFonts, withLoadedFonts, renderPageToCanvas, registerResourceImage,
defaultResourceTypes, parseTSV, setAlignment, inlineSvgFonts,
} 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 = 'technical-book-crossref-chapters';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: a night-blue band, one burnt-orange accent, an amber for the charts
const palette = {
ink: '#1c1f24', // text: a cool near-black
night: '#1f3247', // openers, the cover, table heads
accent: '#a8471a', // numbers, kickers, references (5.6:1 on paper)
sun: '#e9a33a', // the charts and the cover only, never text on paper
tint: '#f5ede3', // daylight in the charts
rule: '#cfc7bc', // hairlines
muted: '#5e636a', // running heads, colophon, chart labels
paper: '#ffffff',
};
// Each colour names its palette entry and carries its hex.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries({ ...palette, 'main-color': palette.accent })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const SERIF = 'IBM Plex Serif', COND = 'IBM Plex Sans Condensed', MONO = 'IBM Plex Mono';
const TRIM = { w: 178, h: 233 }; // mm: a technical-book trim
const MARGIN = { top: 22, bottom: 22, inner: 20, outer: 32 }; // a 126 mm measure
const LEAD = 13.8; // pt: the body leading
const MEASURE = TRIM.w - MARGIN.inner - MARGIN.outer;
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const caps = (size, extra = {}) => ({ fontFamily: MONO, fontSize: pt(size), fontWeight: 600,
letterSpacing: pt(size * 0.14), textTransform: 'uppercase', align: 'left', ...extra });
// #region answer: one set of identifiers for the whole book, resolved across chapters
// buildBundle lays the chapters out in order. Each chapter whose text names a target it
// does not hold (`@sec:array-size` in chapter 1, `@tbl:loads` in chapter 3) is laid out
// with the outline of the whole book, so the reference prints "section 2.3" and its page,
// and links to it. Headings carry their ids in the Markdown, `## Sizing the array
// {#sec:array-size}`; figures and tables are resources whose ids are what follows the @.
Das zusammengesetzte script.js läuft unverändert: Fügen Sie es in das Modulskript einer beliebigen Seite ein, oder öffnen Sie das Rezept auf CodePen. Ordner des Rezepts auf GitHub ↗ (öffnet sich in einem neuen Tab)
Varianten
#„Figure 2.1“ im Text drucken
Ein Buch, das seine Verweise ausschreibt, verwendet statt der kurzen Bezeichnung den vollen Namen des Typs.
- shortLabel: type.id === 'table' ? t({ en: 'table', es: 'tabla' })
- : t({ en: 'fig.', es: 'figura' }),
+ shortLabel: type.id === 'table' ? t({ en: 'Table', es: 'Tabla' })
+ : t({ en: 'Figure', es: 'Figura' }),#Abschnitte mit einem Paragrafenzeichen nummerieren
- section: t({ en: 'section {n}', es: 'apartado {n}' }), // @sec:losses → "section 2.2"
+ section: '§ {n}',Häufige Fehler
Häufiger Fehler
Eine Abbildung wird dort nummeriert und platziert, wo sie zuerst zitiert wird
Der erste Verweis :ref oder @fig: auf eine Abbildung oder Tabelle gibt ihr die Nummer und den Platz, ein Verweis in einem früheren Kapitel zieht sie also dorthin. Verweisen Sie vorausgreifend auf den Abschnitt, der sie enthält, und auf die Abbildung selbst erst, wenn sie platziert ist. Querverweise →
- Ein Verweis auf eine ID, die kein Kapitel setzt, gibt ? aus. Prüfen Sie die IDs, nachdem Sie eine Überschrift in einem Kapitel umbenannt haben: Die Verweise darauf stehen in den anderen.
- Setzen Sie die Kapitel einzeln mit
buildDocument, gehen sowohl die Zähler als auch die Gliederung des Buchs verloren: Jedes Kapitel nummeriert seine Abbildungen wieder ab 1, und ein Verweis auf ein anderes Kapitel gibt ? aus. Verwenden SiebuildBundle, oder verketten Siecontinuationund übergeben Sieoutlineselbst. - Gleichungen haben in Postext keine eigene Nummerierung, also findet
@eq:nur ein Verweisziel, das Sie selbst setzen, und gibt dessen Text aus; dieses Buch schreibt seine beiden Formeln ohne Nummern. - Ein Abschnittstitel, der eine Seite eröffnet oder unter einer an ihren Kopf geglittenen Tabelle steht, wie 3.3 auf Seite 9, verliert standardmäßig seinen gesamten Raum darüber, und das Einrasten ins Raster setzt dann den Zeilenbruchteil, den er braucht, unter den Titel, weiter von seinem Text entfernt als bei Titeln mitten auf der Seite.
headings.columnHeadSpace: 'remainder'hält diesen Zeilenbruchteil über dem Titel (Überschriften). Das passt zu einem einspaltigen Buch, in dem keine Nachbarspalte zeigt, dass der Titel etwas tiefer beginnt.
Nachweise
- Rezept
- Ignacio Ferro
- Text
- Eigener Text, CC BY 4.0
- Schriften
- IBM Plex Serif (SIL OFL 1.1) · IBM Plex Sans Condensed (SIL OFL 1.1) · IBM Plex Mono (SIL OFL 1.1)
- Code
- MIT, wie Postext
Diesen Text bearbeiten ↗ (öffnet sich in einem neuen Tab)Ordner des Rezepts auf GitHub ↗ (öffnet sich in einem neuen Tab)


