如何在四开PDF中交叉引用脚注(和页码)?

huangapple go评论61阅读模式
英文:

How to cross-reference footnotes (and page numbers) in quarto pdf?

问题

在一本书中,使用脚注来引用文本的其他部分是相当正常的,无论是脚注还是实际页面,您都可以在特定位置引用一些您正在讨论的主题。例如,您可以编写一个脚注,如*“关于此,请参见上文,第235页。”“见下文,注释25,第136页。”*

这与放置类似HTML的锚点(例如见[上面]{#My_anchor})不同,因为虽然这在渲染的HTML输出中有效,但在PDF中却没有意义,因为它只会导致“见上文。”看上去上文在哪里呢?- 这就是问题所在。

在我看来,这似乎可以像Quarto中的其他交叉引用类型(如表格、章节、图表等)一样工作。您可以有一种特殊的方式来引用锚点,并在tex文件中以适当的方式呈现它们。

总之,我想出了一种解决我的特定问题的变通方法,但也许有人可以想出一个Lua过滤器或类似的东西,以简化这个过程,因为我的变通方法似乎太啰嗦了。

所以这是我正在做(并实现的)的一个示例:

[...]

这里是一个带有一些文本的段落。^[And]{#My_footnote_anchor}\label{My_footnote_anchor} 这里是我想要引用的脚注。

[...]

现在这里是文本的另一部分,我在之前提到了一个主题。^[关于此,请参见注释\ref{My_footnote_anchor} [上文](#My_footnote_anchor),第\pageref{My_footnote_anchor}页。]

结果有两种不同的结果。在HTML中,我得到:

> [...]
>
> 这里是一个带有一些文本的段落。1
> _________________
> 1 这里是我想要引用的脚注。
>
> [...]
>
> 现在这里是文本的另一部分,我在之前提到了一个主题。2
> _________________
> 2 关于此,请参见注释 ??? 上文,第 .

有趣的是HTML生成器不会忽略\ref{}原始的LaTeX命令。我只能猜想这可能与其他某些命令冲突。无论如何,链接有效,由于HTML不是我的最终格式,所以“n. ???”和“p. ”保留下来也无所谓,只要“上面”的链接有效(它确实有效),以便我可以用它来检查交叉引用是否确实有效。

现在,在PDF中我得到了我需要的结果。有人指出“上面”是不必要的,这是正确的,但它也不是不正确的,所以它并不困扰我:

> [... p. 25]
>
> 这里是一个带有一些文本的段落。1
>
> _________________
> 1 这里是我想要引用的脚注。
>
> [... p. 56]
>
> 现在这里是文本的另一部分,我在之前提到了一个主题。2
>
> _________________
> 2 关于此,请参见注释 1 上文,第 25 页。

所以,整个问题在于,我必须写5次My_footnote_anchor,而更合理的是只写2次,一次用于锚点,另一次用于交叉引用。而且这种交叉引用在我的领域(哲学和人文科学)中更频繁,至少比Quarto/Pandoc中提供的所有其他交叉引用类型的总和更频繁。我想知道为什么它被如此忽视,尤其是考虑到它在LaTeX中实际上运作得非常好。

英文:

In a book, it is quite normal to use footnotes to refer to other portions of the text, either a footnote or actual pages where you discussed some topic you are referring to at a given point. You could, for instance, write a footnote saying “Regarding this, see above, p. 235.” or “See below, n. 25, p. 136.”

This is different than putting an HTML-like anchor (like See [above]{#My_anchor}.), because, while this works in a rendered html output, it doesn’t mean anything in a PDF, as it will result merely in “See above.” See above where? – That’s the point.

It seems to me this might work as the other cross-ref types in Quarto, such as tables, sections, figures, etc. You could have some special way to refer to anchors and have them rendered the appropriate way in the tex file.

Anyhow, I came up with a workaround, that solved my particular problem, but perhaps someone could figure out a Lua filter or something like that to streamline the process a bit, for my workaround seems too prolix as it is.

So here is an example of what I’m doing (and achieving):

[...]

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}.]

The result has two different results. In HTML I get:

> [...]
>
> Here is a paragraph with some text.<sup>1</sup>
> _________________
> <sup>1</sup> And 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.<sup>2</sup>
> _________________
> <sup>2</sup> Regarding this, see n. ??? above, p. .

What is intriguing is the fact that the HTML generator does not ignore the \ref{} raw latex command. I can only guess there is some conflict with some other command. Anyhow, the link works, and since HTML is not my final format, I don’t care for the “n. ???” and “p. ” that remain there as long as the link in “above” works (and it does work), so that I can use it to check whether the cross-reference is actually working.

Now, in the PDF I get what I need. Someone pointed out that the “above” is unnecessary, which is true, but, then, it is not incorrect either, so it doesn’t bother me:

> [... p. 25]
>
> Here is a paragraph with some text.<sup>1</sup>
>
> _________________
> <sup>1</sup> And here is a footnote I want to refer to.
>
> [... p. 56]
>
> Now here is another portion of the text, where I deal with a subject I mentioned sometime ago.<sup>2</sup>
>
> _________________
> <sup>2</sup> Regarding this, see n. 1 above, p. 25.

So, the whole point here is that I’m having to write My_footnote_anchor 5 times, whereas it would be more reasonable two write it only 2 times, one for the anchor, another for the cross-reference. And this is a kind of cross-ref that is more frequent – at least in my field of work (Philosophy and Human Sciences) – than the sum of all the other cross-ref types available in Quarto/Pandoc. I wonder why it has been so neglected, even more considering that it actually works quite well in LaTeX.

答案1

得分: 0

我可以提供一个仅适用于 PDF 格式的解决方案。

根据我的理解,您正在尝试生成与以下 Quarto 片段相同的内容,

---
title: Cross-Referencing a Footnote
format: pdf
include-in-header: 
  text: |
    \usepackage{lipsum}
---

\lipsum[1-2]

### first footnote

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

\lipsum[1-7]

### Referencing to that first footnote

Now here is another portion of the text, where I deal with a subject I
mentioned sometime ago.^[Regarding this, See n. \ref{footnote1}, p. \pageref{footnote1}]

\lipsum[1-3]

生成的输出片段如下,

<hr>

[![first footnote][1]][1]

<hr>

[![referencing to the first footnote within a second footnote][2]][2]

<hr>

[![second footnote][3]][3]

<hr>

Now we can write up a Lua filter to make the process less repetitive,

**footref.lua**

function Span(el)
  local id = el.identifier:gsub("fref-", "")
  table.insert(
    el.content, 
    pandoc.RawInline('latex', '\\label{' .. id .. '}')
    )
  return el
end

function RawInline(el)
  local fid = el.text:match("\\footref{(.*)}")
  local ref_text = {
    pandoc.Str('See n.'),
    pandoc.Space(),
    pandoc.RawInline('latex', '\\ref{' .. fid .. '}'),
    pandoc.Str(', p.'),
    pandoc.Space(),
    pandoc.RawInline('latex', '\\pageref{' .. fid .. '}')
  }
  return ref_text
end

首先,在第一个脚注中使用格式 #fref-<footnote-label> 创建一个锚点,然后要从其他地方引用此脚注,使用 footref{<footnote-label>}要创建锚点,您需要在其前面加上 fref-,但要引用它,只需使用 <footnote-label>

footref 将生成输出如 n. <footnote-no>, p. <page-no>

---
title: Cross Referencing a footnote
format: pdf
include-in-header:
  text: |
    \usepackage{lipsum}
filters:
  - footref.lua
---

\lipsum[1-2]

### first footnote

Here is a paragraph with some text.^[And here is a footnote I want to refer to.]

\lipsum[1-7]

### Referencing to that first footnote

Now here is another portion of the text, where I deal with a subject I mentioned sometime ago.^[Regarding this, See \footref{footnote1}]

生成的输出将与上述内容完全相同。但请注意,这仅适用于 PDF 格式,不适用于 HTML

此外,如果您想要创建带有单词的链接到该脚注,可以按照以下方式操作,

Regarding this, See \footref{footnote1} [above](#fref-<footnote-label>)

其中 1, 2, 3 是图片链接的标签。

英文:

I can offer a solution that works just for pdf format.

So as per my understanding, you are trying to do the same as the following Quarto snippet generates,

---
title: Cross-Referencing a Footnote
format: pdf
include-in-header: 
  text: |
    \usepackage{lipsum}
---

\lipsum[1-2]

### first footnote

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

\lipsum[1-7]

### Referencing to that first footnote

Now here is another portion of the text, where I deal with a subject I
mentioned sometime ago.^[Regarding this, See n. \ref{footnote1}, p. \pageref{footnote1}]

\lipsum[1-3]

Snippets of generated output,

<hr>

如何在四开PDF中交叉引用脚注(和页码)?

<hr>

如何在四开PDF中交叉引用脚注(和页码)?

<hr>

如何在四开PDF中交叉引用脚注(和页码)?

<hr>

Now we can write up a Lua filter to make the process less repetitive,

footref.lua

function Span(el)
  local id = el.identifier:gsub(&quot;fref%-&quot;, &quot;&quot;)
  table.insert(
    el.content, 
    pandoc.RawInline(&#39;latex&#39;, &#39;\\label{&#39; .. id .. &#39;}&#39;)
    )
  return el
end

function RawInline(el)
  local fid = el.text:match(&quot;\\footref{(.*)}&quot;)
  local ref_text = {
    pandoc.Str(&#39;See n.&#39;),
    pandoc.Space(),
    pandoc.RawInline(&#39;latex&#39;, &#39;\\ref{&#39; .. fid .. &#39;}&#39;),
    pandoc.Str(&#39;, p.&#39;),
    pandoc.Space(),
    pandoc.RawInline(&#39;latex&#39;, &#39;\\pageref{&#39; .. fid .. &#39;}&#39;),
  }
  return ref_text
end

Here at first, you create an anchor inside the first footnote using the format #fref-&lt;footnote-label&gt; and then to refer to this footnote from other places, use footref{&lt;footnote-label&gt;}. So to create the anchor you need to prefix it with fref-, but to refer to it, just use the &lt;footnote-label&gt;.

footref will generate output as n. &lt;footnote-no&gt;, p. &lt;page-no&gt;.

---
title: Cross Referencing a footnote
format: pdf
include-in-header:
  text: |
    \usepackage{lipsum}
filters:
  - footref.lua
---

\lipsum[1-2]

### first footnote

Here is a paragraph with some text.^[[And]{#fref-footnote1} here is a footnote I want to refer to.]

\lipsum[1-7]

### Referencing to that first footnote

Now here is another portion of the text, where I deal with a subject I mentioned sometime ago.^[Regarding this, See \footref{footnote1}]


\lipsum[1-3]

The generated output will be identical to the above. But note that, this will work only for pdf format, not for html.

Also, if you want to create a link with words to that footnote, you can do this as follows,

Regarding this, See \footref{footnote1} [above](#fref-&lt;footnote-label&gt;)

huangapple
  • 本文由 发表于 2023年4月17日 06:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定