Passing variables to raw strings?

Hello!

What I really like about Julia is that it is possible to write something as this:

a = 2
t  = "The number of apples is $a"
println(t)
     The number of apples is 2

Sometimes though I need to work with raw strings / latex strings (using LaTeXStrings) and then this kind of syntax does not work.

Is it possible to use raw strings in connection with this syntax? Or do I have to use a normal string, in which I escape every char?

Example that will not work:

a = 2
t  = raw"The number of apples is $a"
println(t)
     The number of apples is \$a

Kind regards

2 Likes

You can concatenate a raw string with a regular string:

julia> a = 2;                                                                      
                                                                          
julia> s = raw"The number of apples is " * "$a";                          
                                                                          
julia> println(s)                                                         
The number of apples is 2
5 Likes

Thanks it works for this simple case, but suppose I have a case in which it is more like:

a = 1
b = 2
c = 3

t = raw"I have $a apple, $b oranges and $c birds on my roof"

Then I would have to split it into a lot of substrings and contacenate I suppose? I thought there might be a way to “overrule” the “raw” command, even though I see why this would go against its purpose…

Kind regards

1 Like

It’s not clear to me, why you want to use raw in the first place, and not just use regular strings and prefix special characters with \. This sounds a bit like an XY problem to me. What do you actually want to achieve?

2 Likes

I am passing a lot of raw LaTeX code which consistently has to be escaped, unless I use “raw”. It is just much easier to write “raw” than having to manually espace everything whenever I do a change :slight_smile:

Since the numeric values can be updated I hoped to use Julia’s $-syntax

Kind regards

EDIT: An example shown below in TexStudio

I have this saved as a “raw” string in Julia and it works just fine. Say I wanted to change the yellow number using a variable, $value, then this is not currently possible using “raw” strings.

1 Like

You could use the LaTeXString constructor directly, but then you would need to escape all $ and \ characters

julia> x = 3
3

julia> LaTeXString("\$x = $x\$")
L"$x = 3$"

Alternatively, perhaps this comment on the corresponding LaTeXStrings issue is helpful:

https://github.com/stevengj/LaTeXStrings.jl/issues/27#issuecomment-655620984

2 Likes

Seems like it should be possible to create yet another _str macro that will do this, using some never-used, outlandish character as the replacement interpolation character (to avoid ever clashing with anything). I.e. something like

interp_L"x = ⊜x"

that would regex scan for “⊜…” (\circledequal, just as an idea) and interpolate the string accordingly. If I knew how to work with regexes, I’d be happy to hack one together. The general idea would look something like this I think, if someone knows how to fill in the crucial pieces:

macro myraw_str(s)
    str = String[]
    # Not the right regex. Needs to match ⊜x, ⊜(x + y), etc.
    for m in eachmatch(r"⊜", s)
        # split the string into before the match, the match itself and everything after
        # replace the match with get(@__MODULE__, Symbol(matched_var))
        # push to str
    end
    isempty(str) ? return s : join(str)
end

You could pass the result from this on to L"" to get latex behavior involved.

3 Likes

I think this could be a very good approach! I tried @simeonschaub suggestion with LaTexString, but found that it was easier for me to just escape everything anyways, so that is what I ended up doing

I think one key aspect would be an option to overload the method with some random variable, so that one is not just stuck using ⊜, in the case that some one is actually using it :slight_smile:

Do you think this would be usable in general for the wider community, or maybe this is a niche case, which should have its own package? Not completely sure if I should post this as a Github issue, linking to this thread.

Kind regards

2 Likes

I think it would make sense for this to be in LaTeXStrings.jl. FWIW, I took a stab at this issue and made a PR. It uses %$ for interpolation, as proposed in that issue, which shouldn’t really conflict with anything else. Let’s see how this goes.

4 Likes

You don’t actually need regexes for this at all. You can just go through the string character by character until you encounter the one used for interpolation and then use Meta.parse(s, i, greedy=false), where s is the string and i is the next index after the interpolation character. This gives you an expression and the index it stopped consuming at and you resume these steps from there.

3 Likes

Thanks for taking iniative!

I think this is a good start, if it is found that this feature will benefit others being in Base, one can always generalize it a bit more, to work both on raw / LaTeXStrings.

Kind regards