How to get document title and current heading into place function/header

I am trying to set up a template, but I am struggling with inserting the used title and current level 1 heading into my header, formatted via place function.
I have also tried inserting it with the hydra package, without success.

Here is a MWE of my issue. (With manual header insertions as place holder):
https://typst.app/project/rpAyYDX06J9t2R6jFoyfoX

Hiya @Cheesey,
Quick and to the point:

To insert the title, you can use:

// insert at the top
#let title = "The new title" // or `[The new title]` if you want content

// replace line 51
#text(blue, size: 24pt, "Document Title (from above)" )

// with
#text(blue, size: 24pt, title )

// also replace line 36
title: [Document Title],

// with
title: title,

To insert the level 1 heading into the header you can:

// insert at the top
#import "@preview/hydra:0.6.2": hydra

// replace line 58
#text(blue, size: 18pt, "Current Level 1 Heading (e.g. 'Introduction')" )

// with
#text(blue, size: 18pt, hydra(1))
// or (if you want the first page to display the heading)
#text(blue, size: 18pt, context hydra(1, skip-starting: false))
// or EDIT: Section body only (no section number)
#text(blue, size: 18pt, context hydra(1, display: (_, it) => it.body, skip-starting: true))

Now, before you go too far, perhaps your “template” could benefit from some refactoring. This to help you along the road. Some reading that would be beneficial (please don’t mind the titles):

I am sure you have read: https://typst.app/docs/tutorial/making-a-template/. The two posts above are helpful in understanding templates better, as well as many other posts on this forum.

The suggested solution is quick and to the point, and I mean by that that it is not using the full potential of templates. I haven’t refactored your code and it could be done in a much more elegant way with some refactoring.

2 Likes

Thank you @vmartel08 for the fast response. I will try out your solution and read through the suggested articles!

1 Like

A few suggestions for refactoring. More could be done (as always) but I have removed the obvious repetitions and realigned the “template” part. See this shared project.

I have edited my post in case you want the heading body only, i.e. Introduction instead of the section number and body, i.e. 1 Introduction.

I’ve also removed most of the markup notation using square brackets when on multiple lines when not needed. It has the tendency to insert spaces in some places (like in front of your email address on the title page).

Let us know should you have any more questions.

Thank you @vmartel08 for the fast response. I will try out your solution and read through the suggested articles!
(I am really sorry, I thought that I had sent this reply already)

1 Like

I seem to have deleted this shared project a bit quickly. Let me know should you want it back (if you didn’t have time to copy the code).

That’s really kind! If you could back it up for about an hour, I will copy it on my system.

I’ve recreated the link to the shared project as I can’t edit my previous post (and will try to remember to not delete it).

1 Like