かんたんな説明
生物の教科書の1章で、注意や実験メモなど6種類の囲みがあります。種類ごとに色と記号が決まっているので、生徒はひと目で見分けられます。
できあがり
高校の生物の教科書『Living Matter』の第2章を、210 × 280 mmのページに組みます。章はページの天の角で断ち切られた細胞の図の下で始まり、細胞の核に章番号が入ります。その後に3色6種類の囲みが続き、それぞれが独自の目印を付けているので、生徒は読む前に目標と注意を見分けられます。マゼンタは、的のアイコンを入れた帯に載せる学習目標と、枠の外にチェックの入ったタイルを置く確認問題に使います。緑は、電球のバッジを頭に載せた細い罫を添える学習のヒントと、3つの安全のピクトグラムを付けた実験の囲みに使います。注意は黄色で、外側の角に注意のバッジを掛けます。BOX 2.1は、黒いタブとフラスコの下にラベンダーの地色を敷きます。太字の用語はその囲みの色になり、BOX 2.1ではマゼンタです。
このレシピが答える質問
- アイコン、色付きの帯、角丸を備えた注記・ヒント・警告の囲みを作るには?
- 番号付きのタブ(「BOX 1-1」)、角のアイコン、罫線付きの余白のアイコンを加えるには?
- 本文や囲みの中で、キーワード(太字やイタリック)に色を付けるには?
手短な答え
// box(id, hue, device) is the shared base: the hue colours the title, bullets and key terms.
const TAB = 5, BADGE = 7.4; // in mm: the tab's height (and offset), the warning badge's size
const calloutStyles = [
// The first style is also the one a missing or misspelt type falls back to.
box('objectives', 'band', { title: t({ en: 'In this chapter', es: 'En este capítulo' }),
background: col('tintBand'), stripe: { enabled: true, width: mm(7.5), color: col('band') },
icon: icon('target', 5) }), // on a side stripe (left by default) the icon is centred on it
box('feature', 'band', { background: col('mist'),
titleStyle: { fontFamily: 'Lexend', fontSize: pt(10.5), color: col('ink'), gap: mm(1.6) },
// Printed only where the fence has label="…": the tab, the flask beside it, a rule to them.
label: { fontFamily: LABEL, fontSize: pt(8), color: col('paper'), background: col('ink'),
position: 'top-left', // this verso's outer corner, above the badge ('top-right' by default)
height: mm(TAB), offset: mm(TAB), paddingX: mm(2.3), // offset = height: on the top edge
icon: { resourceId: 'flask', width: mm(4.4), gap: mm(1.2) },
rule: { enabled: true, color: col('ink'), width: pt(1.2) } } }),
box('warning', 'warnInk', { background: col('tintWarn'), borderRadius: mm(1.4),
border: { enabled: true, color: col('warn'), width: pt(0.75) },
// The badge hangs half past the outer corner: sides of half badge + GAP align the title.
padding: { top: mm(2.8), right: mm(BADGE / 2 + GAP), bottom: mm(3.2),
left: mm(BADGE / 2 + GAP) },
icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'outer' }) }),
]; // straight into config(), followed by the 'more' styles
材料
- 機能
- 囲み番号付きの囲みのタブ囲みのアイコンと角のバッジ囲みの横のマーカー段囲みの中や本文中の段組み段抜きの囲みフロートする囲み太字・イタリックとその色セマンティックカラーパレットデザインした章扉段抜きの章見出し帯ページデザインの画像見出しの属性タイトル内の改行柱とノンブル見開きの余白リソースとしての図と表図を配置する参照図の配置キャンバス上のページ
- 種類
- Noto Serif, Lexend, Barlow Semi Condensed (SIL OFL 1.1)
- 素材
- なし:画像はすべてコードで描画
作り方
#1 · 囲みの種類ごとに色を決める
const palette = { ink: '#1f2430', paper: '#ffffff', // text; type on stripes and tabs
band: '#ab2e78', tintBand: '#fae3ef', // the chapter's magenta: opener, objectives, self-check
tip: '#25734a', tintTip: '#e2f0e7', // study tips and lab work
warn: '#e39422', tintWarn: '#fcefd8', warnInk: '#94540a', // frame and badge; tint; its type
mist: '#e8e3f0', rule: '#dccfd8', muted: '#675e66' }; // feature box; lines in drawings; heads
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the band, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.band })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
6種類の囲みが3つの色相を分け合います。マゼンタは目標と確認問題、緑は学習のヒントと実験、黄色は注意です。BOX 2.1はラベンダーの地色で、キーワードにはマゼンタを使います。各色相には地色用の淡いティントがあり、色の設定はすべてパレットの項目にリンクしているので、色相を変えるときは値をひとつ書き換えるだけです。この大きさの文字には4.5:1のコントラストが必要です。ラベンダーの上のマゼンタは4.9:1ありますが、注意の地色の上の黄色は2.2:1しかありません。そこで注意の囲みは、枠とバッジに黄色を使い、タイトルとキーワードは5.2:1の暗い黄色warnInkで組みます。
#2 · それ以外はすべて共有する
const box = (id, hue, device) => ({ id, marginTop: pt(LEAD), marginBottom: pt(LEAD / 2),
padding: { top: mm(2.8), right: mm(3.4), bottom: mm(3.2), left: mm(3.4) },
titleStyle: { fontFamily: LABEL, fontSize: pt(8.5), color: col(hue), // bold by default
textTransform: 'uppercase', letterSpacing: pt(1.4), gap: mm(GAP) },
body: { fontFamily: LABEL, fontSize: pt(9.6), lineHeight: pt(12.6), color: col('ink'),
boldColor: col(hue), textAlign: 'left', firstLineIndent: pt(0), paragraphSpacing: true },
lists: { color: col(hue), gap: mm(2.2), itemSpacing: pt(2.4) }, // bullets and numbers
...device }); // the kind's title, fill and device: its spread wins over the base
// An SVG or bitmap resource, fitted into a square of that size (width × size with `width`).
const icon = (id, size, more) => ({ kind: 'resource', resourceId: id, size: mm(size), ...more });
box()には、すべての囲みが共有する設定をまとめます。上下のアキ、内側余白、タイトルと本文の文字、リストの間隔です。引数のhueは、タイトル、箇条書きの記号と番号、太字の部分に色を付けます。body.boldColorにより、目標の囲みではfluid mosaic modelがマゼンタに、注意の囲みではcell wallが黄褐色になります。本文中の太字は墨のままです(囲みの中の文字組み)。calloutStylesを宣言すると、エンジンが標準で持つ唯一のスタイル、灰色の囲みnoteが置き換えられます。typeがないか綴りを誤った囲みは最初のスタイルになるので、どの章にもある目標をリストの先頭に置きます。
#3 · 帯、タブ、バッジ
コードは前出の手短な答えにあります。目標の帯は幅7.5 mm、的のアイコンは5 mmなので、アイコンは左右に余裕を残して色の中に収まります。タブのoffsetをheightと同じにすると、タブは囲みの上辺に立ち、タイトルにかかりません。'top-left'はこの偶数ページの外側の角です。フラスコはタブの後に続き、罫はフラスコから上辺に沿って反対の角まで伸びます。position: 'corner'は注意のバッジを幅の半分だけ辺の外に掛け、cornerSide: 'outer'はバッジを小口側、つまりこの偶数ページでは左に送ります。その側では、内側余白がバッジの半分とタイトルの間隔をすでに覆っていない限り、エンジンがタイトルを字下げしてバッジを避けます。そこで注意の囲みは両側にBADGE / 2 + GAPの内側余白を取り、タイトルを本文にそろえます。borderRadiusを指定しているのはこの囲みだけです。枠線のある囲みはこれひとつで、1.4 mmの角丸が四隅をやわらげます。帯のある囲みに角丸を付けると、帯の外側の角も一緒に丸くなります(囲みスタイル)。
#4 · さらに3つの目印
const BULB = 6.4; // the study tip's badge in mm: the text clears it
const moreStyles = [
box('tip', 'tip', { title: t({ en: 'Study tip', es: 'Para recordar' }),
backgroundEnabled: false, stripe: { enabled: true, width: pt(2.5), color: col('tip') },
// A stripe narrower than its icon: the badge sits on it at the top and hides it there.
padding: { top: mm(0.6), right: mm(0), bottom: mm(0.6), left: mm(BULB / 2 + 2) },
icon: icon('bulb', BULB) }),
box('safety', 'tip', { background: col('tintTip'), marginTop: pt(4), // under its heading
icon: icon('safety', 7, { width: mm(24.5), align: 'center' }) }), // 3 pictograms, 1 image
box('check', 'band', { background: col('tintBand'),
// Outside the frame: [marker][rule][gap][box], the rule as tall as the box.
marker: { kind: 'resource', resourceId: 'check', size: mm(7), align: 'top', gap: mm(3),
rule: { enabled: true, color: col('band'), width: pt(0.75) } } }),
];
学習のヒントの帯は幅2.5 ptで、6.4 mmの電球のバッジに対して細い罫として刷られ、その上端にバッジが載ります。左の内側余白をバッジの半分に2 mmを足した値にして、本文を電球から離します。icon.widthは画像を正方形ではなく横長の枠に収めます。これで1枚の24.5 mmの画像として描いた3つの安全のピクトグラムが、実験の囲みのアイコン列になり、本文に対して中央に置かれます。確認問題の目印は枠の外にあり、目印、罫、間隔、囲みの順に並びます。0.75 ptの罫に載せた四角いタイルが、丸いバッジの付いた学習のヒントとの違いを示します。開始行にspan="page" placement="bottom"を加えて、確認問題を29ページの地にフロートとして配置します。
#5 · アイコンはリソース
const svgResource = (id, width, height, extra) => ({ id, typeId: 'figure', kind: 'svg',
svg: { fileId: `${id}.svg`, width, height }, createdAt: 0, updatedAt: 0, ...extra });
const pageTop = { position: 'top', span: 'page' }; // a 'top' float opens the page after its :ref
const resources = [
// Uncited, so never placed as figures: the box styles and the opener use them by id.
...['target', 'bulb', 'caution', 'check'].map((id) => svgResource(id, 240, 240)),
svgResource('flask', 200, 240), svgResource('safety', 760, 240), svgResource('cell', 2000, 2000),
svgResource('mosaic', 3480, 1240, { placement: pageTop, caption: t({
en: 'The fluid mosaic: magenta phospholipids, green proteins, an amber carrier with its '
+ 'glucose, grey cholesterol, amber sugars.', // one line: the page is wide
es: 'El mosaico fluido: fosfolípidos magenta, proteínas verdes, transportadora ámbar con su '
+ 'glucosa, colesterol gris, azúcares ámbar.' }),
altText: t({ en: 'A cell membrane in section', es: 'Una membrana celular en sección' }) }),
svgResource('fusion', 1800, 500, { placement: { position: 'here' }, caption: t({
en: 'Mouse proteins in green, human proteins in magenta (the red dye): the two cells, the '
+ 'hybrid just after fusion and the same hybrid 40 minutes later.',
es: 'En verde, las proteínas de ratón; en magenta, las humanas (el colorante rojo): las dos '
+ 'células, el híbrido recién fusionado y el mismo híbrido 40 minutos después.' }),
altText: t({ en: 'Two cells fusing into a hybrid', es: 'Dos células que se fusionan' }) }),
svgResource('osmosis', 3480, 860, { placement: pageTop, caption: t({
en: 'Red blood cells in a hypertonic, an isotonic and a hypotonic solution. Arrows show the '
+ 'net flow of water.',
es: 'Glóbulos rojos en una disolución hipertónica, una isotónica y una hipotónica. Las flechas '
+ 'indican el flujo neto de agua.' }),
altText: t({ en: 'Blood cells in three solutions', es: 'Glóbulos rojos en tres medios' }) }),
];
すべてのアイコン、タブの横のフラスコ、章扉の細胞は、コンテンツと一緒に宣言してファイルidで登録したSVGのリソースです。スタイルはリソースidでそれらを指定し、本文からは引用しないので、どれも番号付きの図にはなりません。✓や☞のようなグリフのほうが短く書けますが、ページが読み込むFontsourceのlatinファイルにはこれらがありません。Canvasはシステムフォントから借りて描き、PDFでは消えてしまいます。図にも文字は入れていません。色はキャプションで説明するので、同じ図を両方の版で使えます。
#6 · 細胞で章を始める
const text = (id, content, family, size, color, placement, extra) => ({ kind: 'text', id,
content, fontFamily: family, fontSize: pt(size), color: col(color), placement,
align: 'left', ...extra }); // an opener's texts wrap by default
const below = (id, y, width) => ({ anchor: { to: `#${id}`, edge: 'below' },
offset: { y: mm(y) }, size: { width: mm(width) } });
const chapter = t({ en: 'Chapter {chapterNumber}', es: 'Capítulo {chapterNumber}' });
const opener = { enabled: true, minHeight: mm(66), slot: { elements: [
{ kind: 'image', id: 'cell', resourceId: 'cell', placement: { anchor: { to: 'page',
edge: 'top-right' }, offset: { x: mm(58), y: mm(-71) }, size: { width: mm(150) } } },
text('numeral', '{chapterNumber}', 'Lexend', 118, 'paper', { anchor: { to: 'page',
edge: 'top-right' }, offset: { x: mm(-OUTER), y: mm(14) } },
{ fontWeight: 800, lineHeight: 1, align: 'right' }),
text('kicker', `${chapter} · {attr.unit}`, LABEL, 9, 'band', { anchor: { to: 'container',
edge: 'top-left' }, offset: { y: mm(3) } },
{ fontWeight: 700, letterSpacing: pt(1.8), textTransform: 'uppercase' }),
text('title', '{titleText}', 'Lexend', 34, 'ink', below('kicker', 2.5, 112),
{ fontWeight: 700, lineHeight: 1.04 }),
text('lead', '{attr.lead}', 'Noto Serif', 10.5, 'ink', below('title', 5, 98),
{ italic: true, lineHeight: 1.42 }),
] } };
細胞は、ページの右上の角に固定して角の外まで押し出した画像要素です。そのため見えるのはおもに左下の4分の1で、核に数字が入ります。どちらもminHeightが章扉のために確保する66 mmの中に収まります。キッカーとリードは見出しの行# Cells and \\ Membranes {unit="The cell" lead="…"}から取り、\\は章扉でだけタイトルを改行します。
レシピの全体
// ═══ Postext Cookbook · Nº 008 · A family of textbook boxes ═══════════════════════
// https://postext.dev/en/cookbook/textbook-box-family
// Code: MIT · Text: original (CC BY 4.0) · Drawings and icons: generated in code (CC BY 4.0)
// Fonts: Noto Serif, Lexend, Barlow Semi Condensed (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'textbook-box-family';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: three hues for six kinds of box; everything links to an entry
const palette = { ink: '#1f2430', paper: '#ffffff', // text; type on stripes and tabs
band: '#ab2e78', tintBand: '#fae3ef', // the chapter's magenta: opener, objectives, self-check
tip: '#25734a', tintTip: '#e2f0e7', // study tips and lab work
warn: '#e39422', tintWarn: '#fcefd8', warnInk: '#94540a', // frame and badge; tint; its type
mist: '#e8e3f0', rule: '#dccfd8', muted: '#675e66' }; // feature box; lines in drawings; heads
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the band, so nothing prints blue.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.band })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
// Label face (box text and titles, tabs, captions, heads); leading in pt; outer margin in mm
const LABEL = 'Barlow Semi Condensed', LEAD = 13.4, OUTER = 16, GAP = 1.5; // GAP: title gap, mm
// #region base: what every box shares; each kind brings its colour and its one device
const box = (id, hue, device) => ({ id, marginTop: pt(LEAD), marginBottom: pt(LEAD / 2),
padding: { top: mm(2.8), right: mm(3.4), bottom: mm(3.2), left: mm(3.4) },
titleStyle: { fontFamily: LABEL, fontSize: pt(8.5), color: col(hue), // bold by default
textTransform: 'uppercase', letterSpacing: pt(1.4), gap: mm(GAP) },
body: { fontFamily: LABEL, fontSize: pt(9.6), lineHeight: pt(12.6), color: col('ink'),
boldColor: col(hue), textAlign: 'left', firstLineIndent: pt(0), paragraphSpacing: true },
lists: { color: col(hue), gap: mm(2.2), itemSpacing: pt(2.4) }, // bullets and numbers
...device }); // the kind's title, fill and device: its spread wins over the base
// An SVG or bitmap resource, fitted into a square of that size (width × size with `width`).
const icon = (id, size, more) => ({ kind: 'resource', resourceId: id, size: mm(size), ...more });
// #endregion
// #region answer: three devices: an icon on a wide stripe, a numbered tab, an outer badge
// box(id, hue, device) is the shared base: the hue colours the title, bullets and key terms.
const TAB = 5, BADGE = 7.4; // in mm: the tab's height (and offset), the warning badge's size
const calloutStyles = [
// The first style is also the one a missing or misspelt type falls back to.
box('objectives', 'band', { title: t({ en: 'In this chapter', es: 'En este capítulo' }),
background: col('tintBand'), stripe: { enabled: true, width: mm(7.5), color: col('band') },
icon: icon('target', 5) }), // on a side stripe (left by default) the icon is centred on it
box('feature', 'band', { background: col('mist'),
組み立てたscript.jsはそのまま動きます。任意のページのモジュールスクリプトに貼り付けるか、レシピをCodePenで開いてください。 GitHub上のレシピのフォルダー ↗ (新しいタブで開きます)
アレンジ
#囲みを両段にまたがらせる
開始行にspan="page"を付けると、どの種類の囲みも両段にまたがって組まれます。BOX 2.1はさらに:::columnsのグループを入れ子にして、図を本文の横に置いています。
-:::callout{type="warning" title="Membrane or wall?"}
+:::callout{type="warning" title="Membrane or wall?" span="page"}#バッジをのど側に掛ける
'inner'はバッジをのど側、つまり奇数ページでは左、偶数ページでは右に移します。囲みの外に掛かる半分のために、その側に場所を空けておいてください。
- icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'outer' }) }),
+ icon: icon('caution', BADGE, { position: 'corner', cornerSide: 'inner' }) }),#タブを右の角に立てる
既定値の'top-right'はラベル全体を左右反転させます。タブは右の角に、フラスコはその左に移り、罫は左から伸びます。
- position: 'top-left', // this verso's outer corner, above the badge ('top-right' by default)
+ position: 'top-right',よくあるつまずき
つまずき
Fontsourceのファイルは文字を収め、記号は収めない
キットは各書体のFontsourceのlatinファイルを埋め込み、本文にč、ł、†があればlatin-extファイルを、α、χがあればgreekファイル(それを持つファミリーのみ。LoraとGelasioにはない)を、画面でもPDFでも加えます。→、≈、✓、★などの記号はどのファイルにもなく、PDFでは欠けます。図として描くか、数式で書くか、それを収めたファイルを持つ書体を選んでください。 PDFに埋め込むフォント →
つまずき
'top'のフロートは参照ページの先頭を許さない限り次のページに置かれる
既定では、フロートが自分の参照より上に置かれることはありません。そのため、Nページで参照したページ幅の'top'フロートはN+1ページの頭に置かれます。リソースまたはその種類のdefaultPlacementにplacement.citingPage: trueを設定すると(本全体ならlayout.floatsAtCitingPage、postext 1.25から)、layout.maxTopFraction以内に収まり参照行がそのページに残る場合は代わりにNページの先頭に置かれ、参照より前の本文はその下に送られます。章の最初のページと強制改ページの後のページの先頭には置かれません。その場合は次のページに置かれ、'auto'または'bottom'なら参照したページの下端に入れられます。 図の配置 →
つまずき
1ページは奇数ページ。ページは物理的な番号で計画する
1ページは右ページで、2ページが最初の偶数ページです。見開きは物理的なページ番号で計画してください。偶数ページの章扉は、その次の奇数ページと向かい合います。 改ページと改段 →
レイアウト警告 · unknownCalloutType
不明な囲みの種類
原因. :::calloutが、calloutStylesに定義されていないtypeを指定しているため、エラーにならずに最初のスタイルが使われます。
対処. スタイルを定義するか、type属性を修正してください。 ドキュメント →
- タブが刷られるのは、開始行に
label="BOX 2.1"があり、スタイルにlabelオブジェクトがある場合だけです。labelオブジェクトのないスタイルはこの属性を無視します。 - Postextは囲みに番号を振りません。ラベルは開始行に書いた文字列そのものなので、BOX 2.1、BOX 2.2と番号を一つひとつ手で打ちます。
- コーナーバッジは囲みの辺から幅の半分だけはみ出し、ページの外側では余白に掛かります。同じページのもう一方の段では段間に掛かるので、バッジ付きの囲みは外側の段に置くか、場所を空けておいてください。
- 囲みの
lists.colorは、箇条書きの記号と番号付きリストの番号の両方に色を付けます。ここでは番号の付いた手順と問いが、それぞれの囲みの色で刷られます。囲みの記号だけに色を付けたいときは、そのスタイルにlists.colorを書かず、文書のunorderedLists.colorを設定します。番号はorderedLists.colorのまま残ります。
クレジット
- 本文
- 書き下ろしの文章, CC BY 4.0
- フォント
- Noto Serif (SIL OFL 1.1) · Lexend (SIL OFL 1.1) · Barlow Semi Condensed (SIL OFL 1.1)


