# Variables in LaTeX expressions?

**URL:** https://talk.observablehq.com/t/variables-in-latex-expressions/1044
**Category:** Help
**Created:** [July 31, 2018, 12:51pm UTC](https://talk.observablehq.com/t/variables-in-latex-expressions/1044 "2018-07-31T12:51:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lukejanicke](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lukejanicke/32/864_2.png) [@lukejanicke](https://talk.observablehq.com/u/lukejanicke)
#### Post date: [July 31, 2018, 12:51pm UTC](https://talk.observablehq.com/t/variables-in-latex-expressions/1044/1 "2018-07-31T12:51:29Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RandomFractals](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/randomfractals/32/759_2.png) [@RandomFractals](https://talk.observablehq.com/u/RandomFractals)
#### Post date: [July 31, 2018, 1:36pm UTC](https://talk.observablehq.com/t/variables-in-latex-expressions/1044/2 "2018-07-31T13:36:16Z")

</div>

easy! see this notebook:

> **[LaTeX Power Slider](https://observablehq.com/@randomfractals/latex-slider)**
>
> An Observable notebook by Taras Novak.

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

---

<div class="post-metadata">

### Author: ![lukejanicke](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lukejanicke/32/864_2.png) [@lukejanicke](https://talk.observablehq.com/u/lukejanicke)
#### Post date: [July 31, 2018, 1:49pm UTC](https://talk.observablehq.com/t/variables-in-latex-expressions/1044/3 "2018-07-31T13:49:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jrus](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/jrus/32/586_2.png) [@jrus](https://talk.observablehq.com/u/jrus)
#### Post date: [August 2, 2018, 11:44pm UTC](https://talk.observablehq.com/t/variables-in-latex-expressions/1044/4 "2018-08-02T23:44:32Z")

</div>

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

```

> **[LaTeX Power Slider](https://observablehq.com/d/4c679acc5d1ac125)**
>
> An Observable notebook by Jacob Rus.
