How to embed one document in another?

I would like to embed one document in another, in such a way that the child document does not interfere with the parent, it just appears as if it were an image. I would also like to be able to show the code of the child document on the parent. Is this possible?

Something like this

#include "./children-doc.typ"

```typ
#include "./children-doc.typ"
_```

You’re close. The #include function is what you want for “embedding”, it’s basically copy&paste with the caveat that the content does get evaluated before it gets embedded so maybe there’s some interference but it should be easy to work around it.

As for displaying the code, you can combine raw() and read() like this:

#include "rect.typ"

#raw(block: true, read("rect.typ"), lang: "typ")

image

1 Like

Welcome to the forum @andres.gimenez ! I’ve updated your question post’s title to better fit our guidelines for the Questions category: How to post in the Questions category

Make sure the title is a question you’d ask to a friend about Typst. :wink:

1 Like

Thank you

This solves the part of adding the code of the document as an example. But the “include”, if the document is complex, sometimes produces an error. It would have to be run separately and then include the result as if it were an image or a pdf.

Yeah it depends on the document and which error specifically. For example, this would be easily solvable: Why can't I use a function in a chapter even though I imported it at the start of my main file? whereas issues with references might require more tinkering.

1 Like

Embedding a document in another without the child document interfering with the parent (different introspection contexts etc.) document is not currently possible, see Embedding another typst document · Issue #3723 · typst/typst · GitHub for a proposal to make this possible.

So far the read + eval solution makes sense. If, however, your child document introduces any elements which depend on global context such as counters, your best bet is to compile the other document and include it as a figure.

2 Likes