4

Currently I am making my figs using "unprincipled" (opportunistic) way to get Unicode characters right. For example:

<<ClassFig>>=
pdf('figs/figA.pdf', h=6, w=6, encoding='CP1250')
plot(x, y, xlab='rečenica')
dev.off()
@

Now, I wonder how would I specify snippet (or global settings, as with opts_chunk$set()) properly to see Uncode characters that I need. For example:

<<ParadigmFig, fig.height=7, fig.width=15, out.width='1\\textwidth'>>=
plot(x, y, xlab='rečenica')
@

Currently, if I use the second, proper variant, I am getting dots instead of spec. character.

Cœur
  • 37,241
  • 25
  • 195
  • 267
striatum
  • 1,428
  • 3
  • 14
  • 31

1 Answers1

2

Try generating this figure with cairo_pdf().

cairo_pdf('test.pdf', family="Helvetica")
plot(1:10, (1:10)^2, xlab=enc2utf8("ąśćźół"))
dev.off()

It may be important to change the font family to one of which knows about regional characters (I'm using Polish letters above). For OS X, this list may give you a hint on font family selection.

Now let's set up knitr to use these settings. Create an uncached code chunk at the beginning of the document with calls to:

library("knitr")
library("tikzDevice")
opts_chunk$set(
   dev='cairo_pdf',
   dev.args=list(family='DejaVu Sans')
   #out.width='5in', 
   #fig.width=5,
   #fig.height=5/sqrt(2),
   #fig.path='figures-knitr/',
   #fig.align='center',
)

(I've commented out the options which are meaningless here, but which you may also be interested in some day). You may find more details on chunk options here.

Here is an exemplary .Rnw file that (at least on my Linux) produces correct results:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[T1,plmath]{polski}
\usepackage[polish]{babel}

\begin{document}

<<cache=FALSE,echo=FALSE,message=FALSE>>=
options(Encoding="UTF-8")
library("knitr")
library("tikzDevice")
opts_chunk$set(
   dev='cairo_pdf',
   dev.args=list(family='Helvetica')
   #out.width='5in', 
   #fig.width=5,
   #fig.height=5/sqrt(2),
   #fig.path='figures-knitr/',
   #fig.align='center',
)
@

<<>>=
plot(1:10, (1:10)^2, xlab="ąśćźół")
@

\end{document}
gagolews
  • 12,836
  • 2
  • 50
  • 75
  • I tried this suggestion, but without any success. I am using Mac OSX. Maybe that causes all problems? – striatum Apr 27 '14 at 12:02
  • Maybe you should choose a different font family? Pick anything from the [font list](http://en.wikipedia.org/wiki/List_of_typefaces_included_with_OS_X) and let me know if it helped (e.g. `Arial`, `Times`, or `Times New Roman`). – gagolews Apr 27 '14 at 12:07
  • I tried so many things: - your variant works in R, but not in Rnw/knitR (and I followed your advice exactly!); - I also tried simply pdf('tmp.pdf', h=5, w=5, encoding='CP1250') and in Rnw with dev='pdf', dev.args=list(encoding='CP1250'), again, it worked in R but nor in Rnw/knitR I am lost! The only way I am getting spec. character is to use: <>= and to run that piece directly in R. This is so annoying... :-( – striatum Apr 27 '14 at 12:30
  • I tried different font families as you suggested, but, again and again, no luck... Depending of what I use two outcomes appear: (a) dots if I do not use enc2utf8() or "<8c><86>" if I use enc2utf8(). So, it is good for loosing mind... :-( – striatum Apr 27 '14 at 12:48
  • I wonder why you are forcing Windows-1250 (CP1250) encoding. AFAIK, R on OS X always uses UTF-8 as native encoding. Also try calling `options(encoding="UTF-8")` from the above chunk of code. Don't give up, we'll solve it together, even if it'll take some time. I also had similar problems with knitr. What is the result of `Sys.getlocale()` when you call it from a knitr document? – gagolews Apr 27 '14 at 14:07
  • Also, try to compile the .Rnw file with RStudio (CTRL+SHIFT+I). – gagolews Apr 27 '14 at 14:24