脚注与参考文献
Typst 原生支持 #footnote[...]、@citation 和 #bibliography(...)。在分页 PDF 里脚注自动落在页底,在 HTML 中则需要决定放在文末还是宽屏侧边栏,多页站点还要隔离每篇文档的编号与文献范围。
文末脚注
Typst HTML 导出默认会在文档末尾收集 #footnote[...] 并生成相互跳转的锚点。但在同一个 Bundle 中连续渲染多篇 #document(...) 时,需要在每篇文档开头清零脚注计数器,避免编号跨文章累加:
#document(page.output, format: "html", title: page.meta.title)[
#counter(footnote).update(0)
#html.main[
#include page.file
]
]如果站点使用了自定义 #html.html 壳,想把脚注严格放进 <article> 内部(而不是落在 <footer> 后面),或者自定义脚注的 HTML 结构,可以自己在正文末尾用 query(selector(footnote).within(current-document().location)) 收集并渲染:
#import "@tola/document:0.0.0": current-document
#let render-endnotes() = context {
let doc = current-document().location
let notes = query(selector(footnote).within(doc))
if notes.len() == 0 { return }
html.section(class: "footnotes", aria-label: "脚注")[
#html.hr()
#html.ol[
#for (i, note) in notes.enumerate() {
let num = str(i + 1)
html.li(id: "fn-" + num)[
#note.body
#html.a(href: "#fnref-" + num, class: "fn-back", aria-label: "返回正文")[↩]
]
}
]
]
}旁注
技术博客和学术笔记(类似 Tufte CSS、tufted 或 thyme 风格)常在宽屏把注释直接放在段落右侧留白里,窄屏则自动折叠成行内展开块,读者不用来回跳页底。
在 HTML 模式下用 show footnote 把每个脚注改写成编号标记加旁注容器,PDF 模式下原样保留:
#let sidenote-counter = counter("sidenote")
#let reset-sidenotes() = sidenote-counter.update(0)
#let sidenote-rule(note) = context {
if target() != "html" { return note }
sidenote-counter.step()
let n = sidenote-counter.get().first()
let id = "sn-" + str(n)
html.span(class: "sidenote-wrapper")[
#html.elem("label", attrs: (for: id, class: "sidenote-number"), str(n))
#html.elem("input", attrs: (type: "checkbox", id: id, class: "sidenote-toggle"))
#html.span(class: "sidenote")[
#html.span(class: "sidenote-prefix")[#n. ]
#note.body
]
]
}.sidenote-toggle { display: none; }
.sidenote-number {
font-size: 0.75em;
vertical-align: super;
cursor: pointer;
color: var(--accent);
}
/* 宽屏:悬浮到正文右侧栏 */
@media (min-width: 1120px) {
.sidenote {
float: right;
clear: right;
width: 16rem;
margin-right: -18rem;
margin-bottom: 0.75rem;
font-size: 0.85rem;
line-height: 1.5;
color: var(--muted);
}
}
/* 窄屏:默认收起,点击角标展开 */
@media (max-width: 1119px) {
.sidenote { display: none; }
.sidenote-toggle:checked + .sidenote {
display: block;
margin: 0.5rem 0;
padding: 0.6rem 0.85rem;
border-left: 2px solid var(--border);
font-size: 0.875rem;
}
}这套方案不依赖 JavaScript,也不改变作者的写作方式——正文里照常写 #footnote[补充说明],导出 PDF 时仍是标准页底脚注,导出网页时自动呈现为旁注。
参考文献
Typst 原生解析 BibTeX(.bib)和 Hayagriva(.yml)文献库。在文档正文里直接写 @key(或 #cite(<key>)),并在文档末尾挂载 #bibliography(...):
// content/posts/incremental-builds.typ
关于增量计算模型,见 @salsa2022 与 @typst2023。
#bibliography("/data/refs.bib", title: "参考文献", style: "ieee")将 #bibliography(...) 放在单篇文档内部,不要放在 site.typ 顶层,避免各文章编号互相干扰。若想由模板统一追加但跳过不含引用的文章,先检查当前文档内是否存在 cite 元素:
#import "@tola/document:0.0.0": current-document
#let maybe-bibliography(bib-path, style: "ieee") = context {
let doc = current-document().location
if query(selector(cite).within(doc)).len() > 0 {
bibliography(bib-path, title: "参考文献", style: style)
}
}页面标题 <label> 与 .bib 文献键共享 @... 语法,给文献键加上固定前缀(或采用作者年份格式)可避免和章节标签冲突。