I’ve got a template setup for our project generating a pdf output, and have recently started exploring what’s needed to adapt it for html output. For this, I need to change quite a few styling options. How I’ve achieved this so far is to have a conditional setup in my template file:
#context {
if target() == "paged" {
set page(...)
// few other setup blocks for pdf output
} else {
html.elem("div", attrs: (class: "my-container"))[
// few other setup blocks for html output
]
}
}
However, since the template is quite large, I have to repeat this if/else clause several time throughout the file, which is quite cumbersome.
Instead, what I though I may be able to do was some sort of function overloading and have all the styling for paged output in one file, and styling for html in another file.
Here’s an example:
paged-defaults.typ
#let _page_defaults(title: none, doc) = {
set text(...)
set page(...)
show raw: set text(...)
// and lots more
doc
}
html-defaults.typ
#let _page_defaults(title: none, doc) = {
html.elem("style")[#read("assets/html-style.css")]
show math.equation: it => html.frame(it)
show math.equation.where(block: false): box
html.elem("div", attrs: (class: "my-container"))[
// and lots more
doc
]
}
And from the main template file I do the following:
#context {
if target() == "paged" {
import "paged-defaults.typ": *
} else {
import "html-defaults.typ": *
}
}
// lots more template setup
#let my_document(my_title, doc) = {
#set document(title: my_title)
#show: _paged_defaults()
doc
}
However, with the above code, the compiler throws:
error: unknown variable: _page_defaults
I’ve tried wrapping the _paged_defaults() call in a context block but still no luck.
I’ve also tried replacing the use of target() with a sys.input variable set in the command line:
#if sys.inputs.at("type", default: "paged") == "paged" {
import "paged-defaults.typ": *
} else {
import "html-defaults.typ": *
}
but still no luck. Perhaps this form of function overloading or conditional import is simply not possible, but I just wanted to check and see if anyone has done something similar. Or if folks have suggestions on alternate ways to achieve something like this, I would appreciate any feedback, thanks!