How to plot `$x^x$` using CeTZ plot?

I want to plot $x^x$ (x raised to the power x) using CeTZ plot. My MWE is given below:

#import "@local/typst-plot:0.0.1": plot, sample

#set page(width: 12cm, height: 10cm)

#plot(sample(x => (#x^x), min: 0, max: 3, samples: 50),
      width: 8cm,
      height: 6cm)

But it does not compile.

How do I correct it so that it will compile?

Thanks.

I can’t comment on the example - (what is typst-plot and how do we get it so that we can test the code?) but x => (#x^x) is not valid typst syntax.

If you want to compute x raised to the power x, the function would look like this: x => calc.pow(x, x)

1 Like

Here is a working example to get your started:

#import "@preview/cetz:0.4.0"
#import "@preview/cetz-plot:0.1.2": plot, chart

#cetz.canvas({
  plot.plot(
    size: (12, 10),
    plot.add(domain: (1, 2), x => calc.pow(x, x))
  )
})
1 Like

Thank you. I wish I could accept two solutions but that seems not possible.

Thank you. It solves my issue.

How does one specify polynomials? And is there a library of examples covering algebraic and transcendental functions? That would help a newcomer like me.

The first example in the gallery (in the readme) uses polynomials, click the picture to see the code. GitHub - cetz-package/cetz-plot: Create Plots and Charts with CeTZ

I’m not sure what you refer to exactly with algebraic and transcendental functions, but be sure to know the list of built-in calc functions which includes trig functions i.e transcendental functions https://typst.app/docs/reference/foundations/calc/

You can use the eqalc package.

There you can convert a mathematical equation to a function for plotting, e.g.:

#import "@preview/eqalc:0.1.3": *
#import "@preview/cetz:0.4.0"
#import "@preview/cetz-plot:0.1.2": plot, chart

#let f = $f(x) = -x^3 + 4/6x^5$

This is the plot of $#f$.

#cetz.canvas({
  plot.plot(
    size: (12, 10),
    plot.add(domain: (-5, 5), math-to-func(f))
  )
})
2 Likes