<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Defective Semantics &#187; pdf</title>
	<atom:link href="http://scarff.id.au/blog/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://scarff.id.au</link>
	<description>Dean Scarff's perpetual struggle with technology, and other anecdotes</description>
	<lastBuildDate>Thu, 03 Nov 2011 22:39:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>R and LaTeX PDF graphics</title>
		<link>http://scarff.id.au/blog/2010/r-and-latex-pdf-graphics/</link>
		<comments>http://scarff.id.au/blog/2010/r-and-latex-pdf-graphics/#comments</comments>
		<pubDate>Mon, 17 May 2010 08:09:16 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[TeX]]></category>

		<guid isPermaLink="false">http://scarff.id.au/?p=349</guid>
		<description><![CDATA[<p>When writing a document in LaTeX that makes use of figures from R, I want to produce a PDF with</p>
<ul>
<li>vector graphics,</li>
<li>consistent fonts,</li>
<li>not to mess around overlaying text in LaTeX,</li>
</ul>
<p>and maybe typeset math in the R graphics.  This post surveys the state of the art in how to achieve the best of all worlds when importing graphics generated by R into documents typeset to PDF with LaTeX.  I look at postscript and PDF figures generated by R&#8217;s X11, Cairo, and finally the new (and awesome) TikZ devices.</p>
<p><span id="more-349"></span></p>
<p>If you&#8217;re using some TeX variant, you probably care a lot about the professional presentation of your document.  Accordingly, you probably cringe when you see figures in a PDF document that aren&#8217;t quite as crisp, or have inferior fonts and math typesetting.  Even some of the Use R books fail blatantly in this regard.  You&#8217;d expect the&#8230; <a href="http://scarff.id.au/blog/2010/r-and-latex-pdf-graphics/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>When writing a document in LaTeX that makes use of figures from R, I want to produce a PDF with</p>
<ul>
<li>vector graphics,</li>
<li>consistent fonts,</li>
<li>not to mess around overlaying text in LaTeX,</li>
</ul>
<p>and maybe typeset math in the R graphics.  This post surveys the state of the art in how to achieve the best of all worlds when importing graphics generated by R into documents typeset to PDF with LaTeX.  I look at postscript and PDF figures generated by R&#8217;s X11, Cairo, and finally the new (and awesome) TikZ devices.</p>
<p><span id="more-349"></span></p>
<p>If you&#8217;re using some TeX variant, you probably care a lot about the professional presentation of your document.  Accordingly, you probably cringe when you see figures in a PDF document that aren&#8217;t quite as crisp, or have inferior fonts and math typesetting.  Even some of the <a href="http://www.springer.com/series/6991">Use R</a> books fail blatantly in this regard.  You&#8217;d expect the <a href="http://had.co.nz/ggplot2/book/qplot.pdf">ggplot2 book</a> to espouse the prettiness of its output, but it has raster figures and an ugly sans-serif font&mdash;a prudent optimisation for file size and rendering speed nevertheless.</p>
<p>The traditional way to get R graphics that are consistent with LaTeX fonts was with a LaTeX toolchain via DVI and postscript.  You can render R graphics to a postscript device that uses the appropriate TeX encoding, according to the instructions in <a href="http://bm2.genes.nig.ac.jp/RGM2/R_current/library/grDevices/html/postscript.html">the R manual for postscript {grDevices}</a>.  I usually run ps2eps on the result to fix the bounding box and other issues; you can also use <code>par(mar=)</code> from R according to the manual.  In the LaTeX preamble you add <code>\usepackage[T1]{fontenc}</code> and then after the document is typeset and it&#8217;s converted from DVI to PDF, the fonts are all magically correct.</p>
<p>However, with some of the new <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/microtype/">typography features</a> (and the shorter toolchain that doesn&#8217;t require ghostscript) available with pdfLaTeX it&#8217;s understandable if you want to move away from plain LaTeX.  However, when you use pdfTeX, you can&#8217;t include EPS graphics.</p>
<p>R is no exception; I&#8217;ve always found integrating graphics with pdfLaTeX to be <acronym title="situation normal, all fouled up">SNAFU</acronym>.  I can personally attest to <a href="http://tclab.kaist.ac.kr/ipe/">ipe</a> (if you can get it to build) and <a href="http://asymptote.sourceforge.net/">asymptote</a> looks good although I&#8217;ve yet to try it.  Using a metapost intermediate format worked well from gnuplot.  xfig is horrible and I detest manually writing any metapost or pstricks.</p>
<p>R can produce PDF output, but R&#8217;s basic PDF device doesn&#8217;t have the same font family and encoding options as its postscript device: you will get the error <q>unknown family &#8216;ComputerModern&#8217;</q> if you try to supply the same parameters.  There are some <a href="http://www.mail-archive.com/r-help@r-project.org/msg01322.html">amusing suggestions</a> for dealing with this, but a lot of the material that turns up from a google search is outdated.</p>
<p>One workaround I tried was to convert the EPS that worked with the previous LaTeX &#8594; DVI &#8594; PDF toolchain.  However:</p>
<ul>
<li>pstopdf results in the correct font, but weird kerning</li>
<li>ps2pdf results in the correct font, weird kerning, and weird scaling/bounding box issues</li>
<li>Apple&#8217;s Preview.app machinery lost the font information</li>
</ul>
<p>Instead of using the EPS route, we can just use a <em>similar</em> font that R does support exporting directly to PDF.  The <a href="http://cm-unicode.sourceforge.net/">Computer Modern &#8211; Unicode</a> project has some ports of the Computer Modern family to OpenType, which R can then utilise.</p>
<p>The X11 device (on Mac OS X) works perfectly with <code class="R">par(family="CMU Serif")</code>, but the pdf device again complains that it&#8217;s not a postscript font, and even if you try to use postscript versions of Computer Modern directly there is an encoding issue.  Paul Murrell has an excellent writeup of <a href="http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html">using cm-lgc</a> to create PDF output with an alternative encoding for the type 1 CM fonts.  I couldn&#8217;t get either CairoX11 or CairoPDF to produce output with CM, but I suspect Murrell&#8217;s method will fix Cairo too.  Quartz works out of the box if you happen to use Mac OS X.</p>
<pre class="codeblock R">
quartz(type="pdf", file="test-quartz.pdf")
par(family="CMU Serif")
</pre>
<p>Ligatures don&#8217;t work, but I can live with that.  Quartz even manages to typeset math made with <code>expression</code>, but in a way that will stand out as blindingly ugly in a TeX document.</p>
<p>This all brings us to the true state of the art (<acronym title="from what I can see">FWICS</acronym>): <a href="http://cran.r-project.org/web/packages/tikzDevice/index.html">tikzDevice</a>.  This project is still in beta, and their <a href="http://tikzdevice.r-forge.r-project.org/">project page</a> has the eerie bareness of archaic Alexandria.  Nevertheless, once I figured out how to install it, I was impressed.</p>
<pre class="codeblock R">
# from R
install.packages("filehash")
install.packages("tikzDevice", dependencies=TRUE,
  repos="http://R-Forge.R-project.org",
  type="source")
</pre>
<p>I took the opportunity to switch from macports&#8217; TeXlive to the MacTeX-derived BasicTeX, and installed a couple of necessary packages:</p>
<pre class="codeblock sh">
# from sh
sudo /usr/texbin/tlmgr install pgf preview
</pre>
<p>It&#8217;s as easy as <code class="R">tikz("tikzfig.tex")</code> and your usual plotting commands to generate a figure, then:</p>
<pre class="codeblock latex">
% in the preamble:
\usepackage{tikz}
\usepackage{color}
% ...
\begin{document}
% ... then in the body:
\begin{figure}[p]
\input{tikzfig}
\caption{TikZ}
\end{figure}
\end{document}
</pre>
<p>It processes all the text in the figure with LaTeX, including math marked up with &#8216;$&#8217;s.  However, don&#8217;t bother using R <code>expression</code>s in your labels: symbols and accents are positioned by R as separate nodes, and would-be TeX specials aren&#8217;t even escaped.</p>
]]></content:encoded>
			<wfw:commentRss>http://scarff.id.au/blog/2010/r-and-latex-pdf-graphics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

