8
votes

I have written some custom R code that wraps a third-party binary. One of that binary's features is to produce a LaTeX document with a figure and some text. One of my code's features is to parse that document and return the LaTeX code for the figure.

The goal is to embed my R code in an Rnw document. When Sweave is run, I want my code to produce a document using the third-party binary, then extract the LaTeX code for the figure and drop it into Sweave's .tex output. Then when I run latex against that output the figure that was generated by the third-party binary should appear, automagically and nicely formatted, in my report.

Instead, the LaTeX code is printed out like this:

[1] " %\\begin{landscape}"
[1] " \\begin{center}"
[1] "\\psset{linecolor=black,tnsep=2pt,tnheight=0cm,treesep=.3cm,levelsep=40pt,radius[1] "% \\def\\psedge#1#2{\\ncangle{#2}{#1}}"
[1] "% \\pstree[treemode=R]"
[1] " \\pstree{\\Tcircle{ 1 }~[tnpos=l]{\\shortstack[r]{nwsprec\\\\$\\leq$ 1.93}}}{"

And so on...

Is there a way to make Sweave treat R's output as LaTeX code?

Thanks in advance. -Wesley

1
The brew package offers an alternative: you first process the code with ?brew(), generating a Sweave file.baptiste
I don't know from brew, but I was able to get my document to compile. The Sweave option line needs to look like this: <<echo=False, results=tex, include=True>>= (results=tex is the crucial part)Wesley
I also had to switch from the print() command in R to the cat() command, which eliminated the nasty mis-formatting.Wesley
glad you got it figured out. You're encouraged to answer your own question and accept it for others who come to this question in the future.Chase

1 Answers

10
votes

I figured it out! The Sweave code needs to look like this:

<<echo=False, results=tex, include=True>>=
...R code goes here...
@

Where the option results=tex is the crucial change that tells Sweave to interpret the output from R as LaTeX code.

And to get rid of the quotes and line numbers, I needed to use cat command in R rather than print. Specifically, I changed print(line) to cat( paste(line, "\n", sep='') ).