本文へスキップ
レシピ番号137

レシピ集 · 第6章 · 囲みと注

プロンプト例を囲みに組んだAIプレプリント

思考の連鎖の論文を組み直します。プロンプトとモデル出力をタブ付きの囲みに並べ、BibTeXから著者–年の引用を作り、図は論文の表の数値から描きます。

1ページ(全7ページ)

  • 英語の見本:日本語版はまだありません
  • 仕上がり215.9 × 279.4 mm
  • 2段, 段間7 mm
  • Newsreader 9.6/12.8
  • IBM Plex Mono
  • IBM Plex Sans
  • 7ページ
  • レベル
  • Postext 1.65.0
  • 組版時間698 ms
  • コード247行

かんたんな説明

有名なAI論文を短くして組み直したものです。プロンプトの例とモデルの答えはラベル付きの囲みに左右に並び、答えには正解か誤りの印が付きます。図は論文自身の数値から描いています。

できあがり

米国レターサイズで7ページの機械学習のプレプリントです。Wei et al.の Chain-of-Thought Prompting Elicits Reasoning in Large Language Models を、CC BY 4.0のarXiv版から短くして組んでいます。1ページ目には論文でいちばん知られた図を、画像として貼るのではなく囲みで組みました。左が標準のプロンプト、右が思考の連鎖のプロンプトで、プロンプトは Model Input のタブの囲みに、答えは Model Output のタブの囲みに入ります。例はIBM Plex Monoで組み、推論の部分は色付きの太字にし、誤った答えには×、正しい答えにはチェックを付けています。本文はNeurIPSの論文と同じく (Brown et al., 2020) の形で引用し、文献データは論文の参考文献をBibTeXに起こしたものです。表2と2つの図は論文が公表している数値から描き直しています。

このレシピが答える質問

  • AI論文のプロンプトとモデル出力の例を、囲みで左右に並べて組むには?
  • arXivやPubMed Centralのオープンアクセス論文を、引用・図・ライセンス表記を保ったままPostextで組み直すには?
  • APA、IEEEなどの引用スタイルで、文献を引用し参考文献を作るには?

手短な答え

script.js · 32〜56行コード全体で見る
// :::columns{count=2 breaks="4"} inside the "figure" box opens the right column at its fourth
// block; each nested box counts as one block.
const tab = (fill) => ({ fontFamily: SANS, fontSize: pt(7), fontWeight: 600,
  color: col('paper'), background: col(fill), position: 'top-left', inset: mm(3),
  height: mm(4.2), offset: mm(2.1), paddingX: mm(2.2) }); // straddles the top edge
const exemplar = (id, fill, ink, extra) => ({ id, background: col(fill),
  border: { enabled: true, color: col('rule'), width: pt(0.6) }, borderRadius: mm(2.4),
  padding: { top: mm(4.4), right: mm(3), bottom: mm(2.6), left: mm(3) },
  marginTop: mm(4.2), marginBottom: ZERO, label: tab(ink),
  body: { fontFamily: MONO, fontSize: pt(7.8), lineHeight: pt(10.4), color: col('ink'),
    boldColor: col(ink), // **…** marks the chain of thought: the highlight of the original
    textAlign: 'left', hyphenation: false, paragraphSpacing: true, firstLineIndent: ZERO },
  ...extra });
const mark = (id) => ({ icon: { kind: 'resource', resourceId: id, size: mm(5.6),
  position: 'corner', cornerSide: 'right' } }); // a badge on the top-right corner
const promptBoxes = [
  { id: 'figure', span: 'page', backgroundEnabled: false, border: { enabled: false },
    padding: mm(0), columnGap: mm(6), marginTop: ZERO, marginBottom: pt(LEAD),
    body: { fontFamily: SANS, fontSize: pt(8.4), lineHeight: pt(11.4), color: col('ink'),
      boldColor: col('accent'), textAlign: 'left', paragraphSpacing: true,
      firstLineIndent: ZERO } },
  exemplar('input', 'tint', 'accent'),
  exemplar('right', 'mint', 'green', mark('mark-right')),
  exemplar('wrong', 'mint', 'green', mark('mark-wrong')),
];

材料

種類
Newsreader, IBM Plex Sans, IBM Plex Mono (SIL OFL 1.1)
素材
なし:画像はすべてコードで描画

作り方

#1 · 図1は3つの囲みスタイルでできている

コードは上の手短な答えにあります。図全体は枠のない全幅の囲み1つです。その中で:::columns{count=2 breaks="4"}が4つ目のブロックから右の段を始め、入れ子の囲みは1つで1ブロックと数えます。プロンプトと答えの囲みは同じ関数から作るので、違うのは地の色、タブの色、角の印だけです。iconにposition: 'corner'を指定するとSVGのリソースを使えるので、文書のフォントにないチェックと×もこうしてページに載せられます。自分の論文では、例を囲みの中のふつうの段落として書き、推論の部分を**…**で示してください。スタイルのboldColorがそれをハイライトにします。

#2 · natbib風の著者–年の引用

script.js · 60〜70行コード全体で見る
registerCitationEngine(createCiteprocEngine({ styles: STYLES, locales: LOCALES }));
// Chicago author-date writes (Brown et al. 2020); ML venues put a comma before the year.
const AUTHOR_YEAR = '<group delimiter=" ">\n            <text macro="author-inline"/>';
const natbib = STYLES['chicago-author-date']
  .replace(AUTHOR_YEAR, AUTHOR_YEAR.replace('" "', '", "'));
const citations = { style: 'custom', customStyle: natbib, link: true,
  bibliography: { fontSize: em(0.8), lineHeight: pt(10.2), entrySpacing: pt(1.8),
    hangingIndent: mm(4), doi: 'text' } };
// The text's two web addresses are links: clickable in the PDF, printed in parentheses on
// paper, `[name census data](https://namecensus.com/)` as "name census data (namecensus.com)".
const links = { print: 'inline', urlDisplay: 'short' };

機械学習の学会はnatbibの著者–年方式を使います。名前と年の間にコンマを入れ、著者が3人以上なら et al. とします。同梱のスタイルでいちばん近いのはChicago author-dateなので、このレシピではCSLのうち著者と年をつなぐグループの区切りを空白からコンマに変えています。自分の.bibファイルはそのまま使えます。:::references{format=bibtex}のブロックに貼り、(Brown et al., 2020) なら[@key]、Brown et al. (2020) なら@keyで引用します。後置きは前に書いたコンマと強調を保つので、[@brown2020language, *inter alia*]は原論文と同じく (Brown et al., 2020, inter alia) と印刷されます。キャプションも同じ書き方で引用できます。図4のキャプションは@jie2022learningと@lan2021mwptoolkitを引き、どちらもnociteなしで参考文献に載ります。

本文にある二つのウェブアドレス、第5節の人名統計データと奥付のライセンスは、[name census data](https://namecensus.com/) のようにリンクとして書きます。links: { print: 'inline', urlDisplay: 'short' } を指定すると、紙面ではリンクの文字の後ろに括弧でアドレスが https:// なしで印刷され、PDF ではリンクがそのまま働きます。アドレスをただの文字として書いても印刷は同じですが、クリックしてもどこにも飛びません。

#3 · タイトルのブロック

script.js · 74〜91行コード全体で見る
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, y, width = MEASURE) => ({ anchor: { to, edge },
  offset: { x: ZERO, y: mm(y) }, size: { width: mm(width), height: 'auto' } });
const titleBlock = { enabled: true, minHeight: mm(58), slot: { elements: [
  text('venue', '{attr.venue}', MONO, 7.5, { fontWeight: 500, letterSpacing: pt(0.6),
    color: col('accent'), placement: at('container', 'top-left', 0) }),
  text('title', '{titleText}', SERIF, 23, { fontWeight: 600, lineHeight: 1.1,
    placement: at('#venue', 'below', 5) }),
  text('authors', '{attr.authors}', SANS, 9.6, { fontWeight: 500, lineHeight: 1.45,
    placement: at('#title', 'below', 6) }),
  text('affiliation', '{attr.affiliation}', SANS, 8.6, { color: col('muted'),
    placement: at('#authors', 'below', 1.2) }),
  { kind: 'rule', id: 'rule', thickness: pt(0.5), color: col('rule'),
    placement: at('#affiliation', 'below', 4) },
  text('note', '{attr.note}', SERIF, 8.2, { lineHeight: 1.35,
    color: col('muted'), placement: at('#rule', 'below', 2) }),
] } };

タイトルは文書で唯一のレベル1の見出しで、2段にまたがるスタイルがデザインしたブロックを描きます。学会名の行、タイトル、2行の著者、所属、短縮版の注記です。著者と注記は見出しの属性で、属性の中の\nはLaTeXの\authorの\\と同じように行を改めます。自分の論文では属性を書き換えるだけで、デザインはそのまま使えます。

#4 · 元の論文と同じ図番号

script.js · 95〜106行コード全体で見る
// Figure 1 is boxes, which the figure counter does not see: number the charts by type.
const numbered = (id, word, n, extra) => ({ id, name: `${word} ${n}`,
  shortLabel: `${word} ${n}`, captionPrefix: `${word} ${n}`, numberingTemplate: '',
  resetOn: 'never', counterFormat: 'decimal', ...extra });
const resourceTypes = [numbered('mark', 'Mark', ''), numbered('fig2', 'Figure', 2),
  numbered('fig4', 'Figure', 4),
  numbered('tab2', 'Table', 2, { captionStyle: { position: 'above' } })];
const ACROSS = { position: 'top', span: 'page' }; // a float over both columns
const svg = (id, typeId, file, [width, height], caption, note, altText, placement) => ({
  id, typeId, kind: 'svg', createdAt: 0, updatedAt: 0,
  placement: placement ?? { position: 'top' }, svg: { fileId: file, width, height },
  caption, note, altText });

エンジンは最初に参照された順に図に番号を付けますが、ここでの図1は囲みなのでカウンターには数えられません。そこで描き直した図にはそれぞれ専用のリソースタイプを用意し、ラベルに番号を書き込んでnumberingTemplateを空にし、arXiv版の番号(図2、図4、表2)を保っています。すべての図がリソースである自分の論文なら、numberingTemplate: '{n}'のfigureタイプ1つで番号が付きます。

#5 · 論文のデータから組む表2

script.js · 480〜508行コード全体で見る
const SETS = ['GSM8K', 'SVAMP', 'ASDiv', 'AQuA', 'MAWPS'];
const RESULTS = [ // model, size, then standard / CoT pairs per benchmark; * CoT beats standard
  ['UL2', '20B', '4.1 4.4* 10.1 12.5* 16.0 16.9* 20.5 23.6* 16.6 19.1*'],
  ['LaMDA', '420M', '2.6 0.4 2.5 1.6 3.2 0.8 23.5 8.3 3.2 0.9'],
  ['', '2B', '3.6 1.9 3.3 2.4 4.1 3.8 22.9 17.7 3.9 3.1'],
  ['', '8B', '3.2 1.6 4.3 3.4 5.9 5.0 22.8 18.6 5.3 4.8'],
  ['', '68B', '5.7 8.2* 13.6 18.8* 21.8 23.1* 22.3 20.2 21.6 30.6*'],
  ['', '137B', '6.5 14.3* 29.5 37.5* 40.1 46.6* 25.5 20.6 43.2 57.9*'],
  ['GPT', '350M', '2.2 0.5 1.4 0.8 2.1 0.8 18.1 8.7 2.4 1.1'],
  ['', '1.3B', '2.4 0.5 1.5 1.7 2.6 1.4 12.6 4.3 3.1 1.7'],
  ['', '6.7B', '4.0 2.4 6.1 3.1 8.6 3.6 15.4 13.4 8.8 3.5'],
  ['', '175B', '15.6 46.9* 65.7 68.9* 70.3 71.3* 24.8 35.8* 72.7 87.1*'],
  ['Codex', '–', '19.7 63.1* 69.9 76.4* 74.0 80.4* 29.5 45.3* 78.7 92.6*'],
  ['PaLM', '8B', '4.9 4.1 15.1 16.8* 23.7 25.2* 19.3 21.7* 26.2 30.5*'],
  ['', '62B', '9.6 29.9* 48.2 46.7 58.7 61.9* 25.6 22.4 61.8 80.3*'],
  ['', '540B', '17.9 56.9* 69.4 79.0* 72.1 73.9* 25.2 35.8* 79.2 93.3*'],
];
const cell = (content, extra) => ({ content, align: 'right', ...extra });
const tableRows = () => [
  [cell('Model', { isHeader: true, align: 'left', colSpan: 2 }), { content: '',
    hiddenBy: { row: 0, col: 0 } }, // gotcha: merged-cells-hiddenby
  ...SETS.flatMap((set, i) => [cell(set, { isHeader: true, align: 'center', colSpan: 2 }),
    { content: '', hiddenBy: { row: 0, col: 2 + 2 * i } }])],
  [cell('', { isHeader: true }), cell('', { isHeader: true }),
    ...SETS.flatMap(() => [cell('standard', { isHeader: true }), cell('CoT', { isHeader: true })])],
  ...RESULTS.map(([model, size, values]) => [cell(model ? `**${model}**` : '', { align: 'left' }),
    cell(size, { align: 'left' }),
    ...values.split(' ').map((v) => cell(v.endsWith('*') ? `**${v.slice(0, -1)}**` : v))]),
];

結果はLaTeXのソースにあるとおり一度だけ入力し、表2と図4の両方に使うので、図と表が食い違うことはありません。ベンチマーク名はそれぞれ2列にまたがります。結合したセルには、広がる位置にhiddenByを付けた埋め草のセルが要ります。太字は思考の連鎖が標準のプロンプトを上回るセルで、元の論文の緑のセルにあたります。

レシピの全体

Sandbox
// ═══ Postext Cookbook · Nº 137 · An AI preprint with prompt exemplars in boxes ═══════
// https://postext.dev/en/cookbook/ai-preprint-prompt-exemplars
// Code: MIT · Text: Wei et al. 2022, arXiv:2201.11903 (CC BY 4.0) · Charts: drawn in code
// Fonts: Newsreader, IBM Plex Sans, IBM Plex Mono (SIL OFL 1.1) · Needs postext ≥ 1.65.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerCitationEngine, registerResourceImage,
  inlineSvgFonts,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
import { createCiteprocEngine, STYLES, LOCALES } from 'https://esm.sh/postext-citeproc';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'ai-preprint-prompt-exemplars';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: a cool ink, a prompt blue, an answer green and a red for the wrong answer
const palette = { ink: '#1b1e24', accent: '#1f5fa6', green: '#21744a', red: '#b3362d',
  tint: '#e9f0f8', mint: '#e6f2eb', rule: '#b4bcc8', muted: '#5b6270', 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, MONO] = ['Newsreader', 'IBM Plex Sans', 'IBM Plex Mono'];
// US letter, two columns of 85.5 mm as ML venues print.
const [TRIM_W, TRIM_H, TOP, BOTTOM, SIDE, GUTTER] = [215.9, 279.4, 22, 22, 19, 7];
const MEASURE = TRIM_W - 2 * SIDE;
const COLUMN = (MEASURE - GUTTER) / 2;
const LEAD = 12.8; // pt
const ZERO = pt(0);

// #region answer: Figure 1 as boxes: two columns, a tab on each box, a ✓ or ✗ in the corner
// :::columns{count=2 breaks="4"} inside the "figure" box opens the right column at its fourth
// block; each nested box counts as one block.
const tab = (fill) => ({ fontFamily: SANS, fontSize: pt(7), fontWeight: 600,
  color: col('paper'), background: col(fill), position: 'top-left', inset: mm(3),
  height: mm(4.2), offset: mm(2.1), paddingX: mm(2.2) }); // straddles the top edge
const exemplar = (id, fill, ink, extra) => ({ id, background: col(fill),
  border: { enabled: true, color: col('rule'), width: pt(0.6) }, borderRadius: mm(2.4),
  padding: { top: mm(4.4), right: mm(3), bottom: mm(2.6), left: mm(3) },
  marginTop: mm(4.2), marginBottom: ZERO, label: tab(ink),
  body: { fontFamily: MONO, fontSize: pt(7.8), lineHeight: pt(10.4), color: col('ink'),
    boldColor: col(ink), // **…** marks the chain of thought: the highlight of the original
    textAlign: 'left', hyphenation: false, paragraphSpacing: true, firstLineIndent: ZERO },
  ...extra });
const mark = (id) => ({ icon: { kind: 'resource', resourceId: id, size: mm(5.6),
  position: 'corner', cornerSide: 'right' } }); // a badge on the top-right corner
const promptBoxes = [
  { id: 'figure', span: 'page', backgroundEnabled: false, border: { enabled: false },

組み立てたscript.jsはそのまま動きます。任意のページのモジュールスクリプトに貼り付けるか、レシピをCodePenで開いてください。 GitHub上のレシピのフォルダー ↗ (新しいタブで開きます)

アレンジ

#APAで引用する

APAは (Roy & Roth, 2015) と書き、著者を20人まで並べます。大規模モデルの論文の参考文献では、たいてい望まれない形です。

-const citations = { style: 'custom', customStyle: natbib, link: true,
+const citations = { style: 'apa', link: true,

#1段で組む

NeurIPS風の1段組みでは、行長を読みやすく保つために左右の余白を広げます。全幅の囲みはその1段いっぱいに入ります。

-const [TRIM_W, TRIM_H, TOP, BOTTOM, SIDE, GUTTER] = [215.9, 279.4, 22, 22, 19, 7];
+const [TRIM_W, TRIM_H, TOP, BOTTOM, SIDE, GUTTER] = [215.9, 279.4, 24, 24, 38, 7];
-  layout: { layoutType: 'double', gutterWidth: mm(GUTTER) },
+  layout: { layoutType: 'single' },

#囲みに入れたコードリスト

プロンプトにコードが入るなら、コードリストとキーのリストの囲みが太字とイタリックで構文に色を付けます。

よくあるつまずき

つまずき

入れ子の囲みはspan、placement、snapToGridを無視する

ほかの囲みの中に入れ子にした囲みは、自分のspan、placement、snapToGrid、floatBarrierを無視し、常に親の内側の幅で親の中を流れます。 入れ子の囲み →

つまずき

結合セルにはhiddenByのプレースホルダーが要る。mergeCellsを使う

セルは行の配列内の位置で配置されるため、結合セルが広がる位置にはhiddenByを付けたプレースホルダーのセルが必要です。HTMLのように省くと、それ以降の列がすべてずれます。結合はmergeCellsで作ってください。postext 1.5からは、グリッドが不ぞろいな表はdoc.contentWarningsにraggedTableGridとして報告され、parseTSV(text, { headerRows })でヘッダー行を指定できます。 データから作る表 →

  • 2段組みの全幅の囲みは、収まったうえで下に本文が2行ほど入る場合にだけ、そのページに置かれます。図1が1ページ目にあるのは、抄録を9.3ポイントで詰めた余白で組んでいるからで、抄録がもっと長いと図は2ページ目に移ります。

クレジット

レシピ
Ignacio Ferro
本文
フォント
Newsreader (SIL OFL 1.1) · IBM Plex Sans (SIL OFL 1.1) · IBM Plex Mono (SIL OFL 1.1)
SandboxPDF