I want to compile 400+ documents into SVGs. Each document is very short. Most of them only have three lines, like the one below. Is there a proper way to do so?
#set page(width: auto, height: auto, margin: 0pt, fill: none)
#let Re = math.op("Re")
$Re u$
(Background: Guide: Render typst math in MkDocs)
My attempts
At present, I pass the document as stdin to the typst executable, and retrieve the SVG from stdout. This approach takes about 3 minutes to compile 400 documents.
from subprocess import run
for doc in documents:
svg_bytes = run(
["typst", "compile", "-", "-", "--format", "svg"],
input=doc.encode(),
check=True,
capture_output=True,
).stdout
Putting the call in a ThreadPoolExecutor will cut the time, but still at the minute level, and it makes handling Ctrl-C complicated.
I’ve also tried typst-py. It’s only a few seconds faster than the first approach for the 400 documents.
import typst
for doc in documents:
svg_bytes = typst.compile(doc.encode(), format="svg")
However, if I join all my short documents into a single long one, then compiling it (with either approach) would take less than a second.
Therefore, I believe these 400+ documents can be compiled in one or two seconds.
Is there a proper way to do so? Joining the documents naively will cause them to interfere with each other (which I don’t want), while #[doc-1] #pagebreak #[doc-2] #pagebreak … looks fragile…