跳到主要内容
食谱编号186

排版食谱 · 第2章 · 字体与文字

引文编号与统计量不落在行首行尾

一篇双栏临床论文排两遍:初校报出并标记落在行首行尾的每个引文编号或运算符号,二校把这些断点计入断行代价,换一种断法。

本页内容

页码211 · 第1页,共6页

  • 英文样例:尚无中文版本
  • 成品尺寸170 × 240 mm
  • 2栏, 栏间距6 mm
  • STIX Two Text 9.5/12.8
  • IBM Plex Sans
  • IBM Plex Sans Condensed
  • 6页
  • 难度
  • Postext 1.79.0
  • 排版用时153 ms
  • 180行代码

简单来说

栏很窄时,[2, 3] 这样的引文编号可能单独落到行首,p < 0.05 也可能在符号后断开。这篇论文排了两遍:一遍标出哪里出现这种断行,一遍改变断行,让它几乎不再出现。

成品一览

一份 170 × 240 mm 儿科期刊中的三页,双栏两端对齐,栏宽 66 mm,字体 STIX Two Text,内容是一篇用肺部超声诊断肺炎的原创研究,参考文献按 Vancouver 格式编号。栏这么窄,断行算法可选的余地很少,英文文本有三处断得不好。引文编号 [2, 3] 单独落到行首,离开了它所支撑的词;阴性似然比 LR− = 0.09 两次在等号后断开。初校把 citationBreaks 和 operatorBreaks 设为 'warn' 来排:断行一处不改,每处断点加一道浅琥珀色底纹,并在参考文献后的校样条上逐条列出。二校用 'avoid' 排,三处留下一处。西班牙语版排的是同一篇研究,小数点用逗号。

这道食谱解答

  • 怎样避免引用编号或 p < 0.05 这样的统计量在行尾被拆开?
  • 怎样像医学期刊那样把引用序号排成上标,并让DOI成为可点击的链接?

简短回答

script.js · 第27–42行在完整代码中
const bodyText = {
  fontFamily: SERIF, fontSize: pt(9.5), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  referenceBold: false, textAlign: 'justify', firstLineIndent: mm(3.5),
  indentAfterHeading: false, hyphenation: { enabled: true },
  optimalLineBreaking: true, // the rules price breaks in the Knuth–Plass breaker
  avoidWidows: true, avoidOrphans: true, avoidRunts: true,
  // A line that opens on a citation, "[8, 9]" (citationBreak), or ends or opens on a sign
  // set between spaces, "p <" / "0.05" (operatorBreak). 'warn' reports each one and changes
  // nothing; 'avoid' adds the penalty to the demerits of those breaks and reports the ones
  // it keeps. The first proof sets 'warn' (see proofs).
  citationBreaks: 'avoid', operatorBreaks: 'avoid',
  // 10000 by default, what a line spaced to the limit costs. A 66 mm column offers few other
  // breaks; at 30000 the breaker takes looser lines, never past maxWordSpacing.
  citationBreakPenalty: 30000, operatorBreakPenalty: 30000,
};

用料

类型
STIX Two Text, IBM Plex Sans, IBM Plex Sans Condensed(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 给两类断点定价

代码见上文简短回答。一行以带编号的引文开头,或以引文的后半截开头,比如 [4, 之后的 5],就是引文断行。一行以前后带空格的符号结尾或开头,就是运算符断行,符号包括 =、<、±、× 和减号。'warn' 两种都报告,行保持原样。'avoid' 给这些断点的代价加上罚分,Knuth–Plass 断行算法于是另找一种断法。它需要开启 optimalLineBreaking:逐行断行没有可以累加的代价。

默认罚分 10000,等于把一行拉到词距上限的代价,所以算法绝不会为了让引文挨着它的词而把一行拉过这个上限。在这个栏宽下,10000 让两个版本各留两处断点。设为 30000 后,算法接受更松的行,但仍在 maxWordSpacing 之内,两个版本各剩一处。再提高也没有变化:最后一处在上限之内没有别的断法。

#2 · 在文字上标出断点

script.js · 第254–279行在完整代码中
// The word before the citation and the citation, or the sign and the number on its other
// side, wrapped in :mark[…]: a band under ordinary text, so the breaks stay where they were.
const wordEnd = (s, i) => i + s.slice(i).search(/[\s;)]|[,.](?!\d)|$/); // 0.09, not 0
function sides(source, w, a, b) { // a warning names line a, before the break, by its source
  if (w.kind === 'citationBreak') { // "colleagues [@simel1991]"
    const cite = source.lastIndexOf('[@', b.sourceStart + 2);
    return [source.lastIndexOf(' ', cite - 2) + 1, source.indexOf(']', cite) + 1];
  }
  if (w.edge === 'end') { // "LR− = 0.09": the sign ends line a, the number opens line b
    const from = source.lastIndexOf(` ${w.text} `, a.sourceEnd) + 1;
    return [from, wordEnd(source, from + w.text.length + 1)];
  }
  const sign = source.indexOf(` ${w.text} `, b.sourceStart - 1); // the sign opens line b
  return [source.lastIndexOf(' ', sign - 1) + 1, sign + 1 + w.text.length];
}
function markBreaks(source, doc, found) {
  const pairs = doc.blocks.flatMap((b) => b.lines.map((line, i) => [line, b.lines[i + 1]]));
  const spans = new Map();
  for (const w of found) {
    const [a, b] = pairs.find(([line]) => line.sourceStart === w.sourceStart) ?? [];
    const [from, to] = b ? sides(source, w, a, b) : [];
    if (from > a?.sourceStart && to > from && to <= b.sourceEnd) spans.set(from, to);
  }
  return [...spans].sort(([x], [y]) => y - x).reduce((md, [from, to]) => `${md.slice(0, from)}`
    + `:mark[${md.slice(from, to)}]{style="edge"}${md.slice(to)}`, source);
}

每条警告都带着类型、引文或符号、两行的文字、第一行在源文本中的位置以及页码。脚本在排好的文档里找到这两行,用 :mark[…] 包住断点:引文和它前面的词,或者符号和另一侧的数字。标记只在文字下面画底纹,断点留在原处。底纹够不到引文样式生成的编号,所以页面上只有前面那个词带标记。

初校第211页:摘要里 LR− = 0.09 被拆开,first 落在行尾,下一行以 [2, 3] 开头。

#3 · 两份校样和一张疑问条

script.js · 第283–303行在完整代码中
const KIND = { citationBreak: t({ en: 'citation', es: 'cita' }),
  operatorBreak: t({ en: 'sign', es: 'signo' }) };
const edge = (s, end) => (end ? s.trim().split(/\s+/).slice(-2) : s.trim().split(/\s+/)
  .slice(0, 2)).join('\u00a0').replace(/[.,;]$/, ''); // no-break spaces: no breaks of its own
const query = (doc, w) => `${t({ en: 'p.', es: 'pág.' })}\u00a0${doc.pages[w.pageIndex]
  .pageLabel}, ${KIND[w.kind]}: …${edge(w.lines[0], true)}\u00a0/ ${edge(w.lines[1])}…`;
const slip = (mode, doc, found) => ['', `:::callout{type="slip" title="${mode === 'warn'
  ? t({ en: 'First proof: queries', es: 'Primeras pruebas: consultas' })
  : t({ en: 'Second proof: queries', es: 'Segundas pruebas: consultas' })}"}`,
`citationBreaks / operatorBreaks: '${mode}' · ${found.length} ${found.length === 1
  ? t({ en: 'break', es: 'corte' }) : t({ en: 'breaks', es: 'cortes' })}.`,
...found.flatMap((w) => ['', query(doc, w)]), ':::', ''].join('\n');
const build = (md, mode) => buildDocumentWithFonts({ markdown: md }, { ...config,
  bodyText: { ...bodyText, citationBreaks: mode, operatorBreaks: mode } }, kitFonts(FONTS));
async function proof(mode) {
  const plain = await build(markdown, mode); // pass 1: where the breaks are
  const found = (plain.contentWarnings ?? []).filter((w) => /^(citation|operator)Break$/
    .test(w.kind));
  return build(markBreaks(markdown, plain, found) + slip(mode, plain, found), mode); // pass 2
}
const docs = { warn: await proof('warn'), avoid: await proof('avoid') };

每份校样排两次。第一次找出断点;第二次排带标记的文章,并在参考文献后附一张校样条,所以校样条落在最后一页。排字工退回校样时附上疑问,也是这个做法。校样条各行的词之间用不换行空格连接,自身不会再产生断点。capture.doc 发布两次带标记的排版,[1, 3]。

二校第213页:校样条列出全文剩下的一处断行。

英文版剩下的一处是第211页的 first 叠 [2, 3]。西班牙语版初校找到四处,其中有 CP+ 叠 = 19,19,还有引文本身被拆开的 [4, 叠 5];二校留下一处,anteriores [4, 叠 5]。

#4 · 方括号里的编号引文

script.js · 第46–50行在完整代码中
registerCitationEngine(createCiteprocEngine({ styles: STYLES, locales: LOCALES }));
const citations = { style: 'elsevier-vancouver', locale: t({ en: 'en-GB', es: 'es-ES' }),
  marker: 'brackets', collapseRanges: true, // [8, 9] and [4–6], set after a word space
  bibliography: { fontSize: em(0.8), lineHeight: pt(9.6), entrySpacing: pt(1),
    labelWidth: mm(6), labelAlign: 'right', doi: 'hide' } };

引文处理器把每个 [@key] 排成词距之后方括号里的编号,并把连续编号合并成 [4–6] 这样的形式。方括号前的空格和括号内逗号后的空格都是可以断行的地方,所以引文多的窄栏需要这条规则。

#5 · 刊头与文章信息块

script.js · 第54–78行在完整代码中
const text = (id, content, family, size, extra) => ({ kind: 'text', id, content, align: 'left',
  fontFamily: family, fontSize: pt(size), color: col('ink'), overflow: 'wrap', ...extra });
const at = (to, edge, x, y, width) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) },
  ...(width && { size: { width: mm(width), height: 'auto' } }) });
const caps = { fontFamily: COND, fontWeight: 600, letterSpacing: pt(1.2),
  textTransform: 'uppercase' };
const JOURNAL = t({ en: 'Ribera Paediatric Journal', es: 'Revista Pediátrica de la Ribera' });
const white = { ...caps, color: col('paper') };
const titleBlock = { enabled: true, minHeight: mm(58), slot: { elements: [
  { kind: 'box', id: 'band', style: { backgroundColor: col('accent') }, placement: {
    anchor: { to: 'page', edge: 'top-left' }, size: { width: 'fill', height: mm(13) } } },
  text('journal', JOURNAL, COND, 9, { ...white, placement: at('page', 'top-left', INNER, 5) }),
  text('issue', '{attr.issue}', COND, 9, { ...white, align: 'right',
    placement: at('page', 'top-right', -OUTER, 5, 60) }),
  text('kind', '{attr.kind}', COND, 8.5, { ...caps, color: col('accent'),
    placement: at('container', 'top-left', 0, 0, MEASURE) }),
  text('title', '{titleText}', SANS, 16, { fontWeight: 600, lineHeight: 1.18,
    placement: at('#kind', 'below', 0, 2.5, MEASURE - 6) }),
  text('authors', '{attr.authors}', SERIF, 10, { inlineMarks: true, // ^1^ → ¹
    placement: at('#title', 'below', 0, 3.5, MEASURE) }),
  text('affiliations', '{attr.affiliations}', SANS, 7.2, { inlineMarks: true, lineHeight: 1.35,
    color: col('muted'), placement: at('#authors', 'below', 0, 1.6, MEASURE) }),
  text('note', '{attr.note}', SANS, 7.2, { color: col('muted'),
    placement: at('#affiliations', 'below', 0, 2.4, MEASURE) }),
] } };

标题样式画出期刊色带、文章类型、标题、带单位编号的作者和一条注释,全都来自 H1 上的属性。两个版本共用这个样式,文字留在 Markdown 里。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 186 · Citations and statistics off the line edges ═══════════
// https://postext.dev/en/cookbook/clinical-paper-line-edges
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: STIX Two Text, IBM Plex Sans and Sans Condensed (SIL OFL 1.1) · Needs postext ≥ 1.79.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerCitationEngine,
} from 'https://esm.sh/postext';
import { createCiteprocEngine, STYLES, LOCALES } from 'https://esm.sh/postext-citeproc';

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

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: an indigo journal, a pale amber band for the proof marks
const palette = { ink: '#1b1e26', accent: '#2c4a7c', tint: '#e9eef6', mark: '#f7dfa9',
  muted: '#596173', paper: '#ffffff' };
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries({ ...palette, 'main-color': palette.accent })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const [SERIF, SANS, COND] = ['STIX Two Text', 'IBM Plex Sans', 'IBM Plex Sans Condensed'];
const [TRIM_W, TRIM_H, TOP, BOTTOM, INNER, OUTER, GUTTER] = [170, 240, 20, 22, 17, 15, 6];
const MEASURE = TRIM_W - INNER - OUTER; // 138 mm: two columns of 66 mm, about 45 characters
const LEAD = 12.8; // pt, for 9.5 pt type

// #region answer: no line opens on a citation, no line ends or opens on a lone sign
const bodyText = {
  fontFamily: SERIF, fontSize: pt(9.5), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  referenceBold: false, textAlign: 'justify', firstLineIndent: mm(3.5),
  indentAfterHeading: false, hyphenation: { enabled: true },
  optimalLineBreaking: true, // the rules price breaks in the Knuth–Plass breaker
  avoidWidows: true, avoidOrphans: true, avoidRunts: true,
  // A line that opens on a citation, "[8, 9]" (citationBreak), or ends or opens on a sign
  // set between spaces, "p <" / "0.05" (operatorBreak). 'warn' reports each one and changes
  // nothing; 'avoid' adds the penalty to the demerits of those breaks and reports the ones
  // it keeps. The first proof sets 'warn' (see proofs).
  citationBreaks: 'avoid', operatorBreaks: 'avoid',
  // 10000 by default, what a line spaced to the limit costs. A 66 mm column offers few other
  // breaks; at 30000 the breaker takes looser lines, never past maxWordSpacing.
  citationBreakPenalty: 30000, operatorBreakPenalty: 30000,
};
// #endregion

// #region citations: numbered references in brackets, the list in the Vancouver order
registerCitationEngine(createCiteprocEngine({ styles: STYLES, locales: LOCALES }));
const citations = { style: 'elsevier-vancouver', locale: t({ en: 'en-GB', es: 'es-ES' }),
  marker: 'brackets', collapseRanges: true, // [8, 9] and [4–6], set after a word space

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

变化

#只报告断点,不做改动

靠人工看校样的期刊可以让规则保持 'warn':Sandbox 在警告中列出每处断点,行保持断行算法原来的断法。

-  citationBreaks: 'avoid', operatorBreaks: 'avoid',
+  citationBreaks: 'warn', operatorBreaks: 'warn',

#保留默认罚分

用 10000 时字距稍紧,每个版本留下两处断点,而不是一处。

-  citationBreakPenalty: 30000, operatorBreakPenalty: 30000,

常见问题

版面警告 · citationBreak

行首的引用编号

原因. 段落中某一行以紧跟在词后的编号引用或其一部分开头:一行末尾是“authors”,下一行开头是“[12]”,或者“[14,”下面是“16]”。数字后面括号里的数字不算引用(“2016 (3)”),括号里的词也不算。从右到左的文字以及中文、日文、韩文保持原样。bodyText.citationBreaks设为'warn'时报告每一处这样的断行;设为'avoid'时,只报告断行保留下来的那些:在词距范围内没有代价低于bodyText.citationBreakPenalty的排法能避开它们。引擎把每一处记入doc.contentWarnings,附上引用、两行文字和页码。

解决. 把bodyText.citationBreaks设为'avoid':断行会给引用之前或引用内部的断行定价,并在词距允许的范围内重排段落;这个空格仍和其他空格一起伸缩。仍然保留的断行,避开它的代价高于bodyText.citationBreakPenalty,往往是因为引用相对行宽太宽:调高惩罚值以换取更松的行,或在词和引用之间写一个不换行空格(U+00A0),无论规则如何都把这一对连在一起。移植现有书籍时保持'warn':移植要重现原书的断行。 文档 →

版面警告 · operatorBreak

行首行尾的符号

原因. 段落中某一行以前后有空格的单独符号结尾或开头,即 = < > ≤ ≥ ± × ≈ ≠ ÷ − + · → 之一:一行末尾是“LR+ =”而“18.29”在下一行,或者一行以“± 0.03”开头。词之间的连字符不算符号。从右到左的文字以及中文、日文、韩文保持原样。bodyText.operatorBreaks设为'warn'时报告每一处这样的断行,每处断行一条(一行同时以符号开头和结尾会得到两条);设为'avoid'时,只报告断行保留下来的那些:在词距范围内没有代价低于bodyText.operatorBreakPenalty的排法能避开它们。引擎把每一处记入doc.contentWarnings,附上符号、所在边(行尾或行首)、两行文字和页码。

解决. 把bodyText.operatorBreaks设为'avoid':断行会给符号两侧空格处的断行定价,并在词距允许的范围内重排段落。仍然保留的断行,避开它的代价高于bodyText.operatorBreakPenalty,常见于窄栏中符号和两侧的项都较宽的情况:调高惩罚值以换取更松的行,或在符号两侧写不换行空格(U+00A0,窄的用U+202F),无论规则如何都把它连在一起。移植现有书籍时保持'warn':移植要重现原书的断行。 文档 →

  • 断点是在断好的行里查找的。改了开本、字号或文字,断点就会移动;请读每次排版的警告,不要照抄上一次的位置。
  • 要让某一处引文在任何栏宽下都挨着它的词,就在 Markdown 里它前面打一个不换行空格。文章其余部分交给这条规则。

致谢

文本
原创文字, CC BY 4.0
字体
STIX Two Text (SIL OFL 1.1) · IBM Plex Sans (SIL OFL 1.1) · IBM Plex Sans Condensed (SIL OFL 1.1)
沙盒