Content5
qmd テンプレート集 — 構造化と多形式連携
Quarto qmd ファイルの相互参照・マルチフォーマット・Div/Callouts・文献管理・Front Matter 上書きのテンプレート集。
1 qmd テンプレート集 — 構造化と多形式連携
このページでは、.qmd ファイルを 構造化 し、複数の出力形式に対応 するためのテンプレートを紹介します。
相互参照、Callouts、文献管理、Front Matter 上書きなど、実践的なパターンをまとめています。
1.1 1. 相互参照テンプレート
Quarto では図・表・数式・節に自動でIDを振り、相互参照できます。
1.1.1 テンプレート
---
title: "相互参照デモ"
format: html
number-sections: true
---
# 実験結果
@fig-plot に実験結果を示します。
平均値は @tbl-stats の通りです。
```{r}
#| label: fig-plot
#| fig-cap: "実験結果の分布"
x <- rnorm(50)
hist(x, main = "実験結果")
```
: 基本統計量 {#tbl-stats}
詳細は @sec-analysis で解説します。
## 分析 {#sec-analysis}
ここに分析内容を記述します。1.1.2 実行結果
@fig-hist にデータの分布を示します。
@tbl-summary から基本統計量を確認できます。
| 統計量 | 値 |
|---|---|
| 標本サイズ | 100 |
| 平均 | 0.013 |
| 標準偏差 | 0.927 |
| 中央値 | 0.008 |
1.1.3 参照構文一覧
| 対象 | ラベル | 参照構文 | 表示例 |
|---|---|---|---|
| 図 | #| label: fig-xxx |
@fig-xxx |
図 1 |
| 表 | {#tbl-xxx} |
@tbl-xxx |
表 1 |
| 節 | {#sec-xxx} |
@sec-xxx |
節 2.1 |
| 数式 | {#eq-xxx} |
@eq-xxx |
式 1 |
| ラインブロック | {#lst-xxx} |
@lst-xxx |
リスト 1 |
1.2 2. マルチフォーマット出力
1つの .qmd ファイルから HTML / PDF / DOCX を同時に出力するテンプレートです。
1.2.1 テンプレート
---
title: "マルチフォーマットレポート"
format:
html:
toc: true
code-fold: true
theme: cosmo
pdf:
documentclass: article
geometry: margin=1in
number-sections: true
docx:
toc: true
highlight-style: github
execute:
echo: true
warning: false
---
# レポートタイトル
このファイルは HTML・PDF・DOCX の3形式で出力されます。
```{r}
#| fig-cap: "散布図"
plot(mtcars$wt, mtcars$mpg,
xlab = "Weight", ylab = "MPG",
col = "steelblue", pch = 19)
```1.2.2 形式ごとの条件分岐
Quarto では format: キーの中で、出力先ごとにオプションを個別設定できます。
---
format:
html:
# HTML でのみ折りたたみ表示
code-fold: true
pdf:
# PDF でのみフォント設定
mainfont: "Noto Serif CJK JP"
---
1.3 3. Div / Callouts / Layout
Quarto 独自のブロック要素を使って、リッチなレイアウトを実現します。
1.3.1 Callouts
::: {.callout-note}
## メモ
これはノートです。
:::
::: {.callout-warning}
## 注意
これは警告です。
:::
::: {.callout-important}
## 重要
重要な内容です。
:::
::: {.callout-tip}
## ヒント
役立つ情報です。
:::
::: {.callout-caution}
## 注意喚起
注意が必要です。
:::
1.3.2 実行結果
Callout は ::: で囲み、{.callout-種類} で種類を指定します。タイトルは ## タイトル で追加できます。
コードブロック内で二重波括弧({{r}})を使うと、Quarto の構文を実行せずに表示できます。
Callout は入れ子にできません。また、PDF 出力でもそのまま反映されます。
appearance: simple や collapse: true などの追加オプションも指定可能です。
1.3.3 Layout パネル
:::: {.grid}
::: {.g-col-6}
左カラムの内容
:::
::: {.g-col-6}
右カラムの内容
:::
::::
1.3.4 タブセット
::: {.panel-tabset}
## R
R のコード例
## Python
Python のコード例
:::
1.3.5 タブセット実行結果
コード
hist(rnorm(100), col = "steelblue", main = "R: ヒストグラム")
コード
plot(mtcars$wt, mtcars$mpg, col = "tomato", pch = 19,
xlab = "Weight", ylab = "MPG", main = "R: 散布図")
1.4 4. 文献管理 + 引用
Quarto は BibTeX / CSL-JSON 形式の文献ファイルをサポートし、@citekey で本文中に引用を挿入できます。
1.4.1 テンプレート
---
title: "文献管理デモ"
bibliography: references.bib
citation: true
link-citations: true
format: html
---
## 導入
Quarto は @quarto2024 で紹介されているように、
再現可能なドキュメント生成が可能です。
また @wickham2019 の設計思想に基づいた
ワークフローが推奨されています。
## 参考文献
1.4.2 文献ファイルの例(references.bib)
@book{wickham2019,
title = {R Packages},
author = {Hadley Wickham},
year = {2019},
publisher = {O'Reilly Media}
}
@online{quarto2024,
title = {Quarto Documentation},
author = {Posit Software},
year = {2024},
url = {https://quarto.org/docs/}
}1.4.3 このプロジェクトでの設定
現在の _quarto.yml には bibliography の設定はありません。必要に応じて追記します:
# _quarto.yml に追加
bibliography: references.bib
link-citations: true1.5 5. Front Matter 上書き
.qmd ファイル単位で _quarto.yml の設定をオーバーライドするパターン集です。
1.5.1 基本ルール
ファイルの YAML front matter に記述した値は、_quarto.yml の該当値を上書きします。
1.5.2 上書きパターン一覧
| 目的 | _quarto.yml |
.qmd Front Matter |
|---|---|---|
| テーマ変更 | theme: spacelab |
theme: darkly |
| 目次制御 | toc: true |
toc: false |
| コード表示 | code-fold: true |
code-fold: show(初期表示) |
| 実行制御 | echo: true |
echo: false / eval: false |
| 個別CSS | css: styles.css |
css: 別ファイル.css |
| 言語切替 | lang: ja |
lang: en |
| PDF専用設定 | documentclass: article |
documentclass: report |
1.5.3 実例:テーマ上書き
_quarto.yml の theme: [cosmo, theme.scss] を、front matter で個別に上書きできます。
---
# content5.qmd の front matter
format:
html:
theme: spacelab # _quarto.yml の theme を上書き
pdf:
documentclass: article
papersize: a4
---1.5.4 実例:コード実行の抑制
---
# このファイルではコードを表示するが実行しない
execute:
echo: true
eval: false
---