1

I am trying to define some LaTeX commands/environments to handle the raw text in an Rnw file (R + LaTeX document compiled with R package knitr). The goal is to allow someone to rename a .Rnw file to a .tex file and be able to compile the document so that LaTeX puts placeholders for the code chunks. It is a different approach to the root problem discussed in How to export changes from a .tex file back to the source .Rnw file.

There are two things that need to happen: \Sexpr{} needs to be redefined and the code block sequence

<< chunk-name, option1 = TRUE, option2 = FALSE, ...>>=
... R code ...
@

Needs to be redefined. Redefining \Sexpr{} was easy:

\newcommand{\Sexpr}[1]{\textbf{\#\#}}

However, I am struggling with how to define a control sequence (expansion?) to process <<chunk name, options>>R code@.

Technically, I can skip over all the errors and end up with a LaTeX document that has the code chunks as plain text. I can accept this, but it would be better to output a placeholder, e.g. a box containing the chunk name but definitely not the R code.

EDIT This looks to be in the right direction, but I'm having trouble understanding what is going on: https://tex.stackexchange.com/a/80370/82573

EDIT EDIT So actually the main issue seems to be that underscores _ in R code (such as in function names) are what actually cause the errors, so some way to ignore the code or wrap it in a verbatim environment is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.