Is it possible to render text in a svg as math?

When I include a svg as an image, the text in the svg is correctly rendered as set in my document. But if I add “$” before and after the text, it is not rendered in math mode, but the “$” are shown as normal. Is it possible to add math to svgs?

Hey, when saying “the text is rendered as set” you are talking about the text attributes like font family or size?

The math rendering is likely not being applied because Typst handles the SVG still as an encapsulated and immutable image type.

The reason why the text attributes take effect are that the text is being embedded in the SVG as string-element and Typst renders it in the documents context.

You could try “insert” just the math-piece into your image, f.e. by placing the math over the image.

Or you create the math with your vector image program.

But i guess the most accurate solution would be using a package like CeTZ to create your diagram.

Yes, by set I mean font, size etc. I was hoping this was possible, as using CeTZ is (imo) magnitudes slower than something like InkScape. I’ll investigate just placing math over the image, that could be a compromise.
Thank you for your answer :)

1 Like

I have been using the following two alternative solutions.

  • Add math formulae in Inkscape

    TexText - Re-editable LaTeX and typst graphics for Inkscape. It is an open-source 18-year-old python extension supporting both LaTeX and Typst.

  • CeTZ overlay

    Replace image(…) with {image(…); place(cetz.canvas(…))}. It places the canvas over the image.

    Details
    1. Fix the width of the image.
    2. Adjust dx, dy of the place, so that the grid is aligned with the top-left corner of the image.
    3. (Optional) Change the length of cetz.canvas and the coordinates of grid as you wish.
    4. Add stroke: none to grid.
    5. Draw here.
    #figure(
      {
        image(…, width: 2em)
        import "@preview/cetz:0.3.1"
        set math.equation(numbering: none)
        place(
          top + left,
          dx: 0.5em,
          dy: 0.5em,
          cetz.canvas(
            length: 5em,
            {
              import cetz.draw: *
              // Define the plot range
              grid((-2, -2), (2, 2), step: 1)
    
              // Draw here
              content(…, $…$)
            },
          ),
        )
      },
      caption: […],
    )
    

Hope they help!