6
votes

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.

1
If you have an answer, do not edit your original question, post an answer and accept it.om-nom-nom

1 Answers

10
votes

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, only available until Scala 2.13.1, see PR #8282 and ee8c1ef8).

One of workarounds suggested in the SI-4706 issue is separating the backslash character:

scala> """\""" + """usepackage{X}"""
res1: String = \usepackage{X}