You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Quarto PDF中实现脚注及页码的交叉引用?

简化Quarto/Pandoc中脚注交叉引用的Lua Filter方案

人文社科、哲学类书籍里,常需要在脚注中引用其他脚注或特定页码,比如:

  • 关于此点,参见上文第235页。
  • 参见下文第25号脚注,第136页。

Quarto/Pandoc自带的HTML式锚点(如See [above]{#My_anchor}.)在PDF输出中只能显示文字,无法准确定位,完全满足不了这类需求。现有临时方案虽能兼顾HTML和PDF,但锚点名称要重复书写5次,操作冗余且易出错:

[...]

Here is a paragraph with some text.^[[And]{#My_footnote_anchor}\label{My_footnote_anchor} here is a footnote I want to refer to.]

[...] 

Now here is another portion of the text, where I deal with a subject I mentioned sometime ago.^[Regarding this, see n. \ref{My_footnote_anchor} [above](#My_footnote_anchor), p. \pageref{My_footnote_anchor}.]

核心问题

  • PDF输出:\ref{}和\pageref{}能正确渲染脚注编号与页码,但HTML中这些LaTeX命令会显示为占位符。
  • 锚点名称重复书写5次,流程繁琐,增加出错概率。

简化方案:Lua Filter

以下Lua Filter可实现锚点仅需定义1次、引用1次,自动生成适配HTML和PDF的格式:

-- footnote-crossref.lua
function Note(note)
  -- 处理脚注内的锚点定义:{#anchor}
  local anchor_def = nil
  local new_content = {}
  for _, elem in ipairs(note.content) do
    if elem.t == "Span" and elem.attributes.id then
      anchor_def = elem.attributes.id
      -- 保留原内容并自动添加LaTeX标签
      table.insert(new_content, elem)
      table.insert(new_content, pandoc.RawInline("latex", string.format("\\label{%s}", anchor_def)))
    else
      table.insert(new_content, elem)
    end
  end
  if anchor_def then
    note.content = new_content
  end
  return note
end

function Inline(inline)
  -- 处理脚注内的交叉引用:@footref{anchor}
  if inline.t == "RawInline" and inline.format == "markdown" then
    local ref_match = inline.text:match("@footref{(.-)}")
    if ref_match then
      -- 生成适配PDF的LaTeX命令与适配HTML的锚点链接
      local latex_part = pandoc.RawInline("latex", string.format("n. \\ref{%s}, p. \\pageref{%s}", ref_match, ref_match))
      local html_part = pandoc.Link("(上文)", "#" .. ref_match)
      return {latex_part, pandoc.Space(), html_part}
    end
  end
  return inline
end

使用方法

  1. 将上述代码保存为footnote-crossref.lua文件。
  2. 在Quarto项目的_quarto.yml中配置:
    filters:
      - footnote-crossref.lua
    
    或编译时直接指定:
    quarto render your-document.qmd --lua-filter=footnote-crossref.lua
    

简化后的书写方式

  • 定义脚注锚点:仅需在脚注内添加{#锚点名称},自动生成LaTeX标签:
    Here is a paragraph with some text.^[[And]{#My_footnote_anchor} here is a footnote I want to refer to.]
    
  • 引用脚注:使用@footref{锚点名称},自动生成双格式引用:
    Now here is another portion of the text, where I deal with a subject I mentioned sometime ago.^[Regarding this, see @footref{My_footnote_anchor}.]
    

输出效果

  • PDF:渲染为Regarding this, see n. 1, p. 42.(实际编号和页码由LaTeX自动生成),支持跳转。
  • HTML:显示为Regarding this, see n. \ref{My_footnote_anchor}, p. \pageref{My_footnote_anchor} (上文),其中“(上文)”为可点击的锚点链接。

内容的提问来源于stack exchange,提问作者Gustavo Laet Gomes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 22:47:35