跳到主要内容
食谱编号65

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

袖珍经典:接排的短章

把《Dom Casmurro》排成袖珍本:短章在栏内的章标题下接排,每处分页都不会孤零零留下一行正文或一个标题。

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

页码212–213 · 第2–3页,共7页

  • 英文样例:尚无中文版本
  • 成品尺寸110 × 178 mm
  • 1栏
  • Tinos 9.5/12.6
  • Abril Fatface
  • League Spartan
  • 7页
  • 难度
  • Postext 1.57.1
  • 排版用时68 ms
  • 167行代码

简单来说

一部巴西古典小说的五页,排成袖珍本。每个短章紧接上一章开始,页首页尾都不会留下孤零零的一行。

成品一览

从Machado de Assis 1899年的小说《Dom Casmurro》接近结尾处选取五页正文,按葡萄牙语原文排成110 × 178 mm的袖珍本,归入一套虚构的丛书Coleção Casuarina。封面由梅紫、奶油、梅紫三道色带构成,标题用Abril Fatface,另有丛书的藏红色圆徽;第213页对面是一幅由扩散模型生成的插页画,画的是Flamengo外海的大浪。正文从第CXVIII章末尾排到第CXXIII章末尾,没有一章另起一页。每一章都在上一章结尾下空两行开始(CXIX处空三行,以填满第213页),章标题由一个梅紫色的罗马数字章号、加大字距的大写标题和一道藏红色短线组成。第214页有两个章标题,第215页和第216页各有一个。除最后一页外,每页正文都止于第33行,也没有哪一页以单独一行开头。

这道食谱解答

  • 怎样避免段末孤行、段首孤行和只有一个词的末行(孤字),并让标题与正文不分开?
  • 怎样让各栏底部齐平,最后一页也排得平衡(垂直两端对齐)?
  • 怎样给标题编号(1、1.1、1.1.1),并给每一级设置不同样式?
  • 怎样设置书眉:左页放书名,右页放章名,页码在外侧?

简短回答

script.js · 第37–59行在完整代码中
// Machado's chapters run a page or two, so none opens a page. The level's break is off (the
// H1 default would open each chapter on a recto) and a chapter starts two lines under the last.
const [NUMERAL, TITLE, TRACK, GAP] = [14, 7.5, 1.3, 1.6]; // pt, pt, pt, mm
const RULE_Y = NUMERAL * PT + GAP + TITLE * 1.2 * PT + GAP; // mm: under the title's line
const chapterHead = { enabled: true, slot: { elements: [ // no span: the head stays in the text
  { kind: 'text', id: 'numeral', content: '{number}', fontFamily: DISPLAY, fontSize: pt(NUMERAL),
    lineHeight: 1, color: col('plum'), align: 'center',
    placement: { ...at('container', 'top'), size: { width: 'fill' } } },
  { kind: 'text', id: 'title', content: '{titleText}', ...caps(TITLE, TRACK), color: col('ink'),
    align: 'center', overflow: 'wrap', // a long title takes a second line
    placement: { anchor: { to: '#numeral', edge: 'below' }, offset: { y: mm(GAP) },
      size: { width: 'fill' } } },
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1), color: col('saffron'),
    placement: { ...at('container', 'top', 0, RULE_Y), size: { width: mm(8) } } },
] } }; // 11.7 mm deep: the head takes three lines, and the text under it stays on the grid
const chapters = { level: 1, numberingTemplate: '{1:I}', // {number} prints CXIX, CXX…
  fontSize: pt(TITLE), // the hidden heading line, measured in the headings' face
  breakBefore: { enabled: false }, marginTop: pt(2 * LEAD), advancedDesign: chapterHead };
// The keep rules are on by default. avoidWidows keeps widowMinLines (2) lines of a paragraph
// at the foot of a page, and headings.keepWithNext takes the head along when the paragraph
// moves on; avoidOrphans keeps two lines at the head of the next page; avoidRunts weighs a
// last line shorter than about 20 characters as a fault. Column balancing adds lines above
// a head so that the page ends on line 33.

用料

类型
Tinos(Apache 2.0) · Abril Fatface, League Spartan(SIL OFL 1.1)
素材
  • plate-1100.jpg
  • The plate of the sea off Flamengo under the Sugarloaf, a painting (Generated With Diffusion Models, 原创)
  • The casuarina roundel, drawn in code in the book’s palette (Ignacio Ferro, MIT)

做法

#1 · 各章接排,章标题与正文不分离

script.js · 第37–59行在完整代码中
// Machado's chapters run a page or two, so none opens a page. The level's break is off (the
// H1 default would open each chapter on a recto) and a chapter starts two lines under the last.
const [NUMERAL, TITLE, TRACK, GAP] = [14, 7.5, 1.3, 1.6]; // pt, pt, pt, mm
const RULE_Y = NUMERAL * PT + GAP + TITLE * 1.2 * PT + GAP; // mm: under the title's line
const chapterHead = { enabled: true, slot: { elements: [ // no span: the head stays in the text
  { kind: 'text', id: 'numeral', content: '{number}', fontFamily: DISPLAY, fontSize: pt(NUMERAL),
    lineHeight: 1, color: col('plum'), align: 'center',
    placement: { ...at('container', 'top'), size: { width: 'fill' } } },
  { kind: 'text', id: 'title', content: '{titleText}', ...caps(TITLE, TRACK), color: col('ink'),
    align: 'center', overflow: 'wrap', // a long title takes a second line
    placement: { anchor: { to: '#numeral', edge: 'below' }, offset: { y: mm(GAP) },
      size: { width: 'fill' } } },
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1), color: col('saffron'),
    placement: { ...at('container', 'top', 0, RULE_Y), size: { width: mm(8) } } },
] } }; // 11.7 mm deep: the head takes three lines, and the text under it stays on the grid
const chapters = { level: 1, numberingTemplate: '{1:I}', // {number} prints CXIX, CXX…
  fontSize: pt(TITLE), // the hidden heading line, measured in the headings' face
  breakBefore: { enabled: false }, marginTop: pt(2 * LEAD), advancedDesign: chapterHead };
// The keep rules are on by default. avoidWidows keeps widowMinLines (2) lines of a paragraph
// at the foot of a page, and headings.keepWithNext takes the head along when the paragraph
// moves on; avoidOrphans keeps two lines at the head of the next page; avoidRunts weighs a
// last line shorter than about 20 characters as a fault. Column balancing adds lines above
// a head so that the page ends on line 33.

章这一级关闭了分页,marginTop让每个章标题落在上一章最后一行下方两行处。它的版面设计没有span,所以章标题在栏内绘制,占一个三行高的块,下面的正文仍在基线网格上(分页前置、span与高级版面设计)。保持规则都用默认值。CXIX和CXXI下的四行段落各在页脚留两行、转两行到下一页;若设avoidOrphans: false,它们会拆成三行和一行,第214页和第215页就会以单独一行开头(段首孤行、段末孤行、孤字)。各栏齐底在CXIX上方加了第三行空白,使第213页止于第33行(各栏齐底)。

Page 213: chapter CXIX starts three lines under the end of CXVIII and keeps two lines of its text at the foot.

#2 · 每页33行,按葡萄牙语断词

script.js · 第63–70行在完整代码中
const bodyText = { fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD),
  color: col('ink'), referenceColor: col('ink'), // a :ref added later prints in ink
  firstLineIndent: mm(5) }; // every paragraph indented, the first after a head too
const page = { sizePreset: 'custom', width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150,
  backgroundColor: col('paper'),
  margins: { top: mm(TOP), bottom: mm(TRIM.height - TOP - LINES * LEAD * PT), // 16.3 mm
    left: mm(INNER), right: mm(OUTER), mirror: true } };
const LOCALE = 'pt'; // the Portuguese hyphenation patterns

下页边距是16.3 mm,即178 mm的页高减去15 mm的上页边距和33行12.6 pt之后剩下的部分,所以每个满页都止于同一行。Tinos的字宽与Times New Roman相同,9.5 pt时在84 mm的行长上每行约排59个字符。locale: 'pt'用葡萄牙语的断词规则:at-tracção、af-fluencia、genealogica-mente(支持的语言)。每段都首行缩进,标题后的第一段也不例外,葡萄牙和巴西的版本通常如此,所以indentAfterHeading保留默认值。

#3 · 左页书眉印书名,右页印章名

script.js · 第74–87行在完整代码中
const SHIFT = (INNER - OUTER) / 2; // mm: the text block sits off the page's centre
const [HEAD_Y, FOLIO_Y] = [8.5, 167]; // mm below the top edge
const head = (id, content, parity, x) => ({ kind: 'text', id, content, parity,
  pages: 'body', // never on the cover or the plate, which are opener pages
  ...caps(7.5, TRACK), color: col('muted'), align: 'center',
  placement: at('page', 'top', x, HEAD_Y) });
const folio = (id, parity, edge, x) => ({ kind: 'text', id, content: '{pageNumber}', parity,
  pages: 'body', ...caps(7.5, 0), color: col('ink'), placement: at('page', edge, x, FOLIO_Y) });
const header = { elements: [
  head('verso-title', '{title}', 'even', -SHIFT), // the title in the frontmatter
  head('recto-chapter', '{chapterTitle}', 'odd', SHIFT), // last chapter begun on or before it
] };
const footer = { elements: [folio('verso-folio', 'even', 'top-left', OUTER),
  { ...folio('recto-folio', 'odd', 'top-right', -OUTER), align: 'right' }] };

{title}印出前置元数据中的标题,{chapterTitle}印出在本页或本页之前开始的最后一章,所以第217页印的是从第216页开始的CXXIII(文字元素)。第213页印的是在其页脚才开始的CXIX,尽管这一页的前25行还属于CXVIII。{chapterTitleAtTop}印的是页面开头所在的章,但这段节选里没有CXVIII的标题。SHIFT把书眉移到版心的中线上,与页面中线相差1 mm,因为内侧页边距比外侧宽2 mm。

#4 · 用标题画出封面和插页

script.js · 第91–134行在完整代码中
// span: 'page', in one column too, makes each an opener page: its art runs past the foot of
// the text column, and no running head or folio prints on it. Each design reaches the foot of
// the page, so the heading reserves the whole page and the text starts on the next one.
const onPage = (y, size) => ({ ...at('page', 'top', 0, y), ...(size && { size }) });
const cover = { id: 'cover', numbered: false, span: 'page',
  advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'box', id: 'top-band', style: { backgroundColor: col('plum') },
      placement: { ...at('page', 'top-left'), size: { width: 'fill', height: mm(62) } } },
    { kind: 'text', id: 'author', content: '{author}', ...caps(10, 2.4), color: col('paper'),
      align: 'center', placement: at('page', 'top', 0, 44) },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(44),
      lineHeight: 1, color: col('plum'), align: 'center', overflow: 'wrap',
      placement: onPage(72, { width: 'fill' }) },
    { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1.5),
      color: col('saffron'), placement: onPage(111, { width: mm(12) }) },
    { kind: 'box', id: 'foot-band', style: { backgroundColor: col('plum') },
      placement: { ...at('page', 'top-left', 0, 124), size: { width: 'fill', height: mm(54) } } },
    { kind: 'image', id: 'roundel', resourceId: 'roundel',
      placement: onPage(133, { width: mm(20) }) },
    { kind: 'text', id: 'series', content: 'Coleção Casuarina', ...caps(7.5, 1.8),
      color: col('saffron'), align: 'center', placement: at('page', 'top', 0, 159) },
  ] } } };
// The plate faces the first page of text: the morning sea off Flamengo, captioned with the
// line of chapter CXXIII it illustrates, which the heading carries in its quote attribute.
const plate = { id: 'plate', numbered: false, span: 'page',
  advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'image', id: 'sea', resourceId: 'sea',
      placement: { ...at('page', 'top-left'), size: { width: 'fill', height: 'fill' } } },
    { kind: 'text', id: 'label', content: '{titleText}', ...caps(7.5, 1.6), color: col('saffron'),
      align: 'center', placement: at('page', 'top', 0, 146) },
    { kind: 'text', id: 'quote', content: '{attr.quote}', fontFamily: TEXT, italic: true,
      fontSize: pt(8.6), lineHeight: 1.35, color: col('paper'), align: 'center', overflow: 'wrap',
      placement: onPage(152, { width: mm(78) }) },
  ] } } };
const resources = [
  { id: 'roundel', typeId: 'figure', kind: 'svg', createdAt: 0, updatedAt: 0,
    svg: { fileId: 'roundel.svg', width: 60, height: 60 },
    altText: 'The series mark: a casuarina tree in a ring.' },
  // The plate is a painting, a JPEG in assets/ cut to the trim, declared at its pixels.
  { id: 'sea', typeId: 'figure', kind: 'bitmap', createdAt: 0, updatedAt: 0,
    bitmap: { fileId: 'plate-1100.jpg', format: 'jpeg', width: 1100, height: 1780 },
    altText: 'A heavy morning sea under the Sugarloaf, a low sun, and a lone swimmer among '
      + 'the breakers.' },
];

两者都是numbered: false的标题样式,所以第一个编号的章仍是CXIX。span: 'page'让每个都成为独占一页的首页。没有它,封面底部的色带和插页的图画只画到版心的底边,书眉和页码也会印在这两页上。插页的题注来自它的标题:CAPÍTULO CXXIII是标题文字,斜体引文是它的quote属性(标题属性)。两个设计都一直画到页面底边,所以标题预留整页,正文从下一页开始,不需要:::pagebreak。

#5 · 告诉节选它在书中的位置

script.js · 第276行在完整代码中
const continuation = { headings: { h1: 118, h2: 0, h3: 0, h4: 0, h5: 0, h6: 0 } };

continuation.headings给出节选起始时的标题计数器。设为h1: 118时,第一个编号标题是第CXIX章;不设时,五章编号为I到V。章这一级的numberingTemplate: '{1:I}'用罗马数字排这些编号,版面设计用{number}印出。封面分页之后的:::numbering{startAt=212}把插页编为第212页,正文页编为第213到217页(编号)。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 065 · Pocket classic: short chapters that run on ═══════════
// https://postext.dev/en/cookbook/short-chapters-run-on
// Code: MIT · Text: Machado de Assis, Dom Casmurro, 1899 (PD, Gutenberg #55752) · Plate: painted
// Fonts: Tinos (Apache 2.0), Abril Fatface, League Spartan (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the viewer's title; the sample is Portuguese
const RECIPE = 'short-chapters-run-on';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
  ink: '#24202a', // the text: a violet near-black
  paper: '#f6f1e6', // the pocket book's paper
  plum: '#4f2a49', // the series colour: cover bands, the chapter numerals
  saffron: '#d9a03c', // the second ink: rules and drawings, never text on paper (2.1:1)
  muted: '#6b616e', // the running heads and the colophon (5.2:1 on paper)
};
// 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).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  // The default bold and italic colours link to main-color, set here to the ink, not blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
const [TEXT, DISPLAY, LABEL] = ['Tinos', 'Abril Fatface', 'League Spartan'];
const TRIM = { width: 110, height: 178 }; // mm: a pocket book
const [TOP, INNER, OUTER] = [15, 14, 12]; // mm; mirrored, so the inner margin is at the spine
const [BODY, LEAD, LINES] = [9.5, 12.6, 33]; // pt, pt, and the lines of a full page
const PT = 25.4 / 72; // mm per point
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const caps = (size, track) => ({ fontFamily: LABEL, fontWeight: 600, fontSize: pt(size),
  letterSpacing: pt(track), textTransform: 'uppercase' });

// #region answer: chapters that run on, each under a centred head drawn in the column
// Machado's chapters run a page or two, so none opens a page. The level's break is off (the
// H1 default would open each chapter on a recto) and a chapter starts two lines under the last.
const [NUMERAL, TITLE, TRACK, GAP] = [14, 7.5, 1.3, 1.6]; // pt, pt, pt, mm
const RULE_Y = NUMERAL * PT + GAP + TITLE * 1.2 * PT + GAP; // mm: under the title's line
const chapterHead = { enabled: true, slot: { elements: [ // no span: the head stays in the text
  { kind: 'text', id: 'numeral', content: '{number}', fontFamily: DISPLAY, fontSize: pt(NUMERAL),
    lineHeight: 1, color: col('plum'), align: 'center',
    placement: { ...at('container', 'top'), size: { width: 'fill' } } },
  { kind: 'text', id: 'title', content: '{titleText}', ...caps(TITLE, TRACK), color: col('ink'),
    align: 'center', overflow: 'wrap', // a long title takes a second line
    placement: { anchor: { to: '#numeral', edge: 'below' }, offset: { y: mm(GAP) },
      size: { width: 'fill' } } },

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

变化

#每章另起一页

每章之前分页时,五页正文变成七页:第214页只有CXIX的标题和四行正文,而没有任何一章开始的第213页,会把插页的标题CAPÍTULO CXXIII当作书眉。

-  breakBefore: { enabled: false }, marginTop: pt(2 * LEAD), advancedDesign: chapterHead };
+  breakBefore: { enabled: true, parity: 'any' }, marginTop: pt(2 * LEAD),
+  advancedDesign: chapterHead };

#右页书眉加上章号

{chapterNumber}印出与章名同一章的章号:CXIX · NÃO FAÇA ISSO, QUERIDA。

-  head('recto-chapter', '{chapterTitle}', 'odd', SHIFT),
+  head('recto-chapter', '{chapterNumber} · {chapterTitle}', 'odd', SHIFT),

常见问题

易错点

avoidWidows管栏底,avoidOrphans管栏顶

Postext对两种孤行的命名自成一套:avoidWidows(widowMinLines、widowPenalty)防止段落的首行单独留在栏底,avoidOrphans(orphanMinLines、orphanPenalty)防止段落的末行单独出现在下一栏的栏顶。很多体例手册的叫法正好相反,所以要按作用的位置选择设置。两者默认开启,字符网格(cjk.grid)除外:网格上avoidWidows默认关闭,日本地区的avoidOrphans也关闭,好让页面排满网格所数的行数(postext 1.28起)。两者都以惩罚值的方式起作用:排版时会把每一项与遵守它所留下的空行放在一起权衡。 段末孤行、段首孤行与孤字 →

易错点

:::numbering从下一页起生效

:::numbering从下一个开始的页面起改变计数,而不是当前页。把它紧跟在:::pagebreak之后(第一章之前用parity odd),新的编号才会从你想要的地方开始。 罗马数字页码的前置部分 →

  • 把widowMinLines提高到3,CXIX下面的四行段落就无处可拆:拆成三行和一行,第214页会以单独一行开头。于是标题连同正文移到下一页,第213页少排六行。
  • 如果CXVIII多出几行,CXIX的标题下就只剩一行的空间。这时keepWithNext会把标题带到第214页,第213页会短四五行;设keepWithNext: false时,标题会在第213页末尾孤零零地收尾,下面没有正文。
  • 插页是H1,所以{chapterTitle}会退回到它:插页之后还没有任何一章开始的正文页,比如CXIX移走之后的第213页,会把CAPÍTULO CXXIII印作书眉。给插页的样式设runningChapter: false,书眉就会跳过它。
  • 关闭avoidRunts,全书最后一行就只剩manhã.,而不是nadador da manhã.。
  • 这些分页只对这段文字、9.5/12.6 pt、84 mm行长成立。修改文字、字号或行长后,要重新检查每页页脚和每个标题上方的空白。

致谢

文本
  • Dom Casmurro (1899), from the end of chapter CXVIII to the end of chapter CXXIII, in the spelling of the Garnier edition, with one transcription slip corrected (homens, chapter CXXIII) · Machado de Assis · 公有领域
  • The colophon, and the series name Coleção Casuarina (invented for this recipe) · Postext Cookbook · 原创
图片
  • The plate of the sea off Flamengo under the Sugarloaf, a painting · Generated With Diffusion Models · 原创
  • The casuarina roundel, drawn in code in the book’s palette · Ignacio Ferro · MIT
字体
Tinos (Apache 2.0) · Abril Fatface (SIL OFL 1.1) · League Spartan (SIL OFL 1.1)
沙盒