latexpandocquartocross-referencefootnotes

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


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.1


1 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.2


2 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.1


1 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.2


2 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.


Solution

  • 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,


    first footnote


    referencing to the first footnote within a second footnote


    second footnote


    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
    

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

    footref will generate output as 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]{#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-<footnote-label>)