Using figures within Equations

For computations involving graphs, it can be nice to be able to use graphs within actual equations. I know that can be done by having images of the graphs and embedding those within the equations, but is there a way to do this with graphs directly built in Typst?

As an example, I’ve tried the basic example below:

#set page(
  width: auto,
  height: auto,
  margin: 2mm,
)

#import "@preview/fletcher:0.5.8" as fletcher: diagram, node, edge, cetz
#import fletcher.shapes: circle, ellipse, cylinder, rect, chevron

$
  diagram(
    node((0, 0), [A])
  )
$

but it just returns the error:

Panicked with: “Failed to resolve coordinate: lr(body: sequence([(], [0], [,], , [0], [)]))”

for Cetz 0.3.4. I don’t know if this is a Cetz specific example or if this reflects the fact that placing content within equations is impossible.

It’s because (0,0) is in math mode, not code mode. In code mode, (0,0) means an array; but in math mode, (0,0) means a pair of () surrounding 0, 0.

To switch to code mode, add a #. You can switch back to math mode by writing another $…$.

#let A = [A variable]
$
  x^2 \
  #diagram( // Now enters the code mode
    node((0, 0),   $A$), // This $A$ switches to math mode
    node((0, 0.5), [A]), // This [A] switches to markup mode
    node((0, 1),    A),  // This A is still in code mode
  )
$

Refer to Syntax – Typst Documentation or Math – Typst Documentation for details.

1 Like

Ah, that’s painfully obvious in hindsight. Thanks for the quick response.

1 Like