# Include Sweave input as verbatim

How do I keep Sweave from compiling things in a verbatim environment? For example:

\documentclass{article}
\begin{document}
\begin{verbatim}
<<>>=
1+1
@
\end{verbatim}
\end{document}


Results for me in a pdf with:

\begin{Schunk}
\begin{Sinput}
> 1+1
\end{Sinput}
\begin{Soutput}
[1] 2
\end{Soutput}
\end{Schunk}


But I just wanted the input commands.

To be more clear, I want the output in this pdf file to be exactly:

<<>>=
1+1
@

-

## migrated from stackoverflow.comNov 20 '11 at 15:47

This question came from our site for professional and enthusiast programmers.

just add <<eval = F>> instead of <<>> to suppress evaluation of the code. i presume you want begin{Schunk} and end{Schunk} to be displayed. –  Ramnath Nov 20 '11 at 14:05
Thanks. but I just want the input without anything including the <<>>= and @ signs. Its for lecture slides on Sweave:) –  Sacha Epskamp Nov 20 '11 at 14:15

If you're willing to add a single space before the <<>> and the @, that'll do the trick. (The Sweave driver only interprets as chunks blocks of text beginning with <<>>= at the beginning of a line -- no spaces allowed!)

Your input file will now look like this:

\documentclass{article}
\begin{document}
\begin{verbatim}
<<>>=
1+1
@
\end{verbatim}
\end{document}


And your output file will look like this, possibly indented, but without leading spaces:

 <<>>=
1+1
@


Alternatively, you can use a chunk of R code that, when Sweave'd, uses cat() to output the desired tex code. This solution is probably formally better (in some sense), and also works as desired:

<<results=tex, echo=FALSE>>=
cat("",
"\\begin{verbatim}",
"<<>>=\n",
"1+1\n",
"@\n",
"\\end{verbatim}")
@

-
I ended up just using \Sexpr{"<<>>="} and \Sexpr{"@"} :) thanks! –  Sacha Epskamp Nov 22 '11 at 23:07

You have your Sweave file (I call it "test.Rnw") that contains:

\documentclass[11pt,a4paper]{article}
\usepackage{Sweave}
\begin{document}

\begin{verbatim}
<<>>=
1+1
@
\end{verbatim}

\input{test.txt}

\end{document}


And you have the file with the stuff you want to show verbatim, including the <<>>= and @ signs (I call it "test.txt"):

\begin{verbatim}
<<>>=
1+1
@
\end{verbatim}


When you know > Sweave( "test.Rnw" ) you get this test.tex file

\documentclass[11pt,a4paper]{article}
\usepackage{Sweave}
\begin{document}

\begin{verbatim}
\begin{Schunk}
\begin{Sinput}
> 1 + 1
\end{Sinput}
\begin{Soutput}
[1] 2
\end{Soutput}
\end{Schunk}
\end{verbatim}

\input{test.txt}

\end{document}


which produces, according to my understanding, exactly what you want:

Or did I still not get it?

-
So what you're suggesting is that the OP write the contents to a file (say test.txt) and \input it rather than coding it directly in the file, otherwise sweave will modify it? That seems to be the case here. Moreover, this a complete departure from your previous answer. Is that still valid? –  Werner Nov 20 '11 at 16:50
Thanks. I don't care about the resulting .tex code. I just want to show the Sweave code and what it will result to in the pdf file. This can be done with inputting the text file but that seems so overdoing this? I'll probably have like 10 different examples and having 10 files to input seems unnecessary. I could of course write an R function to return a verbatim environment but that also seems a weird way of doing this. Surely there is an easier method? –  Sacha Epskamp Nov 20 '11 at 17:01
A complete departure from the first answer, yes - I did not understand the question correctly. Not a departure from the second answer in the comment - the comment format was not good, so I wrote it as a new answer with complete file contents. Yes, your understanding of my suggestion is correct, and it achieves the result the OP wants (as I understand it). Sorry for the confusion but I think the solution I suggest here will work. –  vaettchen Nov 20 '11 at 17:02
@SachaEpskamp: You can write a single file that writes contents to another file and eventually include it using \input. This functionality is provided by the filecontents package. As mentioned, that way you keep a single .tex file that both writes contents to some file and reads it back in. A clean-up script could remove these additionally-generated files. –  Werner Nov 20 '11 at 17:06
<<eval=FALSE,echo=FALSE>>=


should do the job!

-
Thanks, but this just returns nothing at all. I want the Sweave code to be included (see edited answer) –  Sacha Epskamp Nov 20 '11 at 15:02
Not 100% sure whether I get you right but from what I understand, this method should help: Type the sweave code that you want to show into a separate text file ("svw.txt"), beginning with \begin{verbatim} and closing with \end{verbatim}. In your main document, \input{swv.txt}. –  vaettchen Nov 20 '11 at 15:36
Is this still valid given your new answer? –  Werner Nov 20 '11 at 16:52
I believe vaettchen's answer is correct. Sweave matches the code chunks using regular expressions, so if you just put <<>>= in your document, Sweave will try to treat it as an R code chunk anyway (just because it matches with ^<<.*>>=). Therefore you have to cheat Sweave by using other commands to include <<>>= in your document, and \input{} is a good approach. –  Yihui Nov 20 '11 at 17:17