5

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?

1 Answer 1

4

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .