跳到主要内容
食谱编号159

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

活动框随文字变高的练习册

用页面模板排的科学练习册:堆叠组里的活动框随文字变高,答题横线画在空框上,放不下的项目带着“接下页”续到新增的一页。

本页内容
输出
Canvas · PDF
难度
高级
Postext
已用Postext 1.65.0测试
需要≥ 1.65.0 · postext-pdf ≥ 1.65.0
许可证
更新于2026年10月31日
代码MIT · 文本CC BY 4.0
  • 英文样例:尚无中文版本
  • 成品尺寸210 × 280 mm
  • 1栏
  • Andika 11.5/16
  • Baloo 2
  • Lexend
  • 5页
  • 难度
  • Postext 1.65.0
  • 排版用时84 ms
  • 220行代码

简单来说

一本关于水的儿童科学练习册,共五页。每道题放在一个彩色框里,框下有写答案的横线。题目长了,框就变高,把下面的内容往下推。一个长的手工项目接着排到下一页。

成品一览

《Water on the move》是一本虚构的四年级科学练习册的第 3 单元,这里排了其中五页,开本 210 × 280 mm。封面上用扩散模型生成的水循环绘画一直画到出血位,下沿融进纸面,上面叠着五个矢量箭头,下面是 54 pt 的标题和一条写名字的横线。每张练习页都出自同一个页面模板:单元名、蓝色标题带、配一幅小绘画的短文,以及三个带编号页签的浅蓝色活动框。第一个和第三个框下面是写答案的横线。每个框的高度跟着自己的文字走,所以法文版的练习页这里那里多出一行,横线就比英文版排得更低,而模板里没有任何位置改变。手工项目是用瓶子做一个雨量计,步骤多到一页放不下:步骤框在页脚处带着 Continued 截断,接到新增一页的顶部,后面再跟六条写观察记录的横线。所有文字,包括页签和观察记录的标题,都写在 Markdown 里。

这道食谱解答

  • 怎样让页面上的框随文字变高,并把下面的框往下推?
  • 活动内容放不下时,怎样带着“接下页”标记续排到下一页?
  • 怎样做练习页:填空横线、答题框、词库、清单?

简短回答

script.js · 第49–75行在完整代码中
// An empty frame is an answer space: the Markdown sends it nothing (optional: no slotEmpty).
// Its `rules` draw a line at the foot of every LINE mm of its height, so the lines are part
// of the frame and move with it when a box above grows. minHeight too: a box that runs out of
// room may not take the lines away from the child.
const answer = (name, lines) => ({ name, optional: true, width: mm(W), height: mm(LINE * lines),
  minHeight: mm(LINE * lines),
  rules: { every: mm(LINE), color: col('rule'), width: pt(0.75) } });
// A box is as tall as its text, never less than drawn: a longer text makes it taller and
// pushes everything after it in the group down the page.
const box = (name, style, minHeight) => ({ name, style, width: mm(W), minHeight: mm(minHeight) });
const sheet = {
  id: 'ficha',
  decoration: { elements: [kicker] },
  frames: [
    ...opening,
    // The group starts where its first box is placed: under the text, and runs down from there.
    { ...box('act-1', 'actividad', 22), placement: at('#texto', 'below', 0, 6) },
    answer('lineas-1', 3),
    box('act-2', 'actividad', 22),
    // The last box goes on overleaf, with the answer space after it, when its text outgrows
    // the page; the other two push it down instead.
    { ...box('act-3', 'actividad', 22), overflow: { continueIn: 'next-page' } },
    answer('lineas-3', 2),
  ],
  groups: [{ name: 'actividades', kind: 'stack', gap: mm(4.5),
    frames: ['act-1', 'lineas-1', 'act-2', 'act-3', 'lineas-3'] }],
};

用料

类型
Andika, Baloo 2, Lexend(SIL OFL 1.1)
素材
  • estados-960.jpg
  • planeta-960.jpg
  • pluviometro-960.jpg
  • portada-1440.jpg
  • The water cycle in a landscape: snowy mountains, a river, a coast and the sea, under sun and rain clouds. (Generated With Diffusion Models, 原创)
  • The Earth seen from space, with a drop of water. (Generated With Diffusion Models, 原创)
  • An ice cube melting, a glass of water and a steaming cup. (Generated With Diffusion Models, 原创)
  • A rain gauge made from a cut bottle, with a ruler and stones. (Generated With Diffusion Models, 原创)

做法

#1 · 按名称把每个活动倒进自己的框

模板就是上面的简短回答。在 Markdown 里,每张练习页是一个 ::::page 页面块:第一个标题进入带 takes: 'h1' 的框,段落进入流文框,每个 :::frame{slot="…"} 进入同名的框,页签上的文字写在 label 里。答题框什么也不接收,所以标了 optional: true,否则空框会触发 slotEmpty 警告。画画的活动用 :::space{lines=3} 在自己的框里留出空白,因为画画要多大地方,本身就是题目的一部分。

:::frame{slot="act-1" label="1 · Read and answer"}
Tom lives in Leeds. The water from his tap comes from a reservoir in the hills…
:::

#2 · 让堆叠组带着横线和框一起移动

每个框的 minHeight 是 22 mm,也就是它画出来的高度,超过这个高度就是 auto。组从 act-1 被放在正文下方的位置开始,把五个框一个接一个往下排,间隔 4.5 mm,所以一个框变高,后面的一切都会往下移。横线属于空框本身:它的 rules 在框高度每隔 8 mm 的位置、文字下方画一条线,所以横线随框移动,答题空间写长了,框就变高。给答题框设一个等于其高度的 minHeight:组里地方不够时,重排阶梯会从组内各框收回空间,直到它们的最小值,而空框的最小值是零,横线就会压到下一个框上。每张练习页的最后一个框 act-3,文字超出页面时会续到下一页(overflow.continueIn: 'next-page'),它下面的答题横线一起过去。

#3 · 把项目接到下一页

script.js · 第79–106行在完整代码中
const project = {
  id: 'proyecto',
  overflowTemplate: 'proyecto-sigue',
  decoration: { elements: [kicker] },
  frames: [
    ...opening,
    { ...box('material', 'material', 20), placement: at('#texto', 'below', 0, 9) },
    // As tall as its text, never past the foot of the page, at full size: the rest ends
    // with the language's mark (Continúa, Continued, À suivre) and goes on on an added page.
    { name: 'pasos', style: 'proyecto', width: mm(W), overflow: { continueIn: 'next-page' } },
  ],
  groups: [{ name: 'trabajo', kind: 'stack', gap: mm(9), frames: ['material', 'pasos'] }],
};
// The added page: the rest of the steps at the head, then lines for the child's notes,
// under a heading the page fence names (notas="…").
const projectGoesOn = {
  id: 'proyecto-sigue',
  decoration: { elements: [kicker,
    { kind: 'text', id: 'notas-titulo', content: '{attr.notas}', fontFamily: DISPLAY,
      fontSize: pt(15), fontWeight: 700, color: col('sea'),
      placement: { ...at('#notas', 'top-left', 0, -3), size: { width: mm(W) } } }] },
  frames: [
    { name: 'pasos', style: 'proyecto', width: mm(W),
      placement: at('container', 'top-left', 0, 9) },
    answer('notas', 6),
  ],
  groups: [{ name: 'trabajo', kind: 'stack', gap: mm(16), frames: ['pasos', 'notas'] }],
};

步骤框没有固定高度:它随文字变高,最多到版心底部为止。能接续到别页的框会先接续,再考虑把文字缩小,所以框以原字号在两行之间截断,末尾加上该语言的接续标记(Continúa、Continued、À suivre,由 locale 决定),再接到 proyecto-sigue 模板一页里的同名框(文字接续时)。新增的一页在框的剩余部分下面画出观察记录的横线,标题来自页面块里的 notas="What I noticed",这样文字仍在 Markdown 里,随每个版本变化。

#4 · 封面画到出血位

script.js · 第110–131行在完整代码中
// and a line for the child's name
const cover = {
  id: 'portada', header: false, footer: false,
  decoration: { elements: [
    { kind: 'image', id: 'cielo', resourceId: 'portada', decorative: true,
      placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: mm(178) } } },
    // The arrows are vector, over the painting: the same frame, so they stay on its features.
    { kind: 'image', id: 'ciclo', resourceId: 'ciclo', decorative: true,
      placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: mm(178) } } },
    { kind: 'rule', id: 'nombre-linea', direction: 'horizontal', thickness: pt(1),
      color: col('sea'), placement: { ...at('#nombre', 'bottom-left', 0, 1),
        size: { width: mm(W) } } },
  ] },
  frames: [
    { name: 'titulo', takes: 'h1', width: mm(W), placement: at('container', 'top-left', 0, 162) },
    { name: 'subtitulo', flow: true, width: mm(W) },
    { name: 'nombre', style: 'campo', width: mm(W), minHeight: mm(12) },
    { name: 'colofon', style: 'colofon', width: mm(W), placement: at('container', 'bottom-left') },
  ],
  groups: [{ name: 'portada', kind: 'stack', gap: mm(4),
    frames: ['titulo', 'subtitulo', 'nombre'] }],
};

封面也是模板页:插图是锚定在出血位上的装饰,标题、丛书名和名字栏组成一个堆叠组,从版心顶部往下 162 mm 处开始。名字栏的线锚定在框底,所以换一种语言后页签占两行,线仍然在文字下面。header: false 和 footer: false 让封面不印页码。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 159 · Activity book with boxes that grow ══════════════
// https://postext.dev/en/cookbook/activity-book-growing-boxes
// Code: MIT · Text: original (CC BY 4.0) · Pictures: generated with diffusion models
// Fonts: Andika, Baloo 2, Lexend (SIL OFL 1.1) · Needs postext ≥ 1.65.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es' | 'fr')
const RECIPE = 'activity-book-growing-boxes';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: one ink, the water blue, the sun, and the pale fields of the boxes
const palette = {
  ink: '#1f2d38', // text
  sea: '#1d659c', // titles, tabs, list numbers
  sky: '#e2f0f9', // activity boxes
  sun: '#f0a531', // the project's box
  sand: '#fcf0d8', // the materials box
  rule: '#93bcd8', // answer lines, blanks
  paper: '#ffffff',
};
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' } })),
  { id: 'main-color', name: 'sea (defaults)', value: { hex: palette.sea, model: 'hex' } },
];
// #endregion

const [TEXT, DISPLAY, LABEL] = ['Andika', 'Baloo 2', 'Lexend']; // Andika: for early readers
const W = 174; // mm: the width of the type area, and of every box
const LINE = 8; // mm between two answer lines: room for a child's handwriting
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });

// The head every sheet shares: the unit over the title band, the text with its drawing.
const kicker = { kind: 'text', id: 'tema', content: '{attr.tema}', fontFamily: LABEL,
  fontSize: pt(8.5), fontWeight: 600, color: col('sea'), textTransform: 'uppercase',
  letterSpacing: pt(1.4), placement: at('container', 'top-left') };
const opening = [
  { name: 'titulo', takes: 'h1', style: 'banda', width: mm(W),
    placement: at('container', 'top-left', 0, 6) },
  { name: 'texto', flow: true, width: mm(W - 52), minHeight: mm(36),
    placement: at('#titulo', 'below', 0, 4) },
  { name: 'dibujo', width: mm(44), placement: at('#titulo', 'below', W - 44, 4) },
];

// #region answer: boxes in a stack grow with their text; empty ruled frames hold the answers

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

变化

#给一个活动多几条横线

横线的条数就是答题框的高度:它的 rules 画出放得下的所有横线。在模板里改这一个值,所有用这个模板的练习页都会有这些横线。

-    answer('lineas-1', 3),
+    answer('lineas-1', 5),

#接续之前先让步骤缩小

能接续到别页的框会先接续,再考虑把文字缩小。写上 fit.preferContinue: false 后,阶梯会先把步骤缩小,最多到 fit.minScale(90 %),仍不够时才移到另一页。给孩子看的书,字号比省一页更要紧,所以这份示例保留默认值。

-      overflow: { continueIn: 'next-page' } },
+      fit: { preferContinue: false }, overflow: { continueIn: 'next-page' } },

常见问题

  • 什么都不接收的框,没有 optional: true 就会触发 slotEmpty。这里每个答题框都标了。
  • 在堆叠组里,框由组来摆放,不看它自己的 placement,第一个框除外:要给组里的第一个框定位,否则整组会从版心顶部开始,压在标题上。

致谢

文本
原创文字, CC BY 4.0
图片
  • The water cycle in a landscape: snowy mountains, a river, a coast and the sea, under sun and rain clouds. · Generated With Diffusion Models · 原创
  • The Earth seen from space, with a drop of water. · Generated With Diffusion Models · 原创
  • An ice cube melting, a glass of water and a steaming cup. · Generated With Diffusion Models · 原创
  • A rain gauge made from a cut bottle, with a ruler and stones. · Generated With Diffusion Models · 原创
字体
Andika (SIL OFL 1.1) · Baloo 2 (SIL OFL 1.1) · Lexend (SIL OFL 1.1)
沙盒PDF