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.
A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.
A string literal is a sequence of characters in double quotes. The characters are either printable unicode character or are described by escape sequences. If the string literal contains a double quote character, it must be escaped, i.e. "\"" . The value of a string literal is an instance of class String .
According to section 3.3 of the Java Language Specification (JLS) a unicode escape consists of a backslash character (\) followed by one or more 'u' characters and four hexadecimal digits.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With