# How do I properly interpolate \`$\` in Markdown?

**URL:** <https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482>\
**Category:** General Usage\
**Tags:** julia-markdown\
**Created:** [July 29, 2021, 7:51am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482 "2021-07-29T07:51:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 29, 2021, 7:51am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/1 "2021-07-29T07:51:21Z")

</div>

I’m a bit confused by this:

```julia
julia> using Markdown

julia> x = 5; y = 7; z = 10;

julia> md"""
       Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a \$ sign before the value: \$$(z)m.        
       """
  Here I interpolate x: (x). Then I also interpolate y: $(y) but for z I want a \ sign before the value: $10m.

```

As Discourse is lacking colours it’s hard to tell, but the middle part of the string is now LaTeX, Pluto output:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/6/6688129fa2d79702e57ff998c1f0ac35edaf9cb9.png)

So the first time I use an escaped dollar sign `\$`, it somehow goes back and turns the first variable interpolation dollar sign in `$(x)` into a “start inline LaTeX” dollar sign. This doesn’t happen for the second `\$`, which displays the dollar sign as expected and leaved the text after it unaltered.

If I remove the first `\$`, then everything works:

```julia
julia> md"""
       Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a dollar sign before the value: \$$(z)m.    
       """
  Here I interpolate x: 5. Then I also interpolate y: 7 but for z I want a dollar sign before the value: $10m.

```

Why does using `\$` somewhere later in the markdown string change the meaning of the first `$(x)`? What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![eduardovrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardovrs/32/20041_2.png) [@eduardovrs](https://discourse.julialang.org/u/eduardovrs)\
**Post date:** [July 29, 2021, 9:37am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/2 "2021-07-29T09:37:38Z")

</div>

I find that this will work in Pluto too.

```julia
let 
	x = 5; y = 7; z = 10;
	md"""
	Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a $ sign before the value: \$$(z)m.
	"""
end

```

I would think that you do not need to escape the `$` if it is alone.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 29, 2021, 9:53am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/3 "2021-07-29T09:53:40Z")

</div>

> [@eduardovrs](#):
>
> ```julia
> x = 5; y = 7; z = 10;
> md"""
> Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a $ sign before the value: \$(z)m.
> """
> 
> ```

Interesting, that’s definitely an improvement. My problem is that in my actual use case I want to write something like `but z is more than $1, indeed it is $10`, in which case the dollar sign disappears when placed right before the 1 (I can of course use `$ 1`, but then there’s an awkward space that I don’t want).

I’d still be interested in understanding how a `\$` later in the string changes the meaning of the first `$` from variable interpolation to start of inline LaTeX.

---

<div class="post-metadata">

**Author:** ![eduardovrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardovrs/32/20041_2.png) [@eduardovrs](https://discourse.julialang.org/u/eduardovrs)\
**Post date:** [July 29, 2021, 10:02am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/4 "2021-07-29T10:02:02Z")

</div>

To tell you the true, I don’t have much idea how Markdown works exactly. But if you use for example `\$$(z)m` you will get `$10m` without a space.

In your first code, if you eliminate the `\$` that is alone, it works well.

```julia
md"""
       Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a $ sign before the value: \$$(z)m.        
"""
Here I interpolate x: 5. Then I also interpolate y: 7 but for z I want a $ sign before the value: $10m.

```

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [July 29, 2021, 10:18am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/5 "2021-07-29T10:18:26Z")

</div>

It’s probably a bug covered by one of the open ones with the [`markdown` label](https://github.com/JuliaLang/julia/labels/markdown). You may be better off using `Markdown.parse` and avoiding the `:julia` “flavor” of syntax like so:

```julia
julia> Markdown.parse(
           """
           Here I interpolate x: $(x). Then I also interpolate y: $(y) but for z I want a \$ sign before the value: \$$(z)m.
           """;
           flavor = :common
       )
  Here I interpolate x: 5. Then I also interpolate y: 7 but for z I want a $ sign before the value: $10m.

```

No idea whether Pluto supports that or not though.

Another option is to try and see if `CommonMark.jl` works right for your usecase, though I’m not sure whether Pluto has fixed the macro issue yet to allow you to actually use `CommonMark.@cm_str` like you would with `Markdown.@md_str`.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 29, 2021, 11:09am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/6 "2021-07-29T11:09:08Z")

</div>

> [@mike](#):
>
> Another option is to try and see if CommonMark.jl works right for your usecase, though I’m not sure whether Pluto has fixed the macro issue yet to allow you to actually use CommonMark.@cm\_str like you would with Markdown.@md\_str.

The new support for analysing macros was added yesterday to the main branch of Pluto.

I tried using `@cm_str` with the main Pluto branch and it seems to properly work.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e775cd8f9ad22424a767a08f90329ec848add6e.png)

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [July 29, 2021, 11:11am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/7 "2021-07-29T11:11:17Z")

</div>

Excellent! That’s great news.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 29, 2021, 11:15am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/8 "2021-07-29T11:15:33Z")

</div>

Ok, Unfortunately I didn’t try changing the variables and they are still not updating inside the cm"" block.  
I’ll check if it’s a bug and an issue should be filed (other macros are analyzed correctly)

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [July 29, 2021, 11:20am UTC](https://discourse.julialang.org/t/how-do-i-properly-interpolate-in-markdown/65482/9 "2021-07-29T11:20:54Z")

</div>

If we need to adjust what `cm""` expands to then I can look into doing that. Please tag `@MichaelHatherly` when you open an issue so I can stay in the loop, thanks.
