How to define document metadata once and access it in other functions?

In my main.typ file, I have defined my document’s metadata:

#set document(
  title: [My book],
  author: "Name Surname",
  description: [Abstract],
  date: auto
)

Is there a way to access the values of the title, author, etc. in other functions?

At the moment, I am defining the same title, author, etc. information again in let functions, and then accessing those when I come to create my title and front matter pages.

You can access these using context. So for example,

#context document.title

Or if you want to use those values in a function, do

#let my-func(arg) = context {
    // stuff here
    let title = document.title
    // more stuff here
}

BTW, you can make your code blocks have correct syntax highlighting by adding the typ tag, like

```typ

```
1 Like

Thank you very much for this!

It seems to work perfectly for content blocks (like the title or description in my example above). However, for strings (like the author), I am getting the following output on my title page:

("Name Surname",)

rather than:

Name Surname

Aside from the #set document function I already mentioned above, I have the following in my file:

#let author(content) = context {
  set text(size: 12pt)
  document.author
}

and on the title page:

#title[]
#subtitle[]
#author[]

Might you have an idea what I am doing wrong or how to solve for this?

I seem to have stumbled upon the answer over on the Discord channel by @dipamsen :

document.author.first()

It is explained there that author is actually a list of strings for multiple authors, so the first command gets the first element in the list.

Thanks to both @aarnent and @dipamsen for getting me to the goal.

Hey @fungai2000, glad you managed to solve it. I’ve marked @aarnent’s answer as a solution - let me know if you still need further help.

1 Like