简单来说
一个社区能源合作社的年度报告,分两栏排版。示例演示怎样让两栏文字在同一高度整齐结束。
成品一览
Tidewell Community Energy的2025年度报告。这是一家虚构的合作社,拥有两台风机、一片太阳能电站和21个屋顶上的光伏板。共五页,开本210 × 280 mm。封面上,一年的风电和光伏发电量画成两条曲带,排在150 pt的“2025”下方。之后是理事长的信、运营报告和财务主管的账目,都用Brygada 1918排成两栏两端对齐的正文,基于50行的网格。排满的栏止于网格的最后一行,未排满的栏与相邻栏齐平收尾。一个含三个关键数据的框横跨理事长信所在的那一页,框上方的信文齐平截断;一则关于计量的说明浮动到第4页顶部。末页两栏在利润表之上止于同一行。一个调色板属性把运营部分的页面变成青绿色,财务页面变成砖红色。
这道食谱解答
- 怎样让各栏底部齐平,最后一页也排得平衡(垂直两端对齐)?
- 怎样在页面中间排一个横跨两栏的框,比如三格并列的“数字一览”面板?
- 怎样把框浮动到页面顶部或底部,同时让正文继续排下去?
- 怎样做一个有表头行、合并单元格、栏宽和逐格对齐的表?
- 怎样把书分成若干篇或部分,每篇有自己的颜色和隔页?
简短回答
// The text block is LINES lines of LEAD deep (MARGIN.bottom is derived from them) and every
// heading takes whole lines, so a column the break rules leave short is short by whole lines.
// Balancing (on by default) stretches it back to its foot with its levers, in this order: a
// box that closes the column moves down to it; a picture with a safe area grows; lines above
// the headings, two each at most (maxLinesBeforeLoose); one line where a list ends; one under
// a display formula or a box, then under a float at the column head; a paragraph set one line
// longer and looser, with up to maxTracking of tracking; last, the lines a heading may still
// take, up to maxLinesPerHeading. A closing band whose columns differ by more than a line is
// cut level instead (trailing), and so is the band a page-wide box leaves when it has to move
// on to the next page (beforeSpan).
const balancing = { maxLinesPerHeading: 1 }; // one line per heading; the next lever takes more
const flowText = { // justification, hyphenation and Knuth–Plass stay at their defaults (on)
fontFamily: 'Brygada 1918', fontSize: pt(9.4), lineHeight: pt(LEAD),
firstLineIndent: mm(4), indentAfterHeading: false,
minWordSpacing: 0.7, maxWordSpacing: 1.8, // word spaces 0.7–1.8 of normal (defaults 0.6–2)
};
const onGrid = { lineHeight: pt(LEAD), marginTop: pt(LEAD), marginBottom: pt(0) }; // one line
// hook-up: headings: { balancing, levels: [..., { level: 2, ...onGrid }] }, bodyText: flowText
用料
做法
#1 · 所有行都在网格上,由调节手段补齐空缺
代码见上面的简短回答。版心正好是50行13.4 pt的高度,下页边距(21.6 mm)由这个高度推算出来。小标题在上方占一条网格线,自身再占一条,所以短了的栏总是短整数行,而各栏齐底补的也正是整行。在第3页,三行的冰雹那一段因为avoidOrphans整段移到第4页(avoidOrphans是Postext的规则名,防止段落最后一行单独留在栏首),右栏于是少了两行。maxLinesPerHeading: 1把其中一行给The Saltings and the roofs上方的空白,另一行留给下一项调节手段,它把这一行加在屋顶列表之后。默认情况下,标题先最多加两行,再轮到它之后的调节手段(maxLinesBeforeLoose),等它们无行可加时才加到最多4行,所以两行都会加到这个标题上方。
#2 · 在横跨页面的框上方齐平截断正文
// In the Markdown: :::callout{type="figures" span="page"} around :::columns{count=3 breaks="3,5"},
// each number a :::paragraphs{style="figure"} of **8.47 GWh**, then its line of text.
const figures = { id: 'figures', background: col('ink'), columnGap: mm(GUTTER),
padding: { top: mm(5), right: mm(5), bottom: mm(5), left: mm(5) },
marginTop: pt(LEAD), marginBottom: pt(LEAD), titleStyle: { ...boxTitle, color: col('sun') },
body: { fontFamily: 'Epilogue', fontSize: pt(9), lineHeight: pt(12), color: col('paper'),
...boxText } };
const bigNumber = { id: 'figure', fontFamily: 'Epilogue', fontSize: pt(26), lineHeight: pt(31),
color: col('paper'), boldColor: col('sun'), ...boxText };
span="page"的框把两栏页面切成若干横带。在第2页,理事长的信在框上方每栏排八行后停下,在框下两栏继续。框内,:::columns{count=3 breaks="3,5"}让第二、第三栏分别从第二、第三个数字开始(即该组的第三和第五个块),而不是在各栏最齐平的地方切开。figure段落样式把这些数字设为26 pt,其boldColor把它们印成黄色。
#3 · 让框浮动到下一页顶部
// In the Markdown: :::callout{type="aside" span="page" placement="top"}. It leaves the flow
// where it stands and takes the head of the next page the flow opens; the text goes on
// filling this one. It has a stripe in the section's colour and no fill. The padding under
// the text drops the floats below the box by a grid line, so they stand clear of it.
const aside = { id: 'aside', backgroundEnabled: false, columnGap: mm(GUTTER),
stripe: { enabled: true, side: 'top', width: pt(2.5), color: col('band') },
padding: { top: mm(3.5), right: pt(0), bottom: mm(3.5), left: pt(0) },
titleStyle: { ...boxTitle, color: col('band') },
body: { fontFamily: 'Epilogue', fontSize: pt(8.6), lineHeight: pt(12.2), color: col('ink'),
...boxText } };
在Markdown中,关于计量的说明紧跟在第3页介绍Harrow Down的第一段之后;placement="top"把它移出文本流,放到下一页顶部。报告填满第3页的其余部分,第4页以横跨两栏的这个框开头。框下,站点表和环形图都是position: 'top',各占一栏的顶部;由于框有3.5 mm的下内边距,它们比没有框时低一条网格线。
#4 · 不用分隔页,给每个部分配上自己的颜色
const palette = {
ink: '#14202b', // text: a blue-black
band: '#0b5d7a', // sea, overridden by palette="band=#…" on the :::part fences
wind: '#3aa6a0', sun: '#f2b134', coral: '#e2674b', // the data colours of the charts
tint: '#eef3f5', // subtotal rows
rule: '#c9d3d9', // hairlines
muted: '#566370', // captions' notes and the running feet
mist: '#9fb1bd', // small print on the ink cover
paper: '#ffffff',
};
// Every colour is linked to its palette entry, so a :::part recolours it by paletteId.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries(palette)
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
设了parts: { page: false },:::part{number="02" title="Operations" palette="band=#1c6e67"}这样的围栏就不会另起一页。它为章首色条和页脚提供该部分的名称,并在下一部分开始之前的每一页替换band。色条、小标题、项目符号、题注标签和旁注框的线都取band,所以运营部分的页面印成青绿色,财务页面印成砖红色,理事长的信则保留基础的海蓝色(按篇配色)。
#5 · 从数据生成表格,并让末页齐平收尾
const DATA = {
// Output in MWh at the export meter, January to December 2025.
wind: [560, 520, 450, 330, 270, 210, 190, 150, 300, 420, 480, 540], // Harrow Down
sun: [90, 160, 300, 440, 560, 600, 580, 500, 380, 250, 120, 70], // the Saltings + 21 roofs
// The turbines' year, % of the hours of both machines: [label, share, palette colour].
hours: [['generating', 78.0, 'wind'], ['waiting for wind', 16.8, 'rule'], ['bearing repair', 2.6,
'coral'], ['servicing and grid', 1.8, 'sun'], ['stopped for bats', 0.8, 'ink']],
// Output by site in MWh: [site, source, capacity in MW, 2025, 2024].
sites: [['Harrow Down', 'wind', 1.8, 4420, 4560], ['The Saltings', 'solar', 3.2, 3190, 2640],
['21 roofs', 'solar', 0.9, 860, 790]],
// £ thousand, [label, 2025, 2024]; a label alone opens a group, '=' prints the running sum.
accounts: [['Income'], ['Electricity sold under the power purchase agreement', 760, 722],
['Electricity sold to roof hosts', 92, 85], ['Feed-in tariff', 236, 229], ['=Total income'],
['Operating costs'], ['Operation and maintenance', -231, -198],
['Rent, rates and insurance', -158, -151], ['Staff and administration', -121, -112],
['Depreciation', -286, -286], ['=Operating surplus'],
['Interest on the Harrow Down loan', -54, -66], ['Interest on members’ shares at 3.5%', -113,
-107], ['Grants to the Tidewell Fund', -84, -70], ['Corporation tax', -5, -7],
['=Surplus for the year']],
};
const fill = { background: col('tint') }; // totals sit on a tint between two hairlines
const right = (content, extra) => ({ content, align: 'right', ...extra });
const figure = (n, digits = 0) => n.toLocaleString('en-GB', { minimumFractionDigits: digits });
const head = (c, i) => (i ? right(c, { isHeader: true }) : { content: c, isHeader: true });
const siteTable = (rows) => ({ headerRowCount: 1, columnWidths: [3, 1, 1.3, 1.3], rows: [
['Site', 'MW', '2025', '2024'].map(head),
...rows.map(([site, source, mw, ...n]) => [{ content: `${site} *(${source})*` },
right(figure(mw, 1)), ...n.map((v) => right(figure(v)))]),
[{ content: '**All sites**', ...fill }, ...[2, 3, 4].map((i, k) => right(`**${figure(rows
.reduce((sum, row) => sum + row[i], 0), k ? 0 : 1)}**`, fill))]] }); // the totals, summed
// Accounting style: losses in brackets, and gains followed by a no-break space, as wide as a
// bracket in a monospaced face, so the digits line up.
const pad = '\u00a0';
const money = (n) => (n < 0 ? `(${figure(-n)})` : `${figure(n)}${pad}`);
function statement(rows) {
const sum = [0, 0];
const cells = [['£ thousand', `2025${pad}`, `2024${pad}`].map(head)];
for (const [label, ...years] of rows) {
years.forEach((n, i) => { sum[i] += n; });
const sub = label.startsWith('='); // a subtotal: the running sum, in bold
cells.push(sub ? [{ content: `**${label.slice(1)}**`, ...fill },
...sum.map((n) => right(`**${money(n)}**`, fill))]
: [{ content: years.length ? label : `*${label}*` }, // a label alone heads a group
...[0, 1].map((i) => right(years.length ? money(years[i]) : ''))]);
}
// mergeCells writes the cells a spanning group head hides (gotcha: merged-cells-hiddenby)
return cells.reduce((model, row, r) => (r && !row[1].content ? mergeCells(model,
{ start: { row: r, col: 0 }, end: { row: r, col: 2 } }) : model),
{ rows: cells, headerRowCount: 1, columnWidths: [5, 1, 1] });
}
statement()把DATA.accounts逐行转成利润表。以=开头的标签会在浅色底上用粗体印出每年的累计和;成本在数据中是负数,所以Operating surplus直接由这个累计得出,不用手工输入任何合计。没有数字的标签,如Income或Operating costs,会变成一个横跨整表的斜体单元格,用mergeCells合并,它还会写入合并所需的占位单元格。在第5页,利润表浮动到页面底部,横跨页面,排在结束全文的财务主管报告之下。不做齐底的话,左栏会排12行,右栏9行;末页截断把两栏都定为11行,再由标题调节在Cash and reserves上方补上右栏还差的那一行。
完整食谱
// ═══ Postext Cookbook · Nº 037 · Annual report with flush columns ═══════════════════════
// https://postext.dev/en/cookbook/annual-report-flush-columns
// Code: MIT · Text: original (CC BY 4.0) · Art: drawn in code · Typefaces: SIL OFL 1.1
// Fonts: Brygada 1918, Epilogue, Spline Sans Mono, Mrs Saint Delafield · Needs postext ≥ 1.28.0
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage, mergeCells, inlineSvgFonts,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'annual-report-flush-columns';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: 'band' is the first section's colour; each later :::part brings its own
const palette = {
ink: '#14202b', // text: a blue-black
band: '#0b5d7a', // sea, overridden by palette="band=#…" on the :::part fences
wind: '#3aa6a0', sun: '#f2b134', coral: '#e2674b', // the data colours of the charts
tint: '#eef3f5', // subtotal rows
rule: '#c9d3d9', // hairlines
muted: '#566370', // captions' notes and the running feet
mist: '#9fb1bd', // small print on the ink cover
paper: '#ffffff',
};
// Every colour is linked to its palette entry, so a :::part recolours it by paletteId.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries(palette)
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const TRIM = { width: 210, height: 280 };
const LEAD = 13.4; // body leading in pt: one line of the baseline grid
const LINES = 50; // grid lines in a full column
const MARGIN = { top: 22, inner: 18, outer: 16 }; // mm, mirrored
MARGIN.bottom = TRIM.height - MARGIN.top - (LINES * LEAD * 25.4) / 72; // 21.6 mm: 50 lines exactly
const GUTTER = 7; // mm between the columns, and between the columns inside the boxes
const mono = { fontFamily: 'Spline Sans Mono', fontWeight: 500, textTransform: 'uppercase' };
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const text = (id, content, look, placement) => ({ kind: 'text', id, content, placement,
align: 'left', ...look });
// #region answer: flush columns: whole grid lines everywhere, and the balancing levers
// The text block is LINES lines of LEAD deep (MARGIN.bottom is derived from them) and every
// heading takes whole lines, so a column the break rules leave short is short by whole lines.
// Balancing (on by default) stretches it back to its foot with its levers, in this order: a
// box that closes the column moves down to it; a picture with a safe area grows; lines above
// the headings, two each at most (maxLinesBeforeLoose); one line where a list ends; one under
// a display formula or a box, then under a float at the column head; a paragraph set one line
// longer and looser, with up to maxTracking of tracking; last, the lines a heading may still
// take, up to maxLinesPerHeading. A closing band whose columns differ by more than a line is
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#让标题占用全部空缺
第3页空出的两行这时都加到The Saltings and the roofs上方,列表之后不再加行。
-const balancing = { maxLinesPerHeading: 1 }; // 每个标题一行;多出的由下一项调节手段承担
+const balancing = {}; // 默认值:每个标题先加两行,再轮到其他调节手段#关闭各栏齐底
这时第3页右栏少两行。第5页左栏排12行、右栏9行,利润表也低一行开始。
-const balancing = { maxLinesPerHeading: 1 }; // 每个标题一行;多出的由下一项调节手段承担
+const balancing = { enabled: false };常见问题
易错点
avoidWidows管栏底,avoidOrphans管栏顶
Postext对两种孤行的命名自成一套:avoidWidows(widowMinLines、widowPenalty)防止段落的首行单独留在栏底,avoidOrphans(orphanMinLines、orphanPenalty)防止段落的末行单独出现在下一栏的栏顶。很多体例手册的叫法正好相反,所以要按作用的位置选择设置。两者默认开启,字符网格(cjk.grid)除外:网格上avoidWidows默认关闭,日本地区的avoidOrphans也关闭,好让页面排满网格所数的行数(postext 1.28起)。两者都以惩罚值的方式起作用:排版时会把每一项与遵守它所留下的空行放在一起权衡。 段末孤行、段首孤行与孤字 →
- 末页两栏只差一行时保持原样,左栏多一行,和排字工的做法一样;末页截断只在相差两行或以上时才起作用。要让两栏一起结束,就把文稿加长或缩短一行。
- 利润表把成本放在括号里。为了让正数与括号里的成本对齐,pen在每个正数后加一个不换行空格:在等宽字体里它与右括号同宽,表格单元格会保留文本末尾的这个空格。
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Brygada 1918 (SIL OFL 1.1) · Epilogue (SIL OFL 1.1) · Spline Sans Mono (SIL OFL 1.1) · Mrs Saint Delafield (SIL OFL 1.1)


