Skip to main content
Recipe number 159

Cookbook · Chapter 12 · Textbooks

Activity book with boxes that grow

A science activity book on page templates: boxes in a stack grow with their text, answer lines sit on empty frames, a project goes on overleaf.

  • Trim 210 × 280 mm
  • 1 column
  • Andika 11.5/16
  • Baloo 2
  • Lexend
  • 5 pages
  • Level
  • Postext 1.65.0
  • Laid out in 84 ms
  • 220 lines of code

In short

Five pages of a children's science workbook about water. Each question has a coloured box with lines below it. A longer question makes its box taller and moves the rest down. A long project goes on overleaf.

What you'll build

Five pages of Water on the move, unit 3 of an invented Year 4 science activity book, on a 210 × 280 mm page. A painting of the water cycle, generated with a diffusion model, fills the cover to the bleed and fades into the paper at its foot; five vector arrows lie over its features, over a 54 pt title and a line for the child's name. Each sheet is the same page template: a unit line, a blue title band, a short text beside a painted vignette, then three activity boxes in pale blue with a numbered tab. Ruled lines for the answers sit under the first and the third box. The boxes are as tall as their text, so the French sheet, a line longer here and there, pushes its lines further down the page than the English one, and no position in the template changes. The project, a rain gauge made from a bottle, has more steps than one page holds: its box stops at the foot with Continued and goes on at the head of an added page, followed by six lines for the child's notes. Every word, the tabs and the notes heading included, is in the Markdown.

This recipe answers

  • How do I make the boxes of a page grow with their text and push the boxes below them down?
  • How do I let an activity that does not fit go on on the next page, with a “Continued” mark?
  • How do I build a worksheet page: fill-in lines, answer boxes, word banks, checklists?

The short answer

script.js · lines 49–75in full code
// 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'] }],
};

Boxes in a stack grow with their text; empty ruled frames hold the answers

Ingredients

Type
Andika, Baloo 2, Lexend (SIL OFL 1.1)
Assets
  • 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, original)
  • The Earth seen from space, with a drop of water. (Generated With Diffusion Models, original)
  • An ice cube melting, a glass of water and a steaming cup. (Generated With Diffusion Models, original)
  • A rain gauge made from a cut bottle, with a ruler and stones. (Generated With Diffusion Models, original)

Method

#1 · Pour each activity into its box by name

The template is the short answer above. In the Markdown, a sheet is one ::::page fence: the first heading goes to the frame that takes: 'h1', the paragraph to the flow frame, and each :::frame{slot="…"} to the frame of that name, with its tab words in label. The answer frames get nothing, which is why they are optional: true: an empty frame would otherwise raise slotEmpty. A drawing activity keeps its own blank room inside its box with :::space{lines=3}, since how much room a drawing needs is part of the question.

:::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 · Let the stack move the lines with the boxes

Each box has a minHeight of 22 mm, the height it is drawn at, and an auto height above that. The group lays its five frames one under another with 4.5 mm between them, from where act-1 is placed under the text, so a taller box moves everything after it. The ruled lines belong to the empty frame: its rules draw one line at the foot of every 8 mm of its height, under its text, so the lines move with the frame and a longer answer space is a taller frame. Give the answer frames a minHeight equal to their height: when a group runs short of room, the reflow ladder takes space back from the frames of the group down to their minimum, and an empty frame's minimum is otherwise nothing, which would leave the lines lying on the next box. The last box of a sheet, act-3, goes on overleaf (overflow.continueIn: 'next-page') when its text outgrows the page, and the answer lines under it go with it.

#3 · Carry the project over the page

script.js · lines 79–106in full code
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'] }],
};

The steps box has no fixed height: it is as tall as its text and stops at the foot of the area. A frame that can go on to another page goes on before its text is set smaller, so the box is cut between two lines at full size, ends with the language's mark (Continúa, Continued, À suivre, from locale) and goes on in the frame of the same name on a page of proyecto-sigue (when the text continues). The added page draws the notes lines under the rest of the box, with the heading that the fence names in notas="What I noticed", so the words stay in the Markdown and change with each edition.

#4 · Draw the cover to the bleed

script.js · lines 110–131in full code
// 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'] }],
};

The cover is a composed page too: its picture is decoration anchored to the bleed, and its title, series line and name field are a stack that starts 162 mm down the type area. The name field's rule is anchored to the bottom of its frame, so a two-line name label in another language would keep the line under the words. header: false and footer: false keep the folio off the cover.

The whole recipe

Sandbox
// ═══ 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

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

#Give an activity more lines

The number of lines is the answer frame's height: its rules draw as many lines as fit. Change it in the template, and every sheet that uses it gets the lines.

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

#Let the steps shrink before they continue

A frame that can go on to another page goes on before its text is set smaller. With fit.preferContinue: false the ladder sets the steps smaller first, down to fit.minScale (90 %), and continues only if that is not enough. For a child's book the type size matters more than one page saved, so the recipe keeps the default.

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

Pitfalls

  • A frame that receives nothing raises slotEmpty unless it is optional: true. Every answer frame here is one.
  • A box in a stack is placed by the group, not by its own placement, except the first one: place the first box of the group, or the stack starts at the top of the area, over the title.

Credits

Text
Original prose, CC BY 4.0
Images
  • The water cycle in a landscape: snowy mountains, a river, a coast and the sea, under sun and rain clouds. · Generated With Diffusion Models · original
  • The Earth seen from space, with a drop of water. · Generated With Diffusion Models · original
  • An ice cube melting, a glass of water and a steaming cup. · Generated With Diffusion Models · original
  • A rain gauge made from a cut bottle, with a ruler and stones. · Generated With Diffusion Models · original
Fonts
Andika (SIL OFL 1.1) · Baloo 2 (SIL OFL 1.1) · Lexend (SIL OFL 1.1)
SandboxPDF