跳到主要内容
食谱编号6

排版食谱 · 第5章 · 图书结构

前置部分用罗马数字页码,然后从第1页开始

封面和前置各页是不编号的标题,用小写罗马数字计页;:::numbering在小说开始的右页把计数重置为1。

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

页码i · 第1页,共14页

  • 英文样例:尚无中文版本
  • 成品尺寸129 × 198 mm
  • 1栏
  • Fanwood Text 10.5/14
  • Cinzel
  • Playfair Display SC
  • 14页
  • 难度
  • Postext 1.57.1
  • 排版用时88 ms
  • 245行代码

简单来说

袖珍版《Frankenstein》的开头几页。示例演示怎样让正文前的页面用i、ii、iii编页码,正文从第1页重新开始。

成品一览

《Frankenstein》开头几页,排成129 × 198 mm的袖珍经典本:封面是Mont Blanc上的闪电,背面留空;半书名页;Walton的船困在冰中的卷首插图,与书名页相对;版权信息排在书名页背面的低处;献给Godwin的献词在Milton的题词上方;一页目录;以及1818年的Preface。这十页编为i到x,但只有目录和Preface印出页码。Letter I在右页开始小说,上方是北极光,页码1与对页x的位置相同。这封信一直排到Walton的署名。目录里Preface旁的ix和信旁的1都取自排好的页面,PDF阅读器也按同样方式标注页面。封面、卷首插图和北极光是用扩散模型生成的画,以JPEG位图加载;出版社标志和冰晶用代码绘制。

这道食谱解答

  • 怎样用罗马数字给前置部分编页码,并从第1章起重新从1开始?
  • 怎样制作封面、简扉页、扉页和版权页?
  • 怎样添加自动更新的目录(前导符、页码、作者、篇名行)?
  • 怎样插入有意留白的页面,或让某一部分从新的跨页开始?
  • 怎样排题词、献词、署名,或带大号引号的引文框?

简短回答

script.js · 第60–82行在完整代码中
// The page counter starts in lower-case roman: the cover is page i. After the Preface, two
// directives in the Markdown count again from 1 on the next recto:
//   :::pagebreak{parity="odd"}
//   :::numbering{format="decimal" startAt=1}
// :::numbering takes effect on the next page that starts (gotcha: numbering-next-page). Level 1
// breaks to 'odd', so here the pagebreak changes nothing; it finds the recto when a level breaks
// with parity 'any' or not at all. Never with 'always-odd': the blank pages would stack up.
const pageNumbering = { format: 'lower-roman', startAt: 1 }; // startAt spelled out: cover = i
// One folio design, centred under the text block. {pageNumber} prints the page's own label,
// so the same design sets "x" in the front matter and "1" on the first page of the novel.
const folio = (pages) => text(`folio-${pages}`, '{pageNumber}', 'Playfair Display SC', 8.5,
  at('top', 0, 9), { letterSpacing: pt(0.8), pages }); // lower case sets as small capitals
// The front matter is a heading style, unnumbered and left out of the contents (a heading
// can opt back in with {toc="true"}). It prints no running heads and puts a folio on every
// page of the section. Parity 'any', because the contents fall on a verso.
const front = {
  id: 'front', numbered: false, toc: false, breakBefore: { enabled: true, parity: 'any' },
  header: { elements: [] }, footer: { elements: [folio('all')] }, ...frontLook,
};
// The novel keeps the document's furniture: heads on its body pages, the folio on openers.
const footer = { elements: [folio('opener')] };
// Hook-up in config(): page: { pageNumbering }, headingStyles: [front] and footer at the top
// level. frontLook, set above, is the look of the prelims' openers.

用料

类型
Fanwood Text, Playfair Display SC, Cinzel(SIL OFL 1.1)
素材
  • band-1400.jpg
  • cover-1000.jpg
  • plate-1000.jpg
  • The cover: lightning over Mont Blanc and the lake of Geneva (Generated With Diffusion Models, 原创)
  • The frontispiece: a brig beset in pack ice, a dog sledge far off (Generated With Diffusion Models, 原创)
  • Letter I’s opener: northern lights over the frozen Neva (Generated With Diffusion Models, 原创)

做法

#1 · 每一页都是一个带设计的标题

script.js · 第86–148行在完整代码中
// Each leaf is a heading, so it opens its page, gets a PDF bookmark and takes a design. The
// leaves are unnumbered, left out of the contents and print no heads or folio (blind folios).
// Parity 'any', or each would inherit level 1's odd break (gotcha: style-inherits-break).
const blind = { numbered: false, toc: false, breakBefore: { enabled: true, parity: 'any' },
  header: { elements: [] }, footer: { elements: [] } };
const PLATE_H = 126; // mm: the frontispiece, as wide as the text block; its caption hangs below
// mm from the trim: the imprint's eleven lines end on the text block's last baseline.
const IMPRINT_TOP = 126.4; // re-tune it whenever the imprint changes
const leaves = [
  // i: bled to the trim. The painting is as tall as the page, so the cover takes it whole.
  { id: 'cover', ...blind, advancedDesign: design([
    image('cover', at('top-left', 0, 0, { width: 'fill', to: 'bleed' })),
    label('author', '{author}', 9, onPage(22), 'bone', 2.6),
    text('title', '{title}', 'Fanwood Text', 56, onPage(31), { color: col('bone'), lineHeight: 1 }),
    text('subtitle', '{subtitle}', 'Fanwood Text', 13, onPage(53), { ...on('ice'), italic: true }),
    label('series', '{attr.publisher}', 7, onPage(168), 'ice', 2),
  ]) },
  // iii: the half title waits for a recto, so the back of the cover, ii, is left blank.
  { id: 'half', ...blind, breakBefore: { enabled: true, parity: 'odd' }, advancedDesign: design([
    text('title', '{title}', 'Playfair Display SC', 17, at('top', 0, 34), { letterSpacing: pt(3) }),
    image('crystal', at('top', 0, 45, { width: 6 })),
  ]) },
  { id: 'plate', ...blind, advancedDesign: design([ // iv: faces the title page
    image('plate', at('top-left', 0, 0, { width: 'fill' })),
    text('caption', '{attr.caption}', 'Fanwood Text', 8.8,
      at('top', 0, PLATE_H + 5, { width: 80 }), { italic: true, lineHeight: 1.3 }),
    label('source', '{attr.source}', 7.5, at('top', 0, PLATE_H + 16), 'muted', 1.3),
  ]) },
  { id: 'title', ...blind, advancedDesign: design([ // v: four frontmatter fields, one attribute
    label('edition', 'The text of {publishDate}', 7.5, at('top', 0, 22), 'accent', 1.8),
    text('title', '{title}', 'Playfair Display SC', 33, at('top', 0, 30),
      { fontWeight: 700, letterSpacing: pt(0.4), lineHeight: 1 }),
    text('subtitle', '{subtitle}', 'Fanwood Text', 14, at('top', 0, 44), { italic: true }),
    { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.8), color: col('accent'),
      placement: at('top', 0, 60, { width: 14 }) },
    label('author', '{author}', 10, at('top', 0, 66), 'ink', 2.4),
    image('mark', at('top', 0, 126, { width: 11 })),
    label('publisher', '{attr.publisher}', 7.5, at('top', 0, 140), 'muted', 1.5),
  ]) },
  // vi: the section's margins set the small print low, in a 70 mm measure against the outer
  // margin. hidden: the heading opens the page and names its bookmark, and prints nothing.
  { id: 'imprint', ...blind, hidden: true,
    margins: { top: mm(IMPRINT_TOP), left: mm(TRIM_W - OUTER - 70) } },
  // vii: dedication and epigraph, under a heading whose design is an ornament and no title.
  { id: 'quiet', ...blind,
    advancedDesign: design([image('crystal', at('top', 0, 24, { width: 6 }))], 34) },
];
// :::paragraphs blocks set the small print, the dedication, the epigraph and Walton's close;
// the dedication and the verse take one paragraph per line, so no line of them can reflow.
const VERSE_IN = 22; // mm: the indent that centres the verse as a block
const SOURCE_IN = 55; // mm: 'Paradise Lost' ends under the end of the longest line of verse
const prelimStyles = [
  { id: 'small-print', fontSize: pt(7.6), lineHeight: pt(10.4), textAlign: 'left',
    firstLineIndent: pt(0), spaceBetween: pt(5.2) },
  // Small capitals, with the italic lines of the 1818 page, leaded as one inscription.
  { id: 'dedication', fontFamily: 'Playfair Display SC', fontSize: pt(10), lineHeight: pt(15),
    textAlign: 'center', firstLineIndent: pt(0) },
  { id: 'verse', fontSize: pt(10), textAlign: 'left', firstLineIndent: mm(VERSE_IN),
    marginTop: pt(LEAD * 4) }, // on the body's 14 pt leading, which a style inherits
  { id: 'source', fontFamily: 'Cinzel', fontSize: pt(7.5), textAlign: 'left',
    firstLineIndent: mm(SOURCE_IN), marginTop: pt(4), color: col('muted') },
  { id: 'signature', textAlign: 'right', firstLineIndent: pt(0) }, // the letter's close
];

标题会另起一页,并成为PDF书签;它的样式中的设计负责放置这一页的文字和图片。用numbered: false和toc: false,这些页不计入章数,也不进目录。空的页眉和页脚给了它们印刷行所说的暗页码:书名页不印页码,但仍然是第v页,所以Preface能是第ix页。只有半书名页保留了奇数页换页,这让封面背面留空。版权信息、献词、题词和Walton的结尾是:::paragraphs块,用摘录末尾的样式排。

#2 · 目录的页码取自版面

script.js · 第152–161行在完整代码中
// Entries take the body's face and ink, and start on the Preface's first baseline, facing it.
const contents = {
  levels: [{ level: 1, fontSize: pt(12), lineHeight: pt(PREFACE.lead) }],
  // The folio's face: "ix" in small capitals, then a plain 1. The leader takes it too.
  pageNumber: { fontFamily: 'Playfair Display SC', fontSize: pt(10), color: col('accent'),
    width: mm(8) },
  leader: { char: '. ', gap: mm(2.5) },
  // A second line from the heading's to="…" attribute: whom each letter is written to.
  subtitle: { enabled: true, attr: 'to', fontSize: pt(9.5), color: col('muted') },
};

:::toc列出其样式允许的每个标题,并附上标题所在页的标签(目录)。Contents和Preface共用front样式,它设了toc: false,Preface用{toc="true"}覆盖它。Preface和Letter I都不编号,所以它们的条目不印章号。信的条目第二行是它的to="…"属性。页码用页码字体排。条目与Preface第一行从同一条基线开始,所以两个对页对齐。

#3 · 小说在右页开始,上方是北极光

script.js · 第165–180行在完整代码中
const BAND = 104; // mm from the trim to the foot of the drawing, which fades into the paper
// minHeight ends AIR mm below it and the first line takes the next grid line. to: 'bleed' is the
// trim until page.cutLines adds a bleed; 3 mm drop the foot 1.8 mm, still above that line.
const AIR = 2;
const opener = design([
  image('band', at('top-left', 0, 0, { width: 'fill', to: 'bleed' })),
  label('to', '{attr.to}', 7.5, onPage(19), 'ice', 2),
  text('title', '{titleText}', 'Playfair Display SC', 46, onPage(25),
    { color: col('bone'), lineHeight: 1 }),
  text('dateline', '{attr.dateline}', 'Fanwood Text', 10.5, onPage(44),
    { italic: true, color: col('bone') }),
  // The drawing reserves down to its foot. minHeight, counted from the text block (TOP below
  // the trim), is a floor AIR mm lower, which sends the first line one grid line further down.
], BAND - TOP + AIR);
// Walton's letters come before Chapter I: they take the novel's opener but no number.
const letter = { id: 'letter', numbered: false };

这幅画从页面顶端出血,底部渐隐到纸色里,信就从那里开始。章首页一直预留到画的底边,距页面顶端104 mm;minHeight把下限再压低2 mm,信于是晚一个网格行开始,避开渐隐的部分(通栏与高级设计)。眉题和日期行来自标题的to和dateline属性。Letter I还带着{style="letter"},这个样式唯一的设置是numbered: false,因为Walton的信排在Chapter I之前,没有章号。

#4 · 书眉随小说回来

script.js · 第184–196行在完整代码中
const HEAD_Y = 12.5; // mm from the trim to the heads' line
const HEAD_GAP = 8; // mm from the outer margin, where the folio hangs, to the words beside it
const head = (id, content, parity, edge, x, extra = {}) => ({ ...label(id, content, 7.5,
  at(edge, x, HEAD_Y, { to: 'page' }), 'muted', 1.4), align: 'left', parity, pages: 'body',
  ...extra }); // pages: 'body' keeps them off the opener
const numeral = { fontFamily: 'Playfair Display SC', fontSize: pt(8.5), fontWeight: 400,
  letterSpacing: pt(0.6), color: col('ink') }; // folios in the folio face, as on the openers
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', 'top-left', OUTER, numeral),
  head('verso-title', '{title}', 'even', 'top-left', OUTER + HEAD_GAP),
  head('recto-title', '{chapterTitle}', 'odd', 'top-right', -(OUTER + HEAD_GAP)),
  head('recto-folio', '{pageNumber}', 'odd', 'top-right', -OUTER, numeral),
] };

除非样式替换,每一节都沿用文档的页眉和页脚。前置部分把两者都替换了,letter样式没有,所以从第1页起,小说在正文页上印出这些书眉,在章首页上印出简短回答里定义的底部页码。{pageNumber}印出所在页的标签,所以folio()辅助函数用同一个设计在书脊一侧排x、另一侧排1。

#5 · PDF带着同样的页码

script.js · 第398–404行在完整代码中
// :::toc lists each heading with the label of the page it lands on; buildDocument lays the
// document out again until those labels stop moving (ix for the Preface, 1 for Letter I).
const doc = await buildDocumentWithFonts({ markdown, resources }, config(), kitFonts(FONTS));
showPages(doc, { title: 'Frankenstein · the opening leaves' });
// The PDF gets /PageLabels from the same labels: a viewer numbers its pages i–x, then 1, 2.
offerPdf(() => renderToPdf(doc, { fontProvider: fontsourceProvider, resourceBytes: imageBytes }),
  `${RECIPE}.pdf`);

buildDocument会重复排版,直到目录里的页面标签不再变化,所以调用一次就够了。renderToPdf把这些标签写进PDF:在能读取页面标签的阅读器里,页码栏从i走到x,然后从1开始,“转到第1页”会打开Letter I。每个标题都成为以其文字命名的书签,所以这些页用的是朴素的标题:Cover、Half title、Frontispiece、Title page、Imprint、Dedication。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 006 · Front matter: roman folios, then page 1 ═══════════
// https://postext.dev/en/cookbook/front-matter-roman-to-arabic
// Code: MIT · Text: M. & P. B. Shelley, 1818 (PD) · Pictures: diffusion models; marks drawn in code
// Fonts: Fanwood Text, Playfair Display SC, Cinzel (SIL OFL 1.1) · Needs postext ≥ 1.34.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} 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 = 'front-matter-roman-to-arabic';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // every colour in the config links to one of these
  ink: '#1c1d21', // text: a cold near-black
  accent: '#3d5a70', // steel blue, the one accent on paper: contents folios, the rule, the marks
  muted: '#6a665f', // running heads, sources, the second lines of the contents
  paper: '#fbf8f2', // the page
  night: '#131a24', ice: '#a9c6d6', bone: '#efe9dc', // the drawings' inks, and the type on them
};
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 accent, so nothing prints blue.
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
const TRIM_W = 129, TRIM_H = 198; // mm: a pocket classic
const TOP = 21, BOTTOM = 23, INNER = 17, OUTER = 15; // margins, mm; heads align to OUTER
const MEASURE = TRIM_W - INNER - OUTER; // 97 mm: the text block, and the frontispiece's width
const LEAD = 14; // body leading in pt: 31 lines fill the text block
const PREFACE = { size: 9.8, lead: 13.3 }; // pt: the preface, a size under the novel's 10.5/14

// Design-slot shorthands. at(): a placement from an anchor's edge; edge 'top' centres it.
const wide = (width) => (width === 'fill' ? 'fill' : mm(width));
const at = (edge, x, y, { width, to = 'container' } = {}) => ({ anchor: { to, edge },
  offset: { x: mm(x), y: mm(y) }, ...(width !== undefined && { size: { width: wide(width) } }) });
const onPage = (y) => at('top', 0, y, { to: 'page' }); // centred on the page, y mm from the trim
const on = (id) => ({ color: col(id) }); // a design text's ink
// overflow 'wrap' for the running heads too, where a text too wide would end in '…'.
const text = (id, content, font, size, placement, extra = {}) => ({ kind: 'text', id, content,
  fontFamily: font, fontSize: pt(size), color: col('ink'), align: 'center', overflow: 'wrap',
  placement, ...extra });
const label = (id, content, size, placement, ink, tracking) => text(id, content, 'Cinzel', size,
  placement, { fontWeight: 600, letterSpacing: pt(tracking), textTransform: 'uppercase',
    ...on(ink) }); // the label voice: Cinzel capitals, tracked
const image = (id, placement) => ({ kind: 'image', id, resourceId: id, placement });
// A heading's design. minHeight (mm) where the text must start lower than the elements reach.
const design = (elements, minHeight) => ({ enabled: true, slot: { elements },

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

变化

#前置各页用大写罗马数字

大写数字在页码字体里印成完整的大写字母,比小写数字得到的小型大写字母高。

-const pageNumbering = { format: 'lower-roman', startAt: 1 }; // startAt spelled out: cover = i
+const pageNumbering = { format: 'upper-roman', startAt: 1 }; // startAt spelled out: cover = I

#全书连续编号

去掉startAt,计数就以阿拉伯数字接着往下编,于是Letter I成为第11页,和那些把前置部分与正文一起计页的书一样。

-:::numbering{format="decimal" startAt=1}
+:::numbering{format="decimal"}

#去掉卷首插图

删除# Frontispiece标题,把书名页送到右页:第iv页保持空白,Preface仍是第ix页,Letter I仍是第1页。

-  { id: 'title', ...blind, advancedDesign: design([ // v: four frontmatter fields, one attribute
+  { id: 'title', ...blind, breakBefore: { enabled: true, parity: 'odd' },
+    advancedDesign: design([ // v: four frontmatter fields, one attribute

#为印刷加上出血和裁切线

用cutLines后,封面和章首页(两者都锚定在出血上)会延伸到成品线外3 mm;页码和页数不变(裁切线)。

     backgroundColor: col('paper'), pageNumbering,
+    cutLines: { enabled: true }, // a 3 mm bleed and crop marks

常见问题

易错点

:::numbering从下一页起生效

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

易错点

标题样式会继承其级别的分页设置

headingStyles中的条目没有写出的字段都取自它所属的标题级别,breakBefore也不例外。在:::pagebreak之后以H1设置样式的目录页或版权页会继承parity 'odd',结果落在一张空白页之后。给这类样式设置breakBefore: { enabled: false }。 标题样式 →

易错点

第1页是右页:按物理页码规划页面

第1页是右页(奇数页),第2页是第一个左页(偶数页),所以要按物理页码规划跨页:偶数页上的章首页对着它后面的奇数页。 换页与换栏 →

易错点

一页采用最后一个在该页开始的节的页眉

两个标题样式在同一页开始时,这一页采用后一个的页眉、页脚和调色板。分隔用的空白页属于它前面的章,为奇偶页补的空白页属于它后面的章。 分节书眉 →

沙盒检查 · parityCascade

奇偶分页连续补页

原因. 奇偶换页层层叠加,连续产生了两页以上的空白页。

解决. 检查always-odd换页和breakBefore的奇偶设置;章较短时,parity设为any通常可以避免这些空白页。 文档 →

沙盒检查 · numberingInvalidFormat

编号格式无效

原因. 某个:::numbering的format不是decimal、lower-roman、upper-roman、lower-alpha或upper-alpha之一。

解决. 使用这五种页码格式之一;列表和资源的格式写法不同。 文档 →

  • 这些页继承了一级标题的'odd'换页。不处理的话,每一页都会等到右页才开始。前置部分就会从十页变成十四页,多出四个空白左页,卷首插图对着的也是空白页而不是书名页。像本食谱这样用parity: 'any'重新声明换页,或者用breakBefore: { enabled: false }关掉它;无论哪种,通栏标题都会另起一页。
  • 当一级标题以引擎自己的H1默认值'always-odd'换页时,去掉:::numbering前面的:::pagebreak。如果它之前的文字结束在右页,pagebreak会补到下一个右页,然后标题再加上它的分隔页并再补一次,结果是连续三个空白页而不是一个。像这里在'odd'换页之后,pagebreak不起作用;在'any'之后,正是它把第1页放到右页。
  • Preface多出一页,会把Letter I往后推两页,因为奇数页换页会在它前面加一个空白左页。每次编辑前置部分后,都要检查页数。
  • 目录的前导点用页码的字体和颜色排,toc.leader没有单独的颜色设置,所以这里印成钢蓝色。

致谢

文本
  • Frankenstein; or, The Modern Prometheus (1818): the dedication, Letter I, a line of Letter IV (the frontispiece caption) and one of volume I, chapter VI (quoted in a code comment) · Mary Shelley · 公有领域
  • The unsigned Preface of the 1818 edition (by common attribution) · Percy Bysshe Shelley · 公有领域
  • Paradise Lost (1667), book X, lines 743–745: the epigraph of the 1818 title leaves · John Milton · 公有领域
图片
  • The cover: lightning over Mont Blanc and the lake of Geneva · Generated With Diffusion Models · 原创
  • The frontispiece: a brig beset in pack ice, a dog sledge far off · Generated With Diffusion Models · 原创
  • Letter I’s opener: northern lights over the frozen Neva · Generated With Diffusion Models · 原创
字体
Fanwood Text (SIL OFL 1.1) · Playfair Display SC (SIL OFL 1.1) · Cinzel (SIL OFL 1.1)
沙盒PDF