跳到主要内容
食谱编号18

排版食谱 · 第3章 · 标题与章首页

七级节标题

节号1.1到1.12放在一个随编号加宽的琥珀色胶囊里,下面再分四级,第七级用标题样式做出来。

本页内容
  • 英文样例:尚无中文版本
  • 成品尺寸176 × 250 mm
  • 2栏, 栏间距6 mm
  • IBM Plex Serif 9.4/13.2
  • IBM Plex Mono
  • IBM Plex Sans Condensed
  • 3页
  • 难度
  • Postext 1.57.1
  • 排版用时89 ms
  • 178行代码

简单来说

一本步道维护手册里讲排水的一章。示例演示怎样把标题分成七级,每级样子不同,读者一眼看出各部分的层次。

成品一览

一本步道维护队野外手册的第1章Drainage,排成三页B5、每页两栏:正文用IBM Plex Serif,标题和节号用IBM Plex Sans Condensed,标签、页码和小节编号用IBM Plex Mono。章首页把章号放在一个大号琥珀色胶囊里,下面是一条步道的高程剖面,琥珀色圆点标出十二个计划新建截水沟的位置。较小的胶囊给1.1到1.12节编号,到1.10时变宽。标题1.2.1、1.5.1和1.5.2是加宽字距的大写字母,上方一条绿线。第4到6级不编号,改用字体来区分(衬线斜体、绿色窄体粗体、等宽大写),第七级用灰色斜体排工具名。安全规则以绿色的段首术语开头。一份不编号的检查清单以空心方块为标记,结束本章,其中的列表按1.、a)和i.编号,还有两个任务复选框。

这道食谱解答

  • 怎样给标题编号(1、1.1、1.1.1),并给每一级设置不同样式?
  • 怎样在标题旁做一个编号徽标,让它在编号变长时(9 → 10)自动变宽?
  • 标题超过六级怎么办?
  • 怎样定制列表:每级的项目符号、(a)/(i)编号、任务复选框、保持在网格上的间距?

简短回答

script.js · 第34–51行在完整代码中
const H2 = 13.5; // pt: the number and the title share one size and one line height,
const LH = 1.2; // so, under the same top padding, they share one baseline
// Every section head starts on a grid line, so the 3 pt the pill falls short of two lines
// is the gap the grid snap leaves between the pill and the text under it.
const PILL_H = 2 * LEAD - 3, PAD = (PILL_H - H2 * LH) / 2; // pt
const face = { fontFamily: DISPLAY, fontWeight: 700, fontSize: pt(H2), lineHeight: LH };
const pill = { kind: 'text', id: 'pill', content: '{number}', ...face, color: col('ink'),
  box: { backgroundColor: col('signal'), borderRadius: mm(3), // no width: the pill is its
    padding: { top: pt(PAD), bottom: pt(PAD), left: mm(1.8), right: mm(1.8) } }, // number
  placement: at('container', 'top-left') }; // plus its padding
// 'right-of' hangs the title on the pill's right edge and aligns its lines left, so a long
// title wraps beside the number, never under it.
const sectionTitle = (from) => ({ kind: 'text', id: 'title', content: '{titleText}', ...face,
  color: col('ink'), overflow: 'wrap', box: { padding: { top: pt(PAD) } },
  placement: at(`#${from}`, 'right-of', mm(2.2)) });
// The H1 counter, a point, the H2 counter: 1.1 … 1.12 in the pill. h2 joins headings.levels.
const h2 = { level: 2, numberingTemplate: '{1}.{2}',
  advancedDesign: { enabled: true, slot: { elements: [pill, sectionTitle('pill')] } } };

用料

类型
IBM Plex Serif, IBM Plex Sans Condensed, IBM Plex Mono(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 把编号放进随它变宽的胶囊

代码见上文简短回答。numberingTemplate: '{1}.{2}'把章计数器和节计数器连起来,得到1.1到1.12(按级别覆盖)。带高级设计的级别不再在标题前印编号,所以由设计自己放置{number}。这里它在一个文本元素中,带填充的圆角box,不设宽度,因此胶囊的宽度等于编号加内边距:1.9是10.1 mm,1.10是12.7 mm。'right-of'把标题挂在胶囊右边缘,各行齐左,所以1.5的长标题在编号旁折行(元素定位)。胶囊比两条网格线的间距矮3 pt,而每个节标题都从网格线开始,所以无论标题是开栏还是跟在段落后面,这3 pt都是胶囊与下方文字之间的间隙。

第3页:胶囊从1.9到1.10变宽,标题右移了多出那一位数字的宽度。

#2 · 第三级加线并加宽字距

script.js · 第55–67行在完整代码中
// A rule, a mono number and a tracked title: three elements, so this head is a design.
const small = { fontSize: pt(8.4), lineHeight: LH };
const DROP = 6; // pt: the rule drops this far toward the number, which keeps its grid line
const h3 = { level: 3, numberingTemplate: '{1}.{2}.{3}', // 1.5.1: restarts under every H2
  advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.75), color: col('band'),
      placement: { ...at('container', 'top-left', mm(0), pt(DROP)), size: { width: 'fill' } } },
    { kind: 'text', id: 'num', content: '{number}', fontFamily: LABEL, fontWeight: 500, ...small,
      color: col('band'), placement: at('#rule', 'below', mm(0), pt(LEAD - DROP)) },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontWeight: 600,
      ...small, letterSpacing: pt(1.35), textTransform: 'uppercase', color: col('ink'),
      overflow: 'wrap', placement: at('#num', 'right-of', mm(2)) },
  ] } } };

第3级也用设计来画:一条0.75 pt的绿线、IBM Plex Mono的编号和旁边加宽字距的标题。'{1}.{2}.{3}'的第三个计数器在每一节下重新开始,所以1.2.1和1.5.1都以1结尾。DROP只降低那条线。编号放在线下方LEAD - DROP处,这样无论DROP取什么值,编号和标题都在标题顶端下方一条网格线处。线离编号比离上面的段落近,读起来就属于标题。

#3 · 第4到6级逐级递减

script.js · 第96–107行在完整代码中
const headings = { fontFamily: DISPLAY, color: col('ink'), // every head sits on the grid,
  lineHeight: pt(LEAD), marginTop: pt(LEAD), marginBottom: pt(0), // a line above, none below
  levels: [
    // Each chapter opens on a recto.
    { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
      numberingTemplate: '{1}', advancedDesign: opener },
    h2, h3,
    // No template below level 3, so no number: each level changes face, colour or case.
    { level: 4, fontFamily: 'IBM Plex Serif', fontWeight: 400, italic: true, fontSize: pt(11) },
    { level: 5, fontSize: pt(9.4), color: col('band') },
    { level: 6, fontFamily: LABEL, fontWeight: 600, fontSize: pt(7.8), textTransform: 'uppercase' },
  ] };

没有numberingTemplate的级别不印编号,所以从第4级往下,标题改用字体、颜色或大小写来区分:第4级是衬线斜体,第5级是绿色的展示字体,第6级是半粗的等宽大写字母。在headings本身设置的行距和外边距作用于每一级,把每个标题放在网格上,上方一个空行,下方没有。第1级用parity: 'odd'让每一章从右页开始。

#4 · 用样式做出第七级和不编号的节

script.js · 第111–130行在完整代码中
const headingStyles = [
  // Markdown stops at ######: '###### Rock bar {style="level7"}' stays level 6 in the
  // outline and is set in lower case, lighter and grey.
  { id: 'level7', fontFamily: DISPLAY, fontWeight: 500, italic: true, fontSize: pt(8.4),
    textTransform: 'none', color: col('muted') },
  // numbered: false: no number, and the H2 counter does not move. An empty {number} would
  // still paint the amber pill, so the style draws a hollow square in its place.
  { id: 'checklist', numbered: false, advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'box', id: 'box', style: { borderColor: col('signal'), borderWidth: pt(1.8),
      borderRadius: mm(1.5) }, placement: { ...at('container', 'top-left'),
      size: { width: pt(PILL_H), height: pt(PILL_H) } } },
    sectionTitle('box'),
  ] } } },
];
const paragraphStyles = [
  // Run-in heads: the bold term opening each rule prints in the accent, not in body ink.
  { id: 'rules', boldColor: col('band'), firstLineIndent: pt(0) },
  { id: 'colophon', fontFamily: LABEL, fontSize: pt(6.8), lineHeight: pt(9),
    color: col('muted'), textAlign: 'left', firstLineIndent: pt(0), marginTop: pt(LEAD) },
];

Markdown到######为止,###### *Rock bar*只是又一个第6级,不过排成斜体。标题样式'level7'把工具名排成灰色、字重500的窄体斜体,比第6级的600轻,textTransform: 'none'关掉它们原本会继承的大写。它们读起来比上方的等宽标签低一级,虽然8.4 pt的字号比标签的7.8 pt大(标题样式)。numbered: false把检查清单排除在计数之外,所以它后面的节仍是1.13。空的{number}仍会画出琥珀色胶囊,所以这个样式自带设计,用一个空心方块代替胶囊。安全规则中绿色的段首术语来自段落样式的boldColor。

#5 · 列表标记随深度变化

script.js · 第134–141行在完整代码中
// Zero margins keep lists on the grid; a '- [ ]' item's bullet becomes taskCheckboxChar, '☐'.
const unorderedLists = { gap: mm(2), marginTop: pt(0), marginBottom: pt(0), color: col('band'),
  levels: [{ level: 2, bulletChar: '–', color: col('sage') }] }; // '•' stays at level 1
// Level 1 keeps the defaults: 'arabic' and a full stop.
const orderedLists = { fontFamily: DISPLAY, color: col('band'), gap: mm(1.6),
  marginTop: pt(0), marginBottom: pt(0), levels: [
    { level: 2, numberFormat: 'lower-alpha', separator: ')' },
    { level: 3, numberFormat: 'lower-roman', color: col('muted') }] };

每一层都从levels取自己的标记、颜色和分隔符:检查清单按1.、a)和i.计数(有序列表按级别覆盖),项目符号从绿色渐变为灰绿色(无序列表按级别覆盖)。第1级保留默认格式'arabic'。结束检查清单的两个- [ ]项在项目符号的位置印出taskCheckboxChar(默认是☐),颜色与第一级项目符号的绿色相同(任务列表扩展)。上下外边距都为零,每个列表都保持在基线网格上。

#6 · 在步道剖面上开始本章

script.js · 第71–92行在完整代码中
const DEPTH = 96; // mm: the profile's foot, measured from the top of the page
const CLEAR = 6; // mm: the least room between the profile's foot and the text under it
const LEGEND = 7; // mm: how far the legend's top sits above the profile's foot
const big = { ...face, fontSize: pt(54), lineHeight: 1, color: col('ink') };
// The opener reserves height down to the profile's foot; minHeight reaches CLEAR mm past
// it, so the text starts on the first grid line CLEAR mm or more under the picture.
const opener = { enabled: true, minHeight: mm(DEPTH - TOP + CLEAR), slot: { elements: [
  { kind: 'image', id: 'profile', resourceId: 'profile',
    placement: { ...at('page', 'top-left'), size: { width: 'fill' } } },
  { kind: 'text', id: 'num', content: '{number}', ...big, box: { backgroundColor: col('signal'),
    borderRadius: mm(4), padding: { top: pt(4), bottom: pt(4), left: mm(4), right: mm(4) } },
    placement: at('container', 'top-left') },
  { kind: 'text', id: 'title', content: '{titleText}', ...big, box: { padding: { top: pt(4) } },
    placement: at('#num', 'right-of', mm(4)) },
  { kind: 'text', id: 'lead', content: '{attr.lead}', fontFamily: 'IBM Plex Serif', italic: true,
    fontSize: pt(11.5), lineHeight: 1.3, color: col('ink'), align: 'left', overflow: 'wrap',
    placement: { ...at('#num', 'below', mm(0), mm(5)), size: { width: mm(100) } } },
  // The legend is design text over the picture: each edition words it in its heading.
  { kind: 'text', id: 'legend', content: '{attr.profile}', fontFamily: LABEL, fontWeight: 500,
    fontSize: pt(7), color: col('tint'), placement: at('page', 'top-right', mm(-OUTER),
      mm(DEPTH - LEGEND)) },
] } };

剖面是章首页的一个图片元素,锚定在页面上,与页面等宽。若改用浮动到顶部、在第1页引用的通页图,它会出现在第2页的开头(图片元素)。章首页预留的高度一直算到图片底边,minHeight再往下多留6 mm,所以正文从剖面下方至少6 mm处的第一条网格线开始。绿色上的图例是设计文本,取自标题的profile属性。大胶囊沿用节胶囊的字体和填充色,{number}印出本章自己的编号,来自numberingTemplate: '{1}'。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 018 · Section heads seven levels deep ═════════════════
// https://postext.dev/en/cookbook/section-heads-field-manual
// Code: MIT · Text: original (CC BY 4.0) · Picture: drawn in code (MIT)
// Fonts: IBM Plex Serif, Sans Condensed, Mono (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 = 'section-heads-field-manual';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // forest green for structure, a signal amber for numbers
  ink: '#1d2320', // text: a green-black
  band: '#2f6b3f', // the accent: rules, run-in terms, bullets, numbers, folios (6.4:1)
  signal: '#e0a526', // the number pills, with ink on them (7.3:1)
  sage: '#7a9e80', // the second bullet and the profile's upper contours
  tint: '#e9f0e6', // the opener's sky; the legend on the green (5.5:1)
  muted: '#5f6a62', // running heads, level 7, roman list numbers, the colophon (5.6:1)
};
// col(id): a colour linked to its palette entry, in text styles and design slots alike.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// Defaults this config does not restate link to 'main-color', so it points at the accent.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.band })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const TRIM = { width: 176, height: 250 }; // mm: ISO B5, a common size for field manuals
const [TOP, INNER, OUTER] = [22, 16, 14]; // mm: margins; the running heads align to OUTER
const LEAD = 13.2; // pt: the body leading, the pitch of the baseline grid
const LINES = 44; // grid lines in the text block, so every full column ends on one baseline
const [DISPLAY, LABEL] = ['IBM Plex Sans Condensed', 'IBM Plex Mono']; // with the serif text
const at = (to, edge, x, y) => ({ anchor: { to, edge }, offset: { x, y } });

// #region answer: section numbers 1.1 … 1.12 in an amber pill that widens with the number
const H2 = 13.5; // pt: the number and the title share one size and one line height,
const LH = 1.2; // so, under the same top padding, they share one baseline
// Every section head starts on a grid line, so the 3 pt the pill falls short of two lines
// is the gap the grid snap leaves between the pill and the text under it.
const PILL_H = 2 * LEAD - 3, PAD = (PILL_H - H2 * LH) / 2; // pt
const face = { fontFamily: DISPLAY, fontWeight: 700, fontSize: pt(H2), lineHeight: LH };
const pill = { kind: 'text', id: 'pill', content: '{number}', ...face, color: col('ink'),
  box: { backgroundColor: col('signal'), borderRadius: mm(3), // no width: the pill is its
    padding: { top: pt(PAD), bottom: pt(PAD), left: mm(1.8), right: mm(1.8) } }, // number
  placement: at('container', 'top-left') }; // plus its padding
// 'right-of' hangs the title on the pill's right edge and aligns its lines left, so a long
// title wraps beside the number, never under it.
const sectionTitle = (from) => ({ kind: 'text', id: 'title', content: '{titleText}', ...face,
  color: col('ink'), overflow: 'wrap', box: { padding: { top: pt(PAD) } },
  placement: at(`#${from}`, 'right-of', mm(2.2)) });

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

变化

#胶囊里去掉章号

把章计数器从模板中去掉,胶囊就读作1到12,到10时变宽;第3级仍印1.5.1,直到它自己的模板也去掉{1}。

-const h2 = { level: 2, numberingTemplate: '{1}.{2}',
+const h2 = { level: 2, numberingTemplate: '{2}',

#让所有胶囊同宽

设一个固定宽度(1.10的宽度),每个编号就居中排在等宽的胶囊里,标题对齐成一列。

-  placement: at('container', 'top-left') }; // 再加上内边距
+  placement: { ...at('container', 'top-left'), size: { width: mm(12.7) } } };

常见问题

易错点

'top'浮动体默认排在引用页的下一页,除非允许它置于引用页顶

默认情况下浮动体不会排在自己的引用之前,所以在第N页引用的整页宽'top'浮动体会出现在第N+1页的顶部。在资源或其类型的defaultPlacement上设置placement.citingPage: true(或者对全书设置layout.floatsAtCitingPage,自postext 1.25起),它就改为置于第N页顶部,引用处之上的文字移到它下方,前提是它不超过layout.maxTopFraction且引用行仍留在该页。它不会置于章首页或强制分页之后的页面顶部:那时仍排在下一页,而'auto'或'bottom'可以占用引用页的底部。 图的放置 →

沙盒检查 · headingHierarchy

标题层级跳跃

原因. 某个标题跳过了一级,比如H1后面直接跟着H3。

解决. 改用下一级标题,或者重新设定你想用的那一级的样式,而不是跳过一级。 文档 →

  • 第七级其实是带level7样式的六级标题,所以示例中没有哪个标题比前一个标题低一级以上:The trench、Tools for the trench、STEEL BARS和Rock bar依次是4、5、6、6。沙盒的警告“标题层级跳跃”报告的正是这种跳级,所以本章不会出现它。

致谢

文本
原创文字, CC BY 4.0
字体
IBM Plex Serif (SIL OFL 1.1) · IBM Plex Sans Condensed (SIL OFL 1.1) · IBM Plex Mono (SIL OFL 1.1)
沙盒