Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

r - Mathematical expression in axis label

In R, I use expression(theta[l]) so that the label of my plot axis is that same as $heta_l$ from LaTeX. For esthetic reasons, I'd rather like to display $heta_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

egin{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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...