跳到主要内容
食谱编号52

排版食谱 · 第8章 · 表

小酒馆菜单:价格排在制表位上

一张双面印刷的小酒馆菜单:制表位把每道菜的价格推到右侧,酒单排在无边框表格里,每道菜类标题两侧锚定一条黄铜色细线。

本页内容
输出
Canvas
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0
许可证
更新于2026年10月11日
代码MIT · 文本CC BY 4.0
  • 英文样例:尚无中文版本
  • 成品尺寸230 × 310 mm
  • 1栏
  • Noticia Text 10.5/14.5
  • Josefin Sans
  • Limelight
  • 2页
  • 难度
  • Postext 1.57.1
  • 排版用时47 ms
  • 154行代码

简单来说

一家虚构巴黎小酒馆的双面菜单。示例演示怎样用制表符把每个价格推到右边缘,以及怎样把酒单排进不显示线条的表格。

成品一览

虚构的巴黎小酒馆Les Tanneurs的秋季菜单:一张230 × 310 mm的单页,双面印刷。正面,一顶条纹遮阳篷悬在用Limelight排的店名上方。每个菜类标题夹在两条延伸到页边距的黄铜色细线之间,下面列四道菜。每道菜由粗体法文菜名及配菜、下一行的英文斜体说明,以及右对齐的粗体价格组成。深绿色V圆标表示素食,描边的SG胶囊标表示无麸质。每道菜是一个段落,制表符把价格送到右页边距处的制表位。背面,酒单排在一排酒瓶之下,杯卖和瓶卖价格分列在表头为12 cl和75 cl的两栏里,放在一个不带线、不带题注的表格中。

这道食谱解答

  • 怎样做一个有表头行、合并单元格、栏宽和逐格对齐的表?
  • 怎样做行内标签:键盘按键、标签、练习用的词库?
  • 怎样排不编号的插图:饰图、小花饰、标志?
  • 怎样用代码(资源)而不是Markdown的![]()添加图片和表格?

简短回答

script.js · 第28–42行在完整代码中
// Each course is a :::paragraphs group in the dish style. Its one tab stop stands at the end of
// the measure and ends there the text after the tab, so `:tab **7**` sets the price flush right
// however long the dish is. A backslash at the end of the line breaks it: the dish and its
// translation are one paragraph, and the price shares the dish's first line.
const dish = { id: 'dish', name: 'Dish', textAlign: 'left', // the card is centred; a dish is not
  tabStops: [{ position: 'end', align: 'end' }],
  spaceBetween: pt(LEAD / 2), // half a line between two dishes: each dish 2½ lines of LEAD
  marginTop: pt(LEAD), // a line of air under the course head
  indent: pt(LEAD / 4), endIndent: pt(LEAD / 4) }; // in line with the wine list's cell text
// The wine list stays a table: its prices stand in two columns under the heads 12 cl and 75 cl.
// The document's tableStyle turns off every rule and fill (#region wines).
const tableStyle = { rules: 'none', cellPadding: pt(LEAD / 4), // a row: 1½ lines of LEAD
  bodyFontFamily: TEXT, bodyFontSize: pt(BODY), bodyColor: col('ink'),
  headerFontFamily: LABEL, headerFontSize: pt(8.5), headerColor: col('wine'),
  headerBackgroundEnabled: false }; // header cells: the wine list's labels

用料

类型
Limelight, Noticia Text, Josefin Sans(SIL OFL 1.1)
素材
  • The awning and the shelf of bottles, drawn in code in the page's palette (Ignacio Ferro, CC BY 4.0)

做法

#1 · 在价格的位置设一个制表位

代码见上文的简短回答。每个菜类是一个:::paragraphs{style="dish"}组,一道菜一个段落(段落样式)。菜品样式只有一个制表位,位于'end'即行长末端,并设align: 'end':制表符之后的文字止于这个制表位。所以在配菜和标识之后写:tab **7**,无论菜名多长,价格都会靠右页边距排齐(制表符与制表位)。:tab会并入它两侧的空格;行末的反斜杠是强制换行,所以斜体译文另起同一段落的第二行,价格留在第一行。spaceBetween在两道菜之间留出半行,所以每道菜占14.5 pt行距的两行半。indent和endIndent各为四分之一行,让菜品与背面酒单单元格中的文字对齐。含制表符的段落逐行排版;这里的每一行本来就是不齐行的。

#2 · 一种资源类型,就地放置,从不编号

script.js · 第46–53行在完整代码中
// No caption prefix and no caption, so no 'Table 1' line prints under the wine list. Placement
// 'here' sets each piece at its ::resource line; a list that outgrows the page is cut
// between rows and goes on at the head of the next one.
const resourceTypes = [{ id: 'menu', name: 'Menu', shortLabel: 'Menu', captionPrefix: '',
  numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal',
  defaultPlacement: { position: 'here' } }];
const piece = (id, kind, body) => ({ id, typeId: 'menu', kind, createdAt: 0, updatedAt: 0,
  ...body });

内置的图和表类型会印出题注行,并把每个资源浮动到首次引用之后的第一个空位。menu类型没有题注前缀,其中各部分也没有题注,所以插图和酒单下面都不印任何东西。给它加上captionPrefix: 'Menu',每一部分下面都会印出一行编号,从遮阳篷下的“Menu 1.”开始,菜单也就排到了四页。defaultPlacement让酒单和每幅插图都放在各自的::resource行上(块嵌入)。没有它,酒单会浮动到背面的页脚,排在Le Comptoir和版权说明之下。遮阳篷和酒瓶是同一类型的SVG资源,用代码画出,并用工具包的loadSvg注册。

#3 · 合并产区行

script.js · 第57–70行在完整代码中
function wineList(tsv) {
  let m = { ...parseTSV(tsv, { headerRows: 1 }), columnWidths: [5, 1, 1] }; // the head row
  m.rows = m.rows.map((row, r) => row.map((cell, c) => (c === 0 ? cell : { ...cell, align: 'right',
    content: r > 0 && cell.content ? `**${cell.content}**` : cell.content }))); // as the dishes'
  m.rows.forEach(([first, ...rest], r) => { // a line with one field names a region
    if (r === 0 || !first.content || rest.some((cell) => cell.content)) return;
    // One cell across the table, centred on the card like the course heads; a label centred
    // in the first column alone would sit 22.6 mm left of them. The label face is the header's.
    m.rows[r][0] = { ...first, isHeader: true, align: 'center' };
    // mergeCells marks the two cells it covers hiddenBy (gotcha: merged-cells-hiddenby).
    m = mergeCells(m, { start: { row: r, col: 0 }, end: { row: r, col: 2 } });
  });
  return piece('wines', 'table', { table: { model: m } });
}

酒单仍是表格:两种价格分列在各自的表头12 cl和75 cl之下,而一行制表位无法给它们配上表头。parseTSV(tsv, { headerRows: 1 })把第一行读作表头行,所以Appellation · cépage、12 cl和75 cl用表头字体Josefin Sans印在各自的列上方。只有一个字段的行表示产区名。它的单元格成为居中的表头单元格,mergeCells把它横跨三列展开,并把它覆盖的两个单元格标为hiddenBy(构建表格模型)。如果只在第一列居中,BLANCS会比La Cave和Le Comptoir居中的轴线偏左22.6 mm。TSV中的空行会变成一行空单元格,高一行半,这一行就是Blancs和Rouges上方的空隙。

#4 · 在菜类标题两侧各挂一条线

script.js · 第74–88行在完整代码中
// The title has no width, so it shrink-wraps its text, and 'top' centres it on the column. Each
// rule hangs off one edge of the title ('left-of', 'right-of'), 4 mm away, and 'fill' runs it
// to the column's edge: 66.7 mm beside PLATS, 56.2 mm beside the longer LE COMPTOIR.
const rule = (edge, x) => ({ kind: 'rule', id: `rule-${edge}`, color: col('brass'),
  thickness: pt(0.75), // heavier than the default 0.5 pt hairline
  placement: { anchor: { to: '#title', edge },
    size: { width: 'fill' }, // to the column's edge
    offset: { x: mm(x), y: pt(6) } } }); // 6 pt down: the middle of Limelight's capitals
const courseHead = { enabled: true, slot: { elements: [
  { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(15),
    lineHeight: 0.96, // 14.4 pt: the title stays inside one 14.5 pt grid line
    textTransform: 'uppercase', color: col('wine'),
    placement: { anchor: { to: 'container', edge: 'top' } } },
  rule('left-of', -4), rule('right-of', 4),
] } };

菜类标题是栏内的一个版面设计。{titleText}元素不设宽度,所以它的框和大写字母一样宽,'top'锚点让它在栏中居中。每条线用'left-of'或'right-of'挂在标题的一侧,并一直填到栏的边缘(元素定位),所以PLATS两侧的线长66.7 mm,LE COMPTOIR两侧的线长56.2 mm,与字母都相距4 mm。标题的lineHeight是倍数0.96,使它的框高14.4 pt,保持在一条14.5 pt的网格线之内。设为1时框高15 pt,每个标题占两行,菜单会排到四页。

#5 · 标识用行内标签,小字用段落样式

script.js · 第92–110行在完整代码中
// A chip is a box around inline text: the V disc is filled, the SG pill only outlined. A chip
// takes the weight of the text around it, so without bold the V is set in 400, a weight not loaded.
const badge = { fontFamily: LABEL, fontSize: pt(7.5), bold: true, // Josefin Sans 700
  borderRadius: pt(8), paddingY: em(0.1), gap: em(0.6) }; // radius clamped to a half-height
const chipStyles = [
  // V: paddingX makes the box as wide as it is tall, a disc. borderWidth 0 removes the default
  // outline, 0.5 pt of main-color, which would ring the green in wine red.
  { id: 'veg', name: 'Vegetarian', ...badge, paddingX: em(0.3), borderWidth: pt(0),
    background: col('bottle'), color: col('paper') },
  { id: 'gf', name: 'Gluten-free', ...badge, paddingX: em(0.45), backgroundEnabled: false,
    borderColor: col('bottle'), borderWidth: pt(0.6), color: col('bottle') },
];
// The notes under the desserts and the colophon on the back: smaller, on the card's axis, a
// line of air above each.
const paragraphStyles = [dish,
  { id: 'notes', name: 'Notes', fontSize: pt(9), marginTop: pt(LEAD) },
  { id: 'colophon', name: 'Colophon', fontSize: pt(7.5), color: col('muted'),
    marginTop: pt(LEAD) },
];

行内标签在行内给一段文字加框(行内标签样式),所以在配菜后面输入:chip[V]{style="veg"},圆标就会出现在菜的法文行上,位于制表符之前。行内标签样式设了bold: true,因为行内标签沿用周围文字的字重,跟在配菜后面就是400,而FONTS只加载了700字重的Josefin Sans。甜点下的说明和背面的版权说明是9 pt和7.5 pt的:::paragraphs块,两种样式都带一行的marginTop(段落样式)。没有这个外边距,说明会紧接在奶酪拼盘英文行的下一行开始,看起来像是那道菜的一部分。

#6 · 让整张菜单沿一条轴线居中

script.js · 第114–126行在完整代码中
// bodyText (in config) centres the lines under the name and the notes; only the dishes and the
// wine list keep a left edge. Ragged text is hyphenated only with bodyText.hyphenation.ragged,
// so no word on this card is divided, the French dish names included. The config's locale
// names each edition's language, English or Spanish, for the bundle the Sandbox opens.
const headings = { fontFamily: DISPLAY, fontWeight: 400, color: col('wine'), textAlign: 'center',
  levels: [ // Limelight ships one weight, 400, and no italic
    // The H1 break is off: by default an H1 opens a recto, which would part the name from the
    // awning and La Cave from its bottles.
    { level: 1, fontSize: pt(48), lineHeight: pt(3 * LEAD), breakBefore: { enabled: false },
      marginTop: pt(LEAD), marginBottom: pt(0) }, // three grid lines, one of air above
    { level: 2, lineHeight: pt(LEAD), advancedDesign: courseHead, // one line; #region heads
      marginTop: pt(LEAD), marginBottom: pt(0) }, // the dishes below add a line of their own
  ] };

headings.textAlign和bodyText.textAlign让菜品和酒单以外的所有内容都位于菜单的中轴线上。lineHeight: pt(3 * LEAD)给48 pt的店名三条网格线。没有它,店名使用默认行距占四行,说明的第二行会移到单独一页,菜单排到四页。一级标题用breakBefore: { enabled: false }关闭H1分页,因为店名和La Cave应当和上方的插图在同一页。保留默认值{ enabled: true, parity: 'always-odd' },每幅插图都会独占一页,一共七页。配置没有设置locale:这张菜单上没有两端对齐的内容,而不两端对齐的文字只有在开启bodyText.hyphenation.ragged时才断词。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 052 · Bistro menu: prices on a tab stop ═══════════════════
// https://postext.dev/en/cookbook/bistro-menu
// Code: MIT · Text: original, in French (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Limelight, Noticia Text, Josefin Sans (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// The autumn menu of an imaginary Paris bistro: two sides of one card, each dish a paragraph
// whose price a tab sends to the right margin, the wine list a table with no rules.
import {
  buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage, parseTSV,
  mergeCells,
} from 'https://esm.sh/postext';

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

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { ink: '#1f2a24', paper: '#f6efdf', // green-black text on cream card
  wine: '#6d1f2c', brass: '#a9823a', // the name, heads and labels; rules and the drawings' metal
  straw: '#e8d4a8', bottle: '#2f4235', sage: '#8a9a78', // awning stripes and labels; glass
  muted: '#6b6457' }; // the colophon
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color' (#295aa3, a blue); this palette makes it the wine red.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.wine })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, DISPLAY, LABEL] = ['Noticia Text', 'Limelight', 'Josefin Sans'];
const [BODY, LEAD] = [10.5, 14.5]; // pt: the text, and the leading every table row keeps

// #region answer: dish on the left, price flush right: a tab stop at the end of the measure
// Each course is a :::paragraphs group in the dish style. Its one tab stop stands at the end of
// the measure and ends there the text after the tab, so `:tab **7**` sets the price flush right
// however long the dish is. A backslash at the end of the line breaks it: the dish and its
// translation are one paragraph, and the price shares the dish's first line.
const dish = { id: 'dish', name: 'Dish', textAlign: 'left', // the card is centred; a dish is not
  tabStops: [{ position: 'end', align: 'end' }],
  spaceBetween: pt(LEAD / 2), // half a line between two dishes: each dish 2½ lines of LEAD
  marginTop: pt(LEAD), // a line of air under the course head
  indent: pt(LEAD / 4), endIndent: pt(LEAD / 4) }; // in line with the wine list's cell text
// The wine list stays a table: its prices stand in two columns under the heads 12 cl and 75 cl.
// The document's tableStyle turns off every rule and fill (#region wines).
const tableStyle = { rules: 'none', cellPadding: pt(LEAD / 4), // a row: 1½ lines of LEAD
  bodyFontFamily: TEXT, bodyFontSize: pt(BODY), bodyColor: col('ink'),
  headerFontFamily: LABEL, headerFontSize: pt(8.5), headerColor: col('wine'),
  headerBackgroundEnabled: false }; // header cells: the wine list's labels
// #endregion

// #region type: one resource type for the whole card: set where it stands, never numbered
// No caption prefix and no caption, so no 'Table 1' line prints under the wine list. Placement
// 'here' sets each piece at its ::resource line; a list that outgrows the page is cut
// between rows and goes on at the head of the next one.

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

变化

#让细线停在固定长度

用固定宽度代替'fill',每条线就保持20 mm长、离标题4 mm,所以PLATS两侧的一对线比LE COMPTOIR两侧的更靠近中心。

-    size: { width: 'fill' }, // 延伸到栏的边缘
+    size: { width: mm(20) }, // 20 mm,无论标题在哪里结束

#用点引到价格

前导符填满制表位之前的空白。'. '重复一个句点和一个空格,从菜品最后一个标识或最后一个词之后半个em开始,到价格之前半个em为止。

-  tabStops: [{ position: 'end', align: 'end' }],
+  tabStops: [{ position: 'end', align: 'end', leader: '. ' }],

#像账本一样给酒单的每行画线

rules: 'horizontal'在酒单每行上下各画一条0.5 pt的黄铜色细线。线不占空间,所以正反两面的版面都不变,Blancs和Rouges上方的空行也会各自多出一对细线。

-const tableStyle = { rules: 'none', cellPadding: pt(LEAD / 4), // 一行:LEAD的一行半
+const tableStyle = { rules: 'horizontal', borderColor: col('brass'),
+  borderWidth: pt(0.5), cellPadding: pt(LEAD / 4),

常见问题

易错点

合并单元格需要hiddenBy占位:使用mergeCells

单元格按其在行数组中的位置排布,所以合并单元格延伸到的地方需要标记hiddenBy的占位单元格;像HTML那样省略它们,后面的每一列都会错位。用mergeCells来合并。从postext 1.5起,网格不规则的表会在doc.contentWarnings中报告为raggedTableGrid,parseTSV(text, { headerRows })会标记表头行。 由数据生成的表 →

在菜品的第一行给价格留出位置。如果菜名和配菜越过了价格所需的位置,制表位会把菜名的最后一个词连同价格一起移到下一行,英文行排在它们之后:这道菜于是占三行,整个菜类也结束得低一行。

致谢

文本
原创文字, CC BY 4.0
图片
  • The awning and the shelf of bottles, drawn in code in the page's palette · Ignacio Ferro · CC BY 4.0
字体
Limelight (SIL OFL 1.1) · Noticia Text (SIL OFL 1.1) · Josefin Sans (SIL OFL 1.1)
沙盒