TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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