Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add spacing around figure in typst

Tags:

typst

I'm using typst 0.11.1. How do you add spacing between figures, and the subsequent text?

If captions become very long, it can be hard to see where it ends, and the surrounding text begins. Example:

#lorem(50)

#figure(block(width: 100%, height: 10em, fill: green), caption: [Shrek green. #lorem(35)])

#lorem(70)

Partial screenshot of the document produced by preceeding code.

Where is the option to add padding around a figure?

like image 533
AsgerHB Avatar asked Oct 30 '25 09:10

AsgerHB


2 Answers

The figure function inherits from the block function. You can therefore set the default block inset for figures using the following show rule:

#show figure: set block(inset: (top: 0.5em, bottom: 2em))

This code adds 0.5em padding above figures, 2em padding below as was asked, and no padding to the sides.

Partial screenshot of the document produced by code referred in the question, and the show-rule.

like image 75
AsgerHB Avatar answered Nov 01 '25 14:11

AsgerHB


Update:

The previous solution was leaving blanks at the places where the figure was originally placed before floating. The Github user eltos has created a better rule (requires Typst >0.12.0):


#let figure_spacing = 0.75em // Additional spacing between figures and the text
#show figure: it => {
  if it.placement == none {
    block(it, inset: (y: figure_spacing))
  } else if it.placement == top {
    place(
      it.placement,
      float: true,
      block(width: 100%, inset: (bottom: figure_spacing), align(center, it))
    )
  } else if it.placement == bottom {
    place(
      it.placement,
      float: true,
      block(width: 100%, inset: (top: figure_spacing), align(center, it))
    )
  }
}



#lorem(20)

#figure(block(width: 100%, height: 10em, fill: green), caption: [Shrek green. #lorem(35)], placement: top)

#lorem(10)

#figure(block(width: 100%, height: 10em, fill: green), caption: [Shrek green. #lorem(35)], placement: none)

#lorem(10)

#figure(block(width: 100%, height: 10em, fill: green), caption: [Shrek green. #lorem(35)], placement: bottom)

#lorem(50)

enter image description here


Old Answer (produces blanks between paragraphs)

in Typst 0.13 you can use the following show rule to add some top and bottom spacing around each figure:

#show figure: f => {[#v(2em) #f #v(2em) ]}
#lorem(50)
#figure(block(width: 100%, height: 10em, fill: green), caption: [Shrek green. #lorem(35)])
#lorem(70)

Figure with added spacing around it

like image 32
Jounathaen Avatar answered Nov 01 '25 13:11

Jounathaen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!