touying-typ / touying

Touying is a powerful package for creating presentation slides in Typst.
https://touying-typ.github.io/
MIT License
556 stars 14 forks source link

[Features] Preserve heading style in title and subtitle #71

Open f3fora opened 1 week ago

f3fora commented 1 week ago

I would like to be able to set a style for all heading, which if not explicitly modified in the theme is honored.

For example, I would like to set the style for all the headings, even if they are the title or the subtitle of the slide. In the following code, I would expect that Title, First Slide and 123 will be always red and smallcaps. Currently, only 123 is red and smallcaps

Also adding this option in the init of the config-methods doesn't work.

#import "@preview/touying:0.5.2": *
#import themes.simple: *

#show: simple-theme.with(aspect-ratio: "16-9")
#show heading: it => text(fill: red, smallcaps(it))

= Title

== First Slide

=== 123

Hello, Touying!

Hello, Typst!
OrangeX4 commented 1 week ago

I think that's hard to implement, you might consider setting the color of primary.

f3fora commented 1 week ago

As you wish, However, I believe the solution is really simple.

You can just remove the .body in https://github.com/touying-typ/touying/blob/334f8a2190ae1767c269b75474c7d33f0aa8f0bc/src/utils.typ#L344, or at least allow an option to trigger this.

OrangeX4 commented 1 week ago

Ok, I thought removing .body would cause numbering and outline errors, but it doesn't seem to be. However, there will still be problems with text size. I would consider adding this as an option in the next release. Thanks!

f3fora commented 1 week ago

I suggest that the function display current heading can be modified as:


#let display-current-heading(
  self: none,
  level: auto,
  numbered: true,
  hierachical: true,
  depth: 9999,
  setting: body => body,
  ..sty,
) = (
  context {
    let sty = if sty.pos().len() == 0 {
      current-heading => {
        if numbered and current-heading.numbering != none {
          _typst-builtin-numbering(
            current-heading.numbering,
            ..counter(heading).at(current-heading.location()),
          ) + h(.3em)
        }
        current-heading
      }
    } else if sty.pos().len() == 1 {
      sty.pos().at(0)
    } else if sty.pos().len() > 1 {
      panic("sty allows only a positional argument")
    }
    let current-heading = utils.current-heading(level: level, hierachical: hierachical, depth: depth)
    if current-heading != none {
      setting(sty(current-heading))
    }
  }
)

To achieve the same results in e.g. in simple theme, modifying header as

  header: self => display-current-heading(
    setting: utils.fit-to-width.with(grow: false, 100%),
    depth: self.slide-level,
    it => it.body,
  ),

Moreover, given that there is a scale factor in the headings with level 1 and 2 (see https://github.com/typst/typst/blob/8a8c4cec77342cc8c0772122cd2d0a71085c2c24/crates/typst/src/model/heading.rs#L266-L287), one may want to set

#show heading.where(level: 1): set text(size: (1em / 1.4))
#show heading.where(level: 2): set text(size: (1em / 1.2))
OrangeX4 commented 1 week ago

This doesn't solve the text-size problem and can lead to double numbering. I'd consider adding a 'native' parameter that defaults to false to control this behavior, so that theme developers can choose from it.

f3fora commented 1 week ago

Nonetheless, the parameter of sty in the utils.display-current-heading currently is

After some thoughts, I suggest this implementation:

#let display-current-heading(
  self: none,
  level: auto,
  hierachical: true,
  depth: 9999,
  sty: (setting: body => body, numbering: false, current-heading) => setting({
    if numbering and current-heading.numbering != none {
      _typst-builtin-numbering(
        current-heading.numbering,
        ..counter(heading).at(current-heading.location()),
      ) + h(.3em)
    }
    current-heading.body
  }),
  ..setting-args,
) = (
  context {
    let current-heading = utils.current-heading(level: level, hierachical: hierachical, depth: depth)
    if current-heading != none {
      if sty == none {
        current-heading
      } else {
        sty(..setting-args, current-heading)
      }
    }
  }
)

In this way, the current behavior is preserved, and one can easily use a custom sty function. For the "native" behavior one can simply set sty: it => it or sty: none.

OrangeX4 commented 1 week ago

I think it's a good idea, and I'll consider adding it in the next release.