跳到主要内容
食谱编号30

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

带作者署名的文集

三篇关于散步的随笔,标题带有作者、年份和刊物:章首页三项都印,左页书眉和目录印作者。

本页内容

第2–3页,共9页

  • 英文样例:尚无中文版本
  • 成品尺寸135 × 180 mm
  • 1栏
  • Spectral 10/14
  • Gloock
  • Hanken Grotesk
  • 9页
  • 难度
  • Postext 1.57.1
  • 排版用时64 ms
  • 175行代码

简单来说

一本收录三篇散步随笔的小书。每篇的作者和年份只写一次,书中需要的地方,比如目录,都会自动印出来。

成品一览

Afoot的九页。这是一本袖珍文集,收录三篇关于散步的随笔,作者分别是Hazlitt(1822)、Thoreau(1862)和Stevenson(1876)。封面是一幅描绘黄昏荒原的画,一张由扩散模型生成的JPEG。目录给每篇随笔一个铁锈色编号、一个用Gloock排的标题、引到页码的疏排前导符,下面用石楠色斜体印作者名。每篇随笔以编号和标题开篇,接着是疏排大写字母的署名行,以及注明首次刊载的刊物和年份的出处行。左页印作者名,右页印随笔标题。每篇随笔的标题都以属性形式带着作者、年份和出处,章首页、书眉和目录都从这里取值,所以加第四篇随笔时不用改配置。

这道食谱解答

  • 怎样添加自动更新的目录(前导符、页码、作者、篇名行)?
  • 怎样给章首页加作者行、提要或带首字下沉的导语?
  • 怎样设置书眉:左页放书名,右页放章名,页码在外侧?

简短回答

script.js · 第44–64行在完整代码中
// Each essay's heading carries its credits, and every {attr.…} below reads them:
//   # Walking {author="Henry David Thoreau" year="1862" source="The Atlantic Monthly"}
// (a value cannot hold { or }, and one with " goes in single quotes; gotcha: attr-values)
// 1 · The opener: inside a heading's design, {attr.author} is that heading's attribute.
const byline = [
  text('byline', '{attr.author}', { ...label, fontSize: pt(8), letterSpacing: pt(1.6),
    color: col('heather') }, below('title', 5)),
  text('dateline', '{attr.source}, {attr.year}', { fontFamily: 'Spectral', italic: true,
    fontSize: pt(9.5), color: col('muted'), align: 'left' }, below('byline', 1.2)),
];
// 2 · The verso running head (head() is in the running-heads region): in the page header,
//     {attr.author} is the attribute of the essay the page belongs to.
const versoAuthor = head('verso-author', '{attr.author}', 'even',
  at('page', 'top-left', MARGIN.outer + HEAD.gap, HEAD.y));
// 3 · The contents: toc.subtitle prints the attribute it names as a line under each title.
//     'author' and italic are the defaults; attr is written out so it can become 'source'.
const authorLine = { enabled: true, attr: 'author', fontFamily: 'Spectral', fontSize: pt(10),
  color: col('heather') };
// Hooked up below: byline → the opener, versoAuthor → header, authorLine → toc.subtitle.
// A heading without author="…" gets an empty byline and running head and no line in the
// contents, and the build gives no warning: check every heading.

用料

类型
Spectral, Gloock, Hanken Grotesk(SIL OFL 1.1)
素材
  • cover-1200.jpg
  • The cover: a heath and hills at dusk, a painting (Generated With Diffusion Models, 原创)

做法

#1 · 给随笔编号,并让正文下沉

script.js · 第68–85行在完整代码中
const opener = { enabled: true,
  // The hairline ends 7 mm above the foot of this reserve, so the text starts on the same grid
  // line under every opener whose title fits on one line (SINK = 14 holds a two-line title).
  minHeight: pt(SINK * LEAD),
  slot: { elements: [
    // {number} prints the essay's number as numberingTemplate '{1:I}' writes it: I, II, III.
    text('number', '{number}', { ...gloock, fontSize: pt(34), lineHeight: 1, color: col('rust') },
      at('container', 'top-left', 0, 8)),
    text('title', '{titleText}', { ...gloock, fontSize: pt(26), lineHeight: 1.08,
      color: col('ink') }, below('number', 3, { width: 'fill' })),
    ...byline,
    { kind: 'rule', id: 'rule', thickness: pt(0.5), color: col('rule'),
      placement: below('dateline', 6) }, // a horizontal rule runs to the column's edge
  ] } };
const essays = { level: 1, numberingTemplate: '{1:I}', advancedDesign: opener,
  marginBottom: pt(0), // the heading's default margin would add to minHeight
  // The cover and contents styles inherit this break (gotcha: style-inherits-break).
  breakBefore: { enabled: true, parity: 'any' } }; // 'any': each piece opens on the next page

该级别的numberingTemplate: '{1:I}'把随笔编为I、II、III,{number}把编号印在标题上方(通栏与高级设计)。minHeight预留12行14 pt,细线止于其上7 mm处,所以标题只有一行时,每个章首页的正文都从距上边缘79 mm处开始。26 pt的标题多一行会增加9.9 mm,把正文推下一条网格线;设SINK = 14后,一行和两行标题的正文从同一行开始。marginBottom: pt(0)防止标题默认的外边距叠加到预留高度上。

#2 · 让目录自己填好

script.js · 第89–99行在完整代码中
const ENTRY = 15; // pt: the essay titles in the contents
const contents = { // passed to the config as `toc`
  // An entry's numeral takes the title's face and size and stands on its baseline.
  levels: [{ level: 1, fontFamily: 'Gloock', fontSize: pt(ENTRY), color: col('ink'),
    numberFontWeight: 400, // Gloock has one weight
    numberColor: col('rust'), numberWidth: mm(8), numberGap: mm(3), marginBottom: pt(LEAD) }],
  pageNumber: { fontFamily: 'Hanken Grotesk', fontSize: pt(8.5), fontWeight: 600,
    color: col('muted'), width: mm(6) }, // the leader dots take this face and colour too
  leader: { char: '. ', gap: mm(2) }, // spaced dots, right-aligned so they line up
  subtitle: authorLine,
};

Markdown中的:::toc为每个列入的标题印一条目录项:编号、标题、前导符、页码标签,以及通过subtitle印出的作者。buildDocument反复排版整本书,直到页码标签不再变化,所以随笔改了名或变长,目录都会更新(目录)。目录项的编号沿用标题的字体和字号,除非numberFontFamily或numberFontSize另有指定;不论字号多大,它都立在标题首行的基线上。带篇的书还会为每个:::part加一行,由toc.parts.design绘制,见用一个属性给各篇配色。

#3 · 左页印作者,右页印随笔标题

script.js · 第103–119行在完整代码中
// Each head: the label face, the pages of its parity, never an opener (pages: 'body'), where
// the byline names the author. A function declaration, so the answer above can call it.
function head(id, content, parity, placement, look = {}) {
  return { ...text(id, content, { ...label, fontSize: pt(7.5), letterSpacing: pt(1.3),
    color: col('muted'), ...look }, placement), parity, pages: 'body' };
}
const folio = { color: col('ink') };
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', at('page', 'top-left', MARGIN.outer, HEAD.y), folio),
  versoAuthor,
  head('recto-title', '{chapterTitle}', 'odd',
    at('page', 'top-right', -(MARGIN.outer + HEAD.gap), HEAD.y), { align: 'right' }),
  head('recto-folio', '{pageNumber}', 'odd', at('page', 'top-right', -MARGIN.outer, HEAD.y),
    { ...folio, align: 'right' }),
] };
const footer = { elements: [{ ...head('drop-folio', '{pageNumber}', 'all', // an opener's folio
  at('container', 'top', 0, 9), { ...folio, align: 'center' }), pages: 'opener' }] };

每个书眉都锚定在页面上,距上边缘11 mm,由parity决定左右:页码与正文外侧边缘对齐,作者名或标题再往里7 mm。head()中的pages: 'body'让书眉不出现在章首页,因为那里的署名行已经写明作者;页脚的pages: 'opener'则在章首页改印页脚页码(文本元素)。{chapterTitle}印出不带编号的随笔标题。

#4 · 封面和目录不计入编号

script.js · 第123–146行在完整代码中
// Unnumbered, so the first essay is I; unlisted; and with no running heads or folio. They
// inherit the level's page break, 'any' (gotcha: style-inherits-break).
const bare = { numbered: false, toc: false, header: { elements: [] }, footer: { elements: [] } };
const cover = { enabled: true, slot: { elements: [
  { kind: 'image', id: 'art', resourceId: 'cover',
    placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: 'fill' } } },
  text('title', '{titleText}', { ...gloock, fontSize: pt(80), lineHeight: 1, color: col('ink') },
    at('page', 'top-left', MARGIN.inner, 22)), // page 1 is a recto: the inner margin is left
  text('subtitle', '{subtitle}', { fontFamily: 'Spectral', italic: true, fontSize: pt(14),
    color: col('ink'), align: 'left' }, below('title', 1)),
  text('authors', '{attr.authors}', { ...label, fontSize: pt(8.5), letterSpacing: pt(2),
    color: col('heather') }, below('subtitle', 5)),
  text('imprint', '{attr.imprint}', { ...label, fontSize: pt(7.5), letterSpacing: pt(1.8),
    color: col('paper') }, at('page', 'bottom-left', MARGIN.inner, -12)),
] } };
// span: 'page' in a one-column book: a design kept in its column is cut at the column's foot,
// which would leave a band of paper under the dusk.
const coverStyle = { id: 'cover', ...bare, span: 'page', advancedDesign: cover };
const contentsOpener = { enabled: true, slot: { elements: [ // {title}, {subtitle}: frontmatter
  text('kicker', '{title} · {subtitle}', { ...label, fontSize: pt(8), letterSpacing: pt(1.6),
    color: col('heather') }, at('container', 'top-left', 0, 8)),
  text('title', '{titleText}', { ...gloock, fontSize: pt(26), color: col('ink') },
    below('kicker', 2.5)),
] } };

设了numbered: false,封面和目录不会推进计数器,所以Hazlitt是I;toc: false让它们不进入目录(标题样式)。两种样式都继承该级别的breakBefore,奇偶性为'any',所以目录不用:::pagebreak就紧接封面排在第2页。在单栏书中,封面样式跨越整页,因为留在栏内的设计会在栏的下边缘被裁掉,风景下方会空出22 mm白纸。

#5 · 每个颜色都连同调色板关联一起写出

script.js · 第14–28行在完整代码中
const palette = {
  ink: '#241f26', // text: a plum-tinted near-black
  paper: '#f7f2e8', // the page
  heather: '#6b4468', // the bylines and the authors in the contents
  rust: '#a4502a', // the essay numbers
  rule: '#d5cabd', // hairlines
  muted: '#6d6570', // running heads, datelines, page numbers in the contents
};
// col() links a colour to its palette entry; the hex is the value the entry holds.
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 ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];

col()在每个颜色的paletteId旁写出它的hex值,引擎跟随的是这条关联:署名行、书眉和封面都取colorPalette里该id下的颜色,所以换一套id相同的调色板,整本书的颜色都跟着变。把main-color指向墨色,可以覆盖配置中没有重新指定的所有默认值,否则它们会印成引擎的蓝色#295AA3。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 030 · Anthology with bylines ═══════════════════════════════
// https://postext.dev/en/cookbook/anthology-with-bylines
// Code: MIT · Text: Hazlitt, Thoreau, Stevenson (public domain) · Cover: diffusion models
// Fonts: Spectral, Gloock, Hanken Grotesk (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'anthology-with-bylines';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: heather for the credits, rust for the numbers, plum ink on warm paper
const palette = {
  ink: '#241f26', // text: a plum-tinted near-black
  paper: '#f7f2e8', // the page
  heather: '#6b4468', // the bylines and the authors in the contents
  rust: '#a4502a', // the essay numbers
  rule: '#d5cabd', // hairlines
  muted: '#6d6570', // running heads, datelines, page numbers in the contents
};
// col() links a colour to its palette entry; the hex is the value the entry holds.
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 ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
// #endregion
const TRIM = { width: 135, height: 180 }; // a pocket book, 3 : 4
const MARGIN = { top: 20, bottom: 22, inner: 19, outer: 15 }; // mirrored
const LEAD = 14; // body leading in pt: the baseline grid
const SINK = 12; // grid lines an essay opener reserves above its first line of text
const HEAD = { y: 11, gap: 7 }; // running heads: mm from the top edge, folio to words
const label = { fontFamily: 'Hanken Grotesk', fontWeight: 600, textTransform: 'uppercase',
  align: 'left' }; // design text is centred by default
// One weight, no italic; 'wrap' breaks a long title onto a second line.
const gloock = { fontFamily: 'Gloock', overflow: 'wrap', align: 'left' };
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const below = (id, y, size) => ({ ...at(`#${id}`, 'below', 0, y), ...(size && { size }) });
const text = (id, content, look, placement) => ({ kind: 'text', id, content, ...look, placement });

// #region answer: one set of heading attributes, read in three places
// Each essay's heading carries its credits, and every {attr.…} below reads them:
//   # Walking {author="Henry David Thoreau" year="1862" source="The Atlantic Monthly"}
// (a value cannot hold { or }, and one with " goes in single quotes; gotcha: attr-values)
// 1 · The opener: inside a heading's design, {attr.author} is that heading's attribute.
const byline = [

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

变化

#列出每篇随笔最初发表的刊物

toc.subtitle可以印出标题的任何属性,所以同一份Markdown也能在每篇标题下列出其最初发表的刊物。

-const authorLine = { enabled: true, attr: 'author', fontFamily: 'Spectral', fontSize: pt(10),
+const authorLine = { enabled: true, attr: 'source', fontFamily: 'Spectral', fontSize: pt(10),

#左页印书名

很多书在左页印书名,右页印章名;{title}从Markdown的前置元数据中读取书名,印出AFOOT。

-const versoAuthor = head('verso-author', '{attr.author}', 'even',
+const versoAuthor = head('verso-author', '{title}', 'even',

#每篇随笔都从右页开始

奇偶性设为'odd'时,Stevenson移到第9页,前面是空白的第8页。这时两种前置部分样式需要各自设'any',否则目录会继承'odd',排到第3页,前面空出一个左页。

-  breakBefore: { enabled: true, parity: 'any' } }; // 'any':每篇都从下一页开始
+  breakBefore: { enabled: true, parity: 'odd' } };
-const bare = { numbered: false, toc: false, header: { elements: [] }, footer: { elements: [] } };
+const bare = { numbered: false, toc: false, breakBefore: { enabled: true, parity: 'any' },
+  header: { elements: [] }, footer: { elements: [] } };

常见问题

易错点

属性值:不能含{ or };含"的值用单引号

属性值在右花括号处结束,所以不能包含{ or }。含双引号的值要放在单引号里;美元符号没有问题。 标题属性 →

易错点

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

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

  • Hazlitt在句子中间引用诗句。诗句放在:::verse{style="verse" align=start}块里,Markdown的一行就是一行诗,缩进8 mm,诗后接续的句子用不缩进的runon样式。Markdown引用块在这里不合用:它的各行会像段落一样连成一片,诗句就排成了散文。
  • 标题缺少的属性什么也不印,也不报警告。缺了author="…",这篇随笔的署名行和左页书眉都是空的,目录中标题下的那一行也会消失。

致谢

文本
图片
  • The cover: a heath and hills at dusk, a painting · Generated With Diffusion Models · 原创
字体
Spectral (SIL OFL 1.1) · Gloock (SIL OFL 1.1) · Hanken Grotesk (SIL OFL 1.1)
沙盒