How can I have the rest of heading line filled with a shape?

I am trying to fill the rest of heading lines with a rectangle like this

#show heading.where(level: 1): it => [
  #stack(
    dir: ltr, 
    spacing: 0.3em,
    it,
    align(horizon, rect(width: 100%, height: 0.2em, fill: orange)), 
  )
]

But this makes the rectangle have the width of the entire text line and thus it overflows the page. How can I give the size of the rest of the line that is not already covered with the heading text?

The problem with using stack is that it currently doesn’t change the meaning of 100% when some space is already occupied by previous children, so the width of the rectangle is that of the full page (minus the margins).

What you can do instead is to use a grid, which translates very easily:

#show heading.where(level: 1): it => grid(
  columns: (auto, 1fr),
  column-gutter: 0.3em,
  align: horizon,
  it, rect(width: 100%, height: 0.2em, fill: orange)
)

When using a grid, I would recommend setting the columns to (auto, 1fr). Otherwise, the second column will compete with the heading for space, which will become noticeable once the heading itself takes up more than half the page.

Alternatively, but this would involve rewriting the whole heading show rule from scratch, one could use an 1fr width box.

1 Like

What does this mean? I understand something like (1fr, 2fr) as first column takes up 1/3 of the parent second column takes up 2/3 of the parent. Does (auto, 1fr) mean that the first column takes up just as much space as the child wants (heading text in this case) and that the second column takes up 1/1 of the remaining space? I guess that’s what it means, because (auto, 2fr) seems to have the same effect. So does auto then take precedence and the fractions just get to divie up the rest of the available space?

To answer myself😂

The documentation for grid explains it well. It doesn’t explain what the mentioned “tracks” are but I guess it means columns, rows maybe even the gutters.

  • auto: The track will be sized to fit its contents. It will be at most as large as the remaining space. If there is more than one auto track width, and together they claim more than the available space, the auto tracks will fairly distribute the available space among themselves.
  • A fixed or relative length (e.g. 10pt or 20% - 1cm): The track will be exactly of this size.
  • A fractional length (e.g. 1fr): Once all other tracks have been sized, the remaining space will be divided among the fractional tracks according to their fractions. For example, if there are two fractional tracks, each with a fraction of 1fr, they will each take up half of the remaining space.

Could you include @laurmaedje addition about correctly sizing the columns? Then I could mark your answer as the solution.