跳到主要内容
食谱编号33

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

菜谱卡片:用料与做法并排

菜谱书的两页,白色菜谱卡片上方有一个SERVES 4页签:左边是用料、标签和清单,右边是配26 pt红色序号的步骤。

页码58–59 · 第1–2页,共2页

  • 西班牙文样例:尚无中文版本
  • 成品尺寸190 × 250 mm
  • 1栏
  • Figtree 9.4/12.8
  • Caveat
  • Young Serif
  • 2页
  • 难度
  • Postext 1.57.1
  • 排版用时53 ms
  • 187行代码

简单来说

一本家常菜谱里的两页,每页一道菜。示例演示怎样在一张卡片上把用料和做法左右并排。

成品一览

Salt & Olive Oil是一本虚构的西班牙家常菜谱,这里是其中相对的两页,成品尺寸190 × 250 mm,一页一道菜。一张由扩散模型生成、以JPEG载入的照片出血铺满顶部104 mm:藏红花色桌面上切出一块的土豆饼,鼠尾草绿桌面上的一碗西班牙冷汤。照片上叠着眉题、42 pt Young Serif标题、表示用时、难度和季节的胶囊标签,还有一句Caveat手写体批注指向菜品。斜体引言下方是一张白色卡片,右上角有番茄红的份量页签。用料沿左栏排下,前面是橄榄绿短横,后面是标签;土豆饼的用料下方,碗、锅和盘子的线描领起一份清单。做法占满右栏,每一步以26 pt的旧式数字开头。

这道食谱解答

  • 怎样在框里分两栏(文字栏旁边是图栏)?
  • 怎样加编号标签(“BOX 1-1”)、角标图标,或带线的边栏图标?
  • 怎样定制列表:每级的项目符号、(a)/(i)编号、任务复选框、保持在网格上的间距?
  • 怎样做行内标签:键盘按键、标签、练习用的词库?
  • 怎样给章首页加作者行、提要或带首字下沉的导语?

简短回答

script.js · 第26–48行在完整代码中
// In the Markdown, :::callout{type="card" label="SERVES 4"} holds a :::columns{count=2} group
// of two nested boxes, :::callout{type="column" title="Ingredients"} and one for the method.
// The group levels its columns by cutting between blocks or lines, so loose lists would run
// the method on under the ingredients. A nested box is one block that never splits, so the
// only cut left is between the two.
const TAB = 5.6; // mm: the tab's height, and how far it rises above the card
const card = { id: 'card', background: col('card'),
  padding: { top: mm(4.5), right: mm(6), bottom: mm(5), left: mm(6) },
  // No space of its own above: the tab starts on the first grid line under the opener.
  columnGap: mm(7), marginTop: mm(0),
  // label="…" on the fence prints here: a tab on the top-right corner, a cutlery pictogram
  // beside it and a rule from the far corner that makes the tab part of the card.
  label: { fontFamily: TEXT, fontSize: pt(8), color: col('card'), // bold by default
    background: col('tomato'), height: mm(TAB), offset: mm(TAB), paddingX: mm(2.6),
    icon: { resourceId: 'cutlery', width: mm(TAB * 6 / 8), gap: mm(1.6) }, // as tall as the tab
    rule: { enabled: true, color: col('tomato'), width: pt(1.2) } } };
// The two columns: frameless boxes whose only device is a tracked title.
const NONE = { top: mm(0), right: mm(0), bottom: mm(0), left: mm(0) };
const column = { id: 'column', backgroundEnabled: false, padding: NONE,
  lists: { gap: mm(2.2), itemSpacing: pt(3.5) }, // for the steps as well as the dashes
  titleStyle: { fontFamily: TEXT, fontSize: pt(8), color: col('tomato'), // bold by default
    textTransform: 'uppercase', letterSpacing: pt(1.5), gap: mm(2.4) } };
// config() plugs them in: calloutStyles: [card, column, prep].

用料

类型
Young Serif, Figtree, Caveat(SIL OFL 1.1)
素材
  • gazpacho-1520.jpg
  • tortilla-1520.jpg
  • The tortilla on its plate, a photograph (Generated With Diffusion Models, 原创)
  • The bowl of gazpacho, a photograph (Generated With Diffusion Models, 原创)
  • The note’s arrow and the pictograms, drawn in code in the page’s palette (Ignacio Ferro, CC BY 4.0)

做法

#1 · 每栏一个自己的框

用松散列表时,框内的分栏组会在各栏最齐的地方切开,可能在块之间,也可能在一个块的行之间,于是冷汤的第一步被栏间距劈成两半:第一行排在用料栏末尾,其余部分排在做法栏开头。像上面简短回答那样嵌套一个框,它就成为一个整块,分栏组永远不会从中切开。放在其中的图片需要一个placement为'here'的::resource,因为任何其他placement都会让它成为浮动体,离开框,像任何被引用的图一样另行放置。卡片样式的label把围栏上的label="SERVES 4"变成页签:offset等于它的height,就让它立在卡片上边缘,rule从远端的角一直画到刀叉图标(标注框样式)。

#2 · 让步骤序号在两行之间居中

script.js · 第66–76行在完整代码中
// Young Serif has old-style figures: 1 and 2 stand 0.56 em, a little above its 0.50 em
// x-height, so 5.1 mm at STEP. A list number is centred 0.3 em (of the text) above the item's
// first baseline, with the canvas 'middle' baseline, which Chrome puts 0.24 em above Young
// Serif's own (gotcha: list-number-centred). DROP centres the figures on the step's first two
// lines, from the cap height of the first to the baseline of the second.
const [STEP, FIGURE, MIDDLE, CAP] = [26, 0.56, 0.24, 0.7]; // pt; em of each face
const DROP = (LEAD - CAP * BODY) / 2 + 0.3 * BODY + (FIGURE / 2 - MIDDLE) * STEP; // pt
const orderedLists = { fontFamily: DISPLAY, fontWeight: 400, // Young Serif ships 400 only
  numberFontSize: pt(STEP), color: col('tomato'),
  separatorColor: col('olive'), separatorGap: pt(0.6), // the default '.' as its own run
  numberVerticalOffset: pt(DROP) };

列表序号画在条目第一条基线稍上方并居中,所以只设numberFontSize会让26 pt的数字与第一行齐平;numberVerticalOffset把它们下移2.5 mm,移到从第一行大写字母顶部到第二行基线这一区间的中部。Young Serif用的是旧式数字:3、4、5会伸到1和2所在的基线以下。设了separatorColor后,分隔符作为单独的一段文字绘制,默认的句点就以橄榄绿印在红色数字旁(有序列表)。

#3 · 用一排线描领起清单

script.js · 第52–62行在完整代码中
// :::callout{type="prep" title="Before you start"}, closed at once, is a header: a box that
// holds only its title and icon. The icon is one picture of three drawings; a width KIT times
// its size makes its box a strip, and the picture is fitted into width × size, left of the
// title. Tasks set inside the box would start after that column, 21 mm in, so the '- [ ]'
// items follow the box, where they print the default task box, '☐'.
const [STRIP, KIT] = [5, 30 / 8]; // mm: the strip's height; the drawing's width over height
const prep = { id: 'prep', backgroundEnabled: false, padding: NONE,
  marginTop: pt(LEAD), marginBottom: column.titleStyle.gap, // the tasks follow at this gap
  icon: { kind: 'resource', resourceId: 'kit', size: mm(STRIP), width: mm(STRIP * KIT),
    align: 'center' }, // the title centred on the strip
  titleStyle: { ...column.titleStyle, color: col('olive') } };

prep框里只有标题和图标,所以它起标题栏的作用:图标width是size的3.75倍,把图标的正方形拉成18.75 × 5 mm的长条,三幅线描排满标题左侧(标注框样式)。图标在框内独占一栏,与框内其他内容并排,所以任务项放在框之后,这样它们与用料的短横对齐,而不是从21 mm处开始。- [ ]条目印出默认的任务框,unorderedLists.marginTop: 0让它们以标题栏的marginBottom紧随其后,即各栏标题保留的2.4 mm间距(无序列表)。

#4 · 把标签做成行内标签

script.js · 第80–82行在完整代码中
const chipStyles = [{ id: 'tag', fontSize: em(0.86), bold: true, background: col('tint'),
  borderWidth: pt(0), borderRadius: em(1), // no outline; a radius past half the height: a pill
  paddingX: em(0.7), paddingY: em(0.18), gap: em(0.3) }];

:chip[Vegetarian]{style="tag"}把一个词放进一个永不跨行断开的框。大于框高一半的borderRadius会被限制为一半,两端就成了半圆;borderWidth: 0时,胶囊只用底色绘制。框高11.4 pt,小于卡片12.8 pt的行距,所以第二行标签不会碰到第一行(行内标签样式)。

#5 · 一套版面设计,每道菜一张照片

script.js · 第86–120行在完整代码中
const BAND = 104; // mm: the photo's foot; the pills sit 13 mm above it, the lead 6 mm below
const NOTE = { x: 18, y: 24, w: 70 }; // mm on the page: the box ends 2 mm before the arrow
const at = (x, y, size) => ({ anchor: { to: 'container', edge: 'top-left' },
  offset: { x: mm(x), y: mm(y - PAGE.top) }, size }); // y in mm from the top of the page
const text = (id, content, family, size, placement, extra) => ({ kind: 'text', id, content,
  fontFamily: family, fontSize: pt(size), color: col('ink'), align: 'left',
  overflow: 'wrap', placement, ...extra }); // a foot wraps too, where the default is '…'
const tracked = { fontWeight: 700, textTransform: 'uppercase', letterSpacing: pt(1.6) };
// Pills are text boxes chained right-of each other; each prints one heading attribute.
const pill = (id, after) => text(id, `{attr.${id}}`, TEXT, 8.4, after ? { anchor:
  { to: `#${after}`, edge: 'right-of' }, offset: { x: mm(1.8) } } : at(0, BAND - 13), {
  fontWeight: 600, box: { backgroundColor: col('card'), borderRadius: mm(3),
    padding: { top: mm(1.1), right: mm(2.8), bottom: mm(1.1), left: mm(2.8) } } });
// One heading style for every recipe; each heading names its photo: art="tortilla".
const opener = { id: 'receta', advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'image', id: 'art', resourceId: '{attr.art}', // filled in per heading, like a text
      placement: { anchor: { to: 'page', edge: 'top-left' },
        size: { width: mm(PAGE.w), height: mm(BAND) } } },
    { kind: 'image', id: 'arrow', resourceId: 'arrow', // the note's arrow, over the photo
      placement: { anchor: { to: 'page', edge: 'top-left' },
        size: { width: mm(PAGE.w), height: mm(BAND) } } },
    text('note', '{attr.note}', HAND, 19, { anchor: { to: 'page', edge: 'top-left' },
      offset: { x: mm(NOTE.x), y: mm(NOTE.y) }, size: { width: mm(NOTE.w) } },
    { fontWeight: 600, align: 'right' }), // on the page, like the arrow and the photo
    pill('time'), pill('level', 'time'), pill('season', 'level'),
    text('title', '{titleText}', DISPLAY, 42, { anchor: { to: '#time', edge: 'above' },
      offset: { y: mm(-3.2) }, size: { width: mm(96) } }, // two lines: the \\ in the heading
    { lineHeight: 1 }), // set solid: 42 pt from line to line
    text('kicker', '{attr.kicker}', TEXT, 8.2, { anchor: { to: '#title', edge: 'above' },
      offset: { y: mm(-2.4) }, size: { width: mm(96) } }, tracked),
    // The lead is the design's lowest element: the opener reserves down to its last line, and
    // the card starts under it without a minHeight.
    text('lead', '{attr.lead}', TEXT, 10.5, at(0, BAND + 6, { width: mm(122) }),
      { italic: true, lineHeight: 1.45 }),
  ] } } };

# Tortilla \\ de patatas {style="receta" art="tortilla" kicker="…" lead="…"}选定标题样式,并用属性填入各个位置,斜体引言也在其中(标题属性)。图片元素的resourceId是'{attr.art}',和文字里的占位符一样,所以两道菜共用一个样式,由各自的标题指明照片(图片元素)。每个胶囊标签锚定在前一个的right-of,标题锚定在第一个胶囊的above,眉题在标题上方,所以标题变长时眉题上移,胶囊标签留在原来那一行。引言是设计里位置最低的元素,章首区一直预留到它的最后一行,所以不设minHeight卡片也从引言下方开始。批注和照片以及箭头一样锚定在页面上,箭头是用代码绘制的第二个图片元素;两页上批注都在箭头前2 mm处结束,尽管对称的页边距让右页的版心向右移了2 mm。

#6 · 把图片注册为资源

script.js · 第271–293行在完整代码中
// The opener's image elements and both icons name a resource id; the resource names the file
// the canvas paints (loadImage or loadSvg registers it). No :ref cites them, so none is numbered.
const photo = (id, altText) => ({ id, typeId: 'figure', kind: 'bitmap', createdAt: 0, updatedAt: 0,
  altText, bitmap: { fileId: `${id}-1520.jpg`, format: 'jpeg', width: 1520, height: 832 } });
const ART = { // markup, width and height in mm, and the alt text
  arrow: [arrow, PAGE.w, BAND, t({ en: 'An arrow to the dish', es: 'Una flecha al plato' })],
  cutlery: [cutlery, 6, 8, t({ en: 'Fork and knife', es: 'Tenedor y cuchillo' })],
  kit: [kit, 8 * KIT, 8, t({ en: 'A bowl with two eggs, a frying pan and a plate',
    es: 'Un bol con dos huevos, una sartén y un plato' })] };
const resources = [
  photo('tortilla', t({ en: 'A potato omelette with a slice pulled out, on a blue-rimmed plate '
    + 'and a red-checked napkin', es: 'Una tortilla de patatas con una porción separada, en un '
    + 'plato de borde azul sobre una servilleta de cuadros rojos' })),
  photo('gazpacho', t({ en: 'A bowl of gazpacho with diced vegetables and a spoon, on a '
    + 'blue-checked napkin beside a tomato', es: 'Un cuenco de gazpacho con dados de verdura '
    + 'y una cuchara, sobre una servilleta de cuadros azules junto a un tomate' })),
  ...Object.entries(ART).map(([id, [, w, h, altText]]) => ({ id, typeId: 'figure',
    kind: 'svg', createdAt: 0, updatedAt: 0, altText, svg: { fileId: `${id}.svg`, width: w * 10,
      height: h * 10 } })), // 10 px a millimetre: the sizes only set the aspect ratio here
];
const photos = resources.filter((r) => r.kind === 'bitmap').map((r) => r.bitmap.fileId);
await Promise.all([...photos.map((file) => loadImage(file, asset(file))),
  ...Object.entries(ART).map(([id, [svg]]) => loadSvg(`${id}.svg`, svg))]);

开篇的两个图片元素、页签的图标和清单标题栏的图标各自指明一个资源,Canvas绘制loadImage或loadSvg为它注册的文件。没有:ref引用这些资源,所以它们都不会成为编号的图。图片里没有文字:手写批注是叠在上面的设计文字。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 033 · Recipe card: ingredients beside the method ═══════
// https://postext.dev/en/cookbook/recipe-card
// Code: MIT · Text: original (CC BY 4.0) · Photos: diffusion models · Pictograms: CC BY 4.0
// Fonts: Young Serif, Figtree, Caveat (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

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

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { ink: '#2b2118', muted: '#76634e', // text; folios and the colophon
  paper: '#f7eddb', card: '#fffdf8', // the cream page; the white recipe card on it
  tomato: '#bf3d29', olive: '#6b7a3a', tint: '#f6e3c1' }; // numbers and tab; dashes; tags
// 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 tomato, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.tomato })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, DISPLAY, HAND] = ['Figtree', 'Young Serif', 'Caveat'];
const PAGE = { w: 190, h: 250, top: 22, inner: 18, outer: 16 }; // mm, mirrored margins
const [BODY, LEAD] = [9.4, 12.8]; // pt: the text of the cards and the notes under them

// #region answer: a white card with a servings tab, two columns inside it
// In the Markdown, :::callout{type="card" label="SERVES 4"} holds a :::columns{count=2} group
// of two nested boxes, :::callout{type="column" title="Ingredients"} and one for the method.
// The group levels its columns by cutting between blocks or lines, so loose lists would run
// the method on under the ingredients. A nested box is one block that never splits, so the
// only cut left is between the two.
const TAB = 5.6; // mm: the tab's height, and how far it rises above the card
const card = { id: 'card', background: col('card'),
  padding: { top: mm(4.5), right: mm(6), bottom: mm(5), left: mm(6) },
  // No space of its own above: the tab starts on the first grid line under the opener.
  columnGap: mm(7), marginTop: mm(0),
  // label="…" on the fence prints here: a tab on the top-right corner, a cutlery pictogram
  // beside it and a rule from the far corner that makes the tab part of the card.
  label: { fontFamily: TEXT, fontSize: pt(8), color: col('card'), // bold by default
    background: col('tomato'), height: mm(TAB), offset: mm(TAB), paddingX: mm(2.6),
    icon: { resourceId: 'cutlery', width: mm(TAB * 6 / 8), gap: mm(1.6) }, // as tall as the tab
    rule: { enabled: true, color: col('tomato'), width: pt(1.2) } } };
// The two columns: frameless boxes whose only device is a tracked title.
const NONE = { top: mm(0), right: mm(0), bottom: mm(0), left: mm(0) };
const column = { id: 'column', backgroundEnabled: false, padding: NONE,
  lists: { gap: mm(2.2), itemSpacing: pt(3.5) }, // for the steps as well as the dashes
  titleStyle: { fontFamily: TEXT, fontSize: pt(8), color: col('tomato'), // bold by default
    textTransform: 'uppercase', letterSpacing: pt(1.5), gap: mm(2.4) } };
// config() plugs them in: calloutStyles: [card, column, prep].

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

变化

#不给各栏装框,改为按块计数

去掉各栏的框后,breaks指定开启右栏的那个子块,每个列表条目、标签那一行和清单的标题栏各算一个:冷汤需要breaks="10"(前面是八种用料和标签),土豆饼是breaks="11"。各栏标题和栏样式在步骤之间留的间距也随之消失。

 :::callout{type="card" label="SERVES 6"}
-:::columns{count=2}
-:::callout{type="column" title="Ingredients"}
+:::columns{count=2 breaks="10"}
 - **1.5 kg** ripe plum tomatoes
@@ seven more ingredients and a blank line @@
 :chip[Vegan]{style="tag"} :chip[No-cook]{style="tag"} :chip[Make ahead]{style="tag"}
-:::
-:::callout{type="column" title="Method"}
+
 1. Chop the tomatoes, seeded pepper, peeled cucumber and garlic.
@@ steps 2 to 5 @@
 :::
-:::
 :::

#把页签立在左上角

'top-left'让页签左右翻转:页签移到左上角,刀叉在它右边,线从右边画过来。

     background: col('tomato'), height: mm(TAB), offset: mm(TAB), paddingX: mm(2.6),
+    position: 'top-left', // 默认为'top-right'

常见问题

易错点

列表编号以首行居中,而不是立在基线上

有序列表的编号用Canvas的'middle'基线绘制,位于该项首行基线之上0.3 em(按正文字号)处,所以换用别的字体或加大numberFontSize的编号会从这里向上下两个方向扩展,而不是立在行上:展示字体会偏高,大号的步骤编号会横跨首行。用orderedLists.numberVerticalOffset调整位置,并按实际尺寸检查效果。目录编号从postext 1.5起落在基线上;列表编号仍然居中。 编号列表 →

易错点

框的lists.color也会改变列表编号的颜色

标注框样式只要设置了lists.color,它就同样作用于框内的项目符号和有序列表的编号:用这种方式给框设橄榄色项目符号,它的步骤编号也会变成橄榄色。(postext 1.4及以前,只有当它与unorderedLists.color不同时才会作用于编号。)只想给框内的项目符号上色时,在文档层面设置unorderedLists.color,框样式里不写lists.color,这样编号保持orderedLists.color。 标注框 →

易错点

高于行距的行内标签会碰到下一行

框高超过行距的行内标签会碰到下一行的行内标签(chipOverlap警告)。减小它的上下内边距、边框或字号,或者加大行距。 行内标签 →

  • 页签的label没有textTransform和letterSpacing,所以SERVES 4和PARA 4在围栏里直接用大写输入。

致谢

文本
原创文字, CC BY 4.0
图片
  • The tortilla on its plate, a photograph · Generated With Diffusion Models · 原创
  • The bowl of gazpacho, a photograph · Generated With Diffusion Models · 原创
  • The note’s arrow and the pictograms, drawn in code in the page’s palette · Ignacio Ferro · CC BY 4.0
字体
Young Serif (SIL OFL 1.1) · Figtree (SIL OFL 1.1) · Caveat (SIL OFL 1.1)
沙盒