How set-style()
/canvas()
sets "global" settings for all drawing functions?
#157
-
In my not-so-bad flowchart "library" I have a lot of functions that represent separate block like: #let flowchart-block(name, start: none, text) = {
import cetz.draw: *
validate-start-coordinate(start)
// Draw block using predefined width, height and provided name.
content(name + ".center", text)
} And a connection line (between all blocks): #let connect(name, start: none, direction: "down", length) = {
import cetz.draw: *
validate-start-coordinate(start)
validate-direction-vector(direction, valid-directions: ("down", "right"))
if direction == "down" {
direction = (0, -length) // Predefined length.
} else if direction == "right" {
direction = (length, 0)
}
line(start, (rel: direction), name: name)
} But all of them use values from the top of the library's file: #import "@preview/cetz:0.1.0"
#let height = 2.3
#let width = height * 1.5
#let length = height / 2 And instead, I want to make 1 of 2 or both things:
#figure({
// set par
// set text
cetz.canvas({
import cetz.draw: *
// Define width, height, length
draw-flowchart(width, height, length, {
// call flowchart drawing functions
})
})
})
#figure({
// set par
// set text
cetz.canvas({
import cetz.draw: *
// Define width, height, length
draw-flowchart({
set-values(width, height, length)
// call flowchart drawing functions
})
})
}) Right now, I either have to provide width/height/length to every single flowchart drawing function (which is stupid), or I have to hard-code the values in the library file (which is not flexible at all). cetz library is not as simple as mine, so I don't know how to navigate through it in hope of finding a solution myself. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
To answer the question in the title, cetz has a dictionary called So on to how you can access Sorry this is a bit short but I need to go. Just a word of warning tho becareful about overwriting/naming collisions in |
Beta Was this translation helpful? Give feedback.
-
As @fenjalien said, you can use To get access to the context you can use a group with a content-callback: Example:
The alternative would be setting ctx values directly without using |
Beta Was this translation helpful? Give feedback.
To answer the question in the title, cetz has a dictionary called
ctx
("context") which stores information like the current transform and styling. It gets passed around to pretty much everything that needs to draw. The styling itself is a dictionary kept inctx.style
which is modified byset-style
. Note thatctx
is not a global variable, it gets created withincanvas
and is passed as a parameter to each function.So on to how you can access
width
,height
andlength
without passing them to a function each time. You essentially want to modifyctx
orctx.style
to add your parameters. The easiest way to do this is useset-style(width: ...)
otherwise use a"before"
or"style"
key in the elemen…