How to create a block with centered and bold text without affecting global settings?

I want to implement a block where I can control its top and bottom margins. Additionally, I need to control the font weight within the block to be bold and centered.

After trying this, I found that defining font weight and alignment within the block affects the global settings. How can I achieve this more effectively?

content.typ

#import "../THEME/ozzyc.typ": *
#show: ozzyc_setting
#show: bc

#bc[xxxxxxxxxxxxxxxxxxxxx]
......content

ozzyc.typ → template file

// ========================Main Style=============================
#let ozzyc_setting(body) = {
  set page(
  paper: "a4",
  header: align(right)[
    xxxx
  ],
  numbering: "1",
  )

  show heading.where(
    level: 1
  ): it => block(width: 100%,below: 1.5em)[
    #set align(center)
    #set text(
      size: 20pt,
      weight: "bold",
      font: ("Noto Serif CJK SC"),
    )
    #smallcaps(it.body)
  ]

  show heading.where(
    level: 2
  ): it => block(width: 100%, below: 1.5em)[
    #set align(left)
    #set text(
      size: 18pt,
      weight: "bold",
      font: ("Noto Serif CJK SC"),
      fill: rgb("#045bac")
    )
    #counter(heading).display(it.numbering)
    #smallcaps(it.body)
  ]

  show heading.where(
    level: 3
  ): it => block(width: 100%, below: 1.2em, above: 2.0em)[
    #set align(left)
    #set text(
      size: 14pt,
      weight: "bold",
      font: ("Noto Serif CJK SC"),
      fill: rgb("#0369c9f1")
    )
    #counter(heading).display(it.numbering)
    #smallcaps(it.body)
  ]

  set heading(numbering: (first, ..nums) => numbering("1.", ..nums))

  set text(
    font: ("Noto Serif CJK SC"),
    lang: "zh",
    region: "CH",
    size: 12pt,
    weight: "regular",
  )

  set par(
    first-line-indent: (
      amount: 1.5em,
      all: true,
    ),
    spacing: 1.25em,
    leading: 1.00em,
    justify: true
  )

  set enum(
    indent: 1.0em
  )

  set list(
    indent: 1.0em,
    marker: "\u{2B26}",
  )

  body
  
}

// ======================Custom Components=========================


#let bc(content) = {
  set align(center)
  set text(weight: "bold", size: 13pt)
  block(width: 100%, below: 1.2em, above: 1.8em, content)
}

How should I modify it?

Additionally, I’d like to ask how content should be organized in a typical template. How should these global style settings and custom components be written? Are there any recommended template structures that can prevent styles from overriding each other?

This will replace your entire document with #bc(your document), which expands to

#set align(center)
#set text(weight: "bold", size: 13pt)
#block(width: 100%, below: 1.2em, above: 1.8em, [
  // your document
  #bc[xxxxxxxxxxxxxxxxxxxxx]
  ......content
])

Which is why the rules apply globally. For that not to happen, remove show: bc.

@ozzyc Just in case, you have seen this?

as well as an even more cleaned up version in How can I prevent level 1 headings from being numbered? - #3 by Andrew

1 Like

This all comes from Non-idiomatic advanced heading styling in documentation · Issue #6273 · typst/typst · GitHub, which supposedly is going to be rewritten soon, but it has been almost 2 months, yet no updates.