I got 2 files from my university for writing thesis using LaTeX. One is a .sty file and other one is .TeX file. In order to work in R studio I've decided to have separate .Rnw files for each chapter and one file for combining all the chapters. I think .TeX file is the one where I can combine all the chapters because it gives sample chapters in output. On R studio's website there is a page titled as 'Working with Multiple Rnw Files' which describes this process (I guess) but is not clear to me. It talks about 'child' files which I think are the chapters in my case. So my simple question is that if I create different .Rnw files, one for each chapter, how can I ask R to combine them in one TeX file which university provided me? Please bear my ignorance as I am new to reproducible research stuff.
2 回答
假设您使用的是 knitr(我强烈推荐 knitr 而不是 sweave),简单的方法是使用child
chunk 选项。
例如,假设您有 2 个章节,分别保存在文件中chap1.Rnw
和chap2.Rnw
一个主文档thesis.Rnw
(大学风格的文件名为thesisStyle
)。你可以把这些放在一起,在里面thesis.Rnw
——假设它们都在同一个目录中——通过:
\documentclass{article}
\usepackage{thesisStyle}
\begin{document}
% "include" chapter 1
<<chap1, child='chapt1.Rnw'>>=
@
% again with chapter 2
<<chap2, child='chap2.Rnw'>>=
@
\end{document}
然后只需编译 RStudio thesis.Rnw
,它就会吐出thesis.tex
,它将所有内容正确捆绑在一起。
但这还不是全部!您可以进行开发chap1.Rnw
,而不必给它自己的序言。也就是说,如果 的内容chap1.Rnw
是
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
@
\chapter{In a world where...}
\section{Great voice actors in movie trailer history}
ANYTHING YOU'D NORMALLY PUT IN AN .Rnw FILE
然后您可以chap1.Rnw
像任何常规 .Rnw 文件一样进行编译,它会thesis.Rnw
在运行您正在使用的任何 TeX 后端(通常是 pdflatex 或 xelatex)之前获取序言。特别是 knitr\documentclass{article}
会\usepackage{thesisStyle}
在chapt1.tex
.
需要注意的是,我发现 knitr 中的子父模型对空格敏感。因此,请确保块上方没有空间
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
@
You have a couple of options.
One option is to just process each of your chapters by hand. You will have a .Rnw file for each chapter, then in Rstudio (or R) you run the knit
function from the knitr package (there may be an Rstudio button or menu to do this directly) to convert your .Rnw file to a .tex file. Then in the parent LaTeX document you just use \include
to include the .tex files for each chapter. This does mean processing each chapter yourself and having to go back and redo it anytime you change anything.
The other option is to create a parent and child documents that knitr will understand and process automatically for you (Rstudio is using knitr to do the processing to .tex and eventually .pdf files). This page has demonstrations on creating the parent and child documents this way, just modify the .tex file given to you to include the important things in the demos (and probably change the name to .Rnw). Make sure that the document class matches the .sty file given to you and the important options from the .tex file remain, but include the child documents as shown in the knitr demo. This way you can process the document as a whole rather than each individual chapter.