Skip to main content
Recipe number 22

Cookbook · Chapter 6 · Boxes & notes

Worksheet with answer boxes and a word bank

A four-page science worksheet: white answer boxes in pale green cards, 2 mm under each question and off the grid, with word banks and blanks made of chips.

  • Trim 200 × 260 mm
  • 1 column
  • Andika 12/17
  • Baloo 2
  • Fredoka
  • 4 pages
  • Level
  • Postext 1.57.1
  • Laid out in 57 ms
  • 185 lines of code

In short

A science worksheet for eight-year-olds, about the parts of a plant. Each question has a box underneath for the answer, and some sentences have gaps to fill from a list of words.

What you'll build

Worksheet 3A of Seedlings Science, an invented series for eight-year-olds, on four 200 × 260 mm pages printed on one side. A leaf-green band carries a yellow unit pill, the title and white fields for name and date; beside it, a watercolour pea plant in a white disc has its six parts numbered in yellow. The watercolour was generated with diffusion models and comes in already set in its disc, as two JPEGs that meet at the band's edge: the top one has the band's green in its corners, the foot has white; the numbers and the leader lines stay in code, over the picture. Every activity is a numbered heading over a pale green card. The cards hold white answer boxes with a small grey prompt and room for several lines, word banks of yellow chips, outlined blanks inside sentences, a table of foods to match with plant parts and a checklist of circles to colour. Each answer box sits 2 mm under its question and 5 mm above what follows, and every card ends 9 mm above whatever comes next on its page. The text is set in Andika, which SIL designed for children learning to read.

This recipe answers

  • How do I nest boxes, such as a worksheet card holding answer boxes, with exact spacing?
  • How do I build a worksheet page: fill-in lines, answer boxes, word banks, checklists?
  • How do I make inline chips: keyboard keys, tags, word banks for exercises?
  • How do I put pictures or icons inside table cells?

The short answer

script.js · lines 25–57in full code
// 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].

Cards that hold white answer boxes, stacked exactly, off the grid

Ingredients

Type
Andika, Baloo 2, Fredoka (SIL OFL 1.1)
Assets
  • plant-1092.jpg
  • plant-foot-1092.jpg
  • The pea plant on the opener, a watercolour (top of the disc) (Generated With Diffusion Models, original)
  • The pea plant’s roots, the foot of the same watercolour below the band (Generated With Diffusion Models, original)
  • The plant's leader lines, the six food icons and the star, drawn in code in the page’s palette (Ignacio Ferro, MIT)

Method

#1 · Take the headings off the grid as well

script.js · lines 162–170in full code
  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 },
    ] },

In the short answer, each box's margins collapse with its neighbours' and the larger one wins. A word bank's 5 mm beat the 2 mm above the list under it, and the last answer box's 5 mm fall inside the card's 5 mm bottom padding instead of adding to it. Left on the grid, a level-2 heading reserves between 10.8 and 14.1 mm instead of its disc's 8.6 mm, because what follows it snaps back to the next 17 pt line; headings.snapToGrid: false keeps it at 8.6 mm. Setting snapToGrid on the nested styles changes nothing, because only the outer card can leave the grid (nested boxes). Column balancing is off because it adds space above headings to fill out a page; on page 2 it would put 33 mm above activity 3 instead of 9.

#2 · Make every blank a chip

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

A chip is an unbreakable box in the line, and its padding is painted beyond the line without making the line taller. A word bank is a row of chips in a nested bank box. The blanks, the dots to join and the circles to colour are chips too, each written :chip[ ]: a chip of one space is an empty box of padding and outline (inline chips). paddingX sets the width: a blank is 34 mm wide in a sentence and 38 mm alone in a column, and a circle gets half its 1.25 em height on each side. In the checklist the circle stands where the bullet would be (bulletChar: ''). At 19.6 pt, a blank is taller than the 17 pt line, which is why each one has a list item to itself, 24 pt from the next.

#3 · Number the activities with a heading design

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

A level-2 heading with an advancedDesign keeps its place in the column, and the design is drawn instead of its text: a disc holding {number}, which prints the level's numberingTemplate, and {titleText} to its right (span and advanced design). The disc and the title are both 8.6 mm tall, and off the grid the heading reserves exactly that, so the design needs no minHeight. Because the numbers come from the level-2 counter, an activity inserted in the Markdown renumbers the ones after it. The self-check heading uses a style with numbered: false, which leaves it out of the count, and an SVG star sits where the disc would be.

#4 · Put the pictures in the table's cells

script.js · lines 131–147in full code
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 draws a resource inside its cell, here at 76 % of the cell's width, and the row grows to fit it. verticalAlign: 'middle' centres the food's name, the dots and the chip on the picture. The widest column, 31 % of the table, stays empty for the pupil's line. The named style rounds the table's frame and clips the white body fill to it. It draws no rules, so the panel is the white fill alone (named table styles). Its resource type has an empty captionPrefix and the table has no caption text, so no caption prints under it. placement: { position: 'here' } sets the table where its ::resource line stands, inside the card. A box keeps a line of its own text above such a table, 6 mm here; layout.inlineResourceGapInBoxes: false turns that off, and a :::space{lines=0.33} before the ::resource line sets the 2 mm the answer boxes keep under their questions.

The whole recipe

Sandbox
// ═══ 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 });

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

#Put the cards back on the grid

With the default snapToGrid: true, whatever follows a card drops to the next 17 pt grid line, and the 9 mm under each card become 14.5 mm under activity 2, 13.2 mm under activity 4 and 11.2 mm under activity 6.

-  snapToGrid: false,
+  snapToGrid: true,

#Give an answer more room

:::space counts in the answer box's own 17 pt lines, and each line added gives the pupil 6 mm more to write in.

 Write here
-:::space{lines=4}
+:::space{lines=6}

Pitfalls

Pitfall

:::space is dropped at the top of a box or column

:::space is dropped at the top of a column, of a :::columns group, of an untitled box that has other content and of the continuation of a split box. Since postext 1.5 a box made only of :::space is as tall as the space asks (an answer box, room to write), and space right under a box's title opens the body that much lower. To open an untitled box with space, give it a title or a first line and add the space after it. Explicit vertical space →

Pitfall

A nested box ignores span, placement and snapToGrid

A callout nested in another ignores its span, placement, snapToGrid and floatBarrier: it always flows inside its parent, at the parent's inner width. Nested boxes →

Pitfall

Chips taller than the line pitch touch the next line

A chip whose box is taller than the line pitch touches the chips on the next line (the chipOverlap warning). Reduce its vertical padding, border or size, or open the leading. Inline chips →

Sandbox check · chipOverlap

Chips touch the next line

Why. A chip style's box is taller than the line pitch, so chips on consecutive lines touch.

Fix. Reduce the chip's vertical padding, outline or size, or increase the leading. Docs →

  • A box cannot rule its own writing lines, because callout styles have no ruled background. The answer boxes stay blank and take their height from :::space, and the short fill-in lines are outlined chips.
  • A heading style with numbered: false still draws its level's design, with {number} left empty. Without a design of its own, the self-check would open with an empty green disc, so review copies the activity design and puts the star where the disc was.

Credits

Text
Original prose, CC BY 4.0
Images
  • The pea plant on the opener, a watercolour (top of the disc) · Generated With Diffusion Models · original
  • The pea plant’s roots, the foot of the same watercolour below the band · Generated With Diffusion Models · original
  • The plant's leader lines, the six food icons and the star, drawn in code in the page’s palette · Ignacio Ferro · MIT
Fonts
Andika (SIL OFL 1.1) · Baloo 2 (SIL OFL 1.1) · Fredoka (SIL OFL 1.1)
Sandbox