# Passing variables to raw strings?

**URL:** <https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951>\
**Category:** New to Julia\
**Created:** [September 20, 2020, 10:44am UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951 "2020-09-20T10:44:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 20, 2020, 10:44am UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/1 "2020-09-20T10:44:13Z")

</div>

Hello!

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

```julia
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:

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

```

Kind regards

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 20, 2020, 11:20am UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/2 "2020-09-20T11:20:44Z")

</div>

You can concatenate a raw string with a regular string:

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

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 20, 2020, 12:17pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/3 "2020-09-20T12:17:33Z")

</div>

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

```julia
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

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 20, 2020, 12:23pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/4 "2020-09-20T12:23:46Z")

</div>

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](https://en.m.wikipedia.org/wiki/XY_problem) to me. What do you actually want to achieve?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 20, 2020, 12:25pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/5 "2020-09-20T12:25:04Z")

</div>

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 🙂

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

Kind regards

EDIT: An example shown below in TexStudio

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/dee6e7c37042cab70017a8c4d1cb3ca2e01bfd21.png)

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.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 20, 2020, 12:43pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/6 "2020-09-20T12:43:51Z")

</div>

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

```julia
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](https://github.com/stevengj/LaTeXStrings.jl/issues/27#issuecomment-655620984)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 20, 2020, 1:19pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/7 "2020-09-20T13:19:38Z")

</div>

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

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 20, 2020, 1:36pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/8 "2020-09-20T13:36:52Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 20, 2020, 1:47pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/9 "2020-09-20T13:47:28Z")

</div>

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](https://github.com/stevengj/LaTeXStrings.jl/pull/40). It uses `%$` for interpolation, as proposed in that issue, which shouldn’t really conflict with anything else. Let’s see how this goes.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 20, 2020, 1:57pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/10 "2020-09-20T13:57:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 20, 2020, 2:07pm UTC](https://discourse.julialang.org/t/passing-variables-to-raw-strings/46951/11 "2020-09-20T14:07:15Z")

</div>

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
