How to check if code is inside html.frame or in a pdf target?

The target() function currently returns either html or paged. Inside a html.frame, it also returns paged. However, I would like to detect whether the outermost target of the compilation is html or paged. My code needs to act differently in a html.frame and a regular pdf. I don’t see a reliable way to do this. Any ideas?

Hi there @LordKekz,

Not sure if Bullseye -- Hit the target (HTML or paged/PDF) when styling your Typst document would be of any help?

Bullseye won’t help here…

I can think of two ways:

  • since HTML export (currently) only works using the CLI, you can use --format html --input format=html to both compile to HTML, and tell your document you’re doing so. In your document, you can then get the output format like this:

    #let outer-target() = sys.inputs.at("format", default: "paged")
    
  • you could also store the outermost target in a state:

    #context state("format").update(target())
    
    // can only be used in context
    #let outer-target() = state("format").get()
    

    note that you can’t just set the state’s initial value, it must be a proper state update so that other parts of your document can see it.