lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Matching backslash in Scheme string (escaping characters for LaTeX)


From: Urs Liska
Subject: Matching backslash in Scheme string (escaping characters for LaTeX)
Date: Wed, 21 Jan 2015 08:31:53 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

Hi all,

I have a Scheme function that processes a string and writes the result to a LaTeX file. For this I have to escape a number of characters that are allowed in a LilyPond string but not in LaTeX input.

Consider this code:

\version "2.19.16"
#(use-modules (ice-9 regex))

str = "This should be \partcombine-d & match everything"

ampString = "\\&"
slashString = "\\textbackslash "

#(set! str
       (regexp-substitute/global #f "&" str
         'pre ampString 'post))

#(set! str
       (regexp-substitute/global #f "\\+" str
         'pre slashString 'post))

#(ly:message "")
#(ly:message str)
#(ly:message "")

The resulting output is "This should be \partcombine-d \& match everything" while I want to have "This should be \textbackslash partcombine-d \& match everything".
The escaping of the ampersand works as expected (although I have to define the replacement string in a LilyPond string while entering it directly as a string in the Scheme function doesn't seem to work), but the backslash is not matched.
Using "\" as regex obviously doesn't work because that would escape the quotation mark so the string isn't properly ended.
Using "\\" give the error "Trailing backslash"
Using "\\+" compiles but doesn't match the backslash.

So what's the way to match a backslash in a LilyPond string?

Additionally I'd be glad about comments to:

- Which other characters would I have to escape when writing LaTeX code
  (so far I've encountered the backslash and the ampersand. But I'd feel
  better if I'd not just wait for errors ...)?
- Is there a more efficient way to escape a number of characters
  than piping the string through a series of regex substitutions?

TIA
Urs

reply via email to

[Prev in Thread] Current Thread [Next in Thread]