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

I’m a bit confused by this:

``````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:

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> 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?

I find that this will work in Pluto too.

``````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.

1 Like

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.

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.

``````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.
``````

It’s probably a bug covered by one of the open ones with the `markdown` label. You may be better off using `Markdown.parse` and avoiding the `:julia` “flavor” of syntax like so:

``````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`.

2 Likes

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.

3 Likes

Excellent! That’s great news.

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)

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.