Using Sweave with XeLaTeX

[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Using R with LaTeX via Sweave is a great way to create reproducible output. However, using specific fonts, e.g. your corporate fonts, can be painful with pdflatex. Over the last few weeks I have fallen in love with the TeX format
XeLaTeX and its XeTeX engine.

With XeLaTeX I had to overcome some hurdles, which I would like to share here:

  • attaching files,
  • trimming and clipping images,
  • learning how to use the tikzDevice package.




Please find the Sweave file of the above document below:

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
%% Copyright 2011 Markus Gesmann
\documentclass[a4paper]{article}
\usepackage[noae,nogin]{Sweave}
\usepackage{tikz}
%% attachfile2 is patched to work with xelatex
\usepackage{attachfile2}
\usepackage{adjustbox}
\usepackage{xltxtra,fontspec,xunicode}
\usepackage{sectsty}
\usepackage{wrapfig}
\usepackage{hyperref}
\setmainfont{Optima}
%% Change the font for section header
\sectionfont{\fontspec{Calibri}}
\title{Using Sweave with \XeLaTeX}
\author{Markus Gesmann}
%% Don't indent new paragraphs
\setlength{\parindent}{0cm}
\setlength{\parskip}{2mm}
\SweaveOpts{prefix.string=fig}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
Using R with {\LaTeX} via Sweave~\cite{sweave} is a great way to create reproducible
output. However, using specific fonts, e.g. your corporate
fonts, can be painful with \texttt{pdflatex}. Over the last few weeks
I have fallen in love with the {\TeX} format
\href{http://scripts.sil.org/xetex}{\XeLaTeX}~\cite{xetex}
and its {\XeTeX} engine\footnote{A good summary over the
various {\TeX} engines is given in the
\href{http://dante.ctan.org/tex-archive/info/luatex/lualatex-doc/lualatex-doc.pdf}{Guide
to \LuaTeX}.}.
With {\XeLaTeX} I had to overcome some hurdles, which I would
like to share here:
\begin{itemize}
\item attaching files,
\item trimming and clipping images,
\item learning about the R package tikzDevice.
\end{itemize}
\section{Moving from \texttt{pdflatex} to \texttt{xelatex}}
{\XeLaTeX} comes with all major {\TeX} distributions such as {\TeX}
Live 2011, MacTeX 2011 and MikTeX 2.9. It is still in development, therefore I
strongly suggest to update your {\TeX} distribution and packages before you start
experimenting and using {\XeLaTeX}.
{\XeTeX} natively supports Unicode and the input file is
assumed to be in UTF-8 encoding by default. As an example, it means I
can type £ instead of having to use \texttt{$\backslash$pound} to get
the same result. Further {\XeTeX} can use any fonts installed in the operating
system without configuring {\TeX} font metrics.
Being able to use your fonts is great, but then you also want to use
the same fonts in your R-plots. Therefore we have to talk about the
\texttt{\href{http://cran.r-project.org/web/packages/tikzDevice/index.html}{tikzDevice}}
package~\cite{tikzDevice} as well. The TikZ device enables \LaTeX-ready output from R
graphics functions. The TikZ device creates a tex-file using the
\href{http://en.wikipedia.org/wiki/PGF/TikZ}{TikZ} graphics language.
All text in a graphic output with the tikz() function will therefore
match the current font used in your document.
\subsection{Using fonts}
Using fonts could not be easier in {\XeLaTeX}:
Insert the following four lines into the preamble of your tex-file and
the most commonly required packages by {\XeLaTeX} will be loaded and
the main font will be changed to Optima and the section font to
Calibri\footnote{Please note that these fonts might not be available
on your system.}.
\begin{verbatim}
\usepackage{xltxtra,fontspec,xunicode}
\usepackage{sectsty} %% change fonts for sections
\setmainfont{Optima}
\sectionfont{\fontspec{Calibri}}
\end{verbatim}
Changing the font within your text temporarily is equally easy, as the
following demonstrate:
\begin{verbatim}
\fontspec{Zapfino} %% Change to Zapfino
Hello World in Zapfino
\fontspec{Optima} %% Change back to Optima
\end{verbatim}
\fontspec{Zapfino}
Hello World in Zapfino
\fontspec{Optima}
Dario Taraborelli has some beautiful examples on his web page:
\url{http://nitens.org/taraborelli/latex}.
\subsection{Attaching files}
To attach a file to a PDF document using {\LaTeX} with
\texttt{pdflatex} I used to use the \texttt{attachfile} package. With
{\XeLaTeX} we have to use the package
\texttt{\href{http://www.ctan.org/pkg/attachfile2}{attachfile2}}~\cite{attachfile2} instead.
The functionality appears to be the same, so to attach a file we write:
\begin{verbatim}
\attachfile[icon=Paperclip]{waves.pdf}{waves.pdf}
\end{verbatim}
This results in:
\attachfile[icon=Paperclip]{waves.pdf}{waves.pdf}. Please note that not all PDF-viewer
provide access to the attachment, if in doubt use \href{http://get.adobe.com/reader/}{Adobe Reader}.
We can even attach the Sweave source file of this document as well:
\attachfile[icon=Paperclip]{SweaveXeLaTeXExample.Rnw}.
\subsection{Trimming and clipping}
Clipping and trimming of pictures in {\XeLaTeX} requires a
little more work and an up-to-date \TeX-system.
\begin{figure}[ht]
\begin{center}
\fbox{\begin{minipage}{0.6\textwidth}
\begin{center}
% add text or graphics here
%% cut of 35mm from the top, 37mm from the bottom, 10m from the right and 21mm from the left
\includegraphics[width=\textwidth]{waves.pdf}
%\caption{Waves}
\end{center}
\end{minipage}}
\caption{Original picture}\label{original}
\end{center}
\end{figure}
Let's look at the following example in Figure~\ref{original}.
Using \texttt{pdflatex} we can clip and trim the excess white space of
the picture like this:
\begin{verbatim}
\includegraphics[trim=20mm 37mm 10mm 35mm, clip]{waves.pdf}
\end{verbatim}
To achieve the same result with {\XeLaTeX} and the \texttt{xetex} engine
we have to use the \texttt{\href{http://www.ctan.org/pkg/adjustbox}{adjustbox}} package
by Martin Scharrer~\cite{adjustbox}.
The following lines result in the desired output of Figure~\ref{clipped}.
\begin{verbatim}
\begin{figure}[ht]
\begin{center}
%% cut of 35mm from the top, 37mm from the bottom,
%% 10m from the right and 21mm from the left
\adjustbox{trim=20mm 37mm 10mm 35mm, clip, width=0.6\textwidth}{
\includegraphics{waves.pdf}
}
\caption{Clipped and trimmed picture}
\end{center}
\end{figure}
\end{verbatim}
\begin{figure}[ht]
\begin{center}
%% cut of 35mm from the top, 37mm from the bottom, 10m from the right and 21mm from the left
\adjustbox{trim=20mm 37mm 10mm 35mm, clip, width=0.6\textwidth}{
\includegraphics{waves.pdf}
}
\caption{Clipped and trimmed picture}\label{clipped}
\end{center}
\end{figure}
\subsection{Using your fonts in R plots}
To achieve a consistent look in our document we also need to ensure
that the labels of our plots are set with the same font as the
document itself.
As mentioned above, the
\texttt{\href{http://cran.r-project.org/web/packages/tikzDevice/index.html}{tikzDevice}}
package by Charlie Sharpsteen and Cameron Bracken provides a graphic
device for R which generates tex output using the TikZ language. As a result we embed the
chart not with the \texttt{includegraphics} command, but with the
\texttt{input} statement. Further we have to add the \texttt{Sweave} and
the \texttt{tikz} package to the preamble of our Rnw-file:
\begin{verbatim}
\usepackage{Sweave}
\usepackage{tikz}
\end{verbatim}
\begin{figure}[ht]
\input{myBarplot.tex}
\caption{A barplot using the Optima font.}\label{barplot}
\end{figure}
As an example we want to replicate the bar plot of the blog
entry \emph{\href{http://lamages.blogspot.com/2011/10/r-related-books-traditional-vs-online.html}{
R related books: Traditional vs online publishing}}.
%\begin{small}
<<results=hide, echo=TRUE>>=
bibfile <- readLines("http://www.r-project.org/doc/bib/R-books.bib")
pub.start.pos <- regexpr("publisher =", bibfile, perl=TRUE)
pub.lines <- which( pub.start.pos > 0 )
pub.split <- strsplit(bibfile[pub.lines], "[ =,]", perl=TRUE)
publishers <- sapply(pub.split, function(x) paste(x[-c(1:5)]))
publishers <- gsub("[{}\"),\\]", "", publishers)
publishers <- gsub("c\\(", "", publishers)
s=c("Springer", "Wiley", "Sage", "Chapman & Hall", "CRC press", "Servicio")
r=c("Springer", "Wiley", "Sage", "Chapman \\& Hall/CRC",
"Chapman \\& Hall/CRC","Universidad de La Rioja")
for(i in seq(along=s)){
publishers[regexpr(s[i], publishers, ignore.case=TRUE) > 0] <- r[i]
}
RBooks <- table(publishers)
RBooks <- RBooks[order(RBooks)]
@
%\end{small}
Creating the TikZ output works in the same way, as with PDF, PNG or
any other graphic device: we create a file connection, execute the R
plotting commands and close the device at the end.
%\begin{small}
<<results=hide>>=
require(tikzDevice)
tikz(file="myBarplot.tex", width=5.2,height=4)
opar <- par(mar = c(4, 13, 0, 4), oma=c(0,0,2,0))
barplot(RBooks, horiz=TRUE, las=1, col="#DC3912",
xlab=format(Sys.time(), "As at %d %b %Y"),
border=NA)
title("Number of R books by publisher", outer=TRUE)
par(opar)
dev.off()
@
%\end{small}
To insert the plot we use the figure environment and use the
\texttt{input} command:
\begin{verbatim}
\begin{figure}
\input{myBarplot.tex}
\caption{A barplot using the Optima font.}
\label{barplot}
\end{figure}
\end{verbatim}
The chart is included as Figure~\ref{barplot} using the Optima font,
as the rest of the body text.
\section{Conclusion}
Using {\XeLaTeX} requires a few minor changes in you work flow, but in
combination with the \texttt{tikzDevice} package and Sweave it allows
you to change the fonts in your document easily.
\begin{thebibliography}{1}
\bibitem{xetex}
Jonathan Kew.
\newblock {X}e{T}e{X},. Version 0.9997.5
\newblock \url{http://scripts.sil.org/xetex}, 2011.
\bibitem{sweave}
Friedrich Leisch.
\newblock Sweave: Dynamic generation of statistical reports using literate data
analysis.
\newblock In Wolfgang H{\"a}rdle and Bernd R{\"o}nz, editors, {\em Compstat
2002 --- Proceedings in Computational Statistics}, pages 575--580. Physica
Verlag, Heidelberg, 2002.
\newblock ISBN 3-7908-1517-9.
\bibitem{attachfile2}
Heiko Oberdiek.
\newblock Package attachfile2: Attach files into pdf. Version 2.6.
\newblock \url{http://www.ctan.org/pkg/attachfile2}, 2011.
\bibitem{adjustbox}
Martin Scharrer.
\newblock Package adjustbox: Apply graphics package macros to general boxes.
Version 0.6.
\newblock \url{http://www.ctan.org/pkg/adjustbox}, 2011.
\bibitem{tikzDevice}
Charlie Sharpsteen and Cameron Bracken.
\newblock {\em tikzDevice: A Device for R Graphics Output in PGF/TikZ Format},
2011.
\newblock R package version 0.6.1.
\end{thebibliography}
\end{document}

To leave a comment for the author, please follow the link and comment on their blog: mages' blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)