Re: [R] Backslash in sub pattern?

From: Marc Schwartz <>
Date: Mon, 14 Jul 2008 13:23:34 -0500

on 07/14/2008 01:09 PM Stephan Kolassa wrote:
> Dear guRus,
> I am trying to replace "~" by "$\sim$" for TeX. However, I can't get the
> backslash to work. I would like to turn "DV~IV" into "DV$\sim$IV".
> sub("~","$\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\\\sim$","DV~IV") => "DV$\\sim$IV"

> Alternatives 1 and 3 also yield warnings about unknown escape sequences
> (no surprise there). I have been unsuccessfully searching the archives.
> sessionInfo below.

> Thank you all for your time!

> Best regards
> Stephan

You want door number 4 because when the double escape is cat()'d to the .tex file, you get:

 > cat("DV$\\sim$IV", "\n")

which is what you want latex to process.

HTH, Marc Schwartz mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 14 Jul 2008 - 18:28:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Jul 2008 - 18:32:02 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive