typst

How to show an image next to text in Typst


I want to show an image next to text using Typst (LaTeX alternative), so that the text flows around the image like this:

Image next to text

How do I do this?


Solution

  • wrap-it can do this (full docs here)

    Example (tested on Typst.app's online editor and also in VSCode with the extensions Typst LSP (to save as PDF) and Typst Preview (for instant preview as you type)):

    #import "@preview/wrap-it:0.1.0": wrap-content
    
    #set par(
      justify: true //to make text flow nicer around image
    )
    
    #let fig = [#figure(
      rect(fill: teal, radius: 0.5em, width: 8em),
      caption: [A figure],
    ) <tealrect>] //must use square brackets for @tealrect reference in the following text to work, since currently Typst can't attach labels to figures in "code mode"
    
    #let body = [In @tealrect we can see an example of a teal rectangle with rounded corners. #lorem(10)
    #parbreak()
    #lorem(100)]
    
    //custom wrapping function to modify the default layout
    #let wrapc(fig, body, align: right) = {
      let boxed = box(fig, inset: (bottom: 0.3em, top: 0.3em)) //box with inset to add some more space
      wrap-content(boxed, align: align)[#body]
    }
    
    #wrapc(fig, body, align: bottom + left) 
    

    align can be changed to other align values such as "right", "left", "bottom + right", etc.

    If you need TWO images to go with a block of text, there is also the wrap-top-bottom function you can use.

    The GitHub repository mentioned in user108903's answer makes you manually divide the text around the image into two parts, while wrap-it adjusts to different layouts automatically.