How can I create multiple PDFs from a single file?

Good afternoon,

Is it possible to generate multiple PDFs from a single .typ file, for example, from the web using a command or something similar? I haven’t found anything about this in the documentation.

Thank you very much for your help.

You may be looking for the techniques described here:

Feel free to follow up if this doesn’t fully address your problem.

1 Like

I think this is related to my problem, but I’m not sure how to implement it. As you mentioned in the post, this method can be used to generate a PDF with the questions and another with both questions and answers. That’s exactly what I’d like to achieve: generating both PDFs from the same .typ file at the same time.

Currently, I have two variables in my .typ file that, when modified, change the content of the PDF. However, this only allows me to generate one version at a time, instead of producing both questions.pdf and solutions.pdf simultaneously.

Here is the code below.

#import "../../../PREAMBULO1.typ": *

#import "../../BTO 2 - MAT II - PREAMBULO.typ": *

#import "../../BTO 2 - MAT II - BANCO EXÁMENES.typ": *

#show: exam.with(
  subject: subj(),
  type_exam: "2ª Ev - Parcial",
  name: true,
  course: cours(),
)

#let solu = true

#let tdah = false

#show: base_exams.with(
  solu: solu,
  ESO: ESO,
  tdah: tdah,
  tags: (("BTO 2 - MAT II - Propiedades métricas","6"),("BTO 2 - MAT II - Propiedades métricas","8"))
)

The variables that allow me to modify the “format” of the final PDF are:

#let solu = true

#let tdah = false

You should probably edit your document to use #let solu = sys.input.solu == "true" and use some sort of shell script to produce two outputs

Example in bash:

#!/bin/bash

typst compile --input solu=true --input tdah=false main.typ answers.pdf

typst compile --input solu=false --input tdah=true main.typ questions.pdf

I am writing this from mobile so there may be errors

2 Likes

Thank you! I have understood the solution you provided.

Is there a way to achieve this directly within the .typ file, so that exporting generates both PDFs automatically, without needing to modify the .typ file or run multiple commands manually?

a single call to the typst compiler can’t output multiple PDF files, so no.

Thank you for the quick response!

Is it possible to modify the name of the generated PDF file within the .typ file itself? That way, even if I generate them manually, I can avoid overwriting the previous one.

No, that’s not possible, you have to specify the output filename in the compile command.

1 Like

More generally: a Typst document has no control over what files Typst writes to. The control is fully with the user of the document, among things for security reasons.

1 Like

I really appreciate your help. I’ve written a script to run it following your instructions.

I have some questions about a few warnings that appeared in the console when I executed it. Can I post them here, or would it be better to open a new topic?

If the warnings are unrelated to this topic it’s best you open a new one