Skip to content

Embedding another Typst document #3723

Open

Description

@tingerrr

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

  1. added
    feature request
    on Mar 19, 2024
  2. added
    change request
    and removed
    feature request
    on Mar 24, 2024
  3. Andrew15-5 commented on May 15, 2024

    @Andrew15-5
    Contributor

    I am not sure if nesting of this mechanism should be supported

    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.

    Packages like hydra could do away with their quite horrible temporary solution of invoking some brittle script before creating the manual.

    The link is broken.

  4. tingerrr commented on May 15, 2024

    @tingerrr
    ContributorAuthor

    I have updated the link, my bad, I didn't use a permalink so renaming the folder broke it.

  5. Andrew15-5 commented on May 15, 2024

    @Andrew15-5
    Contributor

    Packages like hydra could do away with their quite horrible temporary solution of invoking some brittle script before creating the manual.

    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()).

  6. tingerrr commented on May 15, 2024

    @tingerrr
    ContributorAuthor

    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.

  7. Andrew15-5 commented on May 15, 2024

    @Andrew15-5
    Contributor

    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.

  8. tingerrr commented on May 15, 2024

    @tingerrr
    ContributorAuthor

    Ok, I think I got it. Why did you include the intermediate files in the repo? No cleaning recipe?

    They are rarely updated, not everyone uses just, let alone nushell and people should still be able to easily contribute to the manual.

  9. added
    feature request
    and removed
    change request
    on Feb 24, 2025
  10. changed the title [-]Embedding another typst document[/-] [+]Embedding another Typst document[/+] on Feb 24, 2025
  11. omentic commented on Jul 10, 2026

    @omentic
    Contributor

    I would like a future #embed function to:

    1. Embed a Typst document as Typst, but evaluated within a self-contained context (as opposed to compiling to PNG/SVG)
    2. Be a custom element, and so be given a counter, be introspectable through show rules, etc

    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 #embed for the sole purpose of using with figures because I think it can be more general, and more useful, and -- as #image already supports PDFs -- I think it would be better to extend #image to support Typst documents, instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions