17

In R, I use expression(theta[l]) so that the label of my plot axis is that same as $\theta_l$ from LaTeX. For esthetic reasons, I'd rather like to display $\theta_\ell$. Can you help me?

enter image description here


EDIT

Before, I did

plot(1:10, 1:10, xlab=expression(theta[l]))

and I exported the resulting picture in pdf. Then, using

\begin{figure}[htbp]
\centerline{\includegraphics[scale=.6]{test.pdf}}
\end{figure}

my picture was inserted in LaTeX.

Following the comments, here is what I now do:

require(tikzDevice)
tikz("test.tex", standAlone=TRUE, width=5, height=5)

plot(1:10, 1:10, xlab="$\\theta_\\ell$")

dev.off()
tools::texi2pdf('test.tex')
system(paste(getOption('pdfviewer'),'test.pdf'))

However, when I insert the resuling plot in LaTeX, the quality of the figure is not as good as before. Is there something more that I can do?

5
  • @agstudy: Windows. Linux is also fine for me.
    – Marco
    Mar 18, 2013 at 15:32
  • @Sven Hohenstein: Thanks for your picture :-)
    – Marco
    Mar 18, 2013 at 15:47
  • 2
    Is this what you're looking for?
    – plannapus
    Mar 18, 2013 at 15:51
  • @plannapus: mmmmm, interesting ! I will investigate that package :-)
    – Marco
    Mar 18, 2013 at 16:11
  • This works with cairo_pdf: plot(1:10, main = "\u2113"). edit: I just realized that you want theta_ell, that wont probably help there... Mar 20, 2013 at 18:33

4 Answers 4

9
+50

There is really no reason to use a possibly unmaintained package like 'tikzDevice' for such a simple problem. Part of the problem with the 'tikz' device is that it doesn't seem to correctly accept the 'xpinch' and 'ypinch' arguments that specify your plot's resolution.

There is a larger question of adding LaTEX notation to plots, but for this localized problem, the question is one of specifying the font to make the base 'plotmath' package display cursive letters for you.

You can change the font for your x-axis label by separating it out from the plot command and choosing a custom font from within the 'title' function with something like this:

plot(1:10, 1:10, xlab="")
windowsFonts(script=windowsFont("Script MT Bold"))
title(xlab=expression(theta[l]), family="script")

What we've done is to specify a null label for the x-axis in the plot command to first make space. Then, we load up a system font into the available font families (I like Script MT Bold for expressions). Finally, we can use the 'title' function to plot the x-axis label and specify the family for any text in that label.

By doing this, we preserve all of the original functionality of the plotting device, so you should no longer have a drop in resolution when converting to PDF.

demo of chart

Now if anyone has a good solution to the LaTEX notation problem outside of the 'tikzDevice' package, I would love to hear about it. The only way I know to do this well is to flip the model and use the 'tikz' LaTEX package to draw the whole graphic manually from within the LaTEX file or to use the 'pixmap' R package to draw an image of my expression on top of the plot. Neither feels like a perfect approach.

4
  • Where do we find the "windowsFonts"?
    – Marco
    Mar 26, 2013 at 5:31
  • These are the fonts installed on your system. You just have to use the name of the font as it is registered on your computer. On a Windows computer, you can use the Character Map application (system tool).
    – Dinre
    Mar 26, 2013 at 11:46
  • 2
    windowsFonts is a function that can display the currently avaialble font mappings for the interactive graphics device, and it can be used to assign new mappings. It's obviously only avaialble on ... Windows. There are corresponding functions on Mac (quartzFonts) and Nix ("X11Fonts).
    – IRTFM
    Mar 27, 2013 at 17:58
  • 1
    I think it's under maintenance. It had been updated at 2015-01-07 when I checked.
    – Ziyuan
    Apr 10, 2015 at 12:42
3

I think tikzDevice is the way to go here. You can install from R-forge.

install.packages("tikzDevice", repos="http://R-Forge.R-project.org")

The tikz /pgf philosophy is to create a plot that can be typeset. You will probably want these to be consistent with your document. Eg, with the same packages, fonts, font size etc

You can set these things within a call to tikz by setting

options such as the document declaration

options(tikzDocumentDeclaration = "\\documentclass[10pt]{article}")

packages

tikzLatexPackages (or similar)

You can also control the font size.

All these things are detailed in

?tikzDevice.


You could also use knitr to create your plots within a literate programming document (.rnw)

In this case, using a tikz device a pdf is created (as external = TRUE), using the same document declaration and header / packages as the whole document.

\documentclass[12pt,a4paper]{article}

\begin{document}

<<setup, include = FALSE>>=
opts_chunk$set(dev = 'tikz', external = TRUE)
@


<<myplot, fig.width=5, fig.height = 5, echo=FALSE>>=
plot(1:10, 1:10, xlab="$\\theta_\\ell$")
@

\end{document}
1
  • If you are installing on a Mac (or Windows) you will probably need to install the proper package of tools and use: install.packages("tikzDevice", repos="http://R-Forge.R-project.org")
    – IRTFM
    Mar 27, 2013 at 18:00
1

This is a somewhat dirty solution, but it makes it:

plot(1,1, xlab=expression(theta))
title(xlab="    \u2113",line=3.2,cex.lab=.7)

First plot with the theta symbol. Then add the \ell symbol with smaller font size and manually setting the position.

enter image description here

4
  • @carelus: Thanks. Does it still appear as \ell when the image is exported in pdf format?
    – Marco
    Mar 23, 2013 at 7:59
  • 2
    just checked and apparently it doesn't :-( Mar 23, 2013 at 19:21
  • 1
    The behavior will depend on the font mappings of the pdf-viewer. On a Mac using the Preview.app as viewer it does appear with the same cursive-ell. Your viewer need to have a proper Symbol font to get the same behavior and I believe the Windows viewers are often not set up properly for this.
    – IRTFM
    Mar 27, 2013 at 18:04
  • @Dwin: Thanks for the precision. Do you have a recommendation regarding the pdf-viewer on windows?
    – Marco
    Mar 29, 2013 at 6:37
1

I found a workaround here. They do explain a lengthy process to get the encoding to work with the standard pdf device. Otherwise, the CairoPDF device can be used by installing the Cairo package. Then something like xlab="\u2113" will show up in the pdf using @Julián Urbano's solution. I had no luck using the character within an expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.