🏠 back to Observable

Variables in LaTeX expressions?


#1

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.


#2

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)}"


#3

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


#4

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}')}`