跳到主要内容
食谱编号1

排版食谱 · 第12章 · 教科书

带边栏的教材

一栏半的页面,外侧栏只放浮动体:span 'side'的图和术语注在那里依次堆叠,captionSide把其余图的题注移进侧栏。

本页内容

页码88–89 · 第2–3页,共4页

  • 英文样例:尚无中文版本
  • 成品尺寸210 × 275 mm
  • 一栏半, 栏间距7 mm
  • Merriweather 9.3/13.5
  • Merriweather Sans
  • 4页
  • 难度
  • Postext 1.57.1
  • 排版用时66 ms
  • 240行代码

简单来说

一本物理课本里的一章,正文占一宽栏,外侧还有一条窄栏。窄栏里放示意图和简短注释,紧挨着相关的正文。

成品一览

Lever and Lens是一本物理入门教材,页面为210 × 275 mm,这里排的是它的第4章。正文排在一个宽栏中,从不进入外侧页边距里一条53 mm宽的侧栏。章号是一个绿色的4,在侧栏中与标题平齐,学习目标排在它下方。光路图从侧栏顶部开始依次堆叠,关键术语放在薄荷绿的术语注中,位于定义它们的段落旁边。留在正文栏中的图排在深色或浅色底板上,题注放在侧栏中,与图的底边平齐。只有棱镜面板横跨两栏。页边距左右镜像,所以侧栏总在切口一侧:右页在右,左页在左。

这道食谱解答

  • 怎样做一栏半版式,一个宽的正文栏加一个窄的侧栏?
  • 怎样把边注或旁注排在它所解释的段落旁边?
  • 怎样加一幅带编号题注的图,并在正文里引用它(“见图3.2”)?
  • 怎样控制图的位置:页面顶部、横跨两栏、就放在这里,还是放在边栏里?
  • 怎样把题注放在图的旁边,或放在表上方的题注条上?
  • 怎样给标题编号(1、1.1、1.1.1),并给每一级设置不同样式?

简短回答

script.js · 第41–56行在完整代码中
const layout = {
  layoutType: 'oneAndHalf', // a wide main column and a narrow side column
  sideColumnPercent: 30, // of the 176 mm content width: a 52.8 mm channel
  sideColumnRole: 'floats', // no body text: side figures, side captions and side boxes only
  sideColumnSide: 'outer', // right on a recto, left on a verso (the margins are mirrored)
  gutterWidth: mm(7), // the text column keeps the rest: 176 − 52.8 − 7 = 116 mm
};
// A figure placed with span 'side' stacks in the channel from the head of the page that cites it,
// under the numeral on a chapter's first page.
const side = { span: 'side' };
// A figure left in the text column (the default span) sets its caption in the channel beside
// it; page-wide floats ignore captionSide and keep theirs underneath.
const resourceTypes = defaultResourceTypes(LANG).map((type) => (type.id !== 'figure' ? type
  : { ...type, defaultPlacement: { captionSide: true } })); // the built-in types, in LANG
// A box fenced :::callout{type="term" span="side"} leaves the flow and lands in the channel at the
// height the text has reached: fence each gloss before the paragraph it should stand beside.

用料

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

做法

#1 · 把边栏交给浮动体

这一步的代码见上文的简短回答。在一栏半版式中,sideColumnPercent: 30把176 mm内容宽度的30 %(52.8 mm)分给侧栏,正文栏得到减去7 mm栏间距后余下的部分(116 mm,约为9.3 pt Merriweather的75个字符)。sideColumnRole: 'floats'让正文不进入侧栏,sideColumnSide: 'outer'把侧栏放在外侧,镜像的页边距会让它逐页换边。图用span: 'side'放进侧栏。围栏上带有span="side"的框会离开正文流,在正文排到的高度进入侧栏,所以每条术语注都与其围栏后面的那一块平齐开始。

#2 · 让引用决定每幅图的位置

script.js · 第502–517行在完整代码中
const drawings = new Map(); // fileId → SVG markup, registered before the build
const figure = (id, { width, height, markup }, placement) => {
  drawings.set(`${id}.svg`, markup);
  return { id, typeId: 'figure', kind: 'svg', createdAt: 0, updatedAt: 0, caption: t(captions[id]),
    altText: t(captions[id]), // read aloud in HTML and tagged PDF; the canvas does not use it
    svg: { fileId: `${id}.svg`, width, height }, ...(placement && { placement }) };
};
const resources = [ // no placement: a main-column float, its caption in the channel
  figure('burning-glass', burningGlass()),
  figure('refraction', refraction(), side),
  figure('critical-angle', criticalAngle(), side),
  figure('fibre', fibre()),
  figure('prism', prism(), { span: 'page', position: 'top' }), // across text column and channel
  figure('principal-rays', principalRays()),
  figure('diverging', diverging(), side),
];

对一幅图的第一次:ref给它编号,并按它的放置方式放置。span: 'side'的图在引用它的那一页从侧栏顶部开始堆叠,不论引用在页面多靠下;如果侧栏剩下的部分太短,它就移到下一页。图4.2和图4.3都在第88页被引用,在该页侧栏中依次向下堆叠,临界角术语注夹在两者之间。棱镜是一个在第88页被引用的通栏top浮动体,而浮动体从不排在引用之前,所以它在第89页开头横跨两栏。图4.1、4.4和4.6留在正文栏中,从图类型的defaultPlacement取得captionSide。它们占据栏底的位置,所以每条题注都与图的底边平齐;如果放在栏顶位置,题注会与图的顶边对齐。

#3 · 在边栏中开启本章

script.js · 第79–112行在完整代码中
// Every element counts toward the opener's depth, the page-anchored numeral too (gotcha:
// opener-reserves-anchored). minHeight fixes that depth at nine lines of the grid, room for a
// one-line title, the rule and a four-line standfirst (41.2 mm), so the text starts on the same
// line in every such chapter, however short its standfirst and even with a smaller numeral. Kicker
// and numeral (40.6 mm) reach the ninth line too; a deeper opener grows past it, line by line.
const [KICKER, NUMERAL] = [8, 104]; // pt
const opener = {
  enabled: true,
  minHeight: pt(LEAD * 9), // 42.9 mm: the text starts on the eleventh line, after marginBottom
  slot: {
    elements: [
      { kind: 'text', id: 'title', content: '{titleText}', fontFamily: SANS, fontWeight: 800,
        fontSize: pt(32), lineHeight: 1.05, color: col('ink'), align: 'left', overflow: 'wrap',
        placement: { anchor: { to: 'container', edge: 'top-left' }, size: { width: 'fill' } } },
      { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1), color: col('accent'),
        placement: { anchor: { to: '#title', edge: 'below' }, offset: { y: mm(4) },
          size: { width: 'fill' } } },
      { kind: 'text', id: 'lead', content: '{attr.lead}', fontFamily: SERIF, italic: true,
        fontSize: pt(10.5), lineHeight: 1.45, color: col('ink'), align: 'left', overflow: 'wrap',
        placement: { anchor: { to: '#rule', edge: 'below' }, offset: { y: mm(3.5) },
          size: { width: 'fill' } } },
      // The kicker hangs from the page's top-right corner, not from the heading: the channel lies
      // outside the heading's column, and on the right only on a recto (so chapters open on one).
      // The numeral hangs from the kicker.
      { kind: 'text', id: 'kicker', content: t({ en: 'Chapter', es: 'Capítulo' }), ...label,
        fontSize: pt(KICKER), align: 'left', placement: { anchor: { to: 'page', edge: 'top-right' },
          offset: { x: mm(-OUTER), y: mm(TOP) }, size: { width: mm(CHANNEL) } } },
      { kind: 'text', id: 'numeral', content: '{chapterNumber}', fontFamily: SANS, fontWeight: 800,
        fontSize: pt(NUMERAL), lineHeight: 1, color: col('accent'), align: 'left',
        placement: { anchor: { to: '#kicker', edge: 'below' }, offset: { y: mm(0.5) },
          size: { width: mm(CHANNEL) } } },
    ],
  },
};

一级标题留在正文栏中,由一个设计槽依次排出标题文字、细线和导语;眉题挂在页面右上角,数字挂在眉题下,两者都与侧栏同宽。minHeight把章首固定为九条网格线(42.9 mm),足够放一行标题、细线和四行导语,所以较短的导语也不会把正文往上拉。眉题和104 pt的数字结束在上页边距以下40.6 mm处,在这九行之内,所以也不会把正文往下推。只有在右页上侧栏才在右侧,所以H1分页到奇数页。学习目标框排在第一段之后,在它之前不引用任何侧栏图,所以它就排在数字下面(见常见问题)。

#4 · 让页码留在边栏一侧

script.js · 第116–138行在完整代码中
const [HEAD_Y, FOOT_Y, HEAD_GAP] = [12.5, -12, 9]; // mm from the top and bottom trim; folio to head
// A text on the physical page: edge picks the corner, x and y are its offsets in mm.
const head = ({ edge, x, y = HEAD_Y, ...text }) => ({
  kind: 'text', pages: 'body', ...label, color: col('muted'), ...text,
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(y) } },
});
const folio = { content: '{pageNumber}', fontSize: pt(8.5), letterSpacing: pt(0),
  color: col('accent') };
const verso = { parity: 'even', edge: 'top-left' }; // x counts in from the left edge
const recto = { parity: 'odd', edge: 'top-right' }; // x counts back from the right edge
const header = { elements: [
  head({ id: 'verso-folio', ...verso, ...folio, x: OUTER }),
  head({ id: 'verso-title', ...verso, content: '{title}', x: OUTER + HEAD_GAP }),
  head({ id: 'recto-title', ...recto, x: -(OUTER + HEAD_GAP),
    content: t({ en: 'Chapter {chapterNumber} · {chapterTitle}',
      es: 'Capítulo {chapterNumber} · {chapterTitle}' }) }),
  head({ id: 'recto-folio', ...recto, ...folio, x: -OUTER }),
] };
// A chapter's first page, always a recto, carries a drop folio at the foot of the channel instead.
const footer = { elements: [
  head({ id: 'drop-folio', ...recto, ...folio, pages: 'opener', edge: 'bottom-right', x: -OUTER,
    y: FOOT_Y }),
] };

每个元素都锚定在物理页面上,并用parity筛选,所以在跨页两侧,页码和书眉都与侧栏在同一边。pages: 'body'让它们不出现在章首页上,章首页的页码改为印在侧栏底部。

#5 · 从第4章开始这本书

script.js · 第529–534行在完整代码中
const continuation = { pageNumbering: { startAt: 87 }, // odd, like page 1: a recto
  headings: { h1: 3, h2: 0, h3: 0, h4: 0, h5: 0, h6: 0 } }; // the next # is chapter 4
const doc = await buildDocumentWithFonts({ markdown, resources, continuation }, config(),
  kitFonts(FONTS));
showPages(doc, { title: t({ en: 'Textbook with a margin column',
  es: 'Libro de texto con columna al margen' }) });

这些页面是一本更长的书的第4章。接续设置把章计数器设为3,所以{chapterNumber}印出4,二级标题的numberingTemplate: '{1}.{2}'把各节编为4.1到4.3;图从4.1编到4.7。## Questions {style="plain"}使用一个带numbered: false的headingStyles条目,所以这个标题没有编号。页码从87开始,因为第一页是右页,而右页的页码是奇数。

#6 · 颜色只命名一次

script.js · 第16–32行在完整代码中
const palette = {
  ink: '#1a222d', // text, and the dark panels of figures 4.1, 4.4 and 4.5
  accent: '#17774f', // the only accent colour: numerals, folios, section headings, labels
  ray: '#f2a516', // light rays in every diagram
  glass: '#cfe6dd', // glass in the diagrams
  tint: '#edf5f1', // the key-term glosses and the light plate of a construction diagram
  muted: '#5b6863', // running heads, the normals in the diagrams, the colophon
  paper: '#ffffff',
};
// A colour carries its id and its hex. The diagrams read `palette` as well, so retint the
// chapter by editing it: the text, the designs and the drawings change together.
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, so nothing prints blue.
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];

配置中的每种颜色都链接到一个调色板条目,同时带有它的十六进制值,由col()从同一个对象中复制。要给本章换色,编辑palette即可:光路图读取的是同一个对象,所以其中的玻璃、光线、深色底板和浅色底板会随数字、标签和术语注一起改变。main-color指向强调色,所以配置未设置的文本样式默认颜色都印成绿色,而不是引擎的蓝色。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 001 · Textbook with a margin column ═══════════════════
// https://postext.dev/en/cookbook/textbook-margin-column
// Code: MIT · Text: original (CC BY 4.0) · Diagrams: generated in code (CC BY 4.0)
// Fonts: Merriweather, Merriweather Sans (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A chapter of a physics textbook in the column-and-a-half layout: the body text keeps to
// the main column, and the outer margin is a channel for diagrams, captions and glosses.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage, defaultResourceTypes,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'textbook-margin-column';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, each linked by id and written out in hex
const palette = {
  ink: '#1a222d', // text, and the dark panels of figures 4.1, 4.4 and 4.5
  accent: '#17774f', // the only accent colour: numerals, folios, section headings, labels
  ray: '#f2a516', // light rays in every diagram
  glass: '#cfe6dd', // glass in the diagrams
  tint: '#edf5f1', // the key-term glosses and the light plate of a construction diagram
  muted: '#5b6863', // running heads, the normals in the diagrams, the colophon
  paper: '#ffffff',
};
// A colour carries its id and its hex. The diagrams read `palette` as well, so retint the
// chapter by editing it: the text, the designs and the drawings change together.
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, so nothing prints blue.
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
// The page in mm, named once: the channel, the opener and the running heads derive from it.
const [TRIM_W, TRIM_H] = [210, 275];
const [TOP, BOTTOM, INNER, OUTER] = [24, 22, 20, 14]; // inner and outer swap on a verso
const LEAD = 13.5; // body leading in pt
const [SERIF, SANS] = ['Merriweather', 'Merriweather Sans'];

// #region answer: a float-only channel on the outer edge, and what goes into it
const layout = {
  layoutType: 'oneAndHalf', // a wide main column and a narrow side column
  sideColumnPercent: 30, // of the 176 mm content width: a 52.8 mm channel
  sideColumnRole: 'floats', // no body text: side figures, side captions and side boxes only
  sideColumnSide: 'outer', // right on a recto, left on a verso (the margins are mirrored)
  gutterWidth: mm(7), // the text column keeps the rest: 176 − 52.8 − 7 = 116 mm
};
// A figure placed with span 'side' stacks in the channel from the head of the page that cites it,

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

变化

#把题注留在图下方

使用图类型的普通默认设置时,图4.1、4.4和4.6的题注会占用正文栏的行,侧栏只放光路图和术语注。

-const resourceTypes = defaultResourceTypes(LANG).map((type) => (type.id !== 'figure' ? type
-  : { ...type, defaultPlacement: { captionSide: true } })); // the built-in types, in LANG
+const resourceTypes = defaultResourceTypes(LANG); // what the engine uses when none are set

#让边栏在每一页都位于右侧

对于在屏幕上一次看一页的文档,可以取消页边距镜像,并把左页的页码和书眉移到右边缘;这样各章可以从任一页开始。

-  sideColumnSide: 'outer', // 右页在右,左页在左(页边距是镜像的)
+  sideColumnSide: 'right', // 页边距不再镜像后,'outer'本来也就是这个意思
-    bottom: mm(BOTTOM), left: mm(INNER), right: mm(OUTER), mirror: true } }, // left:右页的内侧
+    bottom: mm(BOTTOM), left: mm(INNER), right: mm(OUTER), mirror: false } },
-      { level: 1, breakBefore: { enabled: true, parity: 'odd' }, marginBottom: pt(LEAD),
+      { level: 1, breakBefore: { enabled: true, parity: 'any' }, marginBottom: pt(LEAD),
-const verso = { parity: 'even', edge: 'top-left' }; // x从左边缘向内计算
+const verso = { parity: 'even', edge: 'top-right' };
-  head({ id: 'verso-folio', ...verso, ...folio, x: OUTER }),
-  head({ id: 'verso-title', ...verso, content: '{title}', x: OUTER + HEAD_GAP }),
+  head({ id: 'verso-folio', ...verso, ...folio, x: -OUTER }),
+  head({ id: 'verso-title', ...verso, content: '{title}', x: -(OUTER + HEAD_GAP) }),
-  head({ id: 'drop-folio', ...recto, ...folio, pages: 'opener', edge: 'bottom-right', x: -OUTER,
+  head({ id: 'drop-folio', ...folio, pages: 'opener', edge: 'bottom-right', x: -OUTER,

#在色带下开启本章

出血色带上的章首页让一条色带从页面顶部出血,并在上面排标题和168 pt的章号。

常见问题

易错点

边栏框不浮动:它们等待空位

span: 'side'的框不浮动:它排在所跟随的块旁边,页边的通道满了就等到下一页。把每条旁注紧跟在它解释的段落后面。如果一章结束时还有框在等,每个框依围栏的先后占用正文之后页面的边栏,排版会给出afterText警告;在postext 1.24及以前,只会开出一页,其余的框无声无息地丢失。 旁注 →

易错点

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

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

易错点

章首页预留的高度一直到最低的页面锚定元素

高级设计的章首页会预留到其最低元素为止的高度,标题下方锚定在页面或出血上的元素也算在内,所以页脚处的装饰会把正文推到下一页。给这样的元素设reserve: false(从postext 1.5起):它绘制在锚定的位置,不预留任何空间。跟随区段本身的元素(锚定在区段底边,或高度为'fill')从不决定区段的高度。 设计过的章首页 →

易错点

SVG插图中不要用<marker>或滤镜(会退回位图)

SVG图只有在不含<marker>、滤镜和蒙版时,才能在PDF中保持矢量;否则会退回位图,而且层层嵌套的滤镜可能让它在Chrome中变成空白。箭头用路径来画。 作为资源的图和表 →

  • 图和术语注按正文排到它们的顺序在侧栏中依次堆叠,从不并排。临界角术语注的围栏写在斯涅尔定律那一段之后、引用图4.3的那一段之前,所以在第88页上它堆叠在图4.2和图4.3之间,位于引入这个术语的段落旁边。如果它的围栏写在那次引用之后,就会落在图4.3下方。
  • 在一章的第一页,侧栏图排在眉题和数字下面。在第一段引用的图会占住这个位置,把学习目标框沿侧栏往下推,所以本章在学习目标框之后才引用第一张侧栏图。

致谢

文本
原创文字, CC BY 4.0
字体
Merriweather (SIL OFL 1.1) · Merriweather Sans (SIL OFL 1.1)
沙盒