Skip to content

如何让引用的 label 缺失时仍然能编译?

如何让引用的 label 缺失时仍然能编译?

typst
#show ref: it => {
  if it.element == none {
    return text(fill: red, "<未找到引用" + str(it.target) + ">")
  }
  it
}

#figure(rect(), caption: "A sample figure") <fig2>
@fig1 @fig2
Typst compiled image

WARNING

以上规则会把引用的文献误判为缺失标签。目前区分这两种情况比较困难,需要自行扫描*.bibhack context机制


在 typst 0.14 等特定版本,上述代码对于 bibliography 的引用 (cite) 失效。一种解决方法是自行扫描 *.bib:

typst
#import "@preview/citegeist:0.2.2": load-bibliography

#let all-bib-entries() = {
  let bib-file = {
    let all-bib = query(bibliography)
    if all-bib.len() > 0 {
      all-bib.at(0).sources.at(0)
    } else {
      none
    }
  }
  if bib-file == none {
    (:)
  } else {
    load-bibliography(read(bib-file))
  }
}

#let possible-missing-ref(it) = {
  if str(it.target) in all-bib-entries() or it.element != none {
    it
  } else {
    text(fill: red, "<未找到引用" + str(it.target) + ">")
  }
}

#show ref: possible-missing-ref

另请参见

贡献者

The avatar of contributor named as Y.D.X. Y.D.X.
The avatar of contributor named as Wenrui Huang Wenrui Huang
The avatar of contributor named as 梦飞翔 梦飞翔
The avatar of contributor named as QuadnucYard QuadnucYard

页面历史

Comments
  • Latest
  • Oldest
  • Hottest

基于 MIT 许可发布