What's the most idiomatic way to use 1pt+red+(join:miter)?

What’s the most idiomatic way to use 1pt+red + (join:miter) with a stroke style?

I much prefer the 1pt+red syntax over stroke(paint:red, thickness:1pt), but this doesn’t work:

1pt+red+stroke(join:"miter")

mitter and others aren’t used that much, so idk if there is an idiomatic way, but usually you’d just pass a dictionary stroke: (paint: red, thickness: 1pt, join: "miter"). See Stroke Type – Typst Documentation.

You can write wrappers to simplify stuff, but it won’t be native, and having custom function all over the place sometimes might get you in the long run.

#import "@preview/t4t:0.4.3": get
#let make-stroke(a, b) = get.stroke-dict(a) + get.stroke-dict(a)
#block(stroke: make-stroke(1pt + red, (join: "miter")))
#block(stroke: get.stroke-dict(1pt + red) + (join: "miter"))
// #block(stroke: (paint: red, thickness: 1pt, join: "miter"))

In my experience, it’s better to use native API rather than save a few chars with a slightly better custom API. Only use wrappers when it’s actually worth it, which is sometimes hard to gauge.

3 Likes

As shown below, the values set in set/show rules will be folded together. Perhaps this feature can help you?

#rect()
#set rect(stroke: (dash: "dashed"))
#rect()
#set rect(stroke: 2pt + red)
#rect()

As for your original demand, 1pt and miter are the defaults, so 1pt + red + miter is usually equivalent to red.

2 Likes

Good point, I tend to avoid “stateful” use of set() because frequently each instance is different.

Is there any way to scope set() within a specific part of the source, something like:

with(rect(stroke: 2pt+red)) {
   /* lots of stuff here, where the 2pt+red applies */
}
// now the 2pt+red is out of scope
// and the draw style reverts to what it was
// before the `with` statement

A top level set rule stays in effect until the end of the file. When nested inside of a block, it is only in effect until the end of that block. With a block, you can thus restrict the effect of a rule to a particular segment of your document.
Styling – Typst Documentation

So this should work:

#[
  #set rect(stroke: 2pt+red)
  lots of stuff here, where the 2pt+red applies
]
now out of scope