# Problem escaping 2nd dollar sign in plot title

**URL:** https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388
**Category:** General Usage
**Tags:** plotting, plots
**Created:** [December 10, 2023, 9:27pm UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388 "2023-12-10T21:27:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [December 10, 2023, 9:27pm UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388/1 "2023-12-10T21:27:45Z")

</div>

Suppose I want a plot with the title “if you give me $2 then i will give you $1”

If i construct  
`str = "if you give me \$2 then i will give you \$1"`  
then  
`println(str)`  
returns  
`if you give me $2 then i will give you $1`  
as I’d hope and expect. But

```julia
x=1:5
y=1:5
print(x, y, title = str, label="")

```

produces this plot  
 ![ad](https://global.discourse-cdn.com/julialang/original/3X/e/2/e242c724345615a5b9d30eda4523c84c93949bf5.png)

whereas omitting the second dollar sign in the title gives the expected (but not desired) output, i.e. if  
`str = "if you give me \$2 then i will give you 1"`  
then  
`plot(x, y, title = str, label="")`  
produces  
 ![ad2](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd064b1b1ef8bd78d64dedb46a4e49a342d606da.png)

Why is the second escaped dollar sign not behaving as I’d expect, and what is the solution?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 10, 2023, 11:08pm UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388/3 "2023-12-10T23:08:10Z")

</div>

A workaround is to use another unicode dollar sign:

```julia
using Plots
x = y = 1:5
str = "if you give me ＄2 then i will give you ＄1"
plot(x, y, title=str, label="", titlefont="Helvetica")

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [December 10, 2023, 11:28pm UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388/4 "2023-12-10T23:28:39Z")

</div>

Formatting text between 2 dollar signs ($) is LaTeX math mode. Funnily enough I never noticed normal strings do this too because I used LaTeXStrings to avoid escaping backslashes and dollar signs from the start. Maybe another preceding escaped backslash `\\\$` makes LaTeX see `\$` and escape it in its own way.

---

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [December 11, 2023, 2:03am UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388/5 "2023-12-11T02:03:22Z")

</div>

@rafael.guerra, using a unicode dollar sign is a good workaround. the julia docs give 💲 as a reasonable alternative.

@benny, i see that plot with most backends does, indeed, allow LaTeX in titles and labels. and i’m sure there’s a way to backslash my way out of trouble, but i couldn’t make your suggestion work. i’ll just use the unicode symbol for now.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 11, 2023, 6:23am UTC](https://discourse.julialang.org/t/problem-escaping-2nd-dollar-sign-in-plot-title/107388/6 "2023-12-11T06:23:25Z")

</div>

I think you are on to something here, the reason for this $ trouble is in the GR library (in the C library, not the wrapper). The math parsing is done in a less than optimal way. But, there is a way to disable this math parsing and to convince Plots to use it:

```julia
julia> import Plots: gr_text

julia> gr_text(x, y, s::AbstractString) =
           if (occursin('\\', s) || occursin("10^{", s)) &&
              match(r".*\$[^\$]+?\$.*", String(s)) === nothing
               GR.textext(x, y, s)
           else
               GR.textx(x, y, s, UInt32(0))
           end
gr_text (generic function with 2 methods)

```

After this fixup (the change is in the `GR.textx` line), the code in the OP:

```julia
str = "if you give me \$2 then i will give you \$1"
x=1:5
y=1:5
plot(x, y, title = str, label="")

```

works well.
