🏠 back to Observable

Variables in LaTeX expressions?


Is there a way to insert a JavaScript value into a LaTeX expression? I want to write mathematical expressions with LaTeX but have some numbers (and maybe even strings) change dynamically.

For example, if I have a slider that changes a variable from 1 to 100 in steps of 1, then a LaTeX expression needs to change from… say, ${tex `x^1`} to ${tex `x^100`} accordingly.


easy! see this notebook:

I created a y number slider for the exponent there and LaTex power expression evaluated for x and y.

"tex x^{${y}} = ${Math.pow(x, y)}"


That! Is the code I was looking for. Much appreciated.


Now you just need to parse out the e+... at the end:

tex `${x}^{${y}} = ${('' + Math.pow(x, y)).replace(/e(\+|(\-))([0-9]+)/, ' \\times 10^{$2$3}')}`