跳到主要内容
食谱编号5

排版食谱 · 第4章 · 书眉与页码

随笔集中按奇偶页设置的书眉

按奇偶页和页面角色筛选页眉元素:左页排书名,右页排截短的随笔标题,页码标签挂在页边距里,章首页只在页脚排页码。

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

页码30–31 · 第2–3页,共5页

  • 英文样例:尚无中文版本
  • 成品尺寸132 × 198 mm
  • 1栏
  • Baskervville 10.2/14
  • Alegreya SC
  • Libre Caslon Display
  • 5页
  • 难度
  • Postext 1.57.1
  • 排版用时63 ms
  • 174行代码

简单来说

一本袖珍散文集里的五页。示例演示怎样在左页顶部印书名,在右页顶部印文章标题。

成品一览

一部袖珍版蒙田《随笔集》(Charles Cotton英译本)的第29至33页:第IV章全文和第V章的开头。左页书眉排书名,用加宽字距的大写字母;右页书眉排随笔标题,用斜体,并以省略号截短,因为第IV章的标题长达81个字符。每个页码都放在外侧页边距里的一个深褐色标签中,与版心边缘的一条0.5 pt细线相距3 mm。章首页不排书眉,只在页脚排页码。正文用Baskervville,排在暖白色的页面上;标题用Libre Caslon Display,页码和书名用Alegreya SC。每一个书眉都在配置中定义,因此引擎内置的书眉(8 pt的蓝色Open Sans)永远不会印出来。

这道食谱解答

  • 怎样设置书眉:左页放书名,右页放章名,页码在外侧?
  • 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?
  • 怎样让标题、粗体字和项目符号不再显示成蓝色?

简短回答

script.js · 第67–95行在完整代码中
const GAP = 3; // mm from the tab to the divider, which lands on the text block's edge
const HEAD = 12; // mm from the top edge of the page to the top of the tab
const CAP = t({ en: 60.5, es: 66.8 }); // mm: the head's widest line, set per edition so
// essay IV's is cut after 'false' and 'objetos'; a short running title suits any title
const page = (edge) => ({ to: 'page', edge }); // the trim box (gotcha: negative-offsets)
const header = { elements: [
  // Verso (even): tab | divider | THE BOOK'S TITLE from the frontmatter
  folio({ id: 'folio-even', parity: 'even', pages: 'body', anchor: page('top-left'),
    x: OUTER - GAP - TAB, y: HEAD }),
  divider({ id: 'rule-even', parity: 'even', from: 'folio-even', edge: 'right-of', x: GAP }),
  { kind: 'text', id: 'book', content: '{title}', parity: 'even', pages: 'body',
    fontFamily: 'Alegreya SC', fontSize: pt(FOLIO_PT), lineHeight: LINE / FOLIO_PT,
    color: col('muted'), textTransform: 'uppercase', letterSpacing: pt(1.3),
    placement: { anchor: { to: '#rule-even', edge: 'right-of' }, // top: the tab's top
      offset: { x: mm(2.5), y: pt(PAD.top) } } }, // PAD.top down: on the folio's baseline
  // Recto (odd): the essay's title, cut short with an ellipsis | divider | tab.
  folio({ id: 'folio-odd', parity: 'odd', pages: 'body', anchor: page('top-right'),
    x: -(OUTER - GAP - TAB), y: HEAD }), // from the right edge, a negative x runs inwards
  divider({ id: 'rule-odd', parity: 'odd', from: 'folio-odd', edge: 'left-of', x: -GAP }),
  { kind: 'text', id: 'essay', content: '{chapterTitle}', parity: 'odd', pages: 'body',
    fontFamily: 'Baskervville', italic: true, fontSize: pt(TITLE_PT),
    lineHeight: LINE / TITLE_PT, color: col('muted'),
    overflow: 'ellipsis-end', // stated, though a running head's default
    placement: { anchor: { to: '#rule-odd', edge: 'left-of' },
      offset: { x: mm(-2.5), y: pt(PAD.top) }, size: { maxWidth: mm(CAP) } } },
] };
// Openers: no head, only a drop folio HEAD above the page's foot, centred under the text block.
const footer = { elements: [folio({ id: 'drop-folio', parity: 'all', pages: 'opener',
  anchor: { to: 'container', edge: 'bottom' }, y: -HEAD })] };

用料

类型
Baskervville, Libre Caslon Display, Alegreya SC(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 替换内置的蓝色书眉

script.js · 第14–26行在完整代码中
const palette = {
  ink: '#1f1b16', // text: a warm near-black, never #000
  sepia: '#8a5a2b', // the one accent: folio tabs, dividers, opener plates (5.3:1 on paper)
  muted: '#6f6558', // running heads, verse glosses, the colophon (5.2:1 on paper)
  paper: '#f8f4ec', // a warm off-white page; also the type reversed out of the sepia
};
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' } })),
  // Text-style defaults (headings, bold, italic, lists, boxes) link to 'main-color':
  // pointed at the sepia, none of them prints in the engine's blue.
  { id: 'main-color', name: 'sepia (defaults)', value: { hex: palette.sepia, model: 'hex' } },
];

配置中的每种颜色都链接到这些条目之一。如果不定义header,引擎会画出内置书眉:右页排{title}、左页排{chapterTitle},下面一条1 pt的线,字体为Open Sans 600、8 pt、#295AA3蓝色(页眉和页脚)。把main-color指向深褐色,会改变文字的默认颜色,那个书眉也跟着变色,但它的字体、线条和每个标题所在的页面仍是引擎定的。所以这里每个书眉都完整写出,每种颜色都链接到调色板的一个条目。粗体、斜体和引用默认也取main-color,bodyText把它们设回墨色。

#2 · 用同一个标签搭出所有书眉

script.js · 第31–48行在完整代码中
const FOLIO_PT = 7.5; // pt: the folio, and the book's title in capitals beside it
const TITLE_PT = 8.8; // pt: the essay's title, whose italic lowercase reads small at 7.5
const LINE = FOLIO_PT * 1.2; // pt: the folio and both titles share this line box and baseline
const PAD = { top: 1, bottom: 1.4 }; // pt: 0.4 pt more below for the descending 3, 5, 7, 9
const TAB = 7.5; // mm: the tab's width
const TAB_H = LINE + PAD.top + PAD.bottom; // pt: the tab's height, which the divider matches
const folio = ({ id, parity, pages, anchor, x = 0, y }) => ({
  kind: 'text', id, content: '{pageNumber}', parity, pages, fontFamily: 'Alegreya SC',
  fontSize: pt(FOLIO_PT), fontWeight: 700, lineHeight: LINE / FOLIO_PT, color: col('paper'),
  box: { backgroundColor: col('sepia'), borderRadius: mm(2), // a pill; the number is centred
    padding: { top: pt(PAD.top), bottom: pt(PAD.bottom) } }, // by default in both directions
  placement: { anchor, offset: { x: mm(x), y: mm(y) }, size: { width: mm(TAB) } },
});
const divider = ({ id, parity, from, edge, x }) => ({
  kind: 'rule', id, parity, pages: 'body', direction: 'vertical', thickness: pt(0.5),
  color: col('sepia'), placement: { anchor: { to: `#${from}`, edge }, offset: { x: mm(x) },
    size: { height: pt(TAB_H) } },
});

简短回答中把每个书眉的标签锚定到'page',因此偏移量从成品边缘量起。从top-right角出发,正的x会跑出页面,所以右页的标签取负值。分隔线挂在标签上,标题挂在分隔线上(元素定位)。标签的尺寸写成具名常量,因为其他部件都以它们为基准。分隔线高TAB_H。页码和两种标题共用一个9 pt的行框,而设计文本的基线位于行框自上而下80%处,所以无论字号多大,三者都站在同一条基线上。

第30页,左页:从外侧页边距向内依次是标签、分隔线和书名。
第31页,右页:随笔标题在分隔线前被截断,标签位于外侧页边距。

#3 · 镜像页边距,按整行计算

script.js · 第52–63行在完整代码中
const TRIM = { width: 132, height: 198 }; // mm
const TOP = 22; // mm: the top margin, which holds the heads (gotcha: header-paints-over-text)
const INNER = 15; // mm
const OUTER = 21; // mm: the outer margin, where the folio tabs hang
const LINES = 31; // whole lines of LEAD in the text block, so full pages end level
const MEASURE = TRIM.width - INNER - OUTER; // the text block's width: 96 mm
const geometry = { // left is the inner margin on a recto; mirror swaps it on the versos
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, // 150 dpi is for the screen
  backgroundColor: col('paper'),
  margins: { top: mm(TOP), bottom: mm(TRIM.height - TOP - (LINES * LEAD * 25.4) / 72),
    left: mm(INNER), right: mm(OUTER), mirror: true },
};

设了mirror: true后,right页边距在右页是外侧,到左页就移到左边,所以OUTER始终是切口一侧的页边距,标签就挂在那里。下页边距由成品尺寸、上页边距和LINES推算,因此无论成品尺寸和上页边距怎么设,版心都正好是LINES行、每行LEAD(这里是31 × 14 pt)。每个排满的页面都在第31行结束,所以30和31这样左右相对的两页底部齐平。

#4 · 让章首页显示完整标题

script.js · 第99–121行在完整代码中
const PLATE = 26; // mm: a square plate at the head of the text block
const opener = {
  enabled: true,
  minHeight: mm(58), // the text of every essay starts on the same line
  slot: { elements: [
    { kind: 'box', id: 'plate', style: { backgroundColor: col('sepia') },
      placement: { anchor: { to: 'container', edge: 'top-left' },
        size: { width: mm(PLATE), height: mm(PLATE) } } },
    { kind: 'text', id: 'numeral', content: '{attr.num}', fontFamily: 'Libre Caslon Display',
      fontSize: pt(48), lineHeight: 1, color: col('paper'),
      placement: { anchor: { to: '#plate', edge: 'align-top' }, // the plate's own box, where
        size: { width: mm(PLATE), height: mm(PLATE) } } }, // the text centres by default
    { kind: 'text', id: 'book', content: '{subtitle}', fontFamily: 'Alegreya SC',
      fontSize: pt(8.5), letterSpacing: pt(1.4), color: col('sepia'), align: 'left',
      placement: { anchor: { to: 'container', edge: 'top-left' },
        offset: { x: mm(PLATE + 4.5), y: mm(PLATE - 2.9) } } }, // its baseline on the plate's foot
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Libre Caslon Display',
      fontSize: pt(20), lineHeight: 1.12, color: col('ink'), align: 'left',
      overflow: 'wrap', // an opener shows the whole title; the running head cuts it
      placement: { anchor: { to: '#plate', edge: 'below' }, offset: { y: mm(6) },
        size: { width: mm(MEASURE) } } },
  ] },
};

章首页用overflow: 'wrap'完整排出标题,并在标题行中每个\\处换行。只有通页的章首页才遵守这些换行,所以该级别设了span: 'page'(标题中的换行)。在书眉中,{chapterTitle}总是把标题放在一行里,省略号截断的就是这一行。序号来自标题的{num="IV"}属性,标牌旁的小型大写字母来自前置元数据中的subtitle。

#5 · 每篇随笔都从右页开始

script.js · 第139–147行在完整代码中
  headings: { // 400, the face's only weight: the default 700 would ask for one that is missing
    fontFamily: 'Libre Caslon Display', fontWeight: 400, color: col('ink'),
    levels: [
      // 'odd' puts every opener on a recto, adding a blank verso when one is needed;
      // span: 'page' makes the design an opener, where a \\ in the title breaks the line.
      { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
        marginTop: pt(0), marginBottom: pt(0), advancedDesign: opener },
    ],
  },

用'odd'时,在右页结束的随笔后面会跟一个空白左页。西班牙语版就有一个,因为它的第IV章在第31页结束;这里第IV章在第32页(左页)结束,第V章从第33页开始。书眉的pages: 'body'让它们不出现在空白页和章首页上(章首页即第一个块是该标题的页面)。页脚的pages: 'opener'只在章首页印出页脚页码(文本元素)。

#6 · 页码从29开始

script.js · 第251–255行在完整代码中
// 28 pages come before this one, so recto and verso, the mirrored margins and the odd/even
// heads follow the book page (gotcha: parity-page1-recto); the folios start at 29.
const continuation = { pageIndexOffset: 28, pageNumbering: { startAt: 29 } };
const doc = await buildDocumentWithFonts({ markdown, continuation }, config(), kitFonts(FONTS));
showPages(doc, { title: t({ en: 'Running heads by parity', es: 'Cabeceras según la paridad' }) });

parity按页面在书中的位置判断,不看它的页码。pageIndexOffset: 28给每页的序号加28,于是左右页、对称页边距和奇偶书眉都跟随书中的页序。pageNumbering.startAt再把这些页编为29到33。没有这个偏移,本次排版的第一页无论印什么页码都是右页。这恰好适合第29页,但如果设startAt: 30,书名会落在第31页、随笔标题落在第32页,各自排错了跨页的一侧。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 005 · Running heads by parity ═══════════════════════════════
// https://postext.dev/en/cookbook/running-heads-by-parity
// Code: MIT · Text: Montaigne, tr. Cotton (PD, Gutenberg #3600); es: new translation (MIT)
// Fonts: Baskervville, Libre Caslon Display, Alegreya SC (SIL OFL 1.1) · Needs postext ≥ 1.57.0
// Pages 29 to 33 of a pocket Montaigne. Versos carry the book's title and rectos the essay's,
// the folios sit in tabs in the outer margin, and an opener prints only a folio at the foot.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'running-heads-by-parity';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: four colours with semantic ids; main-color points at the sepia
const palette = {
  ink: '#1f1b16', // text: a warm near-black, never #000
  sepia: '#8a5a2b', // the one accent: folio tabs, dividers, opener plates (5.3:1 on paper)
  muted: '#6f6558', // running heads, verse glosses, the colophon (5.2:1 on paper)
  paper: '#f8f4ec', // a warm off-white page; also the type reversed out of the sepia
};
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' } })),
  // Text-style defaults (headings, bold, italic, lists, boxes) link to 'main-color':
  // pointed at the sepia, none of them prints in the engine's blue.
  { id: 'main-color', name: 'sepia (defaults)', value: { hex: palette.sepia, model: 'hex' } },
];
// #endregion
const LEAD = 14; // pt: the body leading, the pitch of the baseline grid

// #region tab: the folio tab and its divider, the two pieces every head is built from
const FOLIO_PT = 7.5; // pt: the folio, and the book's title in capitals beside it
const TITLE_PT = 8.8; // pt: the essay's title, whose italic lowercase reads small at 7.5
const LINE = FOLIO_PT * 1.2; // pt: the folio and both titles share this line box and baseline
const PAD = { top: 1, bottom: 1.4 }; // pt: 0.4 pt more below for the descending 3, 5, 7, 9
const TAB = 7.5; // mm: the tab's width
const TAB_H = LINE + PAD.top + PAD.bottom; // pt: the tab's height, which the divider matches
const folio = ({ id, parity, pages, anchor, x = 0, y }) => ({
  kind: 'text', id, content: '{pageNumber}', parity, pages, fontFamily: 'Alegreya SC',
  fontSize: pt(FOLIO_PT), fontWeight: 700, lineHeight: LINE / FOLIO_PT, color: col('paper'),
  box: { backgroundColor: col('sepia'), borderRadius: mm(2), // a pill; the number is centred
    padding: { top: pt(PAD.top), bottom: pt(PAD.bottom) } }, // by default in both directions
  placement: { anchor, offset: { x: mm(x), y: mm(y) }, size: { width: mm(TAB) } },
});
const divider = ({ id, parity, from, edge, x }) => ({
  kind: 'rule', id, parity, pages: 'body', direction: 'vertical', thickness: pt(0.5),
  color: col('sepia'), placement: { anchor: { to: `#${from}`, edge }, offset: { x: mm(x) },
    size: { height: pt(TAB_H) } },
});

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

变化

#给长标题一个短书眉

在标题行中写上书眉用的短标题{num="IV" short="Of false objects"},然后印这个属性而不是完整标题;没有short的章在那里什么也不印。

-  { kind: 'text', id: 'essay', content: '{chapterTitle}', parity: 'odd', pages: 'body',
+  { kind: 'text', id: 'essay', content: '{attr.short}', parity: 'odd', pages: 'body',

#随笔可以从任一侧开始

用'any'时,随笔从下一页开始,不论左右页,也不会添加空白页。标牌挂在版心的左上角,页脚页码居中排在它下方,所以两者在左页和右页上的位置相同。

-      { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
+      { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'any' },

#把空白左页涂成强调色

带pages: 'blank'的框只印在为右页分页而添加的页面上:在西班牙语版中,它把第32页填成深褐色;英语版没有空白页,版面不变。

 const footer = { elements: [folio({ id: 'drop-folio', parity: 'all', pages: 'opener',
-  anchor: { to: 'container', edge: 'bottom' }, y: -HEAD })] };
+  anchor: { to: 'container', edge: 'bottom' }, y: -HEAD }),
+  { kind: 'box', id: 'blank-leaf', pages: 'blank', style: { backgroundColor: col('sepia') },
+    placement: { anchor: page('top-left'),
+      size: { width: mm(TRIM.width), height: mm(TRIM.height) } } }] };

常见问题

易错点

相对容器的负偏移什么也渲染不出来

自动宽度的设计文本被限制在容器内,所以相对容器的负偏移会把它推出去,结果什么也不渲染。把这类元素用明确的mm偏移锚定到页面或出血上,或者给它们一个固定宽度。 锚定设计元素 →

易错点

页眉和页脚元素画在正文之上

页眉和页脚元素画在页面上层,正文区域不会为它们让出位置。把它们放在页边距以内,为它们预留空间的正是页边距。 书眉与页码 →

易错点

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

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

版面警告 · designTextTruncated

设计槽位中的文字被截断

原因. 设计中的文字元素(书眉、页码、章首页、篇名页、目录中的篇名行)比它的空间宽,一行被截掉了一部分:按overflow的设置以省略号截断,或在overflow: 'clip'下超出框的部分被裁掉。书眉、页码和目录篇名行默认以省略号截断;标题和篇名页的设计默认换行,除非元素另有设置。一章每页都被截断的书眉只报告一次。

解决. 给元素设overflow: 'wrap'让文字分成几行,或给它更多空间(加大size.width或maxWidth、缩小fontSize、收紧letterSpacing),或缩短文字。设计本来就是单行的地方(如书眉)保留省略号即可,这时警告只是确认被截掉的内容。 文档 →

沙盒检查 · headerFooterUnknownPlaceholder

未知占位符

原因. 某段设计文字用了不存在的{name}占位符,因此它按原样印出。

解决. 使用支持的占位符({pageNumber}、{chapterTitle}、{title}、{attr.x}……),字面花括号写成{{ and }}。 文档 →

沙盒检查 · headerFooterMetadataMissing

缺少占位符元数据

原因. {author}或{title}之类的占位符在文档元数据中没有值,因此印出来是空的。

解决. 把这个值加到前置元数据里并加上引号,或删掉该占位符。 文档 →

  • CAP是按每个版本中第IV篇随笔的标题定的,这里只有它出现在书眉中。省略号跟在排得下的最后一个完整单词之后,而这个词可能是介词:取64 mm时,西班牙语版的书眉会以“…sus pasiones sobre…”结尾。长标题多的书需要“变化”中的短书眉。
  • 在英语版中,把minWordSpacing从0.7提高到0.75,第V篇的第一行(“Quintus Marcius, the Roman legate…”)在允许范围内找不到断点,词间距会拉到正常宽度的2.4倍。西班牙语版没有这样的行。每次修改后,都要检查截图列出的最松的几行。

致谢

文本
字体
Baskervville (SIL OFL 1.1) · Libre Caslon Display (SIL OFL 1.1) · Alegreya SC (SIL OFL 1.1)
沙盒