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机制

相关 issue:Downgrade missing references from errors to warning · Issue #4035 · typst/typst


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

typst
#import "@preview/citegeist:0.2.0": 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
What do you think?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v3.5.1

基于 MIT 许可发布