#Styling of page heading

18 messages ยท Page 1 of 1 (latest)

polar night
#

In the typst template I am working on, I want to have the current main chapter on top right of each page (excl. titlepage) with an underline across the page width.
For that, I did follow the example mentioned in the docs: https://typst.app/docs/reference/introspection/query/
This is what I got so far:

set page(
  paper: "a4",
  numbering: "1", 
  number-align: center,
  header: locate(loc => {
    let elems = query(
      selector(heading).before(loc),
      loc,
    )
  let header-text = smallcaps[
  ]
  if elems == () {
    align(right, header-text)
    line(length: 100%, stroke: 0.5pt)
  } else {
    let body = elems.last().body
    header-text + h(1fr) + smallcaps(body)
    line(length: 100%, stroke: 0.5pt)
  }
  }
  )
)

However, there are a couple of issue, I don't know how to solve:

  1. I don't want this to show up on the title page, the table of contents. Currently it shows up on every page, including the title page. How do I prevent that/filter that?

  2. I only want to show top level headings e.g. heading 1. or 2., but not headings like 1.1 or 2.1 or smaller. How do I do that?

  3. I would like to decrease the vertical space between the heading text and the horizontal line. How can I do that?

  4. There is an edge case when showing the heading following the table of contents. It still shows "TABLE OF CONTENTS" on the page, even tho the table of contents already ended on the previous page. Instead I would expect to see "INTRODUCTION".

Can someone help me with these issues?

Typst

Documentation for the query function.

agile mirage
#

You might want to borrow from this template or hydra and their other package, antimatter

GitHub

Typst thesis template for bioinformatics @ TUM+LMU - GitHub - lkndl/typst-bioinfo-thesis: Typst thesis template for bioinformatics @ TUM+LMU

GitHub

A typst package for displaying the active section in the page header or footer. - GitHub - tingerrr/hydra: A typst package for displaying the active section in the page header or footer.

#
  1. this is a question of when in the doc / template you define the header; must be AFTER the title page.

  2. use the heading.where(level: 1) syntax. It's not that exactly, but ~ish

  3. v(-4pt) or something like that maybe?

  4. This is one of the things hydra is great for ๐Ÿ™ƒ

polar night
polar night
#

Okay, so I am currently trying to use the hydra package, but I am still struggling to get the things the way I want.

I can't use smallcaps() on the chap variable to make it all uppercase. Also, I still don't get it to show up only after the title page, even when I define it later in my template.

This is what I have so far:

let chap = figure.where(kind: "chapter")
let sect = custom(heading.where(level: 1))
set page(header: align(right, hydra(chap)))
set page(header: hydra() + line(length: 100%))

And more importantly, in some cases, no heading shows up at all, when the top level heading is defined on the same page:

agile mirage
#

Do you maybe have a minimal working example?

Your document looks like this i suppose:

set page(paper: "a4")
# Title page
// **now** define the header
let chap ...
set page(header: ...)

Does that still show the header even on the title page? (If yes, is that code in a template function thing that is called with #show: thing.with(... and thing gets a body argument?)

The chap as you define it above isn't a variable; it's a selector -- which is why you cannot style it. Something like align(right, smallcaps(hydra(chap))) should work.

I think your last point is the skip-starting option (see manual).

polar night
#

@agile mirage I created a simple example, to make it easier to work out the problems:

#import "@preview/hydra:0.3.0": hydra, selectors
#import selectors: custom

#set page(
  paper: "a4",
  numbering: "1", 
  number-align: center
  )
)

#set text(
  size: 12pt,
  font: "New Computer Modern", 
  lang: "en" 
)

#set heading(
  numbering: "1."
)

#v(0.3fr)
#align(center, text(1.6em, "Organization"))
#align(center, text(1.6em, "Department"))
#v(0.3fr)
#align(center, text(1.6em, smallcaps("Title")))
#v(0.3fr)
#align(center, text(1.2em, "Submitted by"))
#v(0.1fr)
#align(center, text(1.2em, weight: 700, "Author Name"))
#v(0.3fr)
#align(center, text(1.2em, "Submitted for the degree of"))
#align(center, text(1.2em, style: "italic", "Master of Science (M.Sc.)"))
#v(0.6fr)
#align(center, text(1.2em, datetime.today().display("[month repr:long], [year]")))
#pagebreak()

// Define header text
#let chap = figure.where(kind: "chapter")
// #let sec = custom(heading.where(level: 1), ancestor: chap)

#set page(header: locate(loc => {
  align(right, hydra(chap))
}))

#set page(header: hydra() + line(length: 100%))
#show heading.where(level: 1): it => pagebreak(weak: true) + it

// Table of contents.
#outline(indent: auto, title: "Table of Contents")
#pagebreak()

= Chapter A
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(100)
== Subchapter C
#lorem(200)
== Subchapter D
#lorem(100)
= Chapter B
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(300)
== Subchapter C
#lorem(100)
== Subchapter D
#lorem(100)
= Chapter C
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(200)
== Subchapter C
#lorem(100)
== Subchapter D
#lorem(500)
#

I for example don't understand why the heading text is not shown anywhere except for subchapter C?? that does not seem to make a lot of sense

#

That being said, not having the text on the title page works, when I define it after the title page, like you said ๐Ÿ‘ So, that's good.

#

So, whats missing now is:

  • On every page, always show the current top level chapter (Chapter A, ChapterB ... etc)
  • Show the chapter in smallcaps / all uppercase/ I don't know if its possible to control the font size here?
  • The toc does not have "Table of Contents" above the horizontal line, like I would expect. I assume its not recognized as a chapter?
#
  • I also don't need the numbering in front of the chapter above the lines. E.g. it should just show "Chapter A", not "1. Chapter A"
#

I appreciate your help btw ๐Ÿ˜„

agile mirage
#

?r

#import "@preview/hydra:0.3.0": hydra, selectors
#import selectors: custom

#set page(
  paper: "a4",
  numbering: "1", 
  number-align: center
)

#set text(
  size: 12pt,
  font: "New Computer Modern", 
  lang: "en" 
)

#set heading(
  numbering: "1."
)

// The title page. Just playing around with scoping and shortening
#{
  set align(center)
  text(size: 1.6em, {
    v(0.3fr)
    [Organization #parbreak() Department] // compare linebreak (via \ ) to parbreak
    v(0.3fr)
    smallcaps("Title")
    v(0.3fr)
  })
  text(size: 1.2em, {
    [Submitted by#v(0.1fr)*Author Name*#v(0.3fr)Submitted for the degree of #parbreak() _Master of Science (M.Sc.)_#v(0.6fr)]
  })
  datetime.today().display("[month repr:long], [year]")
  pagebreak()
}

/* Dou you still need this?
// Define header text
#let chap = figure.where(kind: "chapter")
// #let sec = custom(heading.where(level: 1), ancestor: chap)

#set page(header: locate(loc => {
  align(right, hydra(chap))
}))

#set page(header: hydra() + line(length: 100%))
*/

// with smallcaps
#set page(header: align(right, smallcaps(hydra(1, skip-starting: false, display: (.., candidate) => candidate.body))) + v(-8pt) + line(length: 100%))

// or with uppercase but smaller
#set page(header: align(right, text(size: 0.8em, upper(hydra(1, skip-starting: false, display: (.., candidate) => candidate.body))) + v(-8pt) + line(length: 100%)))

#show heading.where(level: 1): it => pagebreak(weak: true) + it

// Table of contents.
#outline(indent: auto, title: "Table of Contents")
#pagebreak()

= Chapter A
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(100)
== Subchapter C
#lorem(200)
== Subchapter D
#lorem(100)
= Chapter B
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(300)
== Subchapter C
#lorem(100)
== Subchapter D
#lorem(100)
= Chapter C
== Subchapter A
#lorem(100)
== Subchapter B
#lorem(200)
== Subchapter C
#lorem(100)
== Subchapter D
#lorem(500)
neat tigerBOT
#

Note: 5 more pages ignored

agile mirage
# polar night I for example don't understand why the heading text is not shown anywhere except...

This is a developer choice in hydra to not show redundant headers. For example, the header in that image from chapter 3 in their manual isn't really useful.

Finding the current chapter is more complicated than "What is the last chapter heading before the location of the header we want to set right now?", because -- again using that little image -- the answer to this question would point to the chapter before Chapter A. Therefore, use that package even if it looks complicated for a bit.

agile mirage