문제

In Python one can write r"a\nb" in order to prevent the \n from being interpreted as an escape sequence for newline.

Is there something similar in Julia? And what about string interpolation like "$variable", is there a way to prevent it?

I know one can simply write "a\\nb" and "\$variable" in Julia, but I would like to write a lot of LaTeX strings without having to care to proper escape every backslash \ and dollar $ characters...

(In Julia, r"..." creates a regular expression.)

도움이 되었습니까?

해결책 2

From Julia 0.6 we have raw"\a$variable"

다른 팁

Ok, I just found out that since one can easily create non-standard string literals in Julia, a pass-through one will do what I was asking for:

macro R_str(s)
    s
end

>>> R"$a\n$b"
"\$a\\n\$b"
>>> print(R"$a\n$b")
$a\n$b

I also discovered that PyPlot.jl defines a «LaTeXString type which can be constructed via L"...." without escaping backslashes or dollar signs», with the additional benefit of rendered equations in IJulia.

A last question remains: is not worth it to have a raw string literal in Julia base?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top