String insertion in string


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:


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

So my attempt was:

xx = L"\frac{\Theta}{T}"

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?


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.


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


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.


It looks as if you want:


which is what Kristoffer suggested.


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

xx = L"\frac{\Theta_\mathrm{E}}{T_0}"

… I meant it to look like:



you need to escape your backslashes.

xx = L"\frac{\Theta_\mathrm{E}}{T_0}"


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

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.


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:

xx = raw"\frac{\Theta_\mathrm{E}}{T_0}"

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…


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


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

# or just escape the $


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.