简单来说
一本地质教材的章首页。带大号章号的彩色色带一直印到页面上缘;这套设计只设定一次,所有章节都能用。
成品一览
入门地质教科书Earth in Motion的章首页,页面尺寸210 × 280 mm。第3章在一条赭红色带下开篇,色带从页面顶端出血。带中依次是加了字距的眉题、按作者指定位置断行的标题、一段斜体导语,以及一个168 pt的章号,它立在色带深色底边上,靠近切口。色带下方是两栏两端对齐的正文,一幅板块下沉的剖面图占据页面底部。第4章用青绿色重复同一设计,书边索引、节号、交叉引用和图的标签都随之变色。版面设计只在一级标题上写一次。每章的标题行以属性的形式提供眉题里的主题和导语;某章要换颜色时,再在标题行里指定一个样式。
这道食谱解答
- 怎样做一个带出血色带、大号章号和标题的章首页?
- 怎样让章标题横跨页面,下面的正文分两栏排?
- 怎样给章首页加作者行、提要或带首字下沉的导语?
- 怎样给每章不同的照片、颜色或章首页样式?
- 怎样给标题编号(1、1.1、1.1.1),并给每一级设置不同样式?
简短回答
const TOP = 24; // top margin, mm: the opener's container starts here
const OUTER = 16; // outer margin, mm: the numeral and the folios line up on it
const BLEED = 0; // mm: 0 on screen; about 3 for print, which also switches on page.cutLines
const BAND = 100; // mm from the trim to the foot of the band
const NUMERAL = 168; // pt; the baseline of a design text sits 0.8 down its line box
const PT = 25.4 / 72; // mm in a point
const AIR = 12; // mm, foot to text: ≥ 0.2 × NUMERAL × PT ≈ 11.9, the numeral box below its baseline
const BEARING = 2; // mm past the text edge: a rough optical nudge, as each digit's bearing differs
const bleedTop = { to: 'bleed', edge: 'top-left' }; // the trim's corner, pushed out by BLEED
// Design text centres by default; in a heading design it wraps by default too.
const onBand = { color: col('paper'), align: 'left' };
const below = (id, y, width) => ({ anchor: { to: `#${id}`, edge: 'below' }, offset: { y: mm(y) },
size: { width: mm(width) } }); // chained: a longer title pushes the standfirst down
const opener = { enabled: true,
// The text starts below the lowest element or minHeight (gotcha: opener-reserves-anchored).
minHeight: mm(BAND - TOP + AIR), // clears the numeral, so the text starts AIR below the foot
slot: {
elements: [ // array order is paint order: the band first, the type on top
{ kind: 'box', id: 'band', style: { backgroundColor: col('band') }, placement: {
anchor: bleedTop, size: { height: mm(BLEED + BAND) } } }, // no width: runs to the far edge
{ kind: 'rule', id: 'foot', color: col('ink'), thickness: mm(1.6), placement: {
anchor: bleedTop, offset: { y: mm(BLEED + BAND) } } },
{ kind: 'text', id: 'numeral', content: '{chapterNumber}', ...onBand,
fontFamily: 'Archivo', fontWeight: 800, fontSize: pt(NUMERAL),
lineHeight: 1, // a box as tall as the type: the y below counts on it
placement: { anchor: { to: 'page', edge: 'top-right' }, // the fore-edge of a recto
offset: { x: mm(BEARING - OUTER), y: mm(BAND - 0.8 * NUMERAL * PT) } } }, // on the foot
{ kind: 'text', id: 'kicker', ...onBand, fontFamily: 'Archivo Narrow', fontWeight: 600,
content: `${t({ en: 'Chapter', es: 'Capítulo' })} {chapterNumber} · {attr.topic}`,
fontSize: pt(8.5), letterSpacing: pt(2), textTransform: 'uppercase',
placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(7) } } },
{ kind: 'text', id: 'title', content: '{titleText}', ...onBand, // breaks at the \\
fontFamily: 'Archivo', fontWeight: 800, fontSize: pt(34), lineHeight: 1.02,
placement: below('kicker', 3, 118) },
{ kind: 'text', id: 'lead', content: '{attr.lead}', ...onBand, fontFamily: 'Roboto Serif',
italic: true, fontSize: pt(10.5), lineHeight: 1.36, placement: below('title', 6, 100) },
],
},
}; // hook-up: headings.levels[0] = { span: 'page', breakBefore, advancedDesign: opener }
用料
做法
#1 · 把色带和章号挂在页面上
代码见上文的简短回答。色带和它的底边挂在出血框的左上角。与其他没有指定宽度的框或线一样,它们会一直延伸到该框的另一边,因此到达裁切线,设置了BLEED后则到达出血边。章号挂在页面的右上角:y: BAND - 0.8 * NUMERAL * PT把它的基线放在底边上,BEARING把它的框向页边距内移2 mm,以抵消数字的侧空,使墨迹大致与正文边缘对齐。眉题挂在容器上。标题通过below锚点挂在眉题下方,导语挂在标题下方,而标题版式中的文字默认换行,所以标题变长时导语会被往下推。导语是一个文本元素,内容来自标题的lead属性;如果要加作者行,就再加一个{attr.author},用同样的方式串接。minHeight: BAND - TOP + AIR决定正文从哪里开始。章号的框比底边低出其字号的0.2倍(11.9 mm),而章首页会为最低的元素预留空间,所以AIR至少要有11.9 mm;小于这个值,正文的起点就由章号的框决定了。
#2 · 让章标题独占一页
headings: {
fontFamily: 'Archivo', color: col('band'),
levels: [
// parity: 'odd' keeps openers on rectos, so the numeral's right-edge anchor is the
// fore-edge. The copy is fitted so no chapter spills a few lines onto a page of its own,
// which would leave that page and a blank verso before the next opener.
{ level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
// 0: the hidden title's default 0.5 em would add a grid line, and AIR alone sets the gap
marginBottom: pt(0), advancedDesign: opener },
{ level: 2, fontSize: pt(11.5), lineHeight: pt(LEAD), numberingTemplate: '{1}.{2}',
marginTop: pt(LEAD), marginBottom: pt(0) }, // one grid line above, none below
],
},
span: 'page'让每章从新的一页开始,并把一级标题排成跨两栏的通栏;advancedDesign绘制章首页,代替标题文字,正文从预留高度的下方开始。breakBefore加上parity: 'odd'让每个章首页都落在右页上,这样章号所挂的右边就是切口。二级标题用numberingTemplate: '{1}.{2}'把各节编为3.1、3.2……,并使用色带的颜色;如果再加一个三级标题,用'{1}.{2}.{3}'就会编到3.1.1。
#3 · 章节颜色只定义一次
const palette = {
ink: '#1b1f23', // text: a cool near-black
band: '#c2562b', // the chapter colour: band, tab, section numbers, references, captions
band2: '#2e6f73', // the second chapter colour, a teal; the sea in Figure 3.1
land: '#d5cbc0', // the crust in the drawings
muted: '#6b6259', // running heads and the colophon
paper: '#ffffff', // type on the band
};
// col(id): a colour linked to its palette entry, with the entry's hex beside the link.
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 band, so nothing prints blue.
{ id: 'main-color', name: 'band (defaults)', value: { hex: palette.band, model: 'hex' } },
];
// {style="teal"} on a heading line swaps 'band' up to the next level-1 heading: the style's
// palette reaches design slots through their link and repaints every text colour equal to
// the base 'band'.
const headingStyles = [{ id: 'teal', palette: { band: palette.band2 } }];
配置中的每种颜色都链接到调色板里的某一项。引擎自身的默认颜色链接到main-color,所以把这一项指向色带颜色,配置里没有设置的默认颜色也会一并改色。第4章的标题行# Fire from \\ Below {style="teal" topic="Volcanoes" lead="…"}选用了这个样式,它会把band替换掉,直到下一个一级标题为止。设计元素通过调色板链接跟着替换;与基础band相同的文字颜色也会重新着色,所以在第43、44页上,书边索引、节号和列表编号、交叉引用、题注标签以及章首页底部的页码都变成青绿色。同一行还填入了{attr.topic}和{attr.lead},其中的\\在作者希望的位置给{titleText}断行(标题属性、标题中的换行)。
#4 · 让第一次引用决定图的位置
const figure = (id, placement, caption) => ({ id, typeId: 'figure', kind: 'svg', createdAt: 0,
updatedAt: 0, svg: { fileId: `${id}.svg`, width: ART[id].width, height: ART[id].height },
placement, caption, altText: ALT[id] }); // altText: read aloud in a PDF or HTML edition
const resources = [
// 'auto' may take the citing page's foot, 'top' never can (gotcha: top-float-next-page)
figure('plate-cycle', { position: 'auto', span: 'page' },
t({ en: 'A plate’s life in cross-section: born at a ridge (left), it cools as it drifts away '
+ 'and sinks under a continent; dots mark earthquakes.',
es: 'La vida de una placa, en corte: nace en la dorsal (izquierda), se enfría al alejarse y '
+ 'se hunde bajo el continente; los puntos son terremotos.' })),
figure('two-volcanoes', { position: 'bottom', span: 'page' }, // under the text on a last page
t({ en: 'Two volcanoes in cross-section: a broad shield built of runny basalt flows (left) '
+ 'and a steep stratovolcano of lava and ash layers (right).',
es: 'Dos volcanes en corte: un escudo ancho, de coladas de basalto fluido (izquierda), y un '
+ 'estratovolcán de capas de lava y ceniza (derecha).' })),
];
:ref为所引用的图编号,并把它放进引用段落之后第一个空位。图3.1设为auto。它在章首页上被引用时,两栏都还有空间,于是占据了这一页的底部;如果设为top浮动体,它就会出现在第42页的顶部,因为浮动体永远不会排在自己的引用之前。图4.1在第43页第二栏的下部被引用,那里已经放不下一条页宽的图带,所以bottom把它送到第44页。那是本章最后一页,在最后一页上,引擎会把页脚浮动体提上来,紧贴在最后几行正文之下,上方不留空。
#5 · 书眉、书边索引和章首页底部页码
const HEAD = 15.5; // mm from the trim to the folio's and the head's baseline: 8.5 above TOP
const INSET = 9; // mm from the folio's edge to the head: room for a three-digit folio
const TAB = { y: 36, w: 9, h: 26 }; // the thumb tab, mm: its top, its width, its height
const tabSize = (w) => ({ width: mm(w), height: mm(TAB.h) });
const baseline = (size) => mm(HEAD - 0.96 * size * PT); // box: 1.2 × size, baseline 0.8 down
const label = { fontFamily: 'Archivo Narrow', fontSize: pt(7.5), fontWeight: 600,
letterSpacing: pt(1.3), textTransform: 'uppercase', color: col('muted') };
const folio = { fontFamily: 'Archivo', fontSize: pt(9), fontWeight: 800, color: col('ink') };
const side = (parity) => { // the fore-edge is a verso's left edge and a recto's right edge
const [edge, s] = parity === 'even' ? ['left', 1] : ['right', -1]; // s: into the page
const at = (to, x, y, size) => ({ anchor: { to, edge: `top-${edge}` },
offset: { x: mm(s * x), y }, ...(size && { size }) });
return [
{ kind: 'text', id: `folio-${parity}`, content: '{pageNumber}', ...folio,
placement: at('page', OUTER, baseline(9)) },
// Textbooks name the chapter on the verso and the section on the recto: {firstMark.h2}
// is the first level-2 heading that starts on the page, or the one in force.
{ kind: 'text', id: `head-${parity}`, content: s > 0 ? '{chapterTitle}' : '{firstMark.h2}',
...label, placement: at('page', OUTER + INSET, baseline(7.5)) },
{ kind: 'box', id: `tab-${parity}`, style: { backgroundColor: col('band') }, // off the trim
placement: at('bleed', 0, mm(BLEED + TAB.y), tabSize(BLEED + TAB.w)) },
{ kind: 'text', id: `tab-no-${parity}`, content: '{chapterNumber}', ...folio, fontSize: pt(13),
color: col('paper'), // design text centres by default: here, on the trimmed tab
placement: at('page', 0, mm(TAB.y), tabSize(TAB.w)) },
].map((element) => ({ ...element, parity, pages: 'body' })); // never on an opener
};
const header = { elements: [...side('even'), ...side('odd')] };
const footer = { elements: [{ kind: 'text', id: 'drop-folio', content: '{pageNumber}',
...folio, color: col('band'), pages: 'opener', // centred by its anchor
placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: mm(10) } } }] };
每个页眉元素都挂在页面的一个上角,并用parity筛选,所以页码和书边索引总在切口一侧。书边索引的框改为挂在出血框的角上,并随BLEED加宽,裁切后仍然出血。pages: 'body'让这些元素都不出现在章首页上,章首页的页码由页脚放在页面底部。左页用{chapterTitle}标出章名,排成一行,因为\\只在章首页里给标题断行。教科书通常在右页标出节名。右页书眉用{firstMark.h2}印出节名:本页开始的第一个节标题,本页没有新节时则是当前所在的节。这四页里看不到它,因为两个右页都是章首页。
#6 · 从第3章开始排这本书
const continuation = { pageNumbering: { startAt: 41 }, // an odd folio: page 1 is still a recto
headings: { h1: 2, h2: 0, h3: 0, h4: 0, h5: 0, h6: 0 } }; // the next # is chapter 3
const doc = await buildDocumentWithFonts({ markdown, resources, continuation }, config(),
kitFonts(FONTS));
showPages(doc, { title: t({ en: 'Chapter opener on a full-bleed band',
es: 'Apertura de capítulo sobre banda a sangre' }) });
这几页是一本更长的书的第3章和第4章,所以buildDocument接收一个continuation,其中是第1、2章结束时的计数器。它的headings里有h1: 2,下一个#就是第3章:{chapterNumber}印出3,节号从3.1开始,第一幅图编为3.1。pageNumbering.startAt: 41让页码从41开始。无论页码是多少,第一页都是右页,因为奇偶按实际页面计算,而pageIndexOffset保持为0;第一个页码41恰好是奇数,与之一致。
完整食谱
// ═══ Postext Cookbook · Nº 003 · Chapter opener on a full-bleed band ══════════════
// https://postext.dev/en/cookbook/chapter-opener-bleed-band
// Code: MIT · Text: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Roboto Serif, Archivo, Archivo Narrow (SIL OFL 1.1) · Needs postext ≥ 1.57.0
// Two chapters of a geology textbook. Every level-1 heading becomes a colour band bled off the
// top of the page, its number standing on the band's foot; a heading style recolours chapter 4.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'chapter-opener-bleed-band';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, and the style that swaps a chapter's
const palette = {
ink: '#1b1f23', // text: a cool near-black
band: '#c2562b', // the chapter colour: band, tab, section numbers, references, captions
band2: '#2e6f73', // the second chapter colour, a teal; the sea in Figure 3.1
land: '#d5cbc0', // the crust in the drawings
muted: '#6b6259', // running heads and the colophon
paper: '#ffffff', // type on the band
};
// col(id): a colour linked to its palette entry, with the entry's hex beside the link.
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 band, so nothing prints blue.
{ id: 'main-color', name: 'band (defaults)', value: { hex: palette.band, model: 'hex' } },
];
// {style="teal"} on a heading line swaps 'band' up to the next level-1 heading: the style's
// palette reaches design slots through their link and repaints every text colour equal to
// the base 'band'.
const headingStyles = [{ id: 'teal', palette: { band: palette.band2 } }];
// #endregion
// #region answer: the opener: a bleed band, the numeral on its foot, texts from the heading
const TOP = 24; // top margin, mm: the opener's container starts here
const OUTER = 16; // outer margin, mm: the numeral and the folios line up on it
const BLEED = 0; // mm: 0 on screen; about 3 for print, which also switches on page.cutLines
const BAND = 100; // mm from the trim to the foot of the band
const NUMERAL = 168; // pt; the baseline of a design text sits 0.8 down its line box
const PT = 25.4 / 72; // mm in a point
const AIR = 12; // mm, foot to text: ≥ 0.2 × NUMERAL × PT ≈ 11.9, the numeral box below its baseline
const BEARING = 2; // mm past the text edge: a rough optical nudge, as each digit's bearing differs
const bleedTop = { to: 'bleed', edge: 'top-left' }; // the trim's corner, pushed out by BLEED
// Design text centres by default; in a heading design it wraps by default too.
const onBand = { color: col('paper'), align: 'left' };
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#加深色带
BAND决定色带的深度、章号的位置和正文上方预留的高度。色带加深后,正文会被推到下一页,所以要重新调整每章的篇幅(见最后一条常见问题),或者让各章接排。
-const BAND = 100; // mm from the trim to the foot of the band
+const BAND = 128; // mm from the trim to the foot of the band#带出血印刷
出血设为3 mm时,色带、它的底边和书边索引都伸出裁切线3 mm,章号仍然立在底边上,page.cutLines也以同样的数值打开。
-const BLEED = 0; // mm: 0 on screen; about 3 for print, which also switches on page.cutLines
+const BLEED = 3; // mm: 0 on screen; about 3 for print, which also switches on page.cutLines#为另一章加一种颜色
每种章节颜色就是调色板里多一项、样式里多一个;在那一章的标题行上写{style="slate"}。
band2: '#2e6f73', // the second chapter colour, a teal; the sea in Figure 3.1
+ band3: '#3d4f6b', // a third chapter colour, a slate blue
@@
-const headingStyles = [{ id: 'teal', palette: { band: palette.band2 } }];
+const headingStyles = [{ id: 'teal', palette: { band: palette.band2 } },
+ { id: 'slate', palette: { band: palette.band3 } }];#让各章接排
设为'any'时,章节从下一页开始。如果上一章在右页结束,新章就从左页开始,前面没有空白页;它的章号挂在页面右边,因而立在订口一侧。这几页不会变化,因为第3章本来就结束在左页上。
- { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
+ { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'any' },常见问题
易错点
章首页预留的高度一直到最低的页面锚定元素
高级设计的章首页会预留到其最低元素为止的高度,标题下方锚定在页面或出血上的元素也算在内,所以页脚处的装饰会把正文推到下一页。给这样的元素设reserve: false(从postext 1.5起):它绘制在锚定的位置,不预留任何空间。跟随区段本身的元素(锚定在区段底边,或高度为'fill')从不决定区段的高度。 设计过的章首页 →
易错点
'top'浮动体默认排在引用页的下一页,除非允许它置于引用页顶
默认情况下浮动体不会排在自己的引用之前,所以在第N页引用的整页宽'top'浮动体会出现在第N+1页的顶部。在资源或其类型的defaultPlacement上设置placement.citingPage: true(或者对全书设置layout.floatsAtCitingPage,自postext 1.25起),它就改为置于第N页顶部,引用处之上的文字移到它下方,前提是它不超过layout.maxTopFraction且引用行仍留在该页。它不会置于章首页或强制分页之后的页面顶部:那时仍排在下一页,而'auto'或'bottom'可以占用引用页的底部。 图的放置 →
- 色带高度是固定的,而眉题、标题和导语从上页边距往下挂、向下生长,所以导语长一些或标题排成三行,就会压到底边上。像两个版本那样把导语控制在五行左右,或者根据最长的章首页推算
BAND。 - 章号
y的公式假定lineHeight: 1。设计文本的基线位于行框自上而下0.8处,只有行框与字号等高时,0.8 * NUMERAL才是这段距离。改了行距或锚点,公式也要跟着改。BEARING是目测确定的,因为每个数字的侧空都不同(这里的3和4末端相差0.8 mm)。 - 样式的调色板会给设计元素和文字改色,图片则保留绘制时的颜色:图4.1是青绿色,是因为代码用
band2绘制它;放在第4章里的一幅赭红色图仍会是赭红色。 - 在
parity: 'odd'下,如果某章比最后一页多出几行,就会留下几乎空白的一页,外加下一个章首页之前的一个空白左页。像这里的两个版本那样调整文字篇幅,并在正文或BAND改动后检查每章的最后一页。
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Roboto Serif (SIL OFL 1.1) · Archivo (SIL OFL 1.1) · Archivo Narrow (SIL OFL 1.1)


