Building on my question here: Include Sweave input as verbatim

Is there a way to include the term \Sexpr{ in the Sweave document as verbatim. For example, to write:

R code can be evaluated inline with the \Sexpr{} function:

Followed by an example of LaTeX code with the \Sexpr{} command in a verbatim environment.

My first guess was to write an R function that generates it:

sxpr <- function(x)

So that I could write \Sexpr{sxpr('')}, however, apparently Sweave runs many times and this does not work. My second guess was to make a seperate file with only \Sexpr{ in it and input it. This works, but not in a verbatim environment.

Is it possible?


I'd do it on the LaTeX side instead of the R side. Within a verbatim can be tricky, though; I used Verbatim from fancyvrb, which requires alternate characters for \{} that aren't used in your verbatim chunk, I used |[] below but you may need to change that.

The result of \ShowSexpr{1+1} is \Sexpr{1+1}.
Here's part of my Sweave file: |VerbSexpr{1+1}

See [ How does one insert a backslash or a tilde (~) into LaTeX?) for where I got the backslash code. Also see [ fancyvrb alternate commandchars and \textcolor ] for an example of Verbatim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.