Skip to main content
Recipe number 49

Cookbook · Chapter 6 · Boxes & notes

Exam paper with an answer sheet

A four-page history exam on US Letter: bubbles and marks as chips, marks on a tab stop, questions numbered 1, a), i) and answer lines ruled by a table.

  • Trim 215.9 × 279.4 mm
  • 1 column
  • PT Serif 11/15
  • Inter Tight
  • 4 pages
  • Level
  • Postext 1.57.1
  • Laid out in 63 ms
  • 180 lines of code

In short

A four-page history exam with a cover, answer bubbles and lined pages for writing. It shows how to number questions and their parts and show the marks for each one.

What you'll build

Paper 2 of a mock history examination on the American Civil War, set as a four-page booklet on US Letter. The cover is a crimson band with the year 1863 drawn in outline, over the candidate's boxes, the instructions in two columns and a grid of lettered bubbles for Section A. Page 2 holds ten multiple-choice questions, each followed by its four options, and every option opens with a lettered bubble like the grid's. Page 3 sets the Gettysburg Address as Source A and numbers the questions on it 11, a), i), with the marks for each part in a chip at the right margin. Page 4 is ruled for the answers, one rule every two lines of text, and each group of rules opens with the number of its part. Every page after the cover carries a folio and a ruled margin for the examiner, and the one right-hand page among them says Turn over.

This recipe answers

  • How do I build a worksheet page: fill-in lines, answer boxes, word banks, checklists?
  • How do I customise lists: bullets per level, (a)/(i) numbering, task checkboxes, spacing that stays on the grid?
  • How do I make inline chips: keyboard keys, tags, word banks for exercises?
  • How do I style several tables differently (fills, zebra cells, rounded frames) in one document?
  • How do I hide running heads on openers and blank pages, or paint a blank verso in the part colour?

The short answer

script.js · lines 35–47in full code
// A table row is one line (the table's size × the body's leading ratio) plus cellPadding above
// and below: at the body size, (ROW − LEAD) / 2 of padding makes every row ROW deep, so the
// rules keep to the text's 15 pt grid, 10.6 mm apart: room for handwriting.
const ROW = 2 * LEAD; // pt
const lines = { id: 'lines', rules: 'horizontal', // a rule on the top and foot of every row
  borderColor: col('rule'), borderWidth: pt(0.5), bodyFontFamily: LABEL, bodyFontSize: pt(BODY),
  bodyColor: col('crimson'), cellPadding: pt((ROW - LEAD) / 2) };
// Every table in this paper is set 'here': where its ::resource directive stands in the text.
const table = (id, styleId, model) => ({ id, typeId: 'form', kind: 'table', createdAt: 0,
  updatedAt: 0, placement: { position: 'here' }, table: { styleId, model } });
// One table per answer, the part's number in its first margin cell.
const answerLines = (id, part, rows) => table(id, 'lines', { columnWidths: [1, 5], rows: Array
  .from({ length: rows }, (_, i) => [{ content: i ? '' : `**${part}**` }, { content: '' }]) });

Ruled answer lines: a table whose rows are two body lines deep

Ingredients

Type
PT Serif, Inter Tight (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · Number the questions and hang their options

script.js · lines 51–63in full code
const [NUMBER, GAP] = [12.5, 6]; // pt: the question numbers' size; number to text
const orderedLists = { fontFamily: LABEL, color: col('ink'), // bold, by default
  separatorColor: col('crimson'), gap: pt(GAP), marginTop: pt(LEAD / 2), marginBottom: pt(0),
  itemSpacing: pt(5), numberWidth: 'level', // 5 pt to the options or next part; text after '10.'
  levels: [{ level: 1, fontSize: pt(NUMBER) }, // 'arabic' and '.', the defaults
    { level: 2, numberFormat: 'lower-alpha', separator: ')' },
    { level: 3, numberFormat: 'lower-roman', separator: ')' }] };
// Options: a bulletless item at the questions' depth, '10.' plus GAP in (boxes set their own).
const width = (s) => { const ctx = new OffscreenCanvas(1, 1).getContext('2d');
  ctx.font = `700 ${NUMBER}pt "${LABEL}"`; return ctx.measureText(s).width * 0.75; }; // pt
const unorderedLists = () => ({ gap: pt(0), marginTop: pt(0), marginBottom: pt(0),
  itemSpacing: pt(LEAD), // after the options: a line before the next question
  indent: pt(width('10') + width('.') + GAP), levels: [{ level: 1, bulletChar: '' }] });

Each level of orderedLists picks its own numberFormat and separator, and separatorColor paints the full stop and the brackets in the accent while the number stays in ink (ordered lists). With numberWidth: 'level' the numbers are right-aligned on the widest one at their depth, so the text of every question starts 23 pt in: the 17 pt of '10.' plus GAP. The four options are one bullet item with an empty bulletChar, at the questions' own depth rather than nested under them. Its text starts at the list's indent, which is '10.' measured in the number face plus GAP, so the options line up under the question's first letter in any font; the instructions box sets indent: pt(0), because the lists in a box take the document's indent. Between two items of one depth the space is the first one's itemSpacing: 5 pt after a numbered item, 15 pt after a line of options. Nested under its question, the line of options would take the question list's 5 pt on both sides.

#2 · Make the bubbles and the marks from chips

script.js · lines 67–76in full code
// A chip is 0.8 em above the baseline and 0.25 em below, plus paddingY and the border on each
// side: 1.29 em and two borders. One capital is 0.63–0.72 em wide, so paddingX 0.3 em (and the
// same two borders) squares the box; a radius over half its smaller side is clamped to that half.
const chip = (id, look) => ({ id, fontFamily: LABEL, fontSize: em(0.8), bold: true,
  paddingY: em(0.12), ...look });
const bubble = (id, fill, ink, edge = 'crimson') => chip(id, { color: col(ink), paddingX: em(0.3),
  background: col(fill), borderColor: col(edge), borderWidth: pt(0.7), borderRadius: em(1) });
const chipStyles = [bubble('bubble', 'paper', 'crimson'), // first: a bare :chip[A] takes it
  bubble('filled', 'ink', 'paper', 'ink'), chip('marks', { color: col('crimson'),
    background: col('blush'), borderWidth: pt(0), paddingX: em(0.45), gap: em(0.5) })];

A chip is a box inside the line, and a line break never falls within it (inline chips). With one capital inside, paddingX: em(0.3) makes the box about as wide as it is tall: 4.5 to 4.8 mm wide in the grid, for a height of 4.7 mm. borderRadius: em(1) is more than half the box, and a chip's radius is clamped to half its smaller side, so the ends are fully round. A chip with no style takes the first entry of chipStyles, so the eighty bubbles, forty in the grid and forty among the options, are bare chips such as :chip[A]; only the filled example in the instructions and the marks name a style. The marks stand flush right: each part ends with :tab before its chip, and the body's one tab stop, at the end of the measure with align: 'end', ends the chip there (tabs and tab stops). The list items of the questions take the body's stops, so the chips of a) and of i) end at the same place. A part whose last line has no room left for its chip takes its last word down to the next line with it.

#3 · Give each form its own table style

script.js · lines 80–101in full code
// No table has a header row; the flag keeps the default grey fill off one added later.
const form = { bodyFontFamily: LABEL, borderRadius: mm(2), headerBackgroundEnabled: false };
const tableStyles = [lines,
  { ...form, id: 'candidate', rules: 'grid', borderColor: col('rule'), borderWidth: pt(0.75),
    bodyFontSize: pt(7.5), bodyColor: col('crimson'), cellPadding: mm(1.8) },
  // No rules: the tint of each cell meets its neighbours' and the grid reads as one panel.
  { ...form, id: 'grid', rules: 'none', bodyBackgroundEnabled: true,
    bodyBackground: col('tint'), bodyFontSize: pt(11.5), cellPadding: mm(1.4) }];
const cell = (content, extra) => ({ content, align: 'center', verticalAlign: 'middle', ...extra });
// The MARK cell sets the row's depth: three lines, the blank one a no-break space.
const candidate = table('candidate', 'candidate', { columnWidths: [4.2, 1.2, 2, 1.3], rows: [[
  ...t({ en: ['NAME', 'CLASS', 'CANDIDATE NUMBER'], es: ['NOMBRE Y APELLIDOS', 'GRUPO',
    'N.º DE EXAMEN'] }).map((l) => cell(`**${l}**`, { align: 'left', verticalAlign: 'top' })),
  cell(`**${t({ en: 'MARK', es: 'NOTA' })}**\n\u00A0\n**/ ${t({ en: '25', es: '10' })}**`,
    { align: 'right', background: col('tint') })]] });
const GROUP = ['A', 'B', 'C', 'D'].map((l) => cell(`:chip[${l}]`)), num = (n) => cell(`**${n}**`);
const grid = { ...table('grid', 'grid', { columnWidths: [0.7, 1, 1, 1, 1, 1.4, 0.7, 1, 1, 1, 1],
  rows: [1, 2, 3, 4, 5].map((n) => [num(n), ...GROUP, cell(''), num(n + 5), ...GROUP]) }),
  caption: t({ en: '**Section A answer grid**', es: '**Parte A: plantilla de respuestas**' }) };
// The grid's title is its caption: a heading would stand a body line off any 'here' table.
const captionStyle = { fontFamily: LABEL, fontSize: pt(9.4), color: col('crimson'),
  position: 'above', gap: mm(1.6) }; // the rubric's size

The candidate boxes, the bubble grid and the answer lines are three tableStyles, picked per table with styleId (named table styles). The answer lines in the short answer use rules: 'horizontal' and a padding that makes each row two body lines deep, so every rule lands 30 pt (10.6 mm) below the one before, on every second line of the text's 15 pt grid. The blank middle line of the MARK cell holds a no-break space, because a cell line that is empty or holds only ordinary spaces adds nothing: that cell is three lines deep and sets the whole row at 14.4 mm, against 10.8 mm with two lines. The grid draws no rules, so each cell's tint meets its neighbour's and the grid reads as one panel. Its title is a caption set 1.6 mm above it (caption style). A heading would stand at least one body line (5.3 mm) higher, because every table placed 'here' keeps that much space above it. The form type has an empty captionPrefix, so no 'Form 1.' label comes before the title.

#4 · Draw the cover from the heading's attributes

script.js · lines 105–120in full code
const BAND = 116; // mm: the crimson band, bled off the top and both sides
const YEAR = { cap: 62, cut: 12, pad: 2 }; // mm: the digits' cap height; the band cuts 12 off
// span 'page' makes the cover an opener page, which the body pages' furniture skips.
const cover = { id: 'cover', span: 'page', margins: { right: mm(LEFT) }, // forms full width
  advancedDesign: { enabled: true, slot: { elements: [ // the date line sets the height
    { kind: 'box', id: 'band', style: { backgroundColor: col('crimson') },
      placement: at(0, 0, { width: mm(PAGE.w), height: mm(BAND) }) },
    // Design text has no outline, so the year is an image, cut off by the band's foot.
    { kind: 'image', id: 'year', resourceId: 'year', placement: at(0, BAND - YEAR.cap
      + YEAR.cut - YEAR.pad, { width: mm(PAGE.w), height: mm(YEAR.cap - YEAR.cut + YEAR.pad) }) },
    text('session', '{attr.session}', LABEL, 8.5, 600, 'blush', at(LEFT, 14), tag),
    // 0.9 mm to the left: the H's side bearing at 64 pt (84 of 2048 units), so its stem aligns.
    text('title', '{titleText}', LABEL, 64, 800, 'paper', at(LEFT - 0.9, 19), { lineHeight: 1 }),
    text('paper', '{attr.paper}', LABEL, 15, 700, 'paper', at(LEFT, 44)),
    text('topic', '{attr.topic}', TEXT, 15, 400, 'blush', at(LEFT, 52), { italic: true }),
    text('date', '{attr.date}', LABEL, 9.5, 700, 'ink', at(LEFT, BAND + 6))] } } };

The cover is one heading, # History {style="cover" …}, and its style draws a band, the year and four lines of text filled in from the heading's attributes. With span: 'page' the page counts as an opener, so the margin rule, the folio and 'Turn over' of the body pages stay off it and the notice prints at its foot. Without it the cover is a body page like the rest. The style's margins widen the cover's column to the full 168 mm for the forms, and the next heading, an unstyled level 1, returns the body pages to the 134 mm measure.

#5 · Keep folios and Turn over off the cover

script.js · lines 124–139in full code
const [MARGIN, FOOT] = [LEFT + MEASURE + 7, -13]; // mm: the margin rule, 7 off the text; the foot
const body = { pages: 'body', ...tag }; // body pages only: never the cover
const NOTE = t({ en: 'Do not write in this margin', es: 'No escribas en este margen' });
const header = { elements: [
  text('running', '{title} · {chapterTitle}', LABEL, 7.5, 600, 'muted', at(LEFT, 12), body),
  { kind: 'rule', id: 'margin', direction: 'vertical', pages: 'body', color: col('rule'),
    thickness: pt(0.75), placement: at(MARGIN, TOP, { height: mm(PAGE.h - TOP - BOTTOM) }) },
  text('note', NOTE, LABEL, 7.5, 600, 'muted', at(MARGIN + 3, TOP, { width: mm(28) }),
    { ...body, lineHeight: 1.3 })] };
const footer = { elements: [
  text('notice', '{attr.notice}', LABEL, 8.5, 700, 'crimson', at(0, FOOT, { width: mm(PAGE.w) },
    'bottom-left'), { pages: 'opener', align: 'center', ...tag }), // from the cover's H1
  text('folio', '{pageNumber}', LABEL, 9, 700, 'ink', at(LEFT, FOOT, { width: mm(MEASURE) },
    'bottom-left'), { ...body, align: 'center' }), // centred under the text
  text('turn', t({ en: 'Turn over ›', es: 'Pasa la página ›' }), LABEL, 9, 700, 'ink', // rectos
    at(-RIGHT, FOOT, null, 'bottom-right'), { ...body, parity: 'odd', align: 'right' })] };

Every running element of the body pages carries pages: 'body', so the cover, an opener, shows only its own notice (pages: 'opener'). The sections therefore start with :::pagebreak and a heading without breakBefore. A heading that breaks the page makes its page an opener, and pages 2 and 3 would lose their folio, running head and margin. parity: 'odd' keeps Turn over on the recto, since a verso already faces the page that follows it.

The whole recipe

Sandbox
// ═══ Postext Cookbook · Nº 049 · Exam paper with an answer sheet ═════════════════
// https://postext.dev/en/cookbook/exam-paper
// Code: MIT · Text: Lincoln (PD); questions, Spanish translation (CC BY 4.0) · Art: in code
// Fonts: PT Serif, Inter Tight (SIL OFL 1.1) · Needs postext ≥ 1.34.0
import {
  buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage, inlineSvgFonts,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'exam-paper';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { ink: '#1b1a1f', paper: '#ffffff', muted: '#6b6466', // heads, credits: 5.8:1
  crimson: '#9b1c31', // the one accent: cover, numbering, bubbles, marks (8.1:1 on white)
  blush: '#f3cdd4', tint: '#fbeff1', // type on the band, marks chips; the grid, the rubric box
  rule: '#b9aeb0' }; // answer lines and hairlines
// col(id): a colour linked to its palette entry, with the entry's hex beside the id.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults are linked to 'main-color': point it at the accent.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.crimson })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, LABEL, PAGE] = ['PT Serif', 'Inter Tight', { w: 215.9, h: 279.4 }]; // US Letter
const [TOP, BOTTOM, LEFT, MEASURE] = [22, 24, 24, 134]; // mm; 134 mm: about 75 characters
const RIGHT = PAGE.w - LEFT - MEASURE; // 57.9 mm: the examiner's margin, never mirrored
const [BODY, LEAD] = [11, 15]; // pt: text size and leading, the grid every line keeps to
const at = (x, y, size, edge = 'top-left') => ({ anchor: { to: 'page', edge },
  offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const text = (id, content, family, size, weight, color, placement, extra) => ({ kind: 'text',
  id, content, fontFamily: family, fontSize: pt(size), fontWeight: weight, color: col(color),
  align: 'left', overflow: 'wrap', placement, ...extra });
const tag = { textTransform: 'uppercase', letterSpacing: pt(1.4) }; // 0.16–0.19 em
const small = { fontFamily: LABEL, fontSize: pt(7.5), lineHeight: pt(10), color: col('muted') };

// #region answer: ruled answer lines: a table whose rows are two body lines deep
// A table row is one line (the table's size × the body's leading ratio) plus cellPadding above
// and below: at the body size, (ROW − LEAD) / 2 of padding makes every row ROW deep, so the
// rules keep to the text's 15 pt grid, 10.6 mm apart: room for handwriting.
const ROW = 2 * LEAD; // pt
const lines = { id: 'lines', rules: 'horizontal', // a rule on the top and foot of every row
  borderColor: col('rule'), borderWidth: pt(0.5), bodyFontFamily: LABEL, bodyFontSize: pt(BODY),
  bodyColor: col('crimson'), cellPadding: pt((ROW - LEAD) / 2) };
// Every table in this paper is set 'here': where its ::resource directive stands in the text.
const table = (id, styleId, model) => ({ id, typeId: 'form', kind: 'table', createdAt: 0,
  updatedAt: 0, placement: { position: 'here' }, table: { styleId, model } });
// One table per answer, the part's number in its first margin cell.
const answerLines = (id, part, rows) => table(id, 'lines', { columnWidths: [1, 5], rows: Array
  .from({ length: rows }, (_, i) => [{ content: i ? '' : `**${part}**` }, { content: '' }]) });
// #endregion

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

The band, the year, the margin rule and the notice take their sizes from PAGE, so A4 is a one-line change. The blocks of the cover keep their places, and the white under the grid grows by 17.6 mm in both editions.

-const [TEXT, LABEL, PAGE] = ['PT Serif', 'Inter Tight', { w: 215.9, h: 279.4 }]; // US Letter
+const [TEXT, LABEL, PAGE] = ['PT Serif', 'Inter Tight', { w: 210, h: 297 }]; // A4

#Add a word bank or a checklist

Word banks, blanks and circles to colour are chips too: the worksheet with answer boxes and a word bank sets a bank of yellow chips in a nested box and a checklist that opens each line with an empty circle.

Pitfalls

Pitfall

A bare $ opens maths: write \$

A dollar sign opens inline maths, so a price such as $40 starts a formula. Write \$40. Escapes and literal characters →

  • Answer lines between the parts of a question split its list: the list after the table starts a new run, and a run right-aligns its numbers on its own widest one, so the text of ii) would start 1 mm to the right of the text of i). Here the answers sit on their own page, and every part stays in one list.

Credits

Text
  • The Gettysburg Address (19 November 1863), text of the Bliss copy · Abraham Lincoln · public domain
  • The questions, the instructions and the Spanish translation of Source A · Ignacio Ferro · CC BY 4.0
Fonts
PT Serif (SIL OFL 1.1) · Inter Tight (SIL OFL 1.1)
Sandbox