跳到主要内容
食谱编号46

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

带移动书边索引的词典

用一个循环为每个字母生成一种标题样式,每种样式的页脚在书口上把该字母的藏青色标签印得比上一个低一格,下面衬着一排浅色索引。

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

第2–3页,共4页

  • 英文样例:尚无中文版本
  • 成品尺寸150 × 200 mm
  • 2栏, 栏间距4 mm
  • Alegreya 8/10.4
  • Alegreya SC
  • Alegreya Sans SC
  • 4页
  • 难度
  • Postext 1.65.0
  • 排版用时172 ms
  • 180行代码

简单来说

一部旧航海词典的四页。每页外侧边缘印着一列字母标签,当前字母的标签颜色加深,并随字母顺序逐级下移。

成品一览

这是Smyth海军上将《Sailor’s Word-Book》(1867)新版中的四页,收A到C三个字母,页面尺寸为150 × 200 mm。词条排成两栏两端对齐的8 pt Alegreya,栏间有一条细线;每个词头为藏青色粗体,每条的转行悬挂缩进1 em。一个字母在栏内开始,用一个四行高的藏青色首字母加一道黄铜色细线标出,词条接着排下去,不另起一页。每一页的书口都印着完整的书边索引,共22个浅色标签。当前字母的标签为藏青色,比其他标签宽3.2 mm,每换一个字母就下移一格。西班牙文版保留Smyth的英文词头,翻译了释义。

这道食谱解答

  • 怎样做书边索引:切口上的标签随着字母或章逐页下移?
  • 怎样排参考文献或术语表(悬挂缩进、较小字号)?
  • 怎样给正文或框里的关键词(粗体或斜体)着色?
  • 怎样设置书眉:左页放书名,右页放章名,页码在外侧?
  • 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?

简短回答

script.js · 第36–68行在完整代码中
// 22 tabs share the 44 lines of the text block, two lines each; letters with few words
// share a tab, as in most thumb-indexed dictionaries.
const TABS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  'QR', 'S', 'T', 'UV', 'W', 'XYZ'];
const STEP = 2 * LEAD * PT; // mm: 7.34
const EDGE = { odd: 'top-right', even: 'top-left' }; // the fore-edge: right on a recto
// A cell of the index, `width` mm inside the trim and 3 mm past it, so that only its
// inner corners show their rounding; its label is centred on the part inside the trim.
const cell = (id, label, i, parity, width, fill, ink, size) => {
  const y = mm(TOP + i * STEP + 0.3); // a 0.6 mm gap between neighbours
  const at = (x, w) => ({ anchor: { to: 'page', edge: EDGE[parity] }, offset: { x: mm(x), y },
    size: { width: mm(w), height: mm(STEP - 0.6) } });
  return [
    { kind: 'box', id, parity, style: { backgroundColor: col(fill), borderRadius: mm(1.2) },
      placement: at(parity === 'odd' ? 3 : -3, width + 3) },
    { kind: 'text', id: `${id}-label`, parity, content: label, fontFamily: LABEL, fontWeight: 700,
      fontSize: pt(size), lineHeight: 1, color: col(ink), align: 'center',
      verticalAlign: 'middle', placement: at(0, width) },
  ];
};
const sides = (make) => ['odd', 'even'].flatMap(make);
// Every page prints the whole index, pale, in the book's header (hook-up in config()).
const ladder = sides((p) => TABS.flatMap((label, i) =>
  cell(`index-${i}-${p}`, label, i, p, 5.8, 'tint', 'slate', 7.5)));
// The section's own tab goes in its footer, which paints after the header: it lies over
// its pale cell. Elements anchored to 'page' can sit anywhere, whichever slot holds them.
const letterStyles = () => TABS.flatMap((label, i) => [...label].map((letter) => ({
  id: letter,
  // '# B {style="B"}' opens the section of B: its pages take this footer, and a page
  // where A ends and B begins takes B's (gotcha: section-last-wins).
  footer: { elements: [...footer.elements,
    ...sides((p) => cell(`tab-${p}`, label, i, p, 9, 'navy', 'paper', 8.5))] },
})));

用料

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

做法

#1 · 给每个字母一种带自己标签的标题样式

代码就是上面的简短回答。TABS列出22个标签,少见的字母合用一个(QR、UV、XYZ)。每个标签高两行正文(7.34 mm),加起来正好填满版心的44行。全书的header在每一页印出书眉和浅色索引。循环为每个字母生成一种标题样式,其footer加上该字母的藏青色标签,位置比上一个低一格(标题样式)。页脚在页眉之后绘制,所以标签盖在它的浅色单元格上,配置里的索引也只写一次,而不是每个字母一次。每个单元格写两次,每种parity一次,并且锚定到页面外侧边缘,而不是所在槽位自身的容器。那个容器只是与版心等宽的页边区域,而标签需要伸到书口,并且纵贯全部44行。一页采用最后一个在该页开始的部分的页脚,所以第2页从A开始,却显示B的标签。

#2 · 第一页不印书眉

script.js · 第72–88行在完整代码中
const HEAD = 11; // mm from the trim's top to the heads' top
const head = (id, content, parity, edge, x, extra) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: LABEL, fontSize: pt(8), fontWeight: 500, letterSpacing: pt(1),
  color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(HEAD) } }, ...extra });
const folio = { fontWeight: 700, color: col('ink'), letterSpacing: pt(0) };
const runningHeads = [
  head('verso-folio', '{pageNumber}', 'even', 'top-left', OUTER, folio),
  head('verso-title', '{title}', 'even', 'top-left', OUTER + 8),
  head('recto-letter', t({ en: 'Letter {chapterTitle}', es: 'Letra {chapterTitle}' }), 'odd',
    'top-right', -(OUTER + 8)),
  head('recto-folio', '{pageNumber}', 'odd', 'top-right', -OUTER, folio),
];
// The first page drops its folio to the foot, under the text block.
const footer = { elements: [head('drop-folio', '{pageNumber}', 'all', 'top', 0, {
  ...folio, pages: 'opener',
  placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: mm(6) } } })] };

书眉是锚定在页面上的文本元素,用parity和pages: 'body'筛选(文本元素)。第1页的书名横跨两栏,使第1页成为章首页。书眉会跳过它,它的页码来自页脚:一个设为pages: 'opener'的元素,印在版心下方6 mm处。在右页上,{chapterTitle}印出该页或此前最后一个一级标题的字母,即Letter C。{firstMark.<key>}和{lastMark.<key>}可以印出一页的第一个和最后一个词头,也就是大多数词典都有的书眉词;本食谱保留字母,因为它的书边标签标的就是字母。

#3 · 只给词头上色

script.js · 第92–105行在完整代码中
// Bold and italic default to the engine's blue: the body sets them to ink, and only the
// entry style prints its bold, the headwords, in navy.
const BODY = 8; // pt: a reference size, about 50 characters to the 60 mm column
const bodyText = { fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), firstLineIndent: mm(0),
  minWordSpacing: 0.7, maxWordSpacing: 1.7,
  maxJustifyTracking: 10, // up to 1 % of tracking before a line's spaces pass 1.7
  hyphenateAcrossColumns: false, // no word is divided over a column or a page
  maxRuntTracking: 0 }; // a short last line stays: tracking it away crushes a 60 mm line
const paragraphStyles = [
  { id: 'entry', hangingIndent: em(1), boldColor: col('navy') }, // :::paragraphs{style="entry"}
  { id: 'colophon', fontSize: pt(7), color: col('muted'), textAlign: 'center',
    marginTop: pt(LEAD) },
];

每个词条是:::paragraphs{style="entry"}块中的一个段落,转行悬挂缩进1 em(段落样式)。正文的boldColor让第1页上的**Note.**保持墨色,词条样式的boldColor把词头印成藏青色。段落样式没有italicColor,所以斜体的子词条保持正文的墨色。

#4 · 在栏内放一个四行下沉的首字母

script.js · 第109–122行在完整代码中
// Its cap line meets the first body line's and it stands on the fourth baseline. Cap
// heights measured on the glyph H: 0.652 em in Alegreya SC 900, 0.646 em in Alegreya.
const DROP = 4; // body lines the letter spans
const LETTER = ((DROP - 1) * LEAD + 0.646 * BODY) / 0.652; // pt: 55.8
// A design line sets its baseline 0.8 of its height below its top; a body line does too.
const letterHead = { enabled: true, slot: { elements: [
  { kind: 'text', id: 'letter', content: '{titleText}', fontFamily: DISPLAY, fontWeight: 900,
    fontSize: pt(LETTER), lineHeight: 1, color: col('navy'), align: 'left',
    placement: { anchor: { to: 'container', edge: 'top-left' },
      offset: { y: pt((DROP - 0.2) * LEAD - 0.8 * LETTER) } } },
  { kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.6), color: col('brass'),
    placement: { anchor: { to: 'container', edge: 'top-left' },
      offset: { y: pt((DROP + 0.55) * LEAD) }, size: { width: 'fill' } } },
] } };

LETTER为55.8 pt:在这个字号下,Alegreya SC 900的大写字母(高0.652 em)立在第四条基线上,顶端正好到达第一行正文的大写字母高度线。字母和黄铜色细线在第五行之内结束,标题块向下对齐到下一条网格线,所以每个字母标题占五行,下面的词条仍在网格上。在第3页上,第二栏顶部的C与第一栏的前四行平齐。一级标题设置breakBefore: { enabled: false },关掉一级标题默认的换页,各字母样式继承这一设置,所以B和C从栏的中间开始。

#5 · 让书名横跨两栏

script.js · 第126–145行在完整代码中
const centred = (id, content, family, size, lineHeight, y, width, extra) => ({ kind: 'text', id,
  content, fontFamily: family, fontSize: pt(size), lineHeight, color: col('navy'), align: 'center',
  overflow: 'wrap', ...extra, placement: { anchor: { to: 'container', edge: 'top' },
    offset: { y: mm(y) }, size: { width: width ? mm(width) : 'fill' } } });
const rule = (id, y, thickness) => ({ kind: 'rule', id, direction: 'horizontal',
  thickness: pt(thickness), color: col('navy'), placement: { anchor: { to: 'container',
    edge: 'top-left' }, offset: { y: mm(y) }, size: { width: 'fill' } } });
const titleBand = { enabled: true, slot: { elements: [
  { kind: 'image', id: 'anchor', resourceId: 'anchor', placement: {
    anchor: { to: 'container', edge: 'top' }, size: { width: 'auto', height: mm(18) } } },
  centred('name', '{titleText}', DISPLAY, 22, 1.1, 20, 0, { fontWeight: 900 }),
  centred('subtitle', '{attr.subtitle}', TEXT, 9, 1.25, 29.5, 92, { italic: true,
    color: col('ink') }),
  centred('byline', '{attr.byline}', LABEL, 7, 1.2, 38.8, 0, { fontWeight: 500,
    letterSpacing: pt(1.2), color: col('muted') }),
  rule('thick', 43.6, 1.2),
  rule('thin', 45, 0.4),
] } };
// '# The Sailor’s Word-Book {style="title" subtitle="…" byline="…"}' opens page 1.
const titleStyle = { id: 'title', span: 'page', advancedDesign: titleBand };

书名样式横跨整页,是一条色带,里面有锚、书名、从标题属性读取的副标题和作者署名,以及一道双线(通栏与高级版面设计)。细线结束在版心顶部下方45.1 mm处,所以色带对齐到13个网格行,即47.7 mm。栏间线从色带下方开始,也就是两栏正文起始的那一行。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 046 · Dictionary with a moving thumb index ═══════════════
// https://postext.dev/en/cookbook/dictionary-thumb-index
// Code: MIT · Text: W. H. Smyth, 1867 (PD); Spanish translation CC BY 4.0 · Pictures: code
// Fonts: Alegreya, Alegreya SC, Alegreya Sans SC (SIL OFL 1.1) · Needs postext ≥ 1.65.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'dictionary-thumb-index';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: ink and navy on a warm paper, brass for the rules and the rope
const palette = {
  ink: '#1b1f24', // text
  navy: '#1f3a5f', // headwords, letters and tabs
  brass: '#a4834f', // hairlines and the rope of the vignette
  rule: '#cfccc3', // the column rule
  tint: '#e4e9f0', // the pale cells of the index
  slate: '#56657b', // their letters
  muted: '#62666b', // running heads
  paper: '#fbfaf6', // the page, and the letter reversed out of each tab
};
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' } }));
// #endregion
const TEXT = 'Alegreya', DISPLAY = 'Alegreya SC', LABEL = 'Alegreya Sans SC';
const PT = 25.4 / 72; // mm in a point
const LEAD = 10.4; // pt: the body's leading, the grid every column is set on
const LINES = 44; // lines in a full column
const TOP = 18, INNER = 12, OUTER = 14; // mm; the foot margin makes the block whole lines
const BOTTOM = 200 - TOP - LINES * LEAD * PT;

// #region answer: one heading style per letter, each with its tab one step down the fore-edge
// 22 tabs share the 44 lines of the text block, two lines each; letters with few words
// share a tab, as in most thumb-indexed dictionaries.
const TABS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  'QR', 'S', 'T', 'UV', 'W', 'XYZ'];
const STEP = 2 * LEAD * PT; // mm: 7.34
const EDGE = { odd: 'top-right', even: 'top-left' }; // the fore-edge: right on a recto
// A cell of the index, `width` mm inside the trim and 3 mm past it, so that only its
// inner corners show their rounding; its label is centred on the part inside the trim.
const cell = (id, label, i, parity, width, fill, ink, size) => {
  const y = mm(TOP + i * STEP + 0.3); // a 0.6 mm gap between neighbours
  const at = (x, w) => ({ anchor: { to: 'page', edge: EDGE[parity] }, offset: { x: mm(x), y },
    size: { width: mm(w), height: mm(STEP - 0.6) } });
  return [

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

变化

#只印当前标签

印刷的书边索引通常只显示当前字母的标签;从页眉中去掉ladder,藏青色标签仍会从A到C逐级下移。

-  header: { elements: [...runningHeads, ...ladder] }, // the same on every page
+  header: { elements: [...runningHeads] }, // the same on every page

常见问题

易错点

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

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

易错点

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

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

易错点

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

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

易错点

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

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

  • 60 mm的栏约容纳50个字符,而Smyth的英文不能改写。只靠词间距来排时,218行两端对齐的英文里有18行(8 %)的词间距被拉到正常宽度的1.7倍以上,西班牙文版不到2 %,因为译文是按栏宽调整过的;Cross-trees的首行则根本排不满。maxJustifyTracking: 10让一行在词间距超过这个限度之前先加上至多1 %的字距:在截图中,217行英文里有25行、223行西班牙文里有9行带有字距,没有一行超过1.7倍。在把排得最差的五个词条(Aground、Aloft、Belay、Cockpit、Crow’s nest)换成别的之前,英文版的这个比例是17 %。要么挑选排得好的词条,要么加宽栏。

致谢

文本
字体
Alegreya (SIL OFL 1.1) · Alegreya SC (SIL OFL 1.1) · Alegreya Sans SC (SIL OFL 1.1)
沙盒