Question

I want to let the chunk itself be displayed as text inside the listings environment. If I enter the chunk inside listings as below it is getting evaluated.

\documentclass{article}
\usepackage{listings}

\begin{document}

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

\end{document}

Is there a way that does not include to comment <<>>= and @ out with %?

Was it helpful?

Solution

Incorporating a literal code chunk is knitr FAQ 8. For this case, use

\documentclass{article}
\begin{document}
<<setup, include=FALSE>>=
render_listings()
@

\begin{lstlisting}
\Sexpr{''}<<>>=
1+1
@
\end{lstlisting}

\end{document}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top