2014-01-13 34 views
23

在寫論文時,我通常使用knitr嵌入我在R中生成的表格和圖表。所有這些對我來說都非常有效。然而,我的一些合作者對這個工作流程並不熱衷,而更願意留給我的互動knitr給我,並專心寫他們的部分,而不必打擾R代碼。他們也不用安裝R,RStudio和各種軟件包。製作可選編織物

那麼,是否有任何方式排版與嵌入式knitr塊LaTeX文件,而不必先通過R運行它們?換句話說,有沒有辦法在排版過程中簡單地忽略塊(或者用虛擬表格/圖表替換它們)?

+8

你可以在'.Rtex'格式,這使將R塊爲乳膠註釋行寫:見https://github.com/yihui/knitr-examples/blob/master/005-latex。 Rtex –

+0

@BenBolker優秀!感謝指針。這意味着,但是,大塊必須是完全獨立的,對吧?我不能有一個產生'\ includegraphic {}'的塊並將該塊包裝在'figure'環境中,因爲常規的LaTeX會阻塞空的浮動塊。 – RoyalTS

+0

或嘗試刪除多行分隔的塊..。 http://unix.stackexchange.com/questions/10226/multiline-pattern-match-using-sed-awk-or-grep? –

回答

2

更新:修訂說明here

這不回答這個確切問題,但也許用例。最近我有一個類似的挑戰:我想將文字和分析合併到一個.rnw文件中,但我的合作者不想使用R/RStudio/GitHub/LaTeX。

因此,我決定通過Dropbox與他們共享我的git回購的子文件夾。此文件夾包含三個.docx文件:introduction.docxmethods.docxdiscussion.docx(我在.rnw文件中寫入結果部分)。唯一的問題是,他們在寫作時必須使用一些非常基本的LaTeX,例如\subsection{heading}用於標題,\cite{key}用於引用,「引號」,轉義\%,\ $和\ &。

早在.rnw文件,我的.docx文件轉換爲.txt:然後

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

之外:

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

,然後將文件擴展名重命名從.txt.texR代碼塊,我打電話給.tex f與爾斯:

\input{introduction}

我貼一個small example到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} 
+0

這很不錯(例如,您可以更有趣並閱讀Google文檔中的部分)!但我真正想要的是讓我的合作者能夠像使用任何舊LaTeX文檔一樣使用knitr文檔,也就是說,我希望他們能夠在沒有我的幫助下自行排版文檔。 – RoyalTS

+0

謝謝。我認爲Google文檔也可以工作。我的合作伙伴是非常傳統的Word /跟蹤更改人員,但我可以看到Google Docs如何更好。 –