2
votes

I am relatively new to Sweave, and R in general and have been having issues trying to render basic math symbols in my plot labels. I believe the problem may be cause by either Sweave or tikzDevice package as I am able to render the math symbols perfectly fine if I use either a png or pdf device.

Writing this code to either a png or pdf device gives expected output --the > symbol

qplot(factor(cyl), data=mtcars, geom="bar", colour=factor(cyl)) + scale_x_discrete(paste("",">","6k"))

Using .tex output file resulting from running Sweave on file below replaces > symbol with ¿ symbol

<<fig=FALSE, echo=FALSE>>=
require(tikzDevice)
require(ggplot2)
require(reshape2)
tikz('sweave-math-symbols.tex', width=5.9, height=2.7)

qplot(factor(cyl), data=mtcars, geom="bar", colour=factor(cyl)) + scale_x_discrete(paste("",">","6k"))
@

LaTeX document that uses output file from Sweave is below

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

%opening
\title{Sweave Math Symbols}
\author{Phiri}

    \begin{document}

    \maketitle

    \begin{abstract}

    \end{abstract}

    \section{Illustration}
    \input{sweave-math-symbols.tex}

    \end{document}

Where is this problem stemming from and how do I go about fixing it?

3
I think this is more appropriate for the programming site.Peter Flom
Thank you @PeterFlom, will do just that.lightonphiri
works for me, but you might try wrapping the > in TeX math delimiters ($$, i.e. $>$)Ben Bolker
Thank you @BenBolker, that did the trick. Please repost your comment as an answer so I can mark the question as answered.lightonphiri

3 Answers

4
votes

You need tikz(..., sanitize = TRUE) so that tikzDevice can escape special characters. See ?tikz for details.

As I always think it is awkward to open a device manually inside a code chunk, here is the equivalent chunk in knitr to keep the gory details away:

<<sweave-math-symbols, dev='tikz', sanitize=TRUE, fig.width=5.9, fig.height=2.7>>=
library(ggplot2)
qplot(factor(cyl), data=mtcars, geom="bar", colour=factor(cyl)) +
  scale_x_discrete(paste("",">","6k"))
@

But > is not considered (yet) in tikzDevice so you have to manually specify it in the options (as \textgreater{}):

<<setup, include=FALSE>>=
options(
  tikzSanitizeCharacters = c("%", "$", "}", "{", "^", "_", "#", "&", "~", ">"),
  tikzReplacementCharacters = c("\\%", "\\$", "\\}", "\\{", "\\^{}", "\\_{}", "\\#", "\\&", "\\char`\\~", "\\textgreater{}")
)
@
3
votes

Your Sweave file worked for me without modification (on Ubuntu 10.04), but you might try wrapping the > in TeX math delimiters ($$, i.e. $>$); frankly, I'm surprised that /don't understand why it works OK without the delimiters in some contexts.

0
votes

You should really use T1 encoding here, see the following answer to a related question on TeX.SX: https://tex.stackexchange.com/a/2376/8057

For tikzDevice, simply have \usepackage[T1]{fontenc} written to the preambles that are used for measuring texts and for generating the final output (options tikzDocumentDeclaration and tikzMetricPackages).