Can I format a heading without a newline?

I’d like to create a heading format so that eg

== My internal header

#lorem(50)

Gets formatted to look the way

#text(14pt, weight: "bold")[My internal header:] #lorem(50)

would look. Is that possible? If I need to remove the blank line between the header and the paragraph text, that’s fine too, though I don’t know if that makes a difference.

You can use something like this:

#show heading.where(level: 2): it => {
  set text(14pt)
  box(it) + ":"
}

== My internal header
#lorem(50)

You could replace box(it) with it.body but this wouldn’t compose very well with other show-it rules since it removes the heading element.

Note that you will need to start the section content right after the title in markup (no empty line).

2 Likes

Good to note, but that’s ok for my current use-case. Thanks!

Hello. Here is a robust yet hacky solution:

#show heading.where(level: 2): it => {
  set text(14pt)
  box(it) + ":"
}

#show: doc => {
  let i = 0
  let elements = doc.children
  while i < elements.len() {
    if elements.at(i).func() == heading and elements.at(i).depth == 2 {
      elements.at(i)
      let j = i + 1
      while (
        j < elements.len() and elements.at(j).func() in (parbreak, [ ].func())
      ) {
        j += 1
      }
      if j >= elements.len() { break }
      " "
      i = j
    }
    elements.at(i)
    i += 1
  }
}

== Heading
// space
#lorem(50)

== Heading
#lorem(50)

== Heading

#lorem(50)

#heading(depth: 2)[Heading]#lorem(50)

== Heading

image

You can start writing a paragraph however you want, but global show rules can break something else. Though only real testing can say for sure. If there is a top-level styling added, then the styled element will have to be parsed a bit differently, though still should be possible.

2 Likes

Woah, neat! I barely understand what’s going on, but the results speak for themselves :smile:

To be clear, the given code (with box(it)) doesn’t have that problem because it preserves the it value instead of replacing it with it.body.

The algorithm goes over all elements in the sequence element and filters out parbreak and space elements that go after the depth: 2 headings. And adds a single space after all the removed stuff if there is still something after the heading.