0

I have a string template, read from a file, with the content:

template <- "\\begin{tabular}\n[results]\n\\end{tabular}"

I want to replace results with some text I have generated in my code, to compose a laTeX table, but when I run:

sub("\\[results\\]","text... \\hline text...",template)

I have the following output:

"\\begin{tabular}\ntext... hline text...\n\\end{tabular}"

The backslash is not escaped and I don't understand why, because I'm using \\ for this purpose.

I am using R-3.0.2.

lawyeR
  • 7,488
  • 5
  • 33
  • 63
Jon Cardoso-Silva
  • 991
  • 11
  • 25

1 Answers1

1

The regex engine is consuming the \\ for a potential capture group, you need to add two more backslashes:

sub("\\[results\\]","text... \\\\hline text...",template)
[1] "\\begin{tabular}\ntext... \\hline text...\n\\end{tabular}"
James
  • 65,548
  • 14
  • 155
  • 193