# String insertion in string

**URL:** <https://discourse.julialang.org/t/string-insertion-in-string/19990>\
**Category:** New to Julia\
**Created:** [January 23, 2019, 3:32pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990 "2019-01-23T15:32:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 23, 2019, 3:32pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/1 "2019-01-23T15:32:02Z")

</div>

Probably a silly/simple question… I have tried to strip down the essence of my problem to illustrate what I want to do — the example doesn’t fully illustrate the usefulness:

The idea is to define a label for a plot using LaTeXStrings notation, say:

```julia
plot!(label=L"x")

```

and replace the `x` with something else, say `L"\frac{\Theta}{T}"`.

So my attempt was:

```julia
xx = L"\frac{\Theta}{T}"
plot(f,label=L"$(xx)$")

```

This doesn’t work.

I hope this gives an idea of what I want to do — the `x` I have used in the example will in reality be some `f(x)`… if I insert the true expression for `x` (say, `L"\frac{\Theta}{T}"`), the label expression becomes long/cumbersome to type, and I’m hoping I can replace the `x` by interpolation/substitution – somewhat like macros in LaTeX.

Any ideas of how to solve this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 23, 2019, 3:39pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/2 "2019-01-23T15:39:41Z")

</div>

You can use the `LaTeXString` constructor (which accepts string interpolation because it is not a string macro) but it removes quite a few of the advantages of using `LaTeXStrings` in the first place.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 23, 2019, 3:47pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/3 "2019-01-23T15:47:05Z")

</div>

Hm… sounds complicated for a “freshman”. I’ll think about it.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 23, 2019, 4:10pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/5 "2019-01-23T16:10:27Z")

</div>

Please use esc and this macro is useless. (This is replying to the LL string macro, the reply to is not working on thepl mobile page…)

The whole point of the LaTeX string literal is due to the syntax conflict so it’s impossible to use just the LaTeX string literal for what you want.

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [January 23, 2019, 4:12pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/6 "2019-01-23T16:12:31Z")

</div>

It looks as if you want:

```julia
plot!(label=LaTeXString("$x"))

```

which is what Kristoffer suggested.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 23, 2019, 4:30pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/7 "2019-01-23T16:30:55Z")

</div>

Hm… it’s getting closer, but is not there yet:

```julia
xx = L"\frac{\Theta_\mathrm{E}}{T_0}"
label=LaTeXString("3($xx)^2\frac{\exp($xx)}{(\exp($xx)-1)^2} 

```

produces:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/9/79d1f7d69cc298caa69f3ed31b68500321b4fe45.png)  
… I meant it to look like:

![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/afb011eb248907cced1f51b6d1bdbb9de8fdf61b.png)

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [January 23, 2019, 4:35pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/8 "2019-01-23T16:35:15Z")

</div>

you need to escape your backslashes.

```julia
xx = L"\frac{\Theta_\mathrm{E}}{T_0}"
label=LaTeXString("3($xx)^2\\frac{\\exp($xx)}{(\\exp($xx)-1)^2}")

```

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [January 23, 2019, 4:45pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/9 "2019-01-23T16:45:52Z")

</div>

I tried this out and saw that it is not sufficient. It is essentially an escaping problem and I fixed it by:

```julia
xx = raw"\frac{\Theta_\mathrm{E}}{T_0}"
label=latexstring("3($xx)^2 \\frac{ \\exp($xx)}{( \\exp($xx)-1)^2}")

```

the additional problem was that `L"..."` syntax surrounds the string with `$ ... $`. Using a raw string fixed this.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 23, 2019, 4:55pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/10 "2019-01-23T16:55:44Z")

</div>

Thanks, `korsbo`. In addition to switching from `L"..."` to `raw"..."`, it was also necessary to switch from `LaTeXString(...)` to `latexstring(...)`.

To make it pretty (with flexible parenthesis), it got quite complex, though:

```julia
xx = raw"\frac{\Theta_\mathrm{E}}{T_0}"
label=latexstring("3\\left($xx\\right)^2\\frac{\\exp\\left($xx\\right)}{\\left(\\exp\\left($xx\\right)-1\\right)^2}")

```

leading to what I wanted.

Thanks again. It could probably have been automated somewhat, but perhaps the problem is rare so it is not worthwhile…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 23, 2019, 6:30pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/11 "2019-01-23T18:30:31Z")

</div>

FWIW, I find that simply constructing the strings is much less error prone than auto-appending the `$`s.

Eg

```julia
# this ends up being nicer for complex expressions, YMMV
raw"$" * string(x) * raw"$"

# or just escape the $
"\$$(x)\$"

```

---

<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:** [January 23, 2019, 7:33pm UTC](https://discourse.julialang.org/t/string-insertion-in-string/19990/12 "2019-01-23T19:33:58Z")

</div>

> [@kristoffer.carlsson](#):
>
> You can use the `LaTeXString` constructor (which accepts string interpolation because it is not a string macro) but it removes quite a few of the advantages of using `LaTeXStrings` in the first place.

If you are just using this for plot labels and are willing to escape all of the backslashes etcetera, you don’t need to use `LaTeXString` at all — at the end of the day, it is just passed as an ordinary string to the plot backend.

One thing that would be nice would be for LaTeXString to support some custom interpolation syntax, e.g. `L"\frac{\Theta}{$$x}"`, so that you get the advantage of not having to escape the backslashes etcetera but you can still interpolate. I’m not sure what syntax to use, though. `$$x` seems problematic because `$$` in LaTeX is a display equation.
