简单来说
一份做日晷的学校科学实验练习册。示例演示几种框:有的分成两半排在两栏,有的移到下一页顶部,有的固定在页脚。
成品一览
《Taller de ciencias》的四页。这是一本西班牙语学校实验练习册,内容是Práctica 4“Construye un reloj de sol”(做一个日晷)。第42页上,安全警示保持完整;十四步的实验步骤铺在黄色底上,角上有一枚指南针徽标,从左栏底部开始,在右栏顶部接着排,行长不变,也不再重复标题。马德里的数据表在这一页离开正文流,排到第43页页首、正文之上。最后一页页脚固定着一枚炭灰色的“Autoevaluación · 10 min”徽标,旁边页边距里有一只指示的手。章首页上,一条日光黄色带里画着马德里纬度下一根木棍从正午到下午5点的影子,一块面板通栏横跨页面,把材料分三栏列出。
这道食谱解答
- 怎样让长框跨栏跨页拆分,或让短框保持完整?
- 怎样把框浮动到页面顶部或底部,同时让正文继续排下去?
- 怎样把徽标或贴纸固定在页面上的某个位置?
- 怎样在页面中间排一个横跨两栏的框,比如三格并列的“数字一览”面板?
简短回答
const HAND = 6, GAP = 2, RULE = 0.75 * PT; // mm: the badge's hand, its gap, the hand's rule
const calloutStyles = [
// Kept whole: keepTogether defaults to true, so a box that does not fit the rest of a
// column moves on in one piece; only a box taller than a whole column splits anyway.
box('seguridad', bar('charcoal', 'aviso')),
// Split: the procedure fits a column, so it takes keepTogether: false to break where it
// falls instead of moving on whole: between steps, or inside one (splitMinLines, default
// 2, keeps at least two lines of the step on each side of the cut). The rest goes on in
// the next column or page without the title or the icon; a corner icon takes no room
// from the text, where an inline one would keep its column in both parts.
box('pasos', { keepTogether: false, background: col('light'),
icon: icon('compas', 7, { position: 'corner', cornerSide: 'outer' }) }),
// Floated: its fence adds placement="top", so the box leaves the flow where the fence
// stands and heads the next page, while the text after it fills this one.
box('datos', { span: 'page', background: col('tint') }),
// Pinned: 'fixed' sets the box on the page where its fence falls, at the bottom-left corner
// of the text block unless fixed.anchor says otherwise, and the column text keeps out of it.
// width: 'auto' shrink-wraps the title, so an empty fence prints a badge.
box('autoevaluacion', { placement: 'fixed', width: 'auto',
// Hang the hand and its rule in the margin (the outer one on this verso), so the badge
// itself lines up with the text: [hand][rule][GAP][badge].
fixed: { offset: { x: mm(-(HAND + RULE + GAP)) } },
marker: { ...icon('mano', HAND), gap: mm(GAP),
rule: { enabled: true, color: col('charcoal'), width: mm(RULE) } },
background: col('charcoal'), borderRadius: mm(3.2),
padding: { top: mm(1.6), right: mm(3.4), bottom: mm(1.6), left: mm(3.4) },
titleStyle: { ...TITLE, color: col('paper') } }),
// Across the page, in the flow: the text above it is cut level and resumes under it.
box('resumen', { span: 'page', backgroundEnabled: false,
stripe: { enabled: true, side: 'top', width: pt(2.5), color: col('charcoal') } }),
];
用料
做法
#1 · 所有标注框共用一个基础样式
const TITLE = { fontFamily: LABEL, fontSize: pt(8), color: col('charcoal'),
textTransform: 'uppercase', letterSpacing: pt(1.2) }; // bold by default
const BOX_TYPE = { fontSize: pt(8.8), lineHeight: pt(12.4) }; // colours inherit bodyText
const box = (id, device) => ({ id, // margins: the defaults, snapped to whole grid lines
padding: { top: mm(3), right: mm(3.6), bottom: mm(3.4), left: mm(3.6) },
titleStyle: { ...TITLE, gap: mm(2) }, body: BOX_TYPE,
lists: { gap: mm(2), itemSpacing: pt(3) }, ...device });
const icon = (id, size, extra) => ({ kind: 'resource', resourceId: id, size: mm(size),
...extra });
// A side bar with its icon centred on it, 1.4 mm narrower than the bar.
const bar = (hue, id, width = 5.6) => ({ backgroundEnabled: false,
stripe: { enabled: true, side: 'left', width: mm(width), color: col(hue) },
icon: icon(id, width - 1.4) });
每个样式都从box()出发:等宽字体的标题、8.8 pt的字号(正文是9.4 pt)和内边距。然后各加一种手段:安全警示是一条炭灰色竖条,实验步骤是黄色底,数据表是纸色底,小结是顶部一条线,徽标是炭灰色的圆角胶囊。学生不用看标题,凭这些手段就能分清各个框。对实验步骤来说,底色还标示了接续:右栏顶部那一段没有标题,只能靠黄色把它和左栏底部的第1至4步联系起来。
#2 · 保持完整、拆分、浮动、固定
代码就是上面的简短回答。实验步骤高192 mm,一栏能容纳211 mm,所以如果整框不拆,它会跳到右栏顶部,在左栏底部留下43 mm空白。keepTogether: false让它在落点处开始,然后在下一栏接着排,也可以接到下一页,第二个变化就是这种情况(:::callout容器)。接续部分会去掉标题和图标。行内图标会在前后两段里都占去步骤的一栏宽度;所以指南针放在框的外角,不占宽度。数据表的围栏带有placement="top",于是这个框在第42页离开正文流,排到下一页页首。徽标是fixed,负的offset.x把指示的手和它的线移进外侧页边距,徽标的左边缘因而和正文对齐。
#3 · 用三栏面板横跨页面
const materials = box('material', { span: 'page', background: col('sun'),
marginBottom: pt(LEAD), // one more grid line of air before the text resumes
padding: { top: mm(3.6), right: mm(4.4), bottom: mm(3.8), left: mm(4.4) },
columnGap: mm(GUTTER), // the page's gutter: the panel's columns sit as far apart as the text's
body: { ...BOX_TYPE, paragraphSpacing: false },
lists: { color: col('ink'), gap: mm(1.8), itemSpacing: pt(1) } });
有了span: 'page',材料框就横跨两栏。它上方的引言齐平截断,正文在它下方恢复两栏(第41页)。围栏里的:::columns{count=3 breaks="5,9"}直接指定每栏从哪里开始,而不是让各栏齐底。breaks按子块计数,每个列表项算一个块,所以第二栏和第三栏分别从第五块和第九块,也就是加粗的小标题开始。columnGap取页面的7 mm栏间距,面板内各栏的间距和正文栏一样。
:::callout{type="material" title="Necesitarás"}
:::columns{count=3 breaks="5,9"}
**Para la esfera**
- Cartón pluma de 20 × 30 cm
- La plantilla de la esfera
- Pegamento y rotulador
**Para el gnomon y la base**
…
:::
:::#4 · 色块用一种黄,长框用浅一些的黄
const palette = {
ink: '#1b2430', // text
charcoal: '#2b2d42', // the safety bar, the summary's rule, the table head, the badge, titles
sun: '#f2b705', // the opener band and the materials panel: fields, never type
light: '#fad65a', // the procedure
tint: '#fff6d6', // the data sheet
rule: '#d7dde3', // hairlines
muted: '#5d6b78', // running heads, notes
paper: '#ffffff',
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at charcoal, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.charcoal })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
日光黄只用于色块,也就是色带和面板,炭灰色和墨色在上面的对比度分别是7.4:1和8.6:1。作为白底上的文字颜色,它只有1.8:1,所以从不用来印字。实验步骤用light,一种更淡的黄:墨色在上面达到11:1,而在图库缩略图里仍看得出拆分处。数据表用更接近纸色的tint,不会和跨页上另一个有底色的框,也就是实验步骤混淆。main-color指向炭灰色,配置里没有覆盖的默认值因此印成炭灰色,而不是引擎的蓝色。
#5 · 章首页用一条黄色带开头
const BAND = 100; // mm from the trim to the foot of the band
const ART_W = 80; // mm: the width of the band's drawing, at the fore-edge
const AIR = 8; // mm between the band and the first line of text
const [TITLE_W, LEAD_W] = [104, 88]; // mm: the title's and the lead's measure
const onBand = { color: col('charcoal'), align: 'left' }; // a long title or lead wraps
const below = (id, y, width) => ({ anchor: { to: `#${id}`, edge: 'below' },
offset: { y: mm(y) }, size: { width: mm(width) } });
const opener = { enabled: true, minHeight: mm(BAND - TOP + AIR), slot: { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('sun') },
placement: { anchor: { to: 'page', edge: 'top-left' }, size: { height: mm(BAND) } } },
{ kind: 'image', id: 'art', resourceId: 'sombras', placement: { anchor: { to: 'page',
edge: 'top-right' }, size: { width: mm(ART_W), height: mm(BAND) } } },
{ kind: 'text', id: 'kicker', content: '{attr.kicker}', ...onBand, fontFamily: LABEL,
fontSize: pt(8.5), fontWeight: 700, letterSpacing: pt(1.7), textTransform: 'uppercase',
placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(4) } } },
{ kind: 'text', id: 'title', content: '{titleText}', ...onBand, fontFamily: TEXT,
fontSize: pt(44), fontWeight: 800, lineHeight: 0.98, placement: below('kicker', 3, TITLE_W) },
{ kind: 'text', id: 'lead', content: '{attr.lead}', ...onBand, fontFamily: TEXT,
fontSize: pt(10.5), lineHeight: 1.4, placement: below('title', 5, LEAD_W) },
{ kind: 'text', id: 'meta', content: '{attr.meta}', ...onBand, fontFamily: LABEL,
fontSize: pt(7.4), fontWeight: 700, letterSpacing: pt(1.1), textTransform: 'uppercase',
placement: below('lead', 4, LEAD_W) },
] } };
色带是锚定在页面上的方框元素,图是切口一侧的图片元素;眉题、导语和课时行来自标题行上的属性。因为色带锚定在页面上,章首页本来就会预留到色带底边的高度,正文会在色带下方3.6 mm处开始。minHeight: BAND - TOP + AIR要求8 mm;预留高度对齐到下一条网格线,正文从色带下方8.4 mm处开始。
完整食谱
// ═══ Postext Cookbook · Nº 021 · Boxes that split, float and pin ══════════════════
// https://postext.dev/en/cookbook/boxes-split-float-pin
// Code: MIT · Text: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Host Grotesk, Commit Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// Four pages of a school lab workbook in Spanish, and five ways a box can sit on them.
import {
buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'es'; // @lang: the language of the sample document (this recipe is Spanish only)
const RECIPE = 'boxes-split-float-pin';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: one yellow for fields, paler ones for two boxes, near-blacks for type and bars
const palette = {
ink: '#1b2430', // text
charcoal: '#2b2d42', // the safety bar, the summary's rule, the table head, the badge, titles
sun: '#f2b705', // the opener band and the materials panel: fields, never type
light: '#fad65a', // the procedure
tint: '#fff6d6', // the data sheet
rule: '#d7dde3', // hairlines
muted: '#5d6b78', // running heads, notes
paper: '#ffffff',
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at charcoal, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.charcoal })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const TEXT = 'Host Grotesk'; // text and display
const LABEL = 'Commit Mono'; // labels: kickers, box titles, step numbers, heads, the table
const LEAD = 13.6; // pt: the body leading, the pitch of the baseline grid
const LINES = 44; // grid lines in a full column
const [TRIM_W, TRIM_H, TOP, INNER, OUTER, GUTTER] = [195, 255, 22, 18, 14, 7]; // mm
const PT = 25.4 / 72; // mm in a point
// #region look: the base of every box (a mono title, smaller type), then one device each
const TITLE = { fontFamily: LABEL, fontSize: pt(8), color: col('charcoal'),
textTransform: 'uppercase', letterSpacing: pt(1.2) }; // bold by default
const BOX_TYPE = { fontSize: pt(8.8), lineHeight: pt(12.4) }; // colours inherit bodyText
const box = (id, device) => ({ id, // margins: the defaults, snapped to whole grid lines
padding: { top: mm(3), right: mm(3.6), bottom: mm(3.4), left: mm(3.6) },
titleStyle: { ...TITLE, gap: mm(2) }, body: BOX_TYPE,
lists: { gap: mm(2), itemSpacing: pt(3) }, ...device });
const icon = (id, size, extra) => ({ kind: 'resource', resourceId: id, size: mm(size),
...extra });
// A side bar with its icon centred on it, 1.4 mm narrower than the bar.
const bar = (hue, id, width = 5.6) => ({ backgroundEnabled: false,
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#实验步骤整框不拆
去掉这项设置后,实验步骤因为放得进一栏,会整框移到右栏顶部,在左栏底部留下43 mm空白。
- box('pasos', { keepTogether: false, background: col('light'),
+ box('pasos', { background: col('light'),#把数据表浮动到页脚
在content.es.md里改成placement="bottom"后,数据表占据第42页底部,也就是它的围栏所在的那一页。被它挤走的实验步骤从右栏顶部开始,跨翻页拆开,在第43页顶部结束。
-:::callout{type="datos" placement="top" title="Hoja de datos · Madrid, 40,4° N · 3,7° O"}
+:::callout{type="datos" placement="bottom" title="Hoja de datos · Madrid, 40,4° N · 3,7° O"}常见问题
易错点
'top'浮动体默认排在引用页的下一页,除非允许它置于引用页顶
默认情况下浮动体不会排在自己的引用之前,所以在第N页引用的整页宽'top'浮动体会出现在第N+1页的顶部。在资源或其类型的defaultPlacement上设置placement.citingPage: true(或者对全书设置layout.floatsAtCitingPage,自postext 1.25起),它就改为置于第N页顶部,引用处之上的文字移到它下方,前提是它不超过layout.maxTopFraction且引用行仍留在该页。它不会置于章首页或强制分页之后的页面顶部:那时仍排在下一页,而'auto'或'bottom'可以占用引用页的底部。 图的放置 →
版面警告 · calloutOverflow
标注框超出所在栏
原因. 某个无法在任何位置切开的标注框(比栏还高的图、表或嵌套在其中的框,或splitMinLines设得太高)放下后超出了所在栏;引擎把它记在doc.warnings中,沙盒会列出它。比空侧栏还高的span: 'side'框同样会超出。
解决. 缩短这个框、用keepTogether: false让它可以拆分、调低splitMinLines,或换一个span值。 文档 →
- 比一整栏还高的标注框,不管
keepTogether怎么设都会拆开。这项设置只对放得进一栏的框起作用,比如这里高192 mm、放进211 mm栏的实验步骤。 splitMinLines(默认为2)是拆分处两侧各自至少留下的行数,按被拆开的那一步来数,所以任何一步都不会只剩一行。实验步骤前面的正文经过调整,让拆分落在第4步和第5步之间。拆分框上方的正文每改一次,拆分位置都可能移动,所以每次修改后都要看看它落在哪里。- 行内图标自己占一栏,接续部分也保留这一栏,只是空着:前后两段的步骤都少了图标的宽度。把图标放在角上,或者设一条侧边色条让图标坐在上面,步骤就能用满整个行长。
- 用
placement="top"浮动的框排在围栏所在页的下一页页首,所以围栏要放在框应当出现的那一页的前一页。这里它紧挨在实验步骤前面,位于第42页。
致谢
- 文本
- The solar-noon table: computed for Madrid with NOAA's solar equations · NOAA Global Monitoring Laboratory · 公有领域
- 图片
- The shadow chart, the dial in profile and the icons, drawn in code in the page's palette · Ignacio Ferro · CC BY 4.0
- 字体
- Host Grotesk (SIL OFL 1.1) · Commit Mono (SIL OFL 1.1)


