Question

In a Scala String need to include this literal verbatim: \usepackage{x}. Thus, desired would be that for

val s = """ ... \usepackage{X} ... """

println(s)
... \usepackage{X} ...

Attempts so far include,

scala> """\usepackage{X}"""
<console>:1: error: error in unicode escape
       """\usepackage{X}"""
            ^

scala> raw"""\usepackage{X}"""
<console>:1: error: error in unicode escape
       raw"""\usepackage{X}"""
               ^

Single double-quoted strings prove unsuccessful as well.

Following http://docs.scala-lang.org/overviews/core/string-interpolation.html , a working example includes

scala> raw"a\nb"
res1: String = a\nb

which does not cover unicode cases.

Était-ce utile?

La solution

You appear to be facing issue SI-4706: Unicode literal syntax thwarts common use cases for triple-quotes.

In Scala, unicode escape sequences are processed not only inside character or string literals. It may not be obvious that the following code would work:

scala> 5 \u002B 10
res0: Int = 15

Unfortunately, there doesn't seem to be a good way around this if you don't want to disable unicode escapes completely (-Xno-uescape). One of workarounds suggested in the issue is separating the backslash character:

scala> """\""" + """usepackage{X}"""
res1: String = \usepackage{X}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top