knitr parses extra verbatim documentclass

Viewed 42

I'd like to include an example of complete LaTeX code inside an Rnw document that is parsed using knitr.

My .Rnw file is shown here (I really want to include a bunch of R code as well but this minimal example shows my problem)

\documentclass{article}

\begin{document}    
Recursiveness, see     
\begin{verbatim}
\documentclass{article}
\begin{document}
Recursiveness
\end{document}
\end{verbatim}

\end{document}

When I run knitr test.Rnw I get a .tex file where the second documentclass is replaced (see below, where the top of the .tex file is shown). I'm pretty sure that this is knitr that replaces the litaral string \documentclass with its own macro that and it does not realize that it shouldn't be parsen when inside the verbatim environment.

\documentclass{article}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

Recursiveness, see 

\begin{verbatim}
\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

Is it possible to circumvent this? I tried to insert a zero-width space character in the middle of documentclass but that gave me a bunch of headaches with the parsing.

1 Answers
Related