Working with Rstudio for writing thesis chapters in .Rnw - r

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.

Assuming you're using knitr (and I highly recommend knitr over sweave) the simple way to do this is with the child chunk option.
As an example, say you had 2 chapters, kept in files chap1.Rnw and chap2.Rnw and a master document thesis.Rnw (with university style file called thesisStyle). You can put these all together, inside thesis.Rnw -- assuming these are all in the same directory -- via:
\documentclass{article}
\usepackage{thesisStyle}
\begin{document}
% "include" chapter 1
<<chap1, child='chapt1.Rnw'>>=
#
% again with chapter 2
<<chap2, child='chap2.Rnw'>>=
#
\end{document}
Then just have RStudio compile thesis.Rnw and it'll spit out thesis.tex which will have everything bundled together properly.
That's not all, though! You can develop chap1.Rnw without having to give it its own preamble. That is, if the content of chap1.Rnw is
<<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
then you can compile chap1.Rnw like any regular .Rnw file and it'll take the preamble from thesis.Rnw before running whatever TeX backend you're using (normally pdflatex or xelatex). In particular, knitr will slap the \documentclass{article} and \usepackage{thesisStyle} lines at the top of chapt1.tex.
One word of caution, I've found the child-parent model in knitr to be white-space sensitive. So, be sure to have no space above the block
<<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.

Related

Automating adding .tex file content to .Rnw files

I use a program to produce a series of .tex files. I then manually copy the contents of the .tex files and add it to a .Rnw file (Sweave, compiled in RStudio IDE). Is there a way to automatically include the contents of .tex files in the .Rnw file such that the .tex files automatically compile and produce pretty text/figures/tables rather than require copying? Something like the below does not appear to produce what I desire when compiled.
<<>>=
# file.tex contains the code for a pretty LaTeX table
source("file.tex")
#
source is wrong, that is for running R code. Instead, if you are using knitr, you can use the child tag in a knitr chunk, something like:
<<my-child-document, child="file.tex">>=
#

Bookdown: Is it possible to "split_by" at the subsection level?

I use the HTML and PDF outputs from bookdown as instructional material in a programming course, and I'd like to achieve the "page break" effect at the subsection level in addition to the chapter and section levels.
From what I have read in the bookdown documentation, "subsection" is not a permissible setting for the split_by argument in my bookdown's "_output.yaml" file.
bookdown::gitbook:
split_by: section
Is there a workaround besides creating individual books for each chapter (thus turning my current book-level subsections into chapter-level sections)? I strongly prefer having one book at the end.
A work around might be using "split_by: rmd"; see this answer for reference.
Then you put contents in each subsection into a separate Rmd file.
I tried this, and it works. The drawback is that I always receive this message when preview the book in Rstudio:
In split_chapters(output, gitbook_page, number_sections, split_by, :
You have 5 Rmd input file(s) but only 3 first-level heading(s). Did
you forget first-level headings in certain Rmd files?
Also, I don't know if there are other drawbacks.

knitr: automate producing multiple versions of PDF for beamer slides

For lectures, I am using knitr to produce LaTeX beamer slides as a PDF. For a given lecture, I want to produce also (a) 1-up handout (using the handout option, and (b) the same handout formatted 4-up.
I find I have to run knitr 3 times to do this as shown below. Is there a way to simplify this work flow?
A lecture stub:
\documentclass[10pt,table]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
...
\end{document}
And I run knitr as
knit2pdf("Lecture1.Rnw")
To get the 1-up handout (which suppresses the separate pages when you use transitions), I edit the first line to:
\documentclass[10pt,table,handout]{beamer}
and run
knit2pdf("Lecture1.Rnw" output="Lecture1-1up.tex")
Finally, to get the 2 x 2 version, I use the LaTeX pgfpages package,
\documentclass[10pt,table,handout]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[letterpaper,landscape]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
And run:
knit2pdf("Lecture1.Rnw" output="Lecture1-4up.tex")
(I found that with beamer, I could not simply print the PDF 4-up using Adobe Acrobat -- it generated a corrupt PDF file. I was forced to use pgfpages)
Then, of course I have to revert my .Rnw file to the original if I need re-do the slides. Very tedious. There must be a better way.

Source or input or include Rmd file in a Rnw document

I am trying to write my paper and using Elsevier document class as a Rnw document. I also tried to use rticle package which I find little difficult to customise. Is it possible to include Rmd file in my Rnw document so that I can use the Rnw frame and also use my Rmd chapters for other purposes like comple it to HTML.
As suggested by r2evans, I want to post what I have found as an answer to my own question. It is not a direct answer to my question but I will be following this for now. Instead of using Rnw file, Rmd file can use tex template. The process is described in this site.

sourcing References in rmarkdown: workaround

I am working with markdonw v2, the rmarkdown package. Throughout the .Rmd file, I create links to websites or images
[Link1][pathLink1]
![Image1][pathImage1]
then, at the end of the document I give the references
[pathLink1]:http://website.com/linkes/Link1.md
![pathImage1]:./images_rmd/
There are other reports that talk about the same citation and use same images in different contexts. I would like to create a separate file containing all the links and path difinitions, so that I could simply source it at the end of each .Rmd file, like I would call in an R environment
source(/Rcode1.R)
Question: How do I "source" another file in .Rmd, so that the sourced code prints needed text strings into the .Rmd file?
This would offer some help with citations and scientific paper composition in HTML and PDF.
http://yihui.name/knitr/demo/child/
```{r child, child = '~/path/to/child.Rmd'}
```
and similarly for .Rnw files:
<<child, child = '~/path/to/child.Rnw'>>=
#
And a full example: https://github.com/yihui/knitr-examples/blob/master/087-child-example.Rnw

Resources