跳到主要内容
食谱编号49

排版食谱 · 第6章 · 框与注释

带答题卡的试卷

一份四页US Letter历史试卷:涂卡圆圈和分值用行内标签画出,分值排在制表位上,题号按1、a)、i)分级,答题线由表格画出。

  • 英文样例:尚无中文版本
  • 成品尺寸215.9 × 279.4 mm
  • 1栏
  • PT Serif 11/15
  • Inter Tight
  • 4页
  • 难度
  • Postext 1.57.1
  • 排版用时63 ms
  • 180行代码

简单来说

一份四页的历史试卷,有封面、涂卡圆圈和书写横线。示例演示怎样给大题和小题编号,并标出每题的分值。

成品一览

这是一场美国内战历史模拟考试的第二份试卷,排成一本四页的US Letter小册子。封面是一条深红色色带,上面用轮廓线画出年份1863,下方是考生信息框、分两栏排的须知,以及Section A的带字母涂卡网格。第2页是十道选择题,每题后面跟着四个选项,每个选项都以一个与网格中相同的带字母圆圈开头。第3页把Gettysburg Address作为Source A排出,关于它的题目按11、a)、i)编号,每个小题的分值放在右页边距处的一个标签里。第4页画好了答题线,每两行文字一条横线,每组横线以对应小题的题号开头。封面之后的每一页都有页码和一条供阅卷人使用的页边线,其中唯一的右页印着Turn over。

这道食谱解答

  • 怎样做练习页:填空横线、答题框、词库、清单?
  • 怎样定制列表:每级的项目符号、(a)/(i)编号、任务复选框、保持在网格上的间距?
  • 怎样做行内标签:键盘按键、标签、练习用的词库?
  • 怎样在一份文档里给几张表设不同的样式(填色、斑马纹单元格、圆角边框)?
  • 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?

简短回答

script.js · 第35–47行在完整代码中
// 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: '' }]) });

用料

类型
PT Serif, Inter Tight(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 给题目编号,让选项悬挂排列

script.js · 第51–63行在完整代码中
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: '' }] });

orderedLists的每一级各自选择numberFormat和separator,separatorColor把句点和括号印成强调色,数字本身仍为墨色(有序列表)。设了numberWidth: 'level'后,编号按同一级中最宽的编号右对齐,所以每道题的文字都从23 pt处开始:“10.”的17 pt加上GAP。四个选项是一个bulletChar为空的无序列表项,与题目处于同一级,而不是嵌套在题目之下。它的文字从列表的indent开始,这个值是用编号字体测得的“10.”宽度加上GAP,所以无论用什么字体,选项都与题目的第一个字母对齐;说明框设了indent: pt(0),因为框里的列表沿用文档的缩进。同一级的两项之间,间距取前一项的itemSpacing:编号项之后为5 pt,选项行之后为15 pt。如果把选项行嵌套在题目之下,它两侧都会取题目列表的5 pt。

#2 · 用行内标签做涂卡圆圈和分值

script.js · 第67–76行在完整代码中
// 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) })];

行内标签是行内的一个框,行永远不会在标签中间断开(行内标签)。里面放一个大写字母时,paddingX: em(0.3)让框的宽度与高度大致相同:网格中宽4.5到4.8 mm,高4.7 mm。borderRadius: em(1)超过框的一半,而标签的圆角半径会被限制为较短边的一半,所以两端完全是圆的。没有style的标签采用chipStyles的第一项,所以八十个圆圈(网格中四十个,选项中四十个)都是像:chip[A]这样不带样式的标签;只有须知中已涂满的示例和分值指定了样式。分值靠右对齐:每个小题在标签之前写:tab,正文唯一的制表位位于行长末端并设align: 'end',让标签止于那里(制表符与制表位)。题目的列表项沿用正文的制表位,所以a)和i)的标签止于同一位置。如果小题的最后一行已放不下标签,最后一个词就会连同标签一起移到下一行。

#3 · 每种表格用各自的表格样式

script.js · 第80–101行在完整代码中
// 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

考生信息框、涂卡网格和答题线是三种tableStyles,每张表用styleId选取(命名表格样式)。简短回答中的答题线使用rules: 'horizontal',内边距让每行高两行正文,所以每条横线都比上一条低30 pt(10.6 mm),落在正文15 pt网格的每隔一行上。MARK单元格中间的空行放了一个不换行空格,因为空的单元格行或只含普通空格的行不占高度:这个单元格高三行,使整行为14.4 mm,只有两行时则是10.8 mm。网格不画线:每个单元格的底色与相邻单元格的相接,整个网格看起来是一整块。它的标题是一个题注,排在网格上方1.6 mm处(题注样式)。如果用标题,至少会高出一行正文(5.3 mm),因为每张放在“here”的表格上方都要留出这么多空间。form类型的captionPrefix为空,所以标题前不会出现“Form 1.”这样的标签。

#4 · 用标题属性画出封面

script.js · 第105–120行在完整代码中
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))] } } };

封面是一个标题# History {style="cover" …},它的样式画出一条色带、年份和四行文字,文字取自标题属性。设span: 'page'后,这一页算作章首页,所以正文页的页边线、页码和“Turn over”不会出现在它上面,页脚印的是那条提示。没有它,封面就和其他页一样是正文页。样式的margins把封面的栏加宽到整整168 mm,用来放各种表格;下一个标题是不带样式的一级标题,使正文页回到134 mm的行长。

#5 · 封面上不印页码和Turn over

script.js · 第124–139行在完整代码中
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' })] };

正文页上每个重复元素都带pages: 'body',所以封面作为章首页只显示它自己的提示(pages: 'opener')。因此各部分都以:::pagebreak加一个不带breakBefore的标题开始。一个带分页的标题会让它所在的页成为章首页,第2页和第3页就会失去页码、书眉和页边线。parity: 'odd'让Turn over只出现在右页上,因为左页本来就对着下一页。

完整食谱

沙盒
// ═══ 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

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

变化

#印在A4上

色带、年份、页边线和提示的尺寸都取自PAGE,所以改成A4只需改一行。封面上各块位置不变,两个语言版本中网格下方的空白都增加17.6 mm。

-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

#加一个词库或核对清单

词库、填空和要涂色的圆圈也都是行内标签:带答题框和词库的练习纸在一个嵌套框里排出一组黄色标签作为词库,还有一张每行以空心圆开头的核对清单。

常见问题

易错点

单独的$会开始数学公式:写成\$

美元符号会开始行内公式,所以$40这样的价格会变成公式的开头。写成\$40。 转义与字面字符 →

  • 如果在一道题的各小题之间插入答题线,就会把它的列表拆开:表格之后的列表另起一组,而每组按自己最宽的编号右对齐,于是ii)的文字会比i)的文字向右偏1 mm。这里答题线单独放在一页上,所有小题都留在同一个列表里。

致谢

文本
  • The Gettysburg Address (19 November 1863), text of the Bliss copy · Abraham Lincoln · 公有领域
  • The questions, the instructions and the Spanish translation of Source A · Ignacio Ferro · CC BY 4.0
字体
PT Serif (SIL OFL 1.1) · Inter Tight (SIL OFL 1.1)
沙盒