Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)
{
  paste(paste("\\\\","Se","","xpr{",sep=""),x,"}",sep="")
}

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
add comment

1 Answer

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.

\documentclass{article}
\newcommand{\ShowSexpr}[1]{\texttt{{\char`\\}Sexpr\{#1\}}}
\begin{document}
The result of \ShowSexpr{1+1} is \Sexpr{1+1}.
\begin{Verbatim}[commandchars=|\[\]]
Here's part of my Sweave file: |VerbSexpr{1+1}
\end{Verbatim}
\end{document}

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
add comment

Your Answer

 
discard

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.