Raw literal strings in Julia

倖福魔咒の 提交于 2019-12-06 19:04:57

问题


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.)


回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/21882353/raw-literal-strings-in-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!