23
votes

When writing a paper I generally use knitr to embed tables and plots that I generate in R. All of this works exceptionally well for me. However, some of my coauthors are not as enthusiastic about this workflow and would much rather just leave interactions with knitr to me and concentrate on writing their sections without having to bother with the R code. They would also much rather not have to install R, RStudio and various packages.

So, is there any way of typesetting LaTeX documents with embedded knitr chunks without having to run them through R first? Is there a way, in other words, to simply ignore the chunks during the typesetting process (or perhaps to replace them with dummy tables/plots)?

1
You could write in .Rtex format, which keeps the R chunks as LaTeX-commented lines: see github.com/yihui/knitr-examples/blob/master/005-latex.Rtex - Ben Bolker
knitr chunks will auto-generate figure environments, captions, etc. for you; if you're willing to have the 'raw' version simply missing the figures (rather than having empty figures), you can use a chunk like <<myfigure,fig.cap="myfigure">> (which you would refer to via \ref{fig:myfigure} -- although your co-authors would end up with missing references). Could you just add a \ or something to make the figure not completely empty? - Ben Bolker
Extending @BenBolker's .Rtex suggestion, you could set include=FALSE for each of the figures and then insert them explicitly with your own calls to \begin{figure}; \includegraphics{}; \caption{\label{}}; \end{figure}. Your coauthors, when they don't have access to the figures created by knitr, can compile the document with placeholders for the figures by setting \usepackage[demo]{graphicx} in the document's preamble. With this approach, references will end up pointing to the appropriate figures, which will at least be identifiable by their captions. - Josh O'Brien
there was a feature request for this, but it got sort of side-tracked and closed, unfortunately. - baptiste
something like \renewcommand{\Sexpr}{\texttt} maybe? - baptiste

1 Answers

2
votes

Update: revised description here

This does not answer the exact question, but maybe the use case. I had a similar challenge recently: I wanted to combine the writing and analysis in one .rnw file, but my collaborators didn't want to use R/RStudio/GitHub/LaTeX.

So I decided to share a subfolder of my git repo with them via Dropbox. This folder contains three .docx files: introduction.docx, methods.docx, and discussion.docx (I write the results section within the .rnw file). The only catch is that they have to use some very basic LaTeX when writing, e.g., \subsection{heading} for headings, \cite{key} for references, ``quotes'', escaping \%, \$, and \&.

Back in the .rnw file, I convert the .docx files to .txt:

system("textutil -convert txt introduction.docx")

and then rename the file extension from .txt to .tex:

file.rename("introduction.txt", "introduction.tex")

Then outside of the R code chunks, I call in the .tex files with:

\input{introduction}

I posted a small example to GitHub.

\documentclass{article}
\makeatletter
\renewcommand{\@biblabel}[1]{\quad#1.}
\makeatother
\date{}
\bibliographystyle{plain}

\begin{document}

\begin{flushleft}
{\Large
\textbf{My Title}
}
\end{flushleft}

\section{Introduction}
% do not write in this section...let collaborators write in introduction.docx

<<intro, include=FALSE>>=
# assumes wd set to root folder collaborate
# convert docx to txt
  system("textutil -convert txt introduction.docx")
# rename txt to tex
  file.rename("introduction.txt", "introduction.tex")
@

% pull in introduction.tex
\input{introduction}

\section{Methods}
<<methods, include=FALSE>>=
  system("textutil -convert txt methods.docx")
  file.rename("methods.txt", "methods.tex")
@

\input{methods}

\section{Results}

<<results>>=
  dat <- data.frame(x=runif(30, 0, 30))
  mean <- mean(dat$x, na.rm=TRUE)
@

The mean is \Sexpr{round(mean, 1)}.

\section{Discussion}
<<discussion, include=FALSE>>=
  system("textutil -convert txt discussion.docx")
  file.rename("discussion.txt", "discussion.tex")
@

\input{discussion}

\bibliography{example.bib}
\end{document}