LaTeXStrings: mix Tex and value of pre-existing variable in string

I’m trying to use LaTeXStrings in Julia 1.1 and PyPlot to programmatically label various subplots. For example, I have a collection of indexed variables \omega_i and \tau_i, but I only want to plot a specific subset of them, e.g., \omega_1, \omega_3, \tau_3, and \tau_4. I’m trying to generate an array labels similar to the below

labels = [[L"$\omega_{$(i)$}$" for i in omega_indices]; 
          [L"$\tau_{$(i)$}$" for i in tau_indices]]

but am unable to bring the value of i into the string expression. I’ve also tried

labels = [[L"$\omega_$"*"$(i)" for i in omega_indices]; 
          [L"$\tau_$"*"$(i)" for i in tau_indices]]

without luck. Is there aproper way to do so?

1 Like
i = 1
latexstring("\$\\omega_{$(i)}\$")
# L"$\omega_{1}$"
2 Likes

@mohamed82008 already provided a solution using LaTeXStrings, but generally I would go with raw strings in plain Julia, eg

raw"$\omega_{" * string(i) * raw"}$"

or

raw"$\omega_" * "{$(i)}" * raw"}$"

All that escaping can become a headache very easily.

3 Likes