cetz-package / cetz

CeTZ: ein Typst Zeichenpaket - A library for drawing stuff with Typst.
https://cetz-package.github.io
GNU Lesser General Public License v3.0
825 stars 35 forks source link

Functions. #90

Closed DVDTSB closed 1 year ago

DVDTSB commented 1 year ago

How would i define functions outside the canvas? For example I would like to have a function named convex_plane to draw this:

scale(0.8)
line((-5,0),(5,0), mark:(start:">",end:">"))
line((0,5),(0,-5),mark:(start:">",end:">"))
content((5,-0.5), "Re")
content((-0.5,5), "Im")
content((-0.22,0.3),"0")
for i in range(-4,5) {
    line((i,-0.1),(i,0.1))
    line((-0.1,i),(0.1,i))
}

How would that be done?

johannes-wolf commented 1 year ago

Easiest is to put an import cetz.draw: * at the top of your function, like this:

#import "@preview/cetz:0.0.1"

#let convex_plane() = {
  import cetz.draw: * /* Import draw commands into namespace */
  scale(0.8)
  line((-5,0),(5,0), mark:(start:">",end:">"))
  line((0,5),(0,-5),mark:(start:">",end:">"))
  content((5,-0.5), "Re")
  content((-0.5,5), "Im")
  content((-0.22,0.3),"0")
  for i in range(-4,5) {
    line((i,-0.1),(i,0.1))
    line((-0.1,i),(0.1,i))
  }
}

#cetz.canvas({
  convex_plane() /* Call it */
})

Note: You should use mark: (fill: black), as version 0.0.1 renders triangle arrows non-filled by default (fixed on master).

Hint: It is possible to place elements relative to other elements by giving them a name. Example:

...
line((-5,0),(6,0), mark:(start:">",end:">"), name: "axis-re")
line((0,5),(0,-5),mark:(start:">",end:">"), name: "axis-im")
content((rel: (0, -0.3), to: "axis-re.end"), "Re", anchor: "above")
content((rel: (-0.3 ,0), to: "axis-im.start"), "Im", anchor: "right")
content((-0.2, 0.2), "0", anchor: "bottom-right")
...

Places the axis labels always at the axis tips.

DVDTSB commented 1 year ago

thanks 🙏 great plugin