How to store footnotes in a separate file?

In latex we have \usepackage{sepfootnotes} which allows us to store all footnotes in a separate file (which can then be mentioned in the preamble) instead of cluttering the main document with footnote entries. How do I do it in Typst? Many thanks in advance.

Hey @Shashidhar_R! I’ve changed your post’s title to a question and added a tag as per our guidelines: How to post in the Questions category . Please keep this in mind in the future :wink:

From your description, I believe the following setup with two files (say, footnotes.typ and main.typ) is sufficient, but let me know if I missed something:

// footnotes.typ
#let footnotes = (
  [The name of the first king.],
  [Their phone number.],
  [Arbitrarily chosen number.],
)

#let foot(n) = footnote(footnotes.at(n))

// main.typ
#import "footnotes.typ": foot

#set page("a4", height: auto)
John#foot(0) said: #lorem(10).

We called 0000#foot(1).

As a result, three#foot(2) dogs came to us.

Named keys are also supported by using a dictionary instead of a list:

// footnotes.typ
#let footnotes = (
  king: [The name of the first king.],
  phone: [Their phone number.],
  number: [Arbitrarily chosen number.],
)

#let foot(k) = footnote(footnotes.at(k))

// main.typ
#import "footnotes.typ": foot

#set page("a4", height: auto)
John#foot("king") said: #lorem(10).

We called 0000#foot("phone").

As a result, three#foot("number") dogs came to us.

I’ll also note that nothing mandates that the footnotes should be on a separate file, and you can also keep all of that code under a single file and it will work fine, if you prefer so.

Typst scripting is really flexible and lets you create lots of little abstractions like this thanks to the power of functions and variables. I hope this example motivates you to dig even deeper into it! :sunglasses:

Thanks for taking time for that quick reply. Looks like this is going to solve my problem, although I have to play around with your commands a bit to get the hang of them. But I get the general idea. If I wanted specifically to store all my footnotes in a separate file, how do I do it? Can I include para and text functions in this separate file without any problem?

In principle, you can just add your footnotes to the dictionary or list in the footnotes.typ file I provided, and use any Typst functions.

If you want to load the footnotes from a .yml file instead for example, you can replace the dictionary with let footnotes = yaml("file.yml").

If you want to be able to use Typst syntax within the yaml file, it is possible, though a bit inconvenient, with the eval function (Evaluate Function – Typst Documentation). You’d still use yaml("file.yml") to read the file into a dictionary, but then you’d have to modify this dictionary to replace the literal strings with evaluated Typst code. For example:

#let footnotes = yaml("file.yml")
#for (name, body) in footnotes {
  footnotes.at(name) = eval(body, mode: "markup")
}

Thanks, PgBiel. I will try that.