\documentclass{article} \usepackage{hyperref} \newcommand{\R}{\textsf{R}} \SweaveOpts{eps=FALSE} \begin{document} % Control width of code chunks <>= options(width=60) @ % Make the example plot nice and big \setkeys{Gin}{width=\textwidth} \section*{Computer Modern fonts in Lattice plots in Sweave documents} This document describes (and demonstrates) the steps that are necessary to produce an Sweave document that includes code chunks that create lattice plots that use Computer Modern fonts. A complete understanding can be gained by reading the processed PDF file \texttt{cmTutorial.pdf} \emph{and} the \texttt{cmTutorial.Rnw} ``source'' file. \begin{enumerate} \item Download a set of Computer Modern fonts with non-\TeX{} encodings. This document uses the cm-lgc \LaTeX{} package by Alexej Kryukov.\footnote{\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/cm-lgc.html}} This tutorial assumes that the \texttt{cm-lgc} directory (and its sub-directories) have been placed in the same directory as the main Sweave document. It is more efficient and proper to install them somewhere central, but such issues are beyond the scope of this tutorial. \item If you are going to use \R{}'s ``mathematical annotation'' features (see \texttt{?plotmath}), then you should also download the special cmsyase font.\footnote{\url{http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html}} It is assumed that the files \texttt{cmsyase.afm} and \texttt{cmsyase.pfb} are stored in the same directory as the main Sweave document. \item Within the Sweave document, the Computer Modern fonts need to be registered with the PDF and PostScript font databases. <>= CM <- Type1Font("CM", c(file.path("cm-lgc", "fonts", "afm", "public", "cm-lgc", c("fcmr8a.afm", "fcmb8a.afm", "fcmri8a.afm", "fcmbi8a.afm")), "./cmsyase.afm")) pdfFonts(CM=CM) postscriptFonts(CM=CM) @ \item Set the Computer Modern fonts as the default font family. <>= ps.options(family="CM") @ \item Draw a pretty picture.\footnote{Because this is a lattice plot, I use one chunk to show the code and another chunk, which reuses the first chunk, to produce the plot. See the ``source'' file \texttt{cmTutorial.Rnw}.} <>= histogram( ~ height | voice.part, data = singer, nint = 17, endpoints = c(59.5, 76.5), layout = c(2,4), aspect = 1, xlab = "Height (inches)") <>= print( <> ) @ \item Embed the Computer Modern font(s) in the graphics file so that the final document can be viewed/printed anywhere. Requires knowledge of the naming scheme used by Sweave for graphics files. <>= embedFonts("cmTutorial-latticeShow.pdf", fontpaths=c(file.path("cm-lgc", "fonts", "type1", "public", "cm-lgc"), ".")) @ \end{enumerate} \end{document}