简单来说
一份给八岁孩子的科学练习单,讲植物的各个部分。每道题下面有答题框,有些句子留了空,从词语表里选词填写。
成品一览
Seedlings Science的练习单3A,这是一套虚构的面向八岁儿童的系列教材,共四页,200 × 260 mm,单面印刷。叶绿色色带上有黄色的单元标签、标题,以及填写姓名和日期的白色栏位;旁边一个白色圆盘里是一株水彩豌豆,六个部位用黄色编号标出。这幅水彩画由扩散模型生成,交来时已经放进圆盘,是在色带边缘相接的两张JPEG:上面一张的四角是色带的绿色,下面一张是白色;编号和引线仍用代码画在图上。每个活动都是一个带编号的标题,下面是一张浅绿色卡片。卡片里有:带灰色小提示、可写好几行的白色答题框,由黄色行内标签组成的词库,句子中描边的空,一张把食物和植物部位对应起来的表格,以及一份用来涂色的圆圈检查清单。每个答题框位于题目下方2 mm,距后面的内容5 mm;每张卡片结束处距本页后面的任何内容都是9 mm。正文字体是Andika,这是SIL为学习阅读的儿童设计的字体。
这道食谱解答
- 怎样嵌套框,比如装着答题框的练习卡片,并精确控制间距?
- 怎样做练习页:填空横线、答题框、词库、清单?
- 怎样做行内标签:键盘按键、标签、练习用的词库?
- 怎样在表格单元格里放图片或图标?
简短回答
// In the Markdown a box inside a box is a fence inside a fence (each ::: closes one):
// :::callout{type="card"}
// What is inside the pea pods?
// :::callout{type="answer"}
// Write here ← the prompt first: a :::space that opens a box is dropped
// :::space{lines=3} (gotcha: space-dropped-box-top)
// :::
// :::
const [ASK, AFTER, HEAD, NEXT] = [2, 5, 3, 9]; // mm: question→box, box→next, heading→card
const card = {
id: 'card', background: col('tint'), borderRadius: mm(4),
padding: { top: mm(4), right: mm(5), bottom: mm(5), left: mm(5) },
marginTop: mm(HEAD), marginBottom: mm(NEXT), // NEXT: under a card, to whatever follows
// Off the grid, or what follows a card drops to the next grid line. Only the outer card can
// set it: a nested box ignores snapToGrid, span and placement (gotcha: nested-callout-limits).
snapToGrid: false,
body: { paragraphSpacing: false }, // the boxes' margins do all the spacing
lists: { bulletChar: '', // no bullet: in the checklist a circle chip opens each item
gap: mm(2), itemSpacing: pt(7) }, columnGap: mm(6),
};
// A nested box's margins collapse with its neighbours', the larger winning (ASK, AFTER); the
// last box's marginBottom falls inside the card's padding instead of adding to it.
const nested = (id, look) => ({ id, background: col('paper'), borderRadius: mm(2.5),
marginTop: mm(ASK), marginBottom: mm(AFTER), ...look });
const answer = nested('answer', { border: { enabled: true, color: col('rule'), width: pt(0.75) },
padding: { top: mm(1.6), right: mm(3), bottom: mm(2), left: mm(3) },
// A small grey prompt on a full 17 pt line; :::space counts in these body lines.
body: { fontFamily: LABEL, fontSize: pt(8), color: col('muted') } });
const bank = nested('bank', { title: t({ en: 'Word bank', es: 'Banco de palabras' }),
padding: { top: mm(2.2), right: mm(3), bottom: mm(2.4), left: mm(3) },
titleStyle: { fontFamily: LABEL, fontSize: pt(7.5), fontWeight: 600, color: col('leaf'),
textTransform: 'uppercase', letterSpacing: pt(1.3), gap: mm(1.6) } });
// config() plugs in all three: calloutStyles: [card, answer, bank].
用料
- 功能
- 嵌套框有意脱离网格行内标签标注框框内或正文中的分栏显式竖向间距标题样式编号标题不编号的章设计过的章首页页面设计中的文字、线条和框页面设计中的图片标题属性标题中的换行命名表格样式表格单元格中的图片自定义资源类型图就放在这里项目符号列表与清单编号列表
- 类型
- Andika, Baloo 2, Fredoka(SIL OFL 1.1)
- 素材
plant-1092.jpgplant-foot-1092.jpg- The pea plant on the opener, a watercolour (top of the disc) (Generated With Diffusion Models, 原创)
- The pea plant’s roots, the foot of the same watercolour below the band (Generated With Diffusion Models, 原创)
- The plant's leader lines, the six food icons and the star, drawn in code in the page’s palette (Ignacio Ferro, MIT)
做法
#1 · 标题也脱离网格
headings: { fontFamily: DISPLAY, snapToGrid: false, // or what follows snaps back to the grid
balancing: { enabled: false }, // no extra space above headings to fill out a page
levels: [
// parity 'any': a sheet opens the next page, on either side.
{ level: 1, span: 'page', breakBefore: { enabled: true, parity: 'any' },
marginTop: pt(0), marginBottom: pt(0), advancedDesign: opener() },
{ level: 2, numberingTemplate: '{2}', marginTop: mm(0), marginBottom: mm(0),
advancedDesign: activity },
] },
在简短回答中,每个框的外边距与相邻框的外边距折叠,取较大的一个。词库的5 mm压过了下方列表上面的2 mm,最后一个答题框的5 mm落在卡片5 mm的底部内边距之内,而不是叠加上去。如果留在网格上,二级标题会占用10.8到14.1 mm,而不是其圆盘的8.6 mm,因为标题后面的内容会回到下一条17 pt的行上;headings.snapToGrid: false让它保持8.6 mm。在嵌套样式上设置snapToGrid没有作用,因为只有外层卡片可以脱离网格(嵌套框)。各栏齐底已关闭,因为它会在标题上方加空来填满页面;在第2页,它会在活动3上方留出33 mm而不是9 mm。
#2 · 每个空都用行内标签
const chip = (id, fill, look) => ({ id, fontFamily: LABEL, background: col(fill),
borderWidth: pt(0), borderColor: col('leaf'), borderRadius: em(1), ...look });
// An empty chip, :chip[ ], is a box of padding and outline alone. It is a band 1.25 em tall,
// so paddingX ROUND makes it a circle.
const [ROUND, OUTLINE] = [em(0.625), pt(0.9)];
// A blank is all padding, 2 × paddingX wide and 19.6 pt tall: taller than the 17 pt line, so
// the card's itemSpacing sets its items 24 pt apart (gotcha: chip-overlap).
const blank = (id, width) => chip(id, 'paper', { borderWidth: OUTLINE, borderRadius: mm(1.4),
paddingX: mm(width / 2), paddingY: em(0.12), fontSize: em(1.15) });
const chipStyles = [
chip('word', 'sun', { bold: true, paddingX: em(0.6), paddingY: em(0.08), gap: em(0.45) }),
blank('blank', 34), blank('wide', 38), // mm: in a sentence; alone in a column
chip('dot', 'leaf', { paddingX: ROUND, fontSize: em(0.6) }),
chip('tick', 'paper', { borderWidth: OUTLINE, paddingX: ROUND, fontSize: em(1.1), gap: em(0.5) }),
];
行内标签是行内一个不可断开的框,它的内边距画在行外,不会让行变高。词库是嵌套的bank框中的一排行内标签。填空、待连线的点和待涂色的圆圈也都是行内标签,每个都写成:chip[ ]:只含一个空格的行内标签是一个空框,只有内边距和描边(行内标签)。宽度由paddingX设定:句中的空宽34 mm,单独占一栏时宽38 mm;圆圈两侧各留其1.25 em高度的一半。在检查清单中,圆圈占据项目符号的位置(bulletChar: '')。空高19.6 pt,比17 pt的行高,所以每个空都独占一个列表项,与下一项相距24 pt。
#3 · 用标题的版面设计给活动编号
const DISC_H2 = 8.6; // mm: the disc and the title are this tall, so the heading reserves this
const numberDisc = { kind: 'text', id: 'num', content: '{number}', // numberingTemplate '{2}'
fontFamily: DISPLAY, fontSize: pt(15), fontWeight: 800, color: col('paper'),
box: { backgroundColor: col('leaf'), borderRadius: mm(DISC_H2 / 2) }, // centred both ways
placement: { anchor: { to: 'container', edge: 'top-left' },
size: { width: mm(DISC_H2), height: mm(DISC_H2) } } };
const activityTitle = { kind: 'text', id: 'title', content: '{titleText}',
fontFamily: DISPLAY, fontSize: pt(17), fontWeight: 700, color: col('ink'), align: 'left',
overflow: 'wrap', placement: { anchor: { to: '#num', edge: 'right-of' },
offset: { x: mm(3) }, size: { width: mm(140), height: mm(DISC_H2) } } };
const activity = { enabled: true, slot: { elements: [numberDisc, activityTitle] } };
// ## How did I do? {style="review"} is not an activity: it has no number and a star for a disc.
const review = { id: 'review', numbered: false, advancedDesign: { ...activity, slot: {
elements: [{ kind: 'image', id: 'num', resourceId: 'star', placement: numberDisc.placement },
activityTitle] } } };
带advancedDesign的二级标题在栏中保留自己的位置,画出来的是版面设计而不是标题文字:一个圆盘装着{number}(印出该级的numberingTemplate),右边是{titleText}(通栏与高级版面设计)。圆盘和标题都是8.6 mm高,脱离网格后标题正好占这么多,所以版面设计不需要minHeight。编号来自二级标题的计数器,所以在Markdown中插入一个活动,后面的活动会自动重新编号。自我检查的标题使用numbered: false的样式,不参与计数,圆盘的位置放一颗SVG星星。
#4 · 把图片放进表格单元格
const tableStyles = [{ id: 'match', borderRadius: mm(3), // no header row, so no header fill
headerBackgroundEnabled: false, bodyBackgroundEnabled: true, bodyBackground: col('paper'),
bodyFontFamily: LABEL, bodyFontSize: pt(13), bodyColor: col('ink'), cellPadding: mm(1.4),
rules: 'none' }]; // the white panel is the body fill alone
const cell = (content, extra) => ({ content, verticalAlign: 'middle', ...extra });
const dotCell = () => cell(':chip[ ]{style="dot"}', { align: 'center' });
const matchRow = (icon, food, part) => [cell('', { image: { resourceId: icon, width: 0.76 },
align: 'center' }), cell(food), dotCell(), cell(''), dotCell(),
cell(`:chip[${part}]{style="word"}`)];
const sheetType = { id: 'sheet', name: 'Worksheet item', shortLabel: '', captionPrefix: '',
numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal' }; // no caption, no number
// Set 'here' in its card, built once FOODS exist. A box keeps a line of its text, 6 mm, over
// an embedded table; with that off (see layout), a :::space{lines=0.33} sets ASK instead.
const foods = () => ({ id: 'foods', typeId: 'sheet', kind: 'table', createdAt: 0, updatedAt: 0,
placement: { position: 'here' }, table: { styleId: 'match', model: {
columnWidths: [0.14, 0.25, 0.06, 0.31, 0.06, 0.18], // the widest gap: room to draw a line
rows: FOODS.map((name, r) => matchRow(ICONS[r], name, PARTS[r])) } } });
TableCell.image在单元格内画出一个资源,这里占单元格宽度的76 %,行高会随之增加。verticalAlign: 'middle'让食物名称、圆点和标签与图片垂直居中。最宽的一栏占表格的31 %,留空给学生画线。命名样式把表格的边框做成圆角,并把白色的表体填充裁到边框内。它不画任何线,面板就只是白色的填充(命名表格样式)。它的资源类型captionPrefix为空,表格也没有题注文字,所以下方不印题注。placement: { position: 'here' }把表格排在其::resource行所在的位置,也就是卡片里面。框会在这样的表格上方留出一行自己的正文,这里是6 mm;layout.inlineResourceGapInBoxes: false把它关掉,::resource行之前的:::space{lines=0.33}留出2 mm,与答题框和题目之间的距离相同。
完整食谱
// ═══ Postext Cookbook · Nº 022 · Worksheet with answer boxes and a word bank ══════
// https://postext.dev/en/cookbook/worksheet-answer-boxes
// Code: MIT · Text: original (CC BY 4.0) · Plant: diffusion models · Icons: drawn in code
// Fonts: Andika, Baloo 2, Fredoka (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 = 'worksheet-answer-boxes';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { ink: '#243040', muted: '#5d6975', paper: '#ffffff', // type; prompts; boxes
leaf: '#2f7d4a', sun: '#f4b43a', soil: '#8a5a36', // the accent; words to pick; drawings
tint: '#e5f1e7', rule: '#a9c9b1' }; // the activity cards; answer-box outlines
// Each colour names its palette entry and carries its hex.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the leaf, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.leaf })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, DISPLAY, LABEL] = ['Andika', 'Baloo 2', 'Fredoka']; // Andika: for early readers
const [TOP, SIDE] = [18, 16]; // mm; a sheet printed one-sided, so the margins do not mirror
// #region answer: cards that hold white answer boxes, stacked exactly, off the grid
// In the Markdown a box inside a box is a fence inside a fence (each ::: closes one):
// :::callout{type="card"}
// What is inside the pea pods?
// :::callout{type="answer"}
// Write here ← the prompt first: a :::space that opens a box is dropped
// :::space{lines=3} (gotcha: space-dropped-box-top)
// :::
// :::
const [ASK, AFTER, HEAD, NEXT] = [2, 5, 3, 9]; // mm: question→box, box→next, heading→card
const card = {
id: 'card', background: col('tint'), borderRadius: mm(4),
padding: { top: mm(4), right: mm(5), bottom: mm(5), left: mm(5) },
marginTop: mm(HEAD), marginBottom: mm(NEXT), // NEXT: under a card, to whatever follows
// Off the grid, or what follows a card drops to the next grid line. Only the outer card can
// set it: a nested box ignores snapToGrid, span and placement (gotcha: nested-callout-limits).
snapToGrid: false,
body: { paragraphSpacing: false }, // the boxes' margins do all the spacing
lists: { bulletChar: '', // no bullet: in the checklist a circle chip opens each item
gap: mm(2), itemSpacing: pt(7) }, columnGap: mm(6),
};
// A nested box's margins collapse with its neighbours', the larger winning (ASK, AFTER); the
// last box's marginBottom falls inside the card's padding instead of adding to it.
const nested = (id, look) => ({ id, background: col('paper'), borderRadius: mm(2.5),
marginTop: mm(ASK), marginBottom: mm(AFTER), ...look });
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
常见问题
易错点
:::space在框顶或栏顶会被丢掉
在栏顶、:::columns组顶部、还有其他内容的无标题框的顶部,以及被拆分的框的接续部分顶部,:::space会被丢掉。从postext 1.5起,只由:::space组成的框高度等于所要求的空白(答题框、填写用的空间),紧接在框标题下面的空白会让框内正文相应地往下开始。要让无标题框以空白开头,给它一个标题或一行首行,再在后面加空白。 显式竖向间距 →
易错点
嵌套框忽略span、placement和snapToGrid
嵌套在另一个标注框中的标注框会忽略自己的span、placement、snapToGrid和floatBarrier:它总是在父框内部排,宽度为父框的内宽。 嵌套框 →
- 框不能给自己画书写横线,因为标注框样式没有横线背景。答题框保持空白,高度由
:::space决定,短的填空线则用描边的行内标签。 - 带
numbered: false的标题样式仍会画出该级的版面设计,只是{number}为空。如果没有自己的版面设计,自我检查会以一个空的绿色圆盘开头,所以review复制了活动的版面设计,把星星放在原来圆盘的位置。
致谢
- 文本
- 原创文字, CC BY 4.0
- 图片
- The pea plant on the opener, a watercolour (top of the disc) · Generated With Diffusion Models · 原创
- The pea plant’s roots, the foot of the same watercolour below the band · Generated With Diffusion Models · 原创
- The plant's leader lines, the six food icons and the star, drawn in code in the page’s palette · Ignacio Ferro · MIT
- 字体
- Andika (SIL OFL 1.1) · Baloo 2 (SIL OFL 1.1) · Fredoka (SIL OFL 1.1)


