跳到主要内容
食谱编号17

排版食谱 · 第3章 · 标题与章首页

一本书里的五种章首页

一级标题有一个章首页,另有四种在Markdown里点名的标题样式;每种样式换一种强调色,有的还改页边距、栏数或页码。

本页内容
输出
Canvas
难度
高级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0
许可证
更新于2026年10月10日
代码MIT · 文本CC BY 4.0

第4–5页,共7页

  • 英文样例:尚无中文版本
  • 成品尺寸180 × 240 mm
  • 1栏
  • Lora 10.8/14.5
  • Fraunces
  • Geist
  • Geist Mono
  • 7页
  • 难度
  • Postext 1.57.1
  • 排版用时72 ms
  • 249行代码

简单来说

一本讲章节开头怎样设计的小书,每一章都用不同的样式开篇。示例演示怎样用一个设置给一章换上自己的颜色、页边距和版式。

成品一览

Five Ways to Begin是一本讲章首页的七页小书,180 × 240 mm,每一章都用它所讲的那种章首页排。第一章是小说式的:加宽字距的眉题、细斜体标题和五行高的红色首字母,后续页面带书眉。第二章把标题和一个花饰放在奶油色花框里,框由两条蓝线画成。第三章把标题以白色Fraunces Black堆叠在出血的品红色底上,下面是反白在方框里的等宽首字母。第四章是手册:等宽字体书眉、标题旁的橙色编号徽标,以及两栏Geist正文。尾声把标题排在空白页的中部,下面是一个上升首字母。第一章使用一级标题自己的章首页,其余四章在标题行上点名一种标题样式。

这道食谱解答

  • 怎样给每章不同的照片、颜色或章首页样式?
  • 怎样给章首页加作者行、提要或带首字下沉的导语?
  • 怎样让章标题横跨页面,下面的正文分两栏排?
  • 怎样在标题旁做一个编号徽标,让它在编号变长时(9 → 10)自动变宽?

简短回答

script.js · 第16–52行在完整代码中
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:
      color: col('accent'), gap: mm(1.2) }, // on the first baseline, rising into the white
    margins: { left: mm(QUIET.inner), right: mm(QUIET.outer) }, // a 106 mm measure
    bodyStyle: { fontSize: pt(QUIET.size), lineHeight: pt(QUIET.lead), textAlign: 'left' } },
]; // hook-up in config(): headings: { levels: [level1(), …] }, headingStyles()

用料

类型
Lora, Fraunces, Geist, Geist Mono(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 在标题级别上设定本书的章首页

script.js · 第97–105行在完整代码中
// The first paragraph, in :::paragraphs{style="lead"}, starts 17 mm under the title.
const literary = design([
  text('kicker', '{attr.kicker}', 'Geist', 8, centred(22), { ...caps(1.7), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 32, at('#kicker', 'below', 0, 4, 'fill'),
    { fontWeight: 300, italic: true, lineHeight: 1.05, align: 'center' }),
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.8), color: col('accent'),
    placement: at('#title', 'below', (MEASURE - 18) / 2, 6, 18) }, // an 18 mm rule, centred
  room('#title', 17),
]);

第一章没有样式,所以采用一级标题自己的设计,所有样式都以它为起点(简短回答列出了这一级别和各样式)。本章的第一段写在Markdown里,放在:::paragraphs{style="lead"}组中,字号比正文大一档(12/17 pt,正文是10.8/14.5),由这一级别的dropCap开头(首字下沉):M立在第五条基线上,不设fontSize时,它的顶端与首行大写字母齐平,两者的大写字母高度都从Fraunces和Lora中量得。章首页以一个空方框room()结束,它位于标题下方17 mm,导语从那里开始;导语下的:::space空出一行,正文从下一行不缩进地开始。

#2 · 用两个方框给标题加框

script.js · 第109–127行在完整代码中
const FRAME = 62; // mm: the outer box's height
const INSET = 3; // mm: the hairline box runs this far inside it
const FLEURON = 44; // mm: the ornament's width, centred under the title
const framed = design([ // array order is paint order: the boxes first, the type on top
  { kind: 'box', id: 'frame', placement: { ...at('container', 'top-left', 0, 0),
    size: { width: 'fill', height: mm(FRAME) } },
  style: { backgroundColor: col('tint'), borderColor: col('accent'), borderWidth: pt(1.6) } },
  { kind: 'box', id: 'hairline', placement: { ...at('#frame', 'align-top', INSET, INSET),
    size: { width: mm(MEASURE - 2 * INSET), height: mm(FRAME - 2 * INSET) } },
  style: { borderColor: col('accent'), borderWidth: pt(0.5) } },
  text('kicker', '{attr.kicker}', 'Geist', 7.5, centred(14), { ...caps(1.6), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 34, at('#kicker', 'below', 0, 3.5, 'fill'),
    { fontWeight: 600, lineHeight: 1.05, align: 'center' }),
  // An image element draws a registered resource, never numbered or captioned. Its resourceId
  // may be '{attr.photo}' (postext ≥ 1.8): then each heading names its own photo.
  { kind: 'image', id: 'fleuron', resourceId: 'fleuron',
    placement: at('#title', 'below', (MEASURE - FLEURON) / 2, 4.5, FLEURON) },
  room('#frame', 9), // the text starts 9 mm under the frame
]);

数组顺序就是绘制顺序,所以方框在前,文字在上;花饰是一个图像元素,绘制一个已注册的SVG资源,这个资源不编号也不被引用。每一章要有自己的照片,也是靠图像元素:从postext 1.8起,它的resourceId接受占位符,于是用resourceId: '{attr.photo}',一种样式就能绘制每个标题点名的照片,例如# The Frame {style="framed" photo="lake"},菜谱卡片里的菜品就是这样做的(图像元素)。简短回答里的framed样式设置了palette,把本章各页的accent变成蓝色,于是线条、眉题和底部页码都印成蓝色;它的margins让文字块在花框下居中。这个样式自己的dropCap指定col('accent'),调色板同样把它变成蓝色,所以三行高的L与花框颜色一致。

#3 · 堆叠标题,给首字母加框

script.js · 第131–160行在完整代码中
const FIELD = 130; // mm from the trim: the foot of the colour field
const S_BEARING = 1.2; // mm: the white left of the title's S (0.0365 em at 94 pt)
// The boxed initial: the style's drop cap sets the white letter; the opener draws the square,
// fitted by hand in pt. The text starts on the grid line 8 mm under the field (from the trim).
const TEXT_TOP = Math.ceil(((FIELD - TOP + 8) * 72) / 25.4 / LEAD) * LEAD; // pt
const BASE = 0.8; // a line's baseline stands 0.8 down its line box
const CAPS = 0.7; // the height of a capital in Lora, em
// Geist Mono, em: cap height, the advance every capital shares, the white either side of an M
const MONO = { caps: 0.71, advance: 0.6, side: 0.044 };
const BOXED = 3; // lines the initial spans
const capsTop = TEXT_TOP + BASE * LEADIN.lead - CAPS * LEADIN.size; // the first line's capitals
const foot = TEXT_TOP + (BOXED - 1 + BASE) * LEADIN.lead; // the third baseline: the letter's foot
// The M runs from the first line's capitals to its foot; the square runs past both as far as
// the M's sides sit in, so the colour is as wide on all four sides.
const letter = (foot - capsTop) / MONO.caps, pad = MONO.side * letter;
const stacked = design([
  { kind: 'box', id: 'field', style: { backgroundColor: col('accent') },
    placement: { anchor: { to: 'bleed', edge: 'top-left' }, size: { height: mm(FIELD) } } },
  text('kicker', '{attr.kicker}', 'Geist', 8, at('container', 'top-left', 0, 4),
    { ...caps(1.8), color: col('paper') }),
  // {titleText} keeps the \\ breaks from the Markdown, so each line ends at the writer's \\.
  // Moved left by the S's side bearing, so its curve meets the text edge.
  text('title', '{titleText}', 'Fraunces', 94,
    at('container', 'top-left', -S_BEARING, 15, 'fill'),
    { fontWeight: 900, lineHeight: 0.84, color: col('paper') }),
  { kind: 'box', id: 'initial', style: { backgroundColor: col('accent') }, reserve: false,
    placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: pt(capsTop - pad) },
      size: { width: pt(MONO.advance * letter), height: pt(foot - capsTop + 2 * pad) } } },
  room('container', (TEXT_TOP * 25.4) / 72, 'top-left'), // the opener ends where the text starts
]);

堆叠写在Markdown里:# Say it \\ out \\ loud.在每个\\处给{titleText}换行,而书眉和PDF书签把标题读作一行(标题中的换行)。色底从出血的顶端垂下,通宽,高130 mm,导语从它下方8 mm处的第一条网格线开始(TEXT_TOP)。加框的首字母由两部分组成。样式的dropCap把白色的M排成导语的三行高,章首页在它下面画一个品红色方框,位置根据TEXT_TOP、导语的行距和Geist Mono的字体度量确定,让M上下露出的颜色与它的左右侧边距一样宽。这个方框在章首页里不占空间(reserve: false);一个空的room方框让章首页结束在导语开始的地方。

#4 · 给手册加上徽标、两栏和自己的书眉

script.js · 第181–208行在完整代码中
const SPEC = 18; // mm: both side margins, for a 144 mm text block in two columns
const PAD = 1.6; // mm of colour over the badge's figures
const spec = design([
  { kind: 'rule', id: 'top-rule', direction: 'horizontal', thickness: pt(2.4), color: col('ink'),
    placement: at('container', 'top-left', 0, 0, 'fill') },
  text('kicker', '{attr.kicker}', 'Geist Mono', 7.5, at('container', 'top-left', 0, 3.2),
    { ...caps(1.2, 500), color: col('graphite') }),
  // The badge's width follows its text, with padding on both sides, so 9 → 10 widens it. Its
  // line box holds 1 mm more under the figures than over them, so the foot pads 1 mm less.
  text('badge', '{chapterNumber}', 'Geist Mono', 26, at('container', 'top-left', 0, 12),
    { fontWeight: 700, lineHeight: 1, box: { backgroundColor: col('accent'), padding: {
      top: mm(PAD), right: mm(2.4), bottom: mm(PAD - 1), left: mm(2.4) } } }),
  // Same size and line height as the badge, dropped by its top padding, so the baselines meet.
  text('title', '{titleText}', 'Geist', 26, at('#badge', 'right-of', 4, PAD),
    { fontWeight: 700, lineHeight: 1 }),
  { kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.5), color: col('ink'),
    placement: at('container', 'top-left', 0, 31, 'fill') }, // room for a one-line title
]);
const specHead = (parity) => { // mono labels over the opener's rule, on every page of the section
  const [edge, s] = parity === 'even' ? ['top-left', 1] : ['top-right', -1];
  const mono = { ...caps(1.1, 500), color: col('graphite'), parity, pages: 'all' };
  return [
    text(`sp-page-${parity}`, '{pageNumber} / {totalPages}', 'Geist Mono', 7,
      at('container', edge, 0, HEAD_Y), mono),
    text(`sp-chap-${parity}`, '§ {chapterNumber} · {chapterTitle}', 'Geist Mono', 7,
      at('container', edge, s * 16, HEAD_Y), mono),
  ];
};

徽标是一个带填充和内边距、但没有宽度的文字元素,所以宽度跟随{chapterNumber};标题用right-of放在它右侧:到第10章,徽标多一位数字,加宽5.5 mm,标题也随之移动。它的下内边距比上内边距少1 mm,因为行框在数字下方多留了这么多空间,这样4才居中。在spec样式里,layout把这一节排成两栏,而这一级别的span: 'page'让章首页横跨两栏。它的margins把文字块加宽到144 mm,bodyStyle在本书14.5 pt的网格上排齐左的Geist,header替换本书的书眉,空的footer去掉页码。

#5 · 给尾声留出空间

script.js · 第212–221行在完整代码中
const quiet = design([ // the kicker is in the accent, which this style turns graphite
  text('kicker', '{attr.kicker}', 'Geist', 7.5, at('container', 'top-left', 0, 92),
    caps(1.6, 500)),
  text('title', '{titleText}', 'Fraunces', 26, at('#kicker', 'below', 0, 3),
    { fontWeight: 300, lineHeight: 1.1 }),
  room('#title', 3), // and the initial's rise, in whole lines: the text 16 mm under the title
]);
const tiny = (parity, edge) => text(`tiny-${parity}`, '{pageNumber}', 'Geist', 6.5,
  at('container', edge, 0, -12), { fontWeight: 500, color: col('muted'), parity });
const tinyFolio = { elements: [tiny('even', 'bottom-left'), tiny('odd', 'bottom-right')] };

眉题挂在文字块顶端以下92 mm处,上升首字母是quiet样式一行高的首字下沉:它的底部立在第一条基线上,字号46 pt,所以向上伸进空白,而不是沉入段落。段落会在上方以整行为单位为这段升高留出空间,所以章首页在标题下的room只有3 mm。在quiet样式里,margins把行长收窄到106 mm,bodyStyle把正文排成12.5/17.5 pt的齐左,footer把底部页码换成外侧角上的小页码,palette把眉题变成灰色。这一页上没有印章号,所以numbered: false只会在尾声之后新增的章里显出效果,那一章仍会是第五章。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 017 · Five chapter openers in one book ═════════════════
// https://postext.dev/en/cookbook/five-chapter-openers
// Code: MIT · Text: original (CC BY 4.0) · Ornament: generated in code (CC BY 4.0)
// Fonts: Lora, Fraunces, Geist, Geist Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A small book about openers, each chapter set in the one it describes. Every heading style
// brings its opener and palette; three move the margins, two bring their own type and folios.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'five-chapter-openers';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region answer: one opener per chapter: the level's own, then heading styles by name
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:

组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)

变化

#让每一章都从右页开始

改成'odd'后,每章都在右页开始,第二至第五章之前各多一个空白左页,全书从7页增加到11页。

-  breakBefore: { enabled: true, parity: 'any' },
+  breakBefore: { enabled: true, parity: 'odd' },

#首字母下沉三行

不设fontSize时,字号随行数而定,所以三行高的首字母同样与首行大写字母对齐。

-  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
+  dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),

#首字母后的小型大写字母

leadIn把首字母后的头几个词排成小型大写字母,许多小说都这样做;words: 'line'则取整个第一行。

-    gap: mm(2.5) } });
+    gap: mm(2.5), leadIn: { words: 'line' } } });

常见问题

易错点

属性值:不能含{ or };含"的值用单引号

属性值在右花括号处结束,所以不能包含{ or }。含双引号的值要放在单引号里;美元符号没有问题。 标题属性 →

  • 导语齐左排,和杂志的章首页一样;两端对齐的导语排在五行高的首字母旁,词少的行会把词距撑得很开。
  • 品红色方框是按Lora 12/17、Geist Mono的M和该行的基线手工调好的,基线位于行框往下0.8处(BASE)。只要改了导语的字体、字号或行距、字母、行数或色底的高度,都要重新对照首行大写字母检查方框。
  • 标题不会旋转,也不会竖排:设计文本总是横排的。改用\\堆叠单词,就像第三章那样。

致谢

文本
原创文字, CC BY 4.0
字体
Lora (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Geist (SIL OFL 1.1) · Geist Mono (SIL OFL 1.1)
沙盒