typst

Continue in same line after heading


For some obscure styling reason, I want to start the text after a heading in the same line as the heading. The text should not count towards the heading in the outline for example.

#outline() // to see what is picked up as the heading

= This is my header

This text should start in the same line as the header but be formatted as normal text!

This is what I get actual image

and this is what I want to get

expected image


Solution

  • Headings are placed in a block by default, which prevents inlining. The easiest way to get your behavior is to wrap this block in a box

    #show heading: box
    
    #set heading(numbering: "1.")
    #outline()
    
    = Hello, World
    #lorem(100)
    

    enter image description here

    However, keep in mind boxes use rectangular boundaries, so headings that wrap across multiple lines won't continue the paragraph as you expect:

    #show heading: box
    
    #set heading(numbering: "1.")
    #outline()
    
    *Beware long headings*
    
    = #lorem(10)
    Some following text
    

    enter image description here

    You can get around this with a more complex show rule, e.g. only showing the numbering + body, but this becomes more difficult to maintain the more heading features you want to support:

    #show heading: it => {
      // Check out all heading information by printing `it.fields`
      // it.fields()
      if it.numbering != none {
        context numbering(it.numbering, ..counter(heading).get())
      }
      h(0.25em)
      it.body
      h(0.25em)
    }
    
    #set heading(numbering: "1.")
    #outline()
    
    *Long headings are now supported*
    
    = #lorem(10)
    Some following text
    

    enter image description here

    The easiest recommendation is to just use a box and ensure your headings fit on one line (which is usually the case).