String insertion in string


#1

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:

plot!(label=L"x")

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

So my attempt was:

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?


#2

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.


#3

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


#5

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.


#6

It looks as if you want:

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

which is what Kristoffer suggested.


#7

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

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

produces:
image
… I meant it to look like:

image


#8

you need to escape your backslashes.

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

#9

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.


#10

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}"
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…


#11

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

Eg

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

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

#12

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.