跳到主要内容
食谱编号73

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

人名索引与主题索引

带index="names"的标记进入人名索引,其余标记进入主题索引;:::index把每个索引排成两栏,构建过程自动确定页码。

本页内容
类型
教科书
输出
Canvas · PDF
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.28.0 · postext-pdf ≥ 1.28.0
许可证
更新于2026年10月11日
代码MIT · 文本CC BY 4.0

第6–7页,共7页

  • 英文样例:尚无中文版本
  • 成品尺寸155 × 235 mm
  • 1栏
  • Crimson Pro 10.6/13.6
  • Bodoni Moda
  • Libre Franklin
  • 7页
  • 难度
  • Postext 1.65.0
  • 排版用时127 ms
  • 142行代码

简单来说

一部讲印刷字体历史的学术著作中的一章。示例演示怎样在书末生成两份索引,一份列人名,一份列主题,并标出所在页码。

成品一览

学术专著《Letters of Metal》的第四章,论述印刷字体,开本155 × 235 mm:正文单栏Crimson Pro,标题用Bodoni斜体,强调色为砖红。文章从古腾堡的textura哥特体讲起,经过Jenson、Aldus和Griffo、Garamond和Plantin、Caslon和Baskerville、Fournier的点制,以及Pradell和Gil刻制、Ibarra印刷的西班牙字体,一直到Bodoni的现代体。它像这类书一样以左右相对两页上的索引结束:左页是印刷商、刻字匠和赞助人,右页是字体、工具和书籍。两个索引都来自同一份带标记的文本,其中每个页码都是标记实际所在的页。

这道食谱解答

  • 怎样用同一份标记好的正文印出两份索引,一份人名索引,一份主题索引?
  • 怎样做一份页码随正文移动自动更新的书末索引?

简短回答

script.js · 第35–57行在完整代码中
// Marks in the Markdown. A person goes to the names index, inverted:
//   :index[Aldus Manutius]{term="Manutius, Aldus" index="names" main}
// A subject goes to the main index, with levels split by "!" and a sort key when the
// printed form would file in the wrong place:
//   :index[textura]{term="Blackletter!textura" main}
//   :index{term="42-line Bible" sort="Forty-two-line Bible"}
//   :index{term="Cursive" see="Italic"}
// Printed at the end, each under its own heading:
//   # Index of Names {style="index"}    then    :::index{index="names"}
//   # Index of Subjects {style="index"} then    :::index
const indexHeading = {
  id: 'index',
  numbered: false, // no chapter number, and the next chapter is still chapter 5
  breakBefore: { enabled: true, parity: 'any' },
  layout: { layoutType: 'double', gutterWidth: mm(7) }, // the section after the heading
};
const index = {
  fontSize: pt(8.6), lineHeight: pt(11.2), // one leading for entries and letter heads
  indent: em(1), turnoverIndent: em(2),
  rangeFormat: 'chicago', // 212–14, as a monograph prints it
  main: { bold: true }, // the page of the principal discussion
  groups: { fontFamily: 'Bodoni Moda', fontSize: pt(11), fontWeight: 600, color: col('brick') },
};

用料

类型
Crimson Pro, Bodoni Moda, Libre Franklin(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 人物标记进人名索引,主题标记进主索引

代码见上面的简短回答,其余都在Markdown里。带index="names"的标记进入由:::index{index="names"}印出的索引,其他标记都进入由不带参数的:::index印出的主索引。所以:index[Aldus Manutius]{term="Manutius, Aldus" index="names" main}在正文中印出人名,并在人名索引的Manutius下登记一个粗体页码;两行之后的:index[italic]{term="Italic" main}则把同一页登记在主题索引的Italic下。如果某个人物本身也是本章的主题,可以同时带两种标记。

#2 · 人名倒装,并按读者会去查的字母排序

人名按姓氏在前登记:term="Garamond, Claude"、term="Dall’Aglio, Margherita"。逗号是条目的一部分,所以排序器会像索引编制者那样把Didot, Firmin排在Didot, François-Ambroise之前。印出的形式会被归到错误字母下时,用sort指定排序键:英文版标记为term="42-line Bible" sort="Forty-two-line Bible",这样这部圣经排在F下,而不是另开一个0–9组。子条目(term="Foundries!Plantin-Moretus")、粗体的主要页码(main)、Bodoni一节首尾的range="start"和range="end",以及互见(see="Italic"、seealso="Matrices")完成其余工作;多级的see目标印出时用冒号连接,如Humanist type. See Roman type: Venetian。

#3 · 构建一次,文档自己确定页码

script.js · 第217–220行在完整代码中
// A pass finds the page of every mark and prints both indexes with those numbers. When the
// printed index moves a mark (an index set before the text would), the layout runs again,
// until no page number changes (in a buildBundle book, the chapter with :::index gets them all).
const doc = await buildDocumentWithFonts({ markdown }, config(), kitFonts(FONTS));

索引印的页码要排版后才存在,而印索引本身又要占页。buildDocument()处理这个问题:它排出本章,读取每个标记所在的页,印出两个索引,只要有页码变了就重新排版。这里索引在正文之后,所以第二遍只是确认第一遍;如果索引排在正文之前,它会让后面的每一页都移动,排版会一遍遍进行,直到结果一致为止。在Bodoni一节加一段,人名索引会自动给出Whittingham的新页码。

#4 · 给每个索引一个两栏的首页

script.js · 第61–75行在完整代码中
const opener = (sink, size) => ({
  enabled: true,
  minHeight: pt(LEAD * sink), // the text starts on the same grid line in every chapter
  slot: { elements: [
    { kind: 'text', id: 'kicker', content: '{attr.kicker}', ...label, fontSize: pt(7.8),
      letterSpacing: pt(1.6), color: col('brick'), align: 'left',
      placement: at('container', 'top-left', 0, 10) },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Bodoni Moda', italic: true,
      fontSize: pt(size), lineHeight: 1.08, color: col('ink'), align: 'left',
      overflow: 'wrap', // design text ends in an ellipsis by default
      placement: { ...at('#kicker', 'below', 0, 3), size: { width: 'fill', height: 'auto' } } },
    { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.75), color: col('brick'),
      placement: { ...at('#title', 'below', 0, 4.5), size: { width: mm(16) } } },
  ] },
});

简短回答中的标题样式为# Index of Names {style="index"}做三件事:开新页(parity: 'any',所以两个索引紧接着排,中间没有空白页),把它后面的部分排成两栏、栏间距7 mm,并且通过numbered: false不计入章的编号。这个样式沿用章首页的设计,下沉六行,标题20 pt;字母标题重复它的Bodoni字体,使用强调色,高度为一行索引,使各栏保持行距节奏。

#5 · 在右页写出索引名

script.js · 第79–96行在完整代码中
const head = (id, content, parity, edge, x, extra = {}) => ({
  kind: 'text', id, content, parity, pages: 'body', // never on openers
  ...label, fontSize: pt(7.2), letterSpacing: pt(1.3), color: col('muted'),
  placement: at('page', edge, x, 12.5), ...extra,
});
const folio = { color: col('brick'), fontWeight: 700 };
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', 'top-left', MARGIN.outer,
    { ...folio, align: 'left' }),
  head('verso-title', '{title}', 'even', 'top-left', MARGIN.outer + 8, { align: 'left' }),
  head('recto-title', '{chapterTitle}', 'odd', 'top-right', -(MARGIN.outer + 8),
    { align: 'right' }),
  head('recto-folio', '{pageNumber}', 'odd', 'top-right', -MARGIN.outer,
    { ...folio, align: 'right' }),
] };
// Openers carry a drop folio instead, centred under the text block.
const footer = { elements: [{ ...head('drop-folio', '{pageNumber}', 'all', 'bottom', 0,
  { ...folio, align: 'center', placement: at('page', 'bottom', 0, -12) }), pages: 'opener' }] };

右页书眉印{chapterTitle},而索引标题和任何一章一样是一级标题,所以它之后的各页顶部印的是Index of Subjects。pages: 'body'让书眉不出现在两个索引的首页上,这两页改为在页脚印页码。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 073 · An index of names and an index of subjects ═══════
// https://postext.dev/en/cookbook/names-and-subjects-indexes
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Crimson Pro, Bodoni Moda, Libre Franklin (SIL OFL 1.1) · Needs postext ≥ 1.28.0
import { buildDocumentWithFonts, renderPageToCanvas } 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 = 'names-and-subjects-indexes';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: a warm black, one brick red for kickers, letter heads and folios
const palette = {
  ink: '#211d19', // text: a warm near-black
  brick: '#8e3b24', // the one accent
  muted: '#6d645a', // running heads, colophon
  rule: '#c8bba9', // hairlines
  paper: '#fcfaf5',
};
// A colour is its hex and the palette entry it follows.
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, never the default blue.
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.brick, model: 'hex' } },
];
// #endregion
const TRIM = { width: 155, height: 235 }; // a scholarly monograph
const MARGIN = { top: 22, bottom: 24, inner: 19, outer: 22 }; // mirrored: 114 mm measure
const LEAD = 13.6; // body leading in pt
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const label = { fontFamily: 'Libre Franklin', fontWeight: 600, textTransform: 'uppercase' };

// #region answer: two indexes from one text, each under a two-column heading style
// Marks in the Markdown. A person goes to the names index, inverted:
//   :index[Aldus Manutius]{term="Manutius, Aldus" index="names" main}
// A subject goes to the main index, with levels split by "!" and a sort key when the
// printed form would file in the wrong place:
//   :index[textura]{term="Blackletter!textura" main}
//   :index{term="42-line Bible" sort="Forty-two-line Bible"}
//   :index{term="Cursive" see="Italic"}
// Printed at the end, each under its own heading:
//   # Index of Names {style="index"}    then    :::index{index="names"}
//   # Index of Subjects {style="index"} then    :::index
const indexHeading = {
  id: 'index',
  numbered: false, // no chapter number, and the next chapter is still chapter 5
  breakBefore: { enabled: true, parity: 'any' },

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

变化

#印一个合并索引

去掉人物标记和第二个标题上的index="names":所有标记都进入主索引,人名和主题排在同一个字母序里。

-:index[Aldus Manutius]{term="Manutius, Aldus" index="names" main}
+:index[Aldus Manutius]{term="Manutius, Aldus" main}

#去掉字母标题

这么短的人名索引不用字母标题也好读。groups.enabled: false去掉字母标题,字母之间留一行空白;这项设置对两个索引都起作用。

-  groups: { fontFamily: 'Bodoni Moda', fontSize: pt(11), fontWeight: 600, color: col('brick') },
+  groups: { enabled: false },

常见问题

  • index配置是共用的:两个索引使用同样的字体、行距和字母标题。
  • 题注或表格单元格中的标记会原样印出。应在引用该图的段落里标记人物。

致谢

文本
  • “From Textura to Bodoni”, an essay on the history of printing types written for this recipe, in English and Spanish · Ignacio Ferro · CC BY 4.0
字体
Crimson Pro (SIL OFL 1.1) · Bodoni Moda (SIL OFL 1.1) · Libre Franklin (SIL OFL 1.1)
沙盒PDF