### Basic Cetz Setup Source: https://github.com/cetz-package/cetz/blob/master/docs/getting-started.mdx Minimal starting point for using Cetz in a .typ file. Imports the library and initializes a canvas. Draw functions are imported within the canvas scope. ```typ #import "@preview/cetz:0.5.2" #cetz.canvas({ import cetz.draw: * ... }) ``` -------------------------------- ### Install CeTZ Locally Source: https://github.com/cetz-package/cetz/blob/master/README.md Instructions for installing the CeTZ package locally using the provided script. Requires a recursive git clone and 'just' and 'cargo' installed. ```bash # to fetch a clean clone: git clone --recursive https://github.com/cetz-package/cetz.git # or if you forgot the "--recursive" flag before: git submodule update --init ``` ```bash just install ``` -------------------------------- ### Previous Coordinate System Example Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Demonstrates referencing the previous coordinate using an empty array. The initial previous coordinate is (0, 0, 0). ```typc line((0,0), (1, 1)) // Draws a circle at (1,1) circle(()) ``` -------------------------------- ### Polar Coordinate System Examples Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Illustrates defining points using polar coordinates with `radius` and `angle`. Supports explicit parameters and implicit array forms for both circles and ellipses. ```typc line((0, 0), (angle: 30deg, radius: 1)) ``` ```typc line( (0, 0), (30deg, 1), (60deg, 1), (90deg, 1), (120deg, 1), (150deg, 1), (180deg, 1) ) ``` -------------------------------- ### Chained Interpolation for Complex Paths Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Demonstrates chaining interpolation calls to create a complex path. The example shows how to define a point based on an interpolation, and then use that interpolated point as a reference for further calculations. ```typc grid((0,0), (3, 2), help-lines: true) line((0,0), (3,2)) stroke(red) line(((0,0), 0.3, (3,2)), (3,0)) fill(red) stroke(none) circle( ( // a (((0, 0), .3, (3, 2))), 0.7, (3,0) ), radius: 2pt ) ``` -------------------------------- ### Barycentric Coordinate System Example Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Demonstrates the barycentric coordinate system, which expresses a point as a linear combination of vectors. This example visualizes different document formats based on their barycentric coordinates relative to 'content', 'structure', and 'form'. ```typc circle((90deg, 3), radius: 0, name: "content") circle((210deg, 3), radius: 0, name: "structure") circle((-30deg, 3), radius: 0, name: "form") for (c, a) in ( ("content", "south"), ("structure", "north"), ("form", "north") ) { content(c, align(center, c + [\ oriented]), padding: .1, anchor: a) } stroke(gray + 1.2pt) line("content", "structure", "form", close: true) for (c, s, f, cont) in ( (0.5, 0.1, 1, "PostScript"), (1, 0, 0.4, "DVI"), (0.5, 0.5, 1, "PDF"), (0, 0.25, 1, "CSS"), (0.5, 1, 0, "XML"), (0.5, 1, 0.4, "HTML"), (1, 0.2, 0.8, "LaTeX"), (1, 0.6, 0.8, "TeX"), (0.8, 0.8, 1, "Word"), (1, 0.05, 0.05, "ASCII") ) { content( (bary: ( content: c, structure: s, form: f )), cont, fill: rgb(50, 50, 255, 100), stroke: none, frame: "circle" ) } ``` -------------------------------- ### Relative Coordinate System Example Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Shows how to place coordinates relative to the previous one, effectively using the previous coordinate as the origin. The `update` parameter controls whether the previous position is updated. ```typc circle((0, 0), stroke: blue) circle((rel: (1, 0)), stroke: red) ``` -------------------------------- ### Run CeTZ Tests Source: https://github.com/cetz-package/cetz/blob/master/README.md Command to run all unit tests for the CeTZ package. Requires 'tytanic' to be installed and in the PATH. ```bash just test ``` -------------------------------- ### XYZ Coordinate System Examples Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Illustrates the usage of the XYZ coordinate system for defining points with x, y, and z components. Supports both explicit dictionary and implicit array forms. ```typc line((0,0), (x: 1)) line((0,0), (y: 1)) line((0,0), (z: 1)) // Implicit form line((2, 0), (3, 0)) line((2, 0), (2, 1, 0)) line((2, 0), (2, 0, 1)) ``` -------------------------------- ### Draw a Tree with Multiple Child Branches Source: https://github.com/cetz-package/cetz/blob/master/docs/libraries/tree.mdx Demonstrates drawing a tree where a node can have multiple child branches. This example shows a node with two distinct child branches. ```typc cetz.tree.tree( ( [A], ( [B], [C] ), ( [D], [E] ) ), direction: "right" ) ``` -------------------------------- ### Draw a Basic Arc Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Use the `arc` function to draw a segment of a circle. Specify the center, start and stop angles, and radius. This example draws a 30-degree arc with a 3mm radius. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) grid((-1.5, -1.5), (1.5, 1.5)) line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) arc((3mm, 0), start: 0deg, stop: 30deg, radius: 3mm) ``` -------------------------------- ### Scale Graphics with `scale` Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Apply scaling to all subsequent drawing operations using the `scale` function. This example scales the entire drawing by a factor of 3. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) scale(3) grid((-1.5, -1.5), (1.5, 1.5)) line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) arc((3mm, 0), start: 0deg, stop: 30deg, radius: 3mm) ``` -------------------------------- ### Drawing Primitives Example Source: https://github.com/cetz-package/cetz/blob/master/docs/getting-started.mdx Demonstrates the usage of basic drawing functions like circle and line within the Cetz canvas. Ensure draw functions are imported inside the canvas block. ```typc example circle((0, 0)) line((0, 0), (2, 1)) ``` -------------------------------- ### Line Drawing with Marks Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/marks.mdx Demonstrates how to draw lines with various mark configurations. It shows examples of no marks, default triangle marks, overriding end marks, multiple marks, and marks with custom strokes. ```typc let c = ((rel: (0, -1)), (rel: (2, 0), update: false)) // Coordinates to draw the line, it is not necessary to understand this for this example. // No marks line((), (rel: (1, 0), update: false)) // Draws a triangle mark at both ends of the line. set-style(mark: (symbol: ">")) line(..c) // Overrides the end mark to be a diamond but the start is still a triangle. set-style(mark: (end: "<>")) line(..c) // Draws two triangle marks at both ends but the first mark of end is still a diamond. set-style(mark: (symbol: (">", ">"))) line(..c) // Sets the stroke of first mark in the sequence to red but the end mark overrides it to be blue. set-style(mark: (symbol: ((symbol: ">", stroke: red), ">"), end: (stroke: blue))) line(..c) ``` -------------------------------- ### Draw Sine and Cosine Lines with Karl Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx This example demonstrates drawing sine and cosine lines using a combination of basic shapes, styling, and coordinate specifications like polar and relative positioning. It sets up the grid and scale before drawing arcs and lines. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) scale(3) grid((-0.5, -0.5), (1, 1)) line((-0.5, 0), (1, 0)) line((0, -0.5), (0, 1)) arc((0, 1), start: 90deg, delta: -120deg) arc( (3mm, 0), start: 0deg, stop: 30deg, radius: 3mm, mode: "PIE", fill: color.mix((green, 20%), white), ) line((30deg, 1cm), (rel: (0, -0.5)), stroke: red + 1.2pt) ``` ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) scale(3) grid((-0.5, -0.5), (1, 1)) line((-0.5, 0), (1, 0)) line((0, -0.5), (0, 1)) arc((0, 1), start: 90deg, delta: -120deg) arc( (3mm, 0), start: 0deg, stop: 30deg, radius: 3mm, mode: "PIE", fill: color.mix((green, 20%), white), ) line((30deg, 1cm), (rel: (0, -0.5)), stroke: red + 1.2pt) line((), (0, 0), stroke: blue + 1.2pt) ``` -------------------------------- ### Import Local CeTZ Package Source: https://github.com/cetz-package/cetz/blob/master/README.md Import a locally installed CeTZ package in your Typst document. The package name is prefixed with '@local'. ```typst #import "@local/cetz:0.5.2" #cetz.canvas({ import cetz.draw: * // Your drawing code goes here }) ``` -------------------------------- ### Interpolate with Angle and Distance Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Illustrates interpolation using an angle to define the direction from the starting point, in addition to a relative distance. This is useful for creating normals or specific directional offsets. ```typc grid((0,0), (3,3), help-lines: true) line((1,0), (3,2)) line((1,0), ((1, 0), 1, 10deg, (3,2))) fill(red) stroke(none) circle(((1, 0), 50%, 10deg, (3, 2)), radius: 2pt) ``` -------------------------------- ### Adding Ticks with For-Loops Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Utilize for-loops to add repetitive elements like ticks on axes. This example demonstrates adding ticks at specific intervals using loops for both x and y axes. The `mark` styling parameter is configured to disable shape transformation and set the fill color. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ), mark: ( transform-shape: false, fill: black ) ) scale(3) grid((-1, -1), (1.5, 1.5)) line((-1, 0), (1.5, 0), mark: (end: "stealth")) line((0, -1), (0, 1.5), mark: (end: "stealth")) circle((0, 0)) arc( (3mm, 0), start: 0deg, stop: 30deg, radius: 3mm, mode: "PIE", fill: color.mix((green, 20%), white), ) for x in (-1, -0.5, 1) { line((x, -1pt), (x, 1pt)) } for y in (-1, -0.5, 0.5, 1) { line((-1pt, y), (1pt, y)) } ``` -------------------------------- ### Employ Path Anchors for Absolute and Relative Distances Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/anchors.mdx Path anchors specify points along a path using absolute distances or ratios. 'start', 'mid', and 'end' are predefined anchors for paths. ```typc line((0,0), (10, 1), name: "line") set-style(content: (frame: "rect", stroke: none, fill: white, padding: .1)) content("line.start", [0%, 0, "start"], anchor: "east") content("line.mid", [50%, "mid"]) content("line.end", [100%, "end"], anchor: "west") content((name: "line", anchor: 75%), [75%]) content((name: "line", anchor: 50pt), [50pt]) ``` -------------------------------- ### Calculate Intersection Point for Tangent Line Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx This example demonstrates how to find the intersection point of two lines to define a coordinate, useful for drawing elements like the tangent line. It uses 'hide' to make helper lines invisible and 'intersections' to calculate the coordinate. ```typc hide({ line((1, 0), (1, 1), name: "upward line") line((0, 0), (30deg, 1.5cm), name: "sloped line") // a bit longer, so that there is an intersection }) intersections("x", "upward line", "sloped line") line((1, 0), "x.0", stroke: orange + 1.2pt) ``` -------------------------------- ### Draw Bezier Curve Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draws a Bezier curve using start and end points, and one or two control points. Useful for creating smooth, custom curves. ```typc // start and end circle((0, 0), radius: 2pt, fill: gray) circle((2, 0), radius: 2pt, fill: gray) // control points circle((1, 1), radius: 2pt, fill: gray) circle((2, 1), radius: 2pt, fill: gray) bezier((0, 0), (2, 0), (1, 1), (2, 1)) ``` -------------------------------- ### Update Dictionary Styles Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/styling.mdx When styling with dictionaries, properties update each other instead of overriding the entire option. For example, setting `stroke: (paint: blue)` on a stroke defined as `(paint: red, thickness: 5pt)` results in `(paint: blue, thickness: 5pt)`. ```typc // Sets the stroke to red with a thickness of 5pt set-style(stroke: (paint: red, thickness: 5pt)) // Draws a line with the global stroke line((0,0), (1,0)) // Draws a blue line with a thickness of 5pt because dictionaries update the style line((0,0), (1,1), stroke: (paint: blue)) // Draws a yellow line with a thickness of 1pt because other values override the style line((0,0), (0,1), stroke: yellow) ``` -------------------------------- ### Nix Development Environment Source: https://github.com/cetz-package/cetz/blob/master/README.md Command to enter the development environment for Nix users, enabling flakes. ```bash nix --extra-experimental-features 'nix-command flakes' develop ``` -------------------------------- ### Set up CeTZ Environment and Draw Axes Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Import the CeTZ package and use the canvas function to draw lines representing axes. The environment automatically reserves space for the picture. ```typ #set page(width: auto, height: auto) #import "@preview/cetz:0.5.2" We are working on #cetz.canvas({ import cetz.draw: * line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) }) ``` ```typ We are working on #cetz.canvas({ import cetz.draw: * line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) }) ``` -------------------------------- ### Draw a Simple Tree with Nodes Source: https://github.com/cetz-package/cetz/blob/master/docs/libraries/tree.mdx Illustrates how to draw a basic tree structure using a list of nodes. The direction parameter controls the layout orientation. ```typc cetz.tree.tree( ( [A], ( [B], ( [C], ([D],) ) ) ), direction: "right" ) ``` -------------------------------- ### Projecting a Point onto a Line Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Demonstrates how to project a point onto a line segment using the `project` coordinate system. This is useful for finding the closest point on a line to a given point. ```typc set-style(fill: black, radius: 0.1) circle(name: "A", (0, 0)) circle(name: "B", (3, 1)) circle(name: "P", (1.9, -1.6)) line("A", "B") line("P", (project: "P", onto: ("A", "B"))) ``` -------------------------------- ### Draw Elements with Basic Styles Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/styling.mdx Style elements directly by passing named arguments like `fill` and `stroke` to their draw functions. `fill` defaults to 'none' and `stroke` defaults to 'black'. ```typc // Draws a red circle with a blue border circle((0, 0), fill: red, stroke: blue) // Draws a green line line((0, 0), (1, 1), stroke: green) ``` -------------------------------- ### Interpolation with Absolute Distances and Units Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Illustrates interpolation using absolute distances with different units (cm, mm). This allows for precise placement of points based on real-world measurements. ```typc grid((0,0), (3, 2), help-lines: true) line((1,0), (3,2)) for (l, c) in ((0cm, "0cm"), (1cm, "1cm"), (15mm, "15mm")) { content(((1,0), l, (3,2)), box(fill: white, $ #c $)) } ``` -------------------------------- ### Import Draw Functions into Canvas Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/canvas.md Import necessary draw functions at the beginning of the canvas body. This makes them available for use within the drawing scope. ```typ #cetz.canvas({ import cetz.draw: * }) ``` -------------------------------- ### Draw and Fill an Angle (Pie Mode) Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draw and fill an angle using the `arc` function with `mode: "PIE"`. This closes the arc around its origin, creating a pie-slice shape. The `fill` parameter colors the interior. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) scale(3) grid((-0.5, -0.5), (1, 1)) line((-0.5, 0), (1, 0)) line((0, -0.5), (0, 1)) arc((0, 1), start: 90deg, delta: -120deg) arc( (3mm, 0), start: 0deg, stop: 30deg, radius: 3mm, mode: "PIE", fill: color.mix((green, 20%), white), ) ``` -------------------------------- ### Using Functions in Coordinate Definitions Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Illustrates how to use a function within a coordinate definition to dynamically calculate a point's position. The function receives the resolved coordinates of its arguments as a vector. ```typc circle((0, 0), name: "c") fill(red) circle((v => cetz.vector.add(v, (0, -1)), "c.west"), radius: 0.3) ``` -------------------------------- ### Draw a Styled Grid Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draws a grid with custom styling, including color and line width. The grid is drawn first, then other elements are layered on top. ```typc grid((-1.5, -1.5), (1.5, 1.5), step: 0.5, stroke: gray + 0.2pt) line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) ``` -------------------------------- ### Interpolate with Rotated Angles Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Shows how to use interpolation with varying angles to create a pattern of circles around a central point. Each circle's position is determined by an angle and a distance from the center. ```typc grid((0,0), (4,4), help-lines: true) fill(black) stroke(none) let n = 16 for i in range(0, n+1) { circle(((2,2), i / 8, i * 22.5deg, (3,2)), radius: 2pt) } ``` -------------------------------- ### Draw Half Circle with Bezier Curves Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Constructs a half-circle using two Bezier curve segments. This method is an alternative to the direct circle function. ```typc line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) bezier((-1, 0), (0, 1), (-1, 0.555), (-0.555, 1)) bezier((0, 1), (1, 0), (0.555, 1), (1, 0.555)) ``` -------------------------------- ### Using Interpolation for Normals on a Tangent Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Shows how to use interpolation to find a point that represents a normal to a tangent line. This is achieved by defining a second interpolation that uses an angle of 90 degrees relative to the initial tangent. ```typc let (a, b) = ((0,0), (3,2)) line(a, b) // Get normal for tangent from a to () with distance .5, at a circle(a, radius: .1, fill: black) line((a, .7, b), (a: (), b: a, number: .5, angle: 90deg), stroke: red) ``` -------------------------------- ### Draw a Default Circle Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draws a circle with the default radius of 1. Includes axes for reference. ```typc line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) ``` -------------------------------- ### Apply Global Stroke Style Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Applies a global stroke style (thickness) to all subsequent draw functions using `set-style`. This allows for consistent line weights across the drawing. ```typc set-style(stroke: 0.4pt) grid((-1.5, -1.5), (1.5, 1.5), step: 0.5, stroke: gray + 0.2pt) line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) ``` -------------------------------- ### Custom Node and Edge Drawing in Tree Source: https://github.com/cetz-package/cetz/blob/master/docs/libraries/tree.mdx Shows how to customize the appearance of nodes and edges in a tree diagram using `draw-node` and `draw-edge` callback functions. The `draw-node` function customizes node shape and content, while `draw-edge` customizes line appearance. ```typc import cetz.tree let data = ([\*], ([A], [A.A], [A.B]), ([B], [B.A])) tree.tree( data, direction: "right", draw-node: (node) => { circle((), radius: .35, fill: blue, stroke: none) content((), text(white, [#node.content])) }, draw-edge: (parent, child) => { let (a, b) = (parent.group-name, child.group-name) line((a, .4, b), (b, .4, a)) } ) ``` -------------------------------- ### Import CeTZ Package Source: https://github.com/cetz-package/cetz/blob/master/README.md Import the CeTZ package for use in your Typst document. Ensure you are using the correct version number. ```typst #import "@preview/cetz:0.5.2" #cetz.canvas({ import cetz.draw: * // Your drawing code goes here }) ``` -------------------------------- ### Draw a Circle Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draws a circle with a specified center and radius. The default radius is 1 if not provided. ```typc circle((0, 0), radius: 10pt) ``` -------------------------------- ### Interpolate with Relative and Absolute Distances Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Demonstrates linear interpolation between two points using both relative percentages and absolute distances. Use relative distances for proportions of the line segment and absolute distances for specific measurements. ```typc grid((0,0), (3,3), help-lines: true) line((0,0), (2,2), name: "a") for i in (0%, 20%, 50%, 80%, 100%, 125%) { // Relative distance content(("a.start", i, "a.end"), box(fill: white, inset: 1pt, [#i])) } line((1,0), (3,2), name: "b") for i in (0, 0.5, 1, 2) { // Absolute distance content(("b.start", i, "b.end"), box(fill: white, inset: 1pt, text(red, [#i]))) } ``` -------------------------------- ### Draw a Path with Multiple Line Segments Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Utilize the 'line' draw function within the canvas body to construct a path with multiple connected line segments. Boilerplate imports and canvas calls are omitted for brevity. ```typc line((-1.5, 0), (1.5, 0), (0, -1.5), (0, 1.5)) ``` -------------------------------- ### Mark Parameters Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/marks.mdx Detailed information on the parameters available for customizing marks in CetZ. ```APIDOC ## Mark Parameters This section details the various parameters that can be used to customize marks in CetZ. ### Parameters #### symbol - **Type**: `none, str, array, dictionary` - **Default Value**: `none` - **Description**: Sets the mark to draw or applies styling to both mark ends of path-based elements. Accepts mark names, shorthands, or an array for multiple marks. A dictionary can be used for per-mark styling. #### start - **Type**: `none, str, array, dictionary` - **Default Value**: `none` - **Description**: Sets the mark to draw at the start of a path-based element, overriding `symbol` options. #### end - **Type**: `none, str, array, dictionary` - **Default Value**: `none` - **Description**: Sets the mark to draw at the end of a path-based element, overriding `symbol` options. #### length - **Type**: `number` - **Default Value**: `0.2cm` - **Description**: The size of the mark in the direction it is pointing. #### width - **Type**: `number` - **Default Value**: `0.15cm` - **Description**: The size of the mark along the normal of its direction. #### inset - **Type**: `number` - **Default Value**: `0.05cm` - **Description**: Specifies a distance by which something inside the arrow tip is set inwards. #### scale - **Type**: `float` - **Default Value**: `1` - **Description**: A factor applied to the mark's length, width, and inset. #### sep - **Type**: `number` - **Default Value**: `0.1cm` - **Description**: The distance between multiple marks along their path. #### position-samples - **Type**: `int` - **Default Value**: `30` - **Description**: Maximum number of samples for calculating curve positions, applicable to curves like bezier and hobby. #### pos - **Type**: `number, ratio, none` - **Default Value**: `none` - **Description**: Overrides the mark's position along a path with an absolute distance or a ratio of the path length. #### offset - **Type**: `number, ratio, none` - **Default Value**: `none` - **Description**: Advances the mark's position along a path instead of overriding it. #### anchor - **Type**: `str` - **Default Value**: `tip` - **Description**: Anchor point for positioning the mark. Can be `base`, `center`, or `tip`. #### slant - **Type**: `ratio` - **Default Value**: `0%` - **Description**: Slant of the mark relative to the arrow's axis (e.g., 0% for no slant, 100% for 45 degrees). #### harpoon - **Type**: `bool` - **Default Value**: `false` - **Description**: When true, only the top half of the mark is drawn. ``` -------------------------------- ### Use Anchor Coordinates with Named Elements Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/anchors.mdx Name an element using the 'name' argument and then reference its anchors to position other elements. This is available for elements that support the 'name' argument. ```typc // Name the circle circle((0,0), name: "circle") // Draw a smaller red circle at "circle"'s east anchor fill(red) stroke(none) circle("circle.east", radius: 0.3) ``` -------------------------------- ### Apply Nested Styles with set-style Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Applies styles to specific draw functions by passing a dictionary to the draw function's named argument within `set-style`. This allows for fine-grained control over element appearance. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) grid((-1.5, -1.5), (1.5, 1.5)) line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) ``` -------------------------------- ### Specify Element-Type Styles Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/styling.mdx Apply styles to specific element types (e.g., `rect`, `circle`) using `set-style`. These styles take precedence over global styles but can be overridden by direct function arguments. Dictionary values update, and 'auto' values inherit from the parent style. ```typc set-style( // Global fill and stroke fill: green, stroke: (thickness: 5pt), // Stroke and fill for only rectangles rect: (stroke: (dash: "dashed"), fill: blue), ) rect((0,0), (1,1)) circle((2.5, 0.5)) rect((4, 0), (5, 1), stroke: (thickness: 1pt)) ``` -------------------------------- ### Use Anchor Argument on Element Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/anchors.mdx Use the 'anchor' argument to translate an element so its specified anchor aligns with a given position. This is supported by all elements with an 'anchor' argument. ```typc // Draw a circle and place its "west" anchor at the origin. circle((0,0), anchor: "west") // Draw a smaller red circle at the origin. fill(red) stroke(none) circle((0,0), radius: 0.3) ``` -------------------------------- ### Draw a Grid Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Adds a grid to the picture within a specified rectangular area. The `step` argument controls the spacing of the grid lines. ```typc line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) grid((-1.5, -1.5), (1.5, 1.5), step: 0.5) ``` -------------------------------- ### Display Mark Shapes and Mnemonics Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/marks.mdx This code renders a table displaying the names, shorthands, and shapes of available marks in Cetz. It iterates through predefined mark shapes and their mnemonics to populate the table. ```typ #set page(margin: 0cm) #align(center, table( columns: 3, [*Name*], [*Shorthand*], [*Shape*], ..(for (name, item) in cetz.mark-shapes.marks { let name-to-mnemonic = (:) for (name, item) in cetz.mark-shapes.mnemonics { let list = name-to-mnemonic.at(item.at(0), default: ()) list += (raw(name) + if item.at(1).at("reverse", default: false) { " (reversed)" },) name-to-mnemonic.insert(item.at(0), list) } ( raw(name), name-to-mnemonic.at(name, default: ([],)).join([, ]), cetz.canvas(cetz.draw.line((), (1, 0), mark: (end: name))) ) }) )) ``` -------------------------------- ### Draw a Rectangle Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Draws a rectangle using two corner coordinates. This is a basic building block and may require many instances for complex shapes like grids. ```typc line((-1.5, 0), (1.5, 0)) line((0, -1.5), (0, 1.5)) circle((0, 0)) rect((0, 0), (0.5, 0.5)) rect((-0.5, -0.5), (-1, -1)) ``` -------------------------------- ### Set Global Styles with set-style Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/styling.mdx Use `set-style` to define default styles for all subsequent elements. Individual draw functions can still override these global styles. `fill` and `stroke` functions can be used as shorthands. ```typc // Draws an empty square with a black border rect((-1, -1), (1, 1)) // Sets the global style to have a fill of red and a stroke of blue set-style(stroke: blue, fill: red) circle((0,0)) // Draws a green line despite the global stroke being blue line((), (1,1), stroke: green) ``` -------------------------------- ### Anchor Coordinates Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Use named anchors to define points relative to other elements. Supports various anchor types like degrees, path percentages, and numerical path positions. ```typc circle((0,0), name: "circle") // Anchor at 30 degree content((name: "circle", anchor: 30deg), box(fill: white, $ 30 degree $)) // Anchor at 30% of the path length content((name: "circle", anchor: 30%), box(fill: white, $ 30 % $)) // Anchor at 3.14 of the path content((name: "circle", anchor: 3.14), box(fill: white, $ p = 3.14 $)) ``` -------------------------------- ### Adding Arrow Marks to Axes Source: https://github.com/cetz-package/cetz/blob/master/docs/tutorials/karl.mdx Use the `mark` styling parameter with `(end: ">")` to add arrow tips to the end of axes. Marks can only be added to elements that do not have a closed path. Ensure `transform-shape: false` is set if marks appear too large. ```typc set-style( stroke: 0.4pt, grid: ( stroke: gray + 0.2pt, step: 0.5 ) ) scale(3) grid((-0.5, -0.5), (1.5, 1.5)) line((-0.5, 0), (1.5, 0), mark: (end: ">")) line((0, -0.5), (0, 1.5), mark: (end: ">")) arc((0, 0), start: 120deg, stop: -30deg, anchor: "origin") arc( (3mm, 0), start: 0deg, stop: 30deg, radius: 3mm, mode: "PIE", fill: color.mix((green, 20%), white), ) line((30deg, 1cm), (rel: (0, -0.5)), stroke: red + 1.2pt) line((), (0, 0), stroke: blue + 1.2pt) hide({ line((1, 0), (1, 1), name: "upward line") line((0, 0), (30deg, 1.5cm), name: "sloped line") }) intersections("x", "upward line", "sloped line") line((1, 0), "x.0", stroke: orange + 1.2pt) ``` -------------------------------- ### Implicit Anchor Syntax Source: https://github.com/cetz-package/cetz/blob/master/docs/basics/coordinate-systems.mdx Utilize implicit dot-separated strings for referencing anchors, simplifying coordinate definitions. This syntax is applicable for accessing anchors of elements within groups. ```typc line((0, 0), (4, 3), name: "line") circle("line.75%", name: "circle") rect("line.start", "circle.east") ``` ```typc group(name: "a", { circle((), name: "b") }) circle("a.b.south", radius: 0.2) circle((name: "a", anchor: "b.north"), radius: 0.2) ```