跳到主要内容
食谱编号8

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

按颜色区分的一组教材标注框

六种教材标注框、三种颜色,靠带图标的色条、徽标、编号页签、一排象形图或框外的标记区分。

本页内容

页码28–29 · 第2–3页,共3页

  • 英文样例:尚无中文版本
  • 成品尺寸210 × 280 mm
  • 2栏, 栏间距8 mm
  • Noto Serif 9.4/13.4
  • Barlow Semi Condensed
  • Lexend
  • 3页
  • 难度
  • Postext 1.57.1
  • 排版用时99 ms
  • 179行代码

简单来说

一本生物课本里的一章,有六种标注框,比如注意事项和实验说明。每种都有自己的颜色和图标,学生一眼就能分清。

成品一览

Living Matter是一本高中生物教材,页面为210 × 280 mm。它的第2章以一幅细胞图开篇,细胞被页面顶角截去一部分,章号在细胞核中。其后是三种颜色的六种标注框,每种有自己的标识,学生在阅读之前就能分清目标和警示。洋红色标记学习目标,排在一条放着靶心的色条上;也标记自我检测,框外有一个带勾的方块。绿色标记学习提示,旁边是一条顶端带灯泡徽标的细线;也标记带三个安全象形图的实验框。警示框是琥珀色,外角有一个警告徽标。BOX 2.1是淡紫色,上方有黑色页签和一个烧瓶。粗体术语取所在框的颜色,BOX 2.1中是洋红色。

这道食谱解答

  • 怎样做带图标、色条和圆角的注释、提示或警告框?
  • 怎样加编号标签(“BOX 1-1”)、角标图标,或带线的边栏图标?
  • 怎样给正文或框里的关键词(粗体或斜体)着色?

简短回答

script.js · 第41–62行在完整代码中
// box(id, hue, device) is the shared base: the hue colours the title, bullets and key terms.
const TAB = 5, BADGE = 7.4; // in mm: the tab's height (and offset), the warning badge's size
const calloutStyles = [
  // The first style is also the one a missing or misspelt type falls back to.
  box('objectives', 'band', { title: t({ en: 'In this chapter', es: 'En este capítulo' }),
    background: col('tintBand'), stripe: { enabled: true, width: mm(7.5), color: col('band') },
    icon: icon('target', 5) }), // on a side stripe (left by default) the icon is centred on it
  box('feature', 'band', { background: col('mist'),
    titleStyle: { fontFamily: 'Lexend', fontSize: pt(10.5), color: col('ink'), gap: mm(1.6) },
    // Printed only where the fence has label="…": the tab, the flask beside it, a rule to them.
    label: { fontFamily: LABEL, fontSize: pt(8), color: col('paper'), background: col('ink'),
      position: 'top-left', // this verso's outer corner, above the badge ('top-right' by default)
      height: mm(TAB), offset: mm(TAB), paddingX: mm(2.3), // offset = height: on the top edge
      icon: { resourceId: 'flask', width: mm(4.4), gap: mm(1.2) },
      rule: { enabled: true, color: col('ink'), width: pt(1.2) } } }),
  box('warning', 'warnInk', { background: col('tintWarn'), borderRadius: mm(1.4),
    border: { enabled: true, color: col('warn'), width: pt(0.75) },
    // The badge hangs half past the outer corner: sides of half badge + GAP align the title.
    padding: { top: mm(2.8), right: mm(BADGE / 2 + GAP), bottom: mm(3.2),
      left: mm(BADGE / 2 + GAP) },
    icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'outer' }) }),
]; // straight into config(), followed by the 'more' styles

用料

类型
Noto Serif, Lexend, Barlow Semi Condensed(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 给每种框一种颜色

script.js · 第14–22行在完整代码中
const palette = { ink: '#1f2430', paper: '#ffffff', // text; type on stripes and tabs
  band: '#ab2e78', tintBand: '#fae3ef', // the chapter's magenta: opener, objectives, self-check
  tip: '#25734a', tintTip: '#e2f0e7', // study tips and lab work
  warn: '#e39422', tintWarn: '#fcefd8', warnInk: '#94540a', // frame and badge; tint; its type
  mist: '#e8e3f0', rule: '#dccfd8', muted: '#675e66' }; // feature box; lines in drawings; heads
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the band, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.band })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));

六种框共用三种色相:洋红用于学习目标和自我检测,绿色用于学习提示和实验,琥珀色用于警示。BOX 2.1底色为淡紫,关键词用洋红色。每种色相都有一个用于底色的浅色调,每项颜色设置都链接到一个调色板条目,所以改一种色相只需改一个值。这么小的字需要4.5:1的对比度。洋红在淡紫上达到4.9:1,但琥珀色在警示框底色上只有2.2:1,所以警示框只把琥珀色用于边框和徽标,标题和关键词用warnInk排,这是一种更深的琥珀色,对比度5.2:1。

#2 · 其余设置全部共用

script.js · 第28–37行在完整代码中
const box = (id, hue, device) => ({ id, marginTop: pt(LEAD), marginBottom: pt(LEAD / 2),
  padding: { top: mm(2.8), right: mm(3.4), bottom: mm(3.2), left: mm(3.4) },
  titleStyle: { fontFamily: LABEL, fontSize: pt(8.5), color: col(hue), // bold by default
    textTransform: 'uppercase', letterSpacing: pt(1.4), gap: mm(GAP) },
  body: { fontFamily: LABEL, fontSize: pt(9.6), lineHeight: pt(12.6), color: col('ink'),
    boldColor: col(hue), textAlign: 'left', firstLineIndent: pt(0), paragraphSpacing: true },
  lists: { color: col(hue), gap: mm(2.2), itemSpacing: pt(2.4) }, // bullets and numbers
  ...device }); // the kind's title, fill and device: its spread wins over the base
// An SVG or bitmap resource, fitted into a square of that size (width × size with `width`).
const icon = (id, size, more) => ({ kind: 'resource', resourceId: id, size: mm(size), ...more });

box()存放所有框共有的设置:外边距、内边距、标题和正文字体、列表间距。它的hue参数给标题、项目符号和编号以及粗体文字上色。body.boldColor让目标框中的fluid mosaic model变成洋红色,让警示框中的cell wall变成琥珀棕色;正文中的粗体仍是墨色(框内排版)。声明calloutStyles会替换引擎自带的唯一样式,即名为note的灰色框。type缺失或拼错的围栏会使用第一种样式,所以列表以每章都有的学习目标开头。

#3 · 色条、页签和徽标

代码见上文的简短回答。目标框的色条宽7.5 mm,靶心图标宽5 mm,所以图标位于色块之内,两侧都留有空隙。页签的offset等于它的height,这样页签立在框的上边缘上,不压住标题。'top-left'是这个左页的外角;烧瓶跟在页签后面,细线沿上边缘从烧瓶延伸到另一角。position: 'corner'让警示框的徽标越过边缘半个宽度,cornerSide: 'outer'把它放到切口一侧,在这个左页上是左边。在这一侧,除非内边距已经覆盖徽标的一半加上标题间距,否则引擎会让标题缩进以避开徽标;所以警示框两侧的内边距都设为BADGE / 2 + GAP,标题与正文对齐。只有这个框设了borderRadius:它是唯一带边框的框,1.4 mm的圆角让四角柔和一些。带色条的框若设圆角,色条外侧的角也会随之变圆(标注框样式)。

#4 · 另外三种标识

script.js · 第66–79行在完整代码中
const BULB = 6.4; // the study tip's badge in mm: the text clears it
const moreStyles = [
  box('tip', 'tip', { title: t({ en: 'Study tip', es: 'Para recordar' }),
    backgroundEnabled: false, stripe: { enabled: true, width: pt(2.5), color: col('tip') },
    // A stripe narrower than its icon: the badge sits on it at the top and hides it there.
    padding: { top: mm(0.6), right: mm(0), bottom: mm(0.6), left: mm(BULB / 2 + 2) },
    icon: icon('bulb', BULB) }),
  box('safety', 'tip', { background: col('tintTip'), marginTop: pt(4), // under its heading
    icon: icon('safety', 7, { width: mm(24.5), align: 'center' }) }), // 3 pictograms, 1 image
  box('check', 'band', { background: col('tintBand'),
    // Outside the frame: [marker][rule][gap][box], the rule as tall as the box.
    marker: { kind: 'resource', resourceId: 'check', size: mm(7), align: 'top', gap: mm(3),
      rule: { enabled: true, color: col('band'), width: pt(0.75) } } }),
];

学习提示的色条宽2.5 pt,灯泡徽标宽6.4 mm,所以色条印出来是一条细线,徽标盖在它的顶端;左内边距取徽标宽度的一半加2 mm,使文字避开灯泡。icon.width把图片放进一个宽框而不是方框,所以画成一幅24.5 mm图像的三个安全象形图构成实验框的图标栏,与文字居中对齐。自我检测的标记在边框之外,顺序为标记、细线、间隙、框。它在0.75 pt细线上的方块与学习提示的圆形徽标区分开来。围栏加上span="page" placement="bottom",把自我检测框浮动到第29页底部。

#5 · 图标都是资源

script.js · 第240–265行在完整代码中
const svgResource = (id, width, height, extra) => ({ id, typeId: 'figure', kind: 'svg',
  svg: { fileId: `${id}.svg`, width, height }, createdAt: 0, updatedAt: 0, ...extra });
const pageTop = { position: 'top', span: 'page' }; // a 'top' float opens the page after its :ref
const resources = [
  // Uncited, so never placed as figures: the box styles and the opener use them by id.
  ...['target', 'bulb', 'caution', 'check'].map((id) => svgResource(id, 240, 240)),
  svgResource('flask', 200, 240), svgResource('safety', 760, 240), svgResource('cell', 2000, 2000),
  svgResource('mosaic', 3480, 1240, { placement: pageTop, caption: t({
    en: 'The fluid mosaic: magenta phospholipids, green proteins, an amber carrier with its '
      + 'glucose, grey cholesterol, amber sugars.', // one line: the page is wide
    es: 'El mosaico fluido: fosfolípidos magenta, proteínas verdes, transportadora ámbar con su '
      + 'glucosa, colesterol gris, azúcares ámbar.' }),
    altText: t({ en: 'A cell membrane in section', es: 'Una membrana celular en sección' }) }),
  svgResource('fusion', 1800, 500, { placement: { position: 'here' }, caption: t({
    en: 'Mouse proteins in green, human proteins in magenta (the red dye): the two cells, the '
      + 'hybrid just after fusion and the same hybrid 40 minutes later.',
    es: 'En verde, las proteínas de ratón; en magenta, las humanas (el colorante rojo): las dos '
      + 'células, el híbrido recién fusionado y el mismo híbrido 40 minutos después.' }),
    altText: t({ en: 'Two cells fusing into a hybrid', es: 'Dos células que se fusionan' }) }),
  svgResource('osmosis', 3480, 860, { placement: pageTop, caption: t({
    en: 'Red blood cells in a hypertonic, an isotonic and a hypotonic solution. Arrows show the '
      + 'net flow of water.',
    es: 'Glóbulos rojos en una disolución hipertónica, una isotónica y una hipotónica. Las flechas '
      + 'indican el flujo neto de agua.' }),
    altText: t({ en: 'Blood cells in three solutions', es: 'Glóbulos rojos en tres medios' }) }),
];

每个图标、页签旁的烧瓶和章首页的细胞,都是随内容声明、按文件id注册的SVG资源。样式按资源id引用它们,而正文从不引用它们,所以它们都不会成为编号的图。✓或☞这样的字符写起来更短,但页面加载的Fontsource latin字体文件里没有它们:Canvas会从系统字体借用,PDF则会丢掉。图中也不含文字:颜色由题注说明,同一幅图两个版本都能用。

#6 · 用一个细胞开启本章

script.js · 第83–102行在完整代码中
const text = (id, content, family, size, color, placement, extra) => ({ kind: 'text', id,
  content, fontFamily: family, fontSize: pt(size), color: col(color), placement,
  align: 'left', ...extra }); // an opener's texts wrap by default
const below = (id, y, width) => ({ anchor: { to: `#${id}`, edge: 'below' },
  offset: { y: mm(y) }, size: { width: mm(width) } });
const chapter = t({ en: 'Chapter {chapterNumber}', es: 'Capítulo {chapterNumber}' });
const opener = { enabled: true, minHeight: mm(66), slot: { elements: [
  { kind: 'image', id: 'cell', resourceId: 'cell', placement: { anchor: { to: 'page',
    edge: 'top-right' }, offset: { x: mm(58), y: mm(-71) }, size: { width: mm(150) } } },
  text('numeral', '{chapterNumber}', 'Lexend', 118, 'paper', { anchor: { to: 'page',
    edge: 'top-right' }, offset: { x: mm(-OUTER), y: mm(14) } },
    { fontWeight: 800, lineHeight: 1, align: 'right' }),
  text('kicker', `${chapter} · {attr.unit}`, LABEL, 9, 'band', { anchor: { to: 'container',
    edge: 'top-left' }, offset: { y: mm(3) } },
    { fontWeight: 700, letterSpacing: pt(1.8), textTransform: 'uppercase' }),
  text('title', '{titleText}', 'Lexend', 34, 'ink', below('kicker', 2.5, 112),
    { fontWeight: 700, lineHeight: 1.04 }),
  text('lead', '{attr.lead}', 'Noto Serif', 10.5, 'ink', below('title', 5, 98),
    { italic: true, lineHeight: 1.42 }),
] } };

细胞是一个锚定在页面右上角并被推出页面的图像元素,所以露出的主要是它的左下四分之一,数字位于细胞核中。两者都在minHeight为章首页预留的66 mm之内。眉题和导语来自标题行# Cells and \\ Membranes {unit="The cell" lead="…"},其中的\\只在章首页让标题换行。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 008 · A family of textbook boxes ═══════════════════════
// https://postext.dev/en/cookbook/textbook-box-family
// Code: MIT · Text: original (CC BY 4.0) · Drawings and icons: generated in code (CC BY 4.0)
// Fonts: Noto Serif, Lexend, Barlow Semi Condensed (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

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

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: three hues for six kinds of box; everything links to an entry
const palette = { ink: '#1f2430', paper: '#ffffff', // text; type on stripes and tabs
  band: '#ab2e78', tintBand: '#fae3ef', // the chapter's magenta: opener, objectives, self-check
  tip: '#25734a', tintTip: '#e2f0e7', // study tips and lab work
  warn: '#e39422', tintWarn: '#fcefd8', warnInk: '#94540a', // frame and badge; tint; its type
  mist: '#e8e3f0', rule: '#dccfd8', muted: '#675e66' }; // feature box; lines in drawings; heads
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the band, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.band })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
// Label face (box text and titles, tabs, captions, heads); leading in pt; outer margin in mm
const LABEL = 'Barlow Semi Condensed', LEAD = 13.4, OUTER = 16, GAP = 1.5; // GAP: title gap, mm

// #region base: what every box shares; each kind brings its colour and its one device
const box = (id, hue, device) => ({ id, marginTop: pt(LEAD), marginBottom: pt(LEAD / 2),
  padding: { top: mm(2.8), right: mm(3.4), bottom: mm(3.2), left: mm(3.4) },
  titleStyle: { fontFamily: LABEL, fontSize: pt(8.5), color: col(hue), // bold by default
    textTransform: 'uppercase', letterSpacing: pt(1.4), gap: mm(GAP) },
  body: { fontFamily: LABEL, fontSize: pt(9.6), lineHeight: pt(12.6), color: col('ink'),
    boldColor: col(hue), textAlign: 'left', firstLineIndent: pt(0), paragraphSpacing: true },
  lists: { color: col(hue), gap: mm(2.2), itemSpacing: pt(2.4) }, // bullets and numbers
  ...device }); // the kind's title, fill and device: its spread wins over the base
// An SVG or bitmap resource, fitted into a square of that size (width × size with `width`).
const icon = (id, size, more) => ({ kind: 'resource', resourceId: id, size: mm(size), ...more });
// #endregion

// #region answer: three devices: an icon on a wide stripe, a numbered tab, an outer badge
// box(id, hue, device) is the shared base: the hue colours the title, bullets and key terms.
const TAB = 5, BADGE = 7.4; // in mm: the tab's height (and offset), the warning badge's size
const calloutStyles = [
  // The first style is also the one a missing or misspelt type falls back to.
  box('objectives', 'band', { title: t({ en: 'In this chapter', es: 'En este capítulo' }),
    background: col('tintBand'), stripe: { enabled: true, width: mm(7.5), color: col('band') },
    icon: icon('target', 5) }), // on a side stripe (left by default) the icon is centred on it
  box('feature', 'band', { background: col('mist'),

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

变化

#让框横跨两栏

在围栏上加span="page",任何一种框都会横跨两栏;BOX 2.1还嵌套了一个:::columns组,把图放在文字旁边。

-:::callout{type="warning" title="Membrane or wall?"}
+:::callout{type="warning" title="Membrane or wall?" span="page"}

#把徽标挂在订口一侧

'inner'把徽标移到订口一侧,右页上在左边,左页上在右边;要在那一侧为伸出框外的一半留出空间。

-    icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'outer' }) }),
+    icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'inner' }) }),

#把页签立在右角

默认值'top-right'会把页签整体镜像过来:页签移到右角,烧瓶在它左边,细线从左边延伸过来。

-      position: 'top-left', // 这个左页的外角,位于徽标上方(默认为'top-right')
+      position: 'top-right',

常见问题

易错点

Fontsource的文件只覆盖字母,不含符号

工具包嵌入每种字体在Fontsource的latin文件;正文用到č、ł、†时再嵌入latin-ext文件,用到α、χ时再嵌入greek文件(字体族须有该文件:Lora和Gelasio没有),屏幕和PDF都一样。→、≈、✓、★等符号不在这些文件里,会在PDF中缺失:把它们画出来、写成数学公式,或换用文件中包含它们的字体。 嵌入PDF的字体 →

易错点

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

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

易错点

第1页是右页:按物理页码规划页面

第1页是右页(奇数页),第2页是第一个左页(偶数页),所以要按物理页码规划跨页:偶数页上的章首页对着它后面的奇数页。 换页与换栏 →

版面警告 · unknownCalloutType

未知标注框类型

原因. 某个:::callout指定的类型没有在calloutStyles中定义,因此它不加提示地套用第一个样式。

解决. 定义该样式,或改正type属性。 文档 →

  • 只有当围栏带有label="BOX 2.1",且样式有label对象时,页签才会印出。没有该对象的样式会忽略这个属性。
  • Postext不给框编号。页签就是围栏里写的文字,所以BOX 2.1、BOX 2.2这些编号要手动输入。
  • 角上的徽标会越过框的侧边半个宽度,在页面外侧伸进页边距。在同一页的另一栏里,它会伸进栏间距,所以带徽标的框要放在外侧栏,或者给它留出空间。
  • 框的lists.color同时给项目符号和有序列表的编号上色,所以这里带编号的步骤和问题都印成所在框的颜色。如果只想给某个框的项目符号上色,就不要在它的样式里写lists.color,改为给文档设置unorderedLists.color;编号则保持orderedLists.color。

致谢

文本
原创文字, CC BY 4.0
字体
Noto Serif (SIL OFL 1.1) · Lexend (SIL OFL 1.1) · Barlow Semi Condensed (SIL OFL 1.1)
沙盒