How to shrink document to output?

I want to generate an image of a single formula. How do I shrink the document size to just fit that formula?

Here is my typst code

#import "@preview/mitex:0.2.4"

#mitex.mitex(`\LARGE{g = 1 + \frac{N}{2} \left( k - 2 - \sum_{i=1}^{k}\frac{1}{n_i}\right)}`)
1 Like

You could use auto for the page height and width like so:

#set page(height: auto, width: auto)

Edit: You can get rid of the surrounding whitespace by adjusting the margins like so:

#set page(height: auto, width: auto, margin: 0pt)

This yields the following result:

1 Like

Thanks. I sadly just realized that typst cannot (yet) produce png images but only pdf.

EDIT
This is just wrong. Read below to see how to change output format.

Also it still has a huge margin around the formula.

Hmm, I think you can compile to a png using:

typst compile -f png your-file.typ

You also can get rid of the margin using set page(margin: 0pt) :)

Ok, easy fix:

#set page(height: auto, width: auto, margin: 0cm)
1 Like

oh yeah you are right. Sorry I am a very sporadic user of typst and I tend to forget things. Hopefully this thread will help someone else in the future.

2 Likes

Nice trick to generate multiple formula images from one document. Add a #pagebreak() between each formula.

// example.typ

#set page(height: auto, width: auto, margin: 0cm)

$ f(x) = f(n-1) + f(n-2) $

#pagebreak()

$ v^2 / 2 + g z + p / rho = "constant" $

#pagebreak()

$ E = m c^2 $

and then you can generate multiple output pictures with this command:

typst c example.typ example_{n}.png

Note that you don’t need to specify -f when you explicitly write the name of the output file because you need to specify the file extension anyway.

Output:


I couldn’t add the third image because I am a new user on the forum but you get the idea.

5 Likes

Very nice! :) Thanks for adding that information to the thread.

1 Like

Instead of manual page breaks, you can also do this:

#set page(height: auto, width: auto, margin: 0cm)
#show math.equation: page

$ f(x) = f(n-1) + f(n-2) $

$ v^2 / 2 + g z + p / rho = "constant" $

$ E = m c^2 $
1 Like

Sadly, this doesn’t quite work as intended as this will create an empty page at the end of the file and therefore also an empty image when rendering to svg or png.

I think that’s an issue that’s fixed in the next release (0.12), you can give it a try with the release candidate…