简单来说
一篇博士论文的最后几页,从最后一章一直到索引。索引由Postext根据正文里标记的词自动生成,并填上页码。
成品一览
一篇关于屏幕阅读与纸面阅读的博士论文的最后七页,用B5页面黑白排版。第6章、附录A、术语表、参考文献和索引都在同样一条62 mm高的黑色色带下开始,标题在色带上反白。章在色带中显示数字,附录显示字母;后置部分的各节带眉题BACK MATTER和一条简短的斜体说明。本章排为两端对齐的单栏。术语表和索引改为两栏齐左,参考文献回到单栏,每个条目的转行都缩进。索引由正文中的标记生成:引擎把术语按首字母归类排序,找到每个标记所在的页,并把连续的页合并成171–73这样的页码范围。粗体页码指向术语表中的释义。
这道食谱解答
- 怎样排参考文献或术语表(悬挂缩进、较小字号)?
- 怎样做一份页码随正文移动自动更新的书末索引?
- 怎样让标题、粗体字和项目符号不再显示成蓝色?
- 怎样给标题编号(1、1.1、1.1.1),并给每一级设置不同样式?
- 怎样设置书眉:左页放书名,右页放章名,页码在外侧?
- 怎样强制分页或分栏,并让每章都从右页开始?
简短回答
// '# Glossary {style="glossary"}' in the Markdown picks a style. Each style starts a page
// of either parity, the appendix a recto (a style that sets no break inherits its level's
// 'odd': gotcha style-inherits-break), stays out of the chapter count (numbered: false, so
// its band has no numeral) and brings its own running heads; the glossary and the index
// set their pages in two columns until the next '#'. The appendix counts, in letters: its
// template prints the level's counter as A, B… config() takes both lists below.
const twoColumns = { layoutType: 'double', gutterWidth: mm(6) };
const backMatter = (id, extra) => ({ id, numbered: false, breakBefore: { enabled: true,
parity: 'any' }, advancedDesign: opener('Back matter'), header: sectionHeads, ...extra });
const headingStyles = () => [
backMatter('appendix', { numbered: true, numberingTemplate: '{1:A}', header: appendixHeads,
breakBefore: { enabled: true, parity: 'odd' }, advancedDesign: opener('Appendix') }),
backMatter('glossary', { layout: twoColumns }),
backMatter('references'),
backMatter('index', { layout: twoColumns }),
];
// One paragraph per entry, in :::paragraphs{style="…"}: the turnover lines hang, so the
// first word of every entry stands clear at the left. Ragged, as APA asks of references,
// with every word whole (hyphenation.ragged is off). The index has its own settings.
const entries = (id, size, lead, hang, extra) => ({ id, fontSize: pt(size),
lineHeight: pt(lead), textAlign: 'left', hangingIndent: em(hang), ...extra });
const paragraphStyles = () => [
entries('term', 9.3, 12.4, 1), // the glossary: a bold term, then its definition
entries('reference', 9.3, 12.4, 1.5, { spaceBetween: pt(2.4) }),
];
用料
做法
#1 · 给后置部分的每一部分一种标题样式
代码见上文的简短回答。# Glossary {style="glossary" note="…"}开启一节,一直延续到下一个一级标题;这一节的页面使用该样式的版式、书眉和章首设计(标题样式)。所以术语表和索引改为两栏,而参考文献的样式没有设置版式,就退回到文档的单栏。numbered: false让这些标题不计入章数(删掉它,术语表的色带会印出2)。每种样式都指定了自己的分页方式,因为没有指定的样式会继承章的'odd',留下空白左页。术语表和参考文献的条目是:::paragraphs{style="…"}块中的段落,字号9.3 pt、行距12.4 pt,正文则是11 pt、行距14.6 pt;转行在术语表中悬挂1 em,在参考文献中悬挂1.5 em(段落样式)。
#2 · 所有章首页共用一条色带
const SINK = 8; // lines reserved, 41.2 mm: 3.2 mm more than the band, and text on the grid
// Design text sets each baseline 0.8 of its line under the line's top, and a line is 1.2 × the
// size unless lineHeight says otherwise. In mm, a line's part above its baseline and below it:
const PT = 25.4 / 72;
const above = (size, lineHeight = 1.2) => 0.8 * size * lineHeight * PT;
const below = (size, lineHeight = 1.2) => 0.2 * size * lineHeight * PT;
const KICKER = 4.3, TITLE = BAND - TOP - 9.5; // mm under the text block's top: two baselines
// A bottom-aligned box that ends below() under a baseline sets its last line on it. The
// numeral's line is 0.72 of its size, so the line fits inside its box.
const text = (id, content, family, size, lineHeight, baseline, edge, w, extra) => ({
kind: 'text', id, content, fontFamily: family, fontSize: pt(size), lineHeight,
color: col('paper'), overflow: 'wrap', align: edge.endsWith('right') ? 'right' : 'left',
verticalAlign: 'bottom', ...extra, placement: { anchor: { to: 'container', edge },
size: { width: mm(w), height: mm(baseline + below(size, lineHeight)) } } });
// The mark is '{number}': the chapter's 6, the appendix's A, nothing on an unnumbered heading.
const opener = (label) => ({ enabled: true, minHeight: pt(SINK * LEAD),
slot: { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('band') }, placement: {
anchor: { to: 'page', edge: 'top-left' }, size: { width: 'fill', height: mm(BAND) } } },
text('label', label, LABEL, 8, 1.2, KICKER, 'top-left', 80,
{ fontWeight: 700, letterSpacing: pt(1.6), textTransform: 'uppercase' }),
text('title', '{titleText}', DISPLAY, 34, 1.04, TITLE, 'top-left', 84),
text('mark', '{number}', DISPLAY, 118, 0.72, TITLE, 'top-right', 34),
text('note', '{attr.note}', TEXT, 8.6, 1.3, TITLE, 'top-right', 44, { italic: true }),
] } });
标记是{number}:章印出6,附录印出A,不编号的标题上为空。一套设计同时服务于这三者,后置部分的各节在本该放数字的位置印出{attr.note}。minHeight预留八行14.6 pt,即从版心顶端起41.2 mm,比色带多出3.2 mm,并让正文保持在网格上。设计文本把基线放在行高的0.8处,所以底边位于某条基线below()的底对齐框,其最后一行就落在那条基线上。标题、118 pt的数字和说明的最后一行都立在标题的基线上,距裁切线52.5 mm。数字的行高取字号的0.72,这一行正好放得进它的框。
#3 · 让附录的样式给它编字母
// The style's '{1:A}' (see answer) prints the level's counter as a letter, and startAt=1 on the
// first appendix restarts it after chapter 6. The band and the running head read that letter.
// A resource template's {h1} is a number unless it names a style: '{h1:A}.{n}' counts the
// tables A.1, A.2… and B.1 in a second appendix, with one type for all of them.
const appendixHeads = heads('Appendix {chapterNumber}. {chapterTitle}');
const appendixTables = { ...defaultResourceTypes(LANG).find((type) => type.id === 'table'),
id: 'table-a', numberingTemplate: '{h1:A}.{n}', resetOn: 'h1' }; // a copy of 'table'
附录的样式设了numberingTemplate: '{1:A}',把所在级别的计数器印成字母;# Interview guide {style="appendix" startAt=1}让计数器重新开始,所以第一个附录是A而不是G(标题样式)。色带通过{number}读到这个字母,书眉通过{chapterNumber}读到。资源类型的{h1}在不指定样式时把计数器印成数字,所以附录里的表用模板为'{h1:A}.{n}'的类型:A.1、A.2,依此类推,第二个附录则是B.1;如果用默认类型,这张表会成为表1.1。本章自己的标题按各级模板计数:'{1}.{2}'印出6.1,continuation.headings.h1: 5让这一章成为第六章。
#4 · 把书眉放在外侧
const HEAD = 17.5, GAP = 9; // mm: the heads' baseline under the trim; the folio to the words
// Each text is placed by its top, above() over HEAD: the folio and the capitals share a baseline.
const head = (id, content, parity, edge, x, size = 7.5, extra) => ({ kind: 'text', id, content,
parity, pages: 'body', fontFamily: LABEL, fontSize: pt(size), fontWeight: 700,
letterSpacing: pt(1.3), textTransform: 'uppercase', color: col('ink'), ...extra, placement: {
anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(HEAD - above(size)) } } });
const folio = { fontFamily: TEXT, fontWeight: 400, letterSpacing: pt(0) };
const heads = (recto) => ({ elements: [
head('verso-folio', '{pageNumber}', 'even', 'top-left', OUTER, 9.5, folio),
head('verso', '{title}', 'even', 'top-left', OUTER + GAP),
head('recto', recto, 'odd', 'top-right', -(OUTER + GAP)),
head('recto-folio', '{pageNumber}', 'odd', 'top-right', -OUTER, 9.5, folio),
// The header's container spans the text block, so one rule serves both pages.
{ kind: 'rule', id: 'hairline', pages: 'body', direction: 'horizontal', thickness: pt(0.5),
color: col('rule'), placement: { anchor: { to: 'container', edge: 'top-left' },
offset: { y: mm(HEAD + 2) }, size: { width: 'fill' } } },
] });
const chapterHeads = heads('Chapter {chapterNumber}. {chapterTitle}');
const sectionHeads = heads('{chapterTitle}'); // 'Glossary', 'References', 'Index'
// Openers drop the folio to the foot, centred under the text block, its baseline 12 mm below.
const footer = { elements: [{ kind: 'text', id: 'drop-folio', content: '{pageNumber}',
pages: 'opener', ...folio, fontSize: pt(9.5), color: col('ink'), align: 'center',
placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: mm(12 - above(9.5)) } } }] };
四个文本元素锚定在页面上,并用parity筛选,让页码在两页上都位于外侧;每个元素按顶边定位,放在距裁切线17.5 mm的基线的above()处,所以9.5 pt的页码和7.5 pt的大写字母立在同一行上。左页印论文标题({title},来自前置元数据),右页印该节标题({chapterTitle}),例如第177页上的INDEX。pages: 'body'让它们不出现在章首页上;章首页只有页脚的页码,在版心下居中。本章的右页会印Chapter 6. Conclusion,附录的右页会印Appendix A. Interview guide,不过在这个示例中只有索引延续到了右页。
#5 · 在正文讨论术语的地方做标记
// Glossary definitions carry 'main' (bold numbers); runs of pages join as 171–72 (Chicago).
// The heads stand on the entries' 11.6 pt pitch, 7.5 pt of space above them: 19 pt from the
// last entry of a letter to the next letter's baseline, 11.6 pt from a letter to its first entry.
const index = { fontFamily: TEXT, fontSize: pt(9), lineHeight: pt(11.6), color: col('ink'),
indent: em(1), turnoverIndent: em(2), rangeFormat: 'chicago',
groups: { fontFamily: DISPLAY, fontSize: pt(13), fontWeight: 400, color: col('ink'),
marginTop: pt(7.5) } };
:index[tablet]印出这个词,并以同一个词归入索引;Rayner:index{term="Rayner, Keith"}什么也不印,把Rayner所在的页归入全名之下;term="interviews!timing of"生成一个子条目。术语表中的术语带main,所以它们的页码排为粗体,正如索引色带中的说明所写。# Index {style="index"}下的:::index把条目排在该样式的两栏中,buildDocument会反复重新排版文档,直到页码不再变化。条目字号9 pt、行距11.6 pt,转行悬挂2 em,子条目缩进1 em;字母用展示字体排,字号13 pt。标记在表格单元格中不起作用,所以表6.1的各行不会增加页码:look-backs列出的是报告它们的那一段,而不是那张表。
#6 · 所有默认颜色都用墨色
const palette = { ink: '#000000', band: '#000000', rule: '#000000', paper: '#ffffff' };
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// Bold, italic and list markers default to 'main-color': pointed at the ink, they print black.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.ink })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
引擎对粗体、斜体、列表标记和引用的默认颜色取自调色板条目main-color,所以把它指向墨色,这些就会印成黑色而不是蓝色(调色板)。referenceBold: false把Table 6.1排为正体,与周围的作者–年份引文一致。
完整食谱
// ═══ Postext Cookbook · Nº 031 · Thesis back matter: appendix, glossary and index ═══
// https://postext.dev/en/cookbook/thesis-back-matter
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Libertinus Serif, Serif Display and Sans (SIL OFL 1.1) · Needs postext ≥ 1.28.0
import {
buildDocumentWithFonts, renderPageToCanvas, defaultResourceTypes,
} 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')
const RECIPE = 'thesis-back-matter';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: one ink; every colour is black or white, each under its own name
const palette = { ink: '#000000', band: '#000000', rule: '#000000', paper: '#ffffff' };
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// Bold, italic and list markers default to 'main-color': pointed at the ink, they print black.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.ink })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const TEXT = 'Libertinus Serif', DISPLAY = 'Libertinus Serif Display', LABEL = 'Libertinus Sans';
const TOP = 24, INNER = 25, OUTER = 31; // mm: a 120 mm measure, about 70 characters at 11 pt
const LEAD = 14.6; // pt: the body's leading, the grid every page is set on
const BAND = 62; // mm from the trim's top: the black band at the head of every opener
// #region answer: back matter as unnumbered heading styles, entries in hanging indents
// '# Glossary {style="glossary"}' in the Markdown picks a style. Each style starts a page
// of either parity, the appendix a recto (a style that sets no break inherits its level's
// 'odd': gotcha style-inherits-break), stays out of the chapter count (numbered: false, so
// its band has no numeral) and brings its own running heads; the glossary and the index
// set their pages in two columns until the next '#'. The appendix counts, in letters: its
// template prints the level's counter as A, B… config() takes both lists below.
const twoColumns = { layoutType: 'double', gutterWidth: mm(6) };
const backMatter = (id, extra) => ({ id, numbered: false, breakBefore: { enabled: true,
parity: 'any' }, advancedDesign: opener('Back matter'), header: sectionHeads, ...extra });
const headingStyles = () => [
backMatter('appendix', { numbered: true, numberingTemplate: '{1:A}', header: appendixHeads,
breakBefore: { enabled: true, parity: 'odd' }, advancedDesign: opener('Appendix') }),
backMatter('glossary', { layout: twoColumns }),
backMatter('references'),
backMatter('index', { layout: twoColumns }),
];
// One paragraph per entry, in :::paragraphs{style="…"}: the turnover lines hang, so the
// first word of every entry stands clear at the left. Ragged, as APA asks of references,
// with every word whole (hyphenation.ragged is off). The index has its own settings.
const entries = (id, size, lead, hang, extra) => ({ id, fontSize: pt(size),
lineHeight: pt(lead), textAlign: 'left', hangingIndent: em(hang), ...extra });
const paragraphStyles = () => [
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#让每一节都从右页开始
送交图书馆装订的论文常让每一节从右页开始。这样术语表、参考文献和索引会移到第175、177和179页,每一节前面都有一个空白左页,索引中的粗体页码指向第175页。
-const backMatter = (id, extra) => ({ id, numbered: false, breakBefore: { enabled: true,
- parity: 'any' }, advancedDesign: opener('Back matter'), header: sectionHeads, ...extra });
+const backMatter = (id, extra) => ({ id, numbered: false, breakBefore: { enabled: true,
+ parity: 'odd' }, advancedDesign: opener('Back matter'), header: sectionHeads, ...extra });#把论文的前面部分也排出来
在第1页之前、用罗马数字计页的前置部分,见前置部分用罗马数字页码,然后从第1页开始。
常见问题
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Libertinus Serif (SIL OFL 1.1) · Libertinus Serif Display (SIL OFL 1.1) · Libertinus Sans (SIL OFL 1.1)


