Adding XeTeX to ess-swv-PDF - r

I am working with an .Rnw file with Hebrew in it, and I would like to compile the resulting .tex file using XeTeX. I work in Emacs, so I am using the Emacs Speaks Statistics (ESS) package, which allows a few options for compiling the .tex file. However, using XeTeX is not currently among the possible selections of ess-swv-PDF, accessed via M-n P.
I would like to add XeTeX to this list. Ideally, this added option would allow me to not only compile the .tex file using XeTeX but also run, as needed, BibTeX on the same file—all from a buffer displaying the .Rnw file. This would be similar to texi2pdf, one of the current options of ess-swv-PDF.

This can be customized if you have ESS already installed: M-x customize-group select ess-sweave and edit ess-swv-pdflatex-commands to add "xelatex" to them. If you want to use xelatex or xetex as the default TeX engine, like I do, you can put that in front (see screenshot below).
Then press Apply and save this should save code into your .init.el or .emacs.el, depending on your setting.
Now when you go to your .Rnw file and hit M-n P you will see xelatex as default since you put it ahead of other commands. That is all to it.

Related

Keep auxiliary TeX files when rendering a rmarkdown document

I am rendering two documents, that cross-reference items in each other. For that I am using the LaTeX package zref.
To make zref work, it needs the *.aux file of the documents which are created when calling pdflatex.
Unfortunately, using RStudio and its basic approach to render the document (the knit button, Cmd+Shift+K or rmarkdown::render()) these files will be deleted after the compilation was successful.
Unchecking the global option Tools -> Global Options -> Sweave -> Clean auxiliary output after compile does not help.
I know of two options to go around this:
Manually compile the tex file after the pdf was rendered.
Write a makefile that does that.
But is there another option I am not aware of?
rmarkdown::render() eventually calls tinytex::latexmk() to compile the intermediate .tex to .pdf. To preserve auxiliary files, you need tinytex::latexmk(..., clean = FALSE). One way to set clean = FALSE is through the global option options(tinytex.clean = FALSE). You can set this in either your .Rprofile or a code chunk of your Rmd document.
The RStudio option you mentioned is only for Sweave documents (.Rnw).

knitR/RMD: select output folder

I'm using a RMD file to create a package vignette. My. rmd file is stored in
.../path_to_package/vignettes/vignette.rmd
When creating a PDF the last line in the R Markdown console window is "Output created: /tmp/....".
How is it possible to create the PDF directly in the vignettes folder and not in a /tmp/.. folder?
I'm using Ubuntu 14.04 LTS and R 3.3.0, rmarkdown 0.9.6 and knitr 1.13.
Regards,
Johannes
rmarkdown::render does indeed output to the same directory as the input folder by default, but you can override that by supplying an output_dir argument to it (and an output_file one if you'd like to specify a different filename to the input file too).
I'm not sure why the Knit button in RStudio is doing something different for you—in my case it also outputs to the same folder as the source (even when I haven't specified a working folder and my home directory is the default), and RStudio doesn't show the function it's calling when you click the button, so it's a little hard to be sure. I'd stick to using rmarkdown::render() with the specified arguments for now.
Instead use
devtools::build_vignettes()
It will automatically put the files where they're supposed to go. Also check out Hadley Wickham's guide. It rocks!

Latex and Sweave, cannot include figure

I have a file for my thesis, which include the chapters.
A few weeks ago, I worked with R-Statistic and *.rnw files to produce a *.tex paper.
Now, I want to include these *.tex paper in my thesis. However, every time I get several errors.
Structure
/home/UserName/R-files/Dokumentation5.rnw
/home/UserName/tex-Files/Dokumentation5.tex // from Dokumentation5.rnw files
In the following folder, I put the TeX and the figures (*.pdf).
/home/UserName/Documentation/chapter/
Edit the Dokumentation5.tex, remove the beginning and "end document"
In thesis file, I added the following commands:
\usepackage{Sweave} \include /home/UserName/Documentation/chapter/
run latex
If I run LaTeX, I got several errors like:
File `Dokumentation5-004' not found.
\begin{document} ended by \end{Schunk}.
Too many }'s
File `Dokumentation5-033' not found.
Create a file master.Rnw that contains nothing but the LaTeX setup (packages to be used, geometry, title, begin/end document, bibliography style and file......)
If you need to use R functions in various places in your paper, create a file func.Rnw where you create these functions. Make this file the first to \SweaveInput{func.Rnw} in your master.Rnw - see here for details
Now create one file for each "unit" of your paper (chapter, section, whatever you like). Make them all chapX.Rnw files, whether they contain R code or not.
\SweaveInput{chapX.Rnw} them in the desired order in your master.Rnw file.
At any time during the process, run Sweave("master.Rnw") in R and then pdflatex master.tex from the console to see what you have produced.

How to make sweave copy generated graphics to a subfolder?

I would love to use some shell script based pdfcrop on all the graphics.pdf created from my Sweave report. Hence i'd be nice if all the graphics were stored to a subfolder instead of being dropped where all the latex stuff like .aux .log files (and so on) is located. If I just had to use the crop script on ALL files in a particular directory it would be much easier.
Here's my Sweave chunk:
\begin{figure}[htbp]
\begin{center}
<<fig=true,echo=false>>=
print(mygraph)
#
\caption{my graph}
\end{center}
\end{figure}
If I run the sweave code mygraph.pdf is stored in the same directory as the report.tex file itself.
Is there a way to store this file in an existing subfolder like /graphics ?
Insert this:
\SweaveOpts{prefix.string=foo/bar}
into your Sweave source file, preferably up the top in the preamble. The above line is taken from the Sweave FAQ, and indicates that the created figures will be stored in the subdirectory (of the directory where the Sweave source is) foo, and each image filename will begin with bar. This, and a whole lot more is discussed in the Sweave FAQ.
Okay RTFM, ran2.
Here's how:
\SweaveOpts{prefix.string=graphics/report}
See also: the manual.
graphics is directory and report is a substring you might wanna use. At least you'll find SO better on google :)
EDIT:
For the sake of completeness I'll add my several lines of shell script here:
#!/bin/sh
R CMD Sweave report.Rnw
for file in `ls graphics`;
do pdfcrop "$file" graphics/"$file"
done
pdflatex report.tex
open report.pdf
Maybe some of you want to use $1 as a directory argument.

How do I Sweave a multiple-file project?

I am writing my thesis in LaTeX and because things got a bit long for my taste, I had split it into several files. Let's call them thesis.tex, intro.tex, mat_n_met.tex, rslts.tex and discsn.tex. I have linked intro.tex, mat_n_met.tex, rslts.tex and discsn.tex through thesis.tex with \include{intro} (and so on...).
I have also created a separate file called r_crunching.Rnw (that I run through Sweave) that holds a chunk that runs the R script with data analysis and chunks that produce pdf outputs of graphs that I embed via \includegraphics (in e.g., rslts.tex). Still following?
If I run a Rnw (i.e. I renamed rslts.tex to rslts.Rnw) without "a link" to the chunk with the R script, you will get a Sweave() error saying the reference in \Sexpr{} doesn't exist. Is there a way, without merging all the files into a single .Rnw, to call \Sexpr{} in say rslts.Rnw?
Other methods how to accomplish this are welcome.
I recommend using RStudio (http://www.rstudio.com/ide/). Sweave is nicely integrated into that IDE and it supports multi-file documents. Even Synctex and TeX error log navigation still work when working with multi-file documents.
From the master file you can include child files using
\SweaveInput{Child.Rnw}
You can link a child file back to the master file by including the directive
% !Rnw root = Master.Rnw
in the child file. That way when working on a child file and typesetting it, RStudio know to typeset the master file.
The details are explained in the RStudio documentation at http://www.rstudio.com/ide/docs/authoring/multiple_rnw_files
Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include and \includeonly offer solutions. Try that with a few simple test files.
Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.
To expand Dirk's and mjm's answer, I would suggest using \include's and Makefiles.
Suppose you have a master file: master.tex. In that file, you include some .tex and .Rnw files, i.e.
\include chapter1
\include chapter2
\include chapter3
....
Now the following Makefile provides functions for creating the .tex, .R and .pdf files:
.SUFFIXES: .tex .pdf .Rnw .R
MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)
all: $(MAIN).pdf
$(MAIN).pdf: $(TEX) $(MAIN).tex
R: $(RFILES)
.Rnw.R:
R CMD Stangle $<
.Rnw.tex:
R CMD Sweave $<
.tex.pdf:
pdflatex $<
bibtex $*
pdflatex $<
pdflatex $<
Essentially, the .SUFFIXES provide a set of rules for convert from one file format to another. For example, to convert from .Rnw to .R, we use the command
`R CMD Stangle $<`
one fairly obvious answer is to use a makefile, possibly using package cachesweave, to process the relevant files in the right order.
My solution to multi-file projects in Sweave (under Rstudio) is the following:
1) Create a master file, say master.Rnw, in which you have the calls to the subfiles intro.Rnw, matmet.Rnw, etc:
\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish
\graphicspath{ {./figs/}
\begin{document}
\SweaveOpts{concordance=TRUE}
\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'
\end{document}
2) Create the subfiles. I'm giving here only the first one, intro.Rnw. Please note that in the subfiles you do not use preambular commands such as \documentclass or \begin{document}
\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.
<<>>=
a <- 1+2
#
Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.
3) After saving modifications in 'intro.Rnw', simply go to 'master.Rnw' and compile it using Ctrl+Shift+K and... voilá:
Screenshot of the file created by the above command.

Resources