Repository navigation
Description
Description
Currently, when including a Typst file using #include "file.typ", it rightfully receives the same compilation context, it has access to the same labels, its content affects introspection of the including file document and vice versa.
Consider adding a function or language mechanism for embedding one Typst document within another as a figure directly from source. A function like so:
#embed("file.typ")
#figure(embed("file.typ"))could produce an SVG/PNG document which is directly as if it was externally compiled and included without producing an on-disk artifact.
I think, depending on the size and complexity of the inner document, either SVG or PNG should be chosen to avoid bloating the resulting PDF with large files. It seems that PNG outshines SVG for small and simple images.
I am not sure if nesting of this mechanism should be supported, but in theory it's up to the user whether they need it or not. Circular dependencies could be caught similar to how circular import and includes are caught (perhaps it should be a keyword, as this mechanism is not something found in regular functions other than eval).
An alternative to a new function or keyword would be to allow eval to isolate the context of compilation somehow, but this doesn't seem to help when one wants to include a whole page as a figure.
Prior art can be seen here (cc: @Myriad-Dreamin), but it of course has no disk access and is quite limited in font and resource loading.
Use Case
When writing documentation or examples which heavily use context expressions and introspection, the outer document influences the inner document if it's just included.
Examples which show full pages are also impossible to render without an external compilation of the figure, which is then included.
Allowing a source file to be included as a fully rendered figure has the following benefits:
- no repository bloat from intermediate images
- no complicated scripts needed to update the figures before the main doc, Typst will track the dependencies itself
- including full pages as figures directly from source
Packages like hydra could do away with their quite horrible temporary solution of invoking some brittle script before creating the manual.
Embedding Typst in a WASM blob as mentioned above is also horrible for its sheer size, the embedded compiler must be kept up to date and has no access to the outer compilers World, embedding it is simply a waste of space when a working compiler with more features is already there to compile the outer document.
Activity
Andrew15-5 commentedon May 15, 2024
Ideally, I need to embed (Typst page that has (Typst diagrams) embedded as figures) in a document as a figure. In fact, several such pages.
The link is broken.
tingerrr commentedon May 15, 2024
I have updated the link, my bad, I didn't use a permalink so renaming the folder broke it.
Andrew15-5 commentedon May 15, 2024
There are no scripts in the linked directory and no document embedding. Only
#include, which is not even close to a solution. I mean, that is what I use for figure embedding, but it doesn't work with document embedding (nested#set page()).tingerrr commentedon May 15, 2024
True, what I linked is just the output of my script. At the risk of getting off-topic, I will only outline where you can find everything related to it on the provided permalink.
Each compilation of the manual is started from the Justfile recipe
doc, which will beforehand generate the output at the aforementioned link. Once this has happened, it scans the directory to find out how many output files were generated and save as a toml file containing this number because typst cannot load the contents of a directory without knowing the exact file names and file count. After all that, the images are included in the actual manual.If this all seems convoluted, this is because it is, the lack of being able to call
embed("example.typ")and getting an array of images is apparent.Andrew15-5 commentedon May 15, 2024
Ok, I think I got it. Why did you include the intermediate files in the repo? No
cleaning recipe?P.S. You can hide the comments that went off-topic later, if necessary/present.
tingerrr commentedon May 15, 2024
They are rarely updated, not everyone uses just, let alone nushell and people should still be able to easily contribute to the manual.
[-]Embedding another typst document[/-][+]Embedding another Typst document[/+]omentic commentedon Jul 10, 2026
I would like a future
#embedfunction to:The use case in which I want this for is transcluding other files within a Typst document for the purpose of HTML export (c.f. something like Forester). I'm opposed to the idea of an
#embedfor the sole purpose of using with figures because I think it can be more general, and more useful, and -- as#imagealready supports PDFs -- I think it would be better to extend#imageto support Typst documents, instead.