# Interpolate values inside math expression

**URL:** https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742
**Category:** Pluto
**Created:** [February 5, 2024, 3:33pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742 "2024-02-05T15:33:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)
#### Post date: [February 5, 2024, 3:33pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/1 "2024-02-05T15:33:06Z")

</div>

I am trying to interpolate the value of an object inside a math equation in Pluto.  
A MWE could be:

```julia
begin
    avg = 15
    md"""
        The average would be given by:

        ``\overline{X} = \frac{\sum_{i=1}^N X_i}{N} = $(avg)``
    """
end

```

However, I am not successful. It works if I move the interpolation to the outside of the math expression. However, this causes the math to display inline and the value to be formatted as plain text.  
Can anyone help me?

---

<div class="post-metadata">

### Author: ![runjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/runjaj/32/23628_2.png) [@runjaj](https://discourse.julialang.org/u/runjaj)
#### Post date: [February 5, 2024, 5:40pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/2 "2024-02-05T17:40:57Z")

</div>

You need to use CommonMark.jl:

 ![SCR-20240205-qhpb](https://global.discourse-cdn.com/julialang/original/3X/c/7/c76841e4ea2ac3d19a9568681d3f7a9efc0493dc.png)

Notice that you need to escape the `\`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 5, 2024, 5:48pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/3 "2024-02-05T17:48:46Z")

</div>

> [@runjaj](#):
>
> You need to use [CommonMark.jl](https://juliahub.com/ui/Packages/CommonMark):

Can’t you just use the [Markdown stdlib](https://docs.julialang.org/en/v1/stdlib/Markdown/)? That’s what the `md"..."` macro uses, after all. For example:

````julia
import Markdown

Markdown.parse("""
```math
\\overline{X} = $avg
```
""")

````

---

<div class="post-metadata">

### Author: ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)
#### Post date: [February 5, 2024, 6:23pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/4 "2024-02-05T18:23:00Z")

</div>

Thank you a lot!

---

<div class="post-metadata">

### Author: ![RomeoV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romeov/32/37687_2.png) [@RomeoV](https://discourse.julialang.org/u/RomeoV)
#### Post date: [February 21, 2025, 11:01pm UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/5 "2025-02-21T23:01:35Z")

</div>

How can I do this for an inline math expression? I.e.

```julia
num_samples = 1
md"""
The number of samples is ``K=$(num_samples)``.
"""

```

Unfortunately I couldn’t get any of the proposed solutions to work, including those with CommonMark.jl.

EDIT: Actually, although `@md_str` doesn’t work here, we can do `Markdown.parse`!

```julia
Markdown.parse("""
The number of samples is ``K=$(num_samples)``.
""")

```

I am not sure why this works, as [the code](https://github.com/JuliaLang/julia/blob/8b9a19f661d2b4505c5eef4310f7fa22aa56b61e/stdlib/Markdown/src/Markdown.jl#L101-L103) for `@md_str` suggests that it is just calling `Markdown.parse`. Perhaps it has to do with how interpolation is escaped in macros?

However, when replacing `@md_str` with `Markdown.parse` other issues occur, such as losing the ability to include Plots.jl figures by simply interpolating

```julia
md"""
$(plt)
"""

```

This is on Julia 1.10 and 1.11.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 22, 2025, 1:40am UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/6 "2025-02-22T01:40:49Z")

</div>

> [@RomeoV](#):
>
> Perhaps it has to do with how interpolation is escaped in macros?

String macros, by default, do no interpolation at all, and the string is treated completely literally. (You also don’t need to escape backslashes.) This allows things like regex macros `r"^\s.$"` to not require excessive escaping.

(Each string macro has to implement interpolation etc. manually if it wishes. See e.g. [\_str string interpolation? - #11 by stevengj](https://discourse.julialang.org/t/str-string-interpolation/125766/11))

---

<div class="post-metadata">

### Author: ![RomeoV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romeov/32/37687_2.png) [@RomeoV](https://discourse.julialang.org/u/RomeoV)
#### Post date: [February 22, 2025, 1:47am UTC](https://discourse.julialang.org/t/interpolate-values-inside-math-expression/109742/7 "2025-02-22T01:47:45Z")

</div>

Thanks Steven.  
I’ve managed to solve my conundrum by chaining the two ways of parsing mardown like so:

```julia
let
c = 1
plt = plot(1:2, 1:2;
	       size=(200, 200), bg="transparent",
           background_color_inside="#FFFFFF66")
centered(content) = PlutoUI.ExperimentalLayout.Div(
	content; style=Dict("display"=>"flex", "justify-content"=>"center"))
str = Markdown.MD(
md"""
Sometimes we use the `@md_str` definition for convenient syntax, like ``\int a(x) dx = 1``.
""",
Markdown.parse("""
Sometimes we use `Markdown.parse`, for example to interpolate into an equation: ``\\int $(c) a(x) = 1``.
"""),
md"""
If we want to show an inline plot, we must put it into the macro:

$(centered(plt))
"""
)
title = "Finally, we can put this into an Admonition like this"
Markdown.MD(
  Markdown.Admonition("correct", title, [str])
)
end

```

 ![screenshot_2025-02-22T01:55:35UTC](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5d10db655d95c3d6372f3de48ef5cecc43ecf12.png)
