My template is supposed to switch between solid color backgrounds and image backgrounds.Are there any concise method?
This is my current code
page(
background: set_background(cover-image), // 背景图片 / Background image
// other code
)
My template is supposed to switch between solid color backgrounds and image backgrounds.Are there any concise method?
This is my current code
page(
background: set_background(cover-image), // 背景图片 / Background image
// other code
)
page:s parameter background takes anything that is of type content Page Function – Typst Documentation . Images are content.
an image
#set page(
background: image("image.png"),
)
To do solid color fill, I would recommend using the parameter fill Page Function – Typst Documentation :
#set page(
fill: color
)
You’d have to reset the alternating fields with none for background and auto for fill. Unless you can automate the logic of which page does what. Though I think this won’t work for page.fill, since it doesn’t allow content and there is no way to auto-change page settings from outside of the set rule. There is show pagebreak, but I don’t think it can do that either. So for that, you’d have to use background for images and colors.
But how can I switch between them?
you can switch between it at any point you desire with this:
#set page(
background: image("image.png"),
)
#lorem(50) //some content which has the image as background
#set page(
fill: red
)
#lorem(50) //some content which has the solid color red as background
But every time you call #set page a new page is created!
What’s the switching rule? Every other page? Based on sections? Based on just a few specific places in the document? Would be good to have some clarification of the desired result.
This does not switch, as I already explained.
Which is a huge problem for most documents, as you don’t know when exactly a new page will be created, therefore it’s 99% the use case for automated page.background script.
Ah whoops, yes you’re correct. I did test it with only fill so yeah…
It’s probably smarter to just have a function which does the automatic switching for you (get the page number and act accordingly for example).
By template setting
The switching by removing/adding template setting on call site:
// template.typ
#let template(
fill: auto,
background: none,
doc
) = {
set page(fill: fill, background: background)
doc
}
// main.typ
#show: template.with(
fill: green,
// background: image("image.png"),
)