Skip to content
This repository has been archived by the owner on Sep 23, 2018. It is now read-only.

Support for plotmath #44

Open
zackw opened this issue Dec 4, 2011 · 6 comments
Open

Support for plotmath #44

zackw opened this issue Dec 4, 2011 · 6 comments
Labels

Comments

@zackw
Copy link

zackw commented Dec 4, 2011

Plotmath expressions are not handled really at all by tikzDevice; anything that uses %operators% causes the string sanitizer to barf, Greek letters show up as the Latin letter corresponding to the position of the character in the Adobe Symbol font, and those are just the problems I've noticed so far.

Ideal behavior would be for the device to translate plotmath into proper TeX math expressions, but I've no idea how difficult that would be.

Concrete test case 1:

suppressPackageStartupMessages(library(tikzDevice))
tikz("plotmath.tex", standAlone=TRUE, width=1, height=1)
par(mai=c(0,0,0,0))
plot.new()
# the next two lines should produce identical (but for positioning)
# draw commands in the output, but they don't
text(.5, .6, expression(alpha+beta+gamma+delta))
text(.5, .4, "$\\alpha+\\beta+\\gamma+\\delta$")
invisible(dev.off())

Concrete test case 2:

suppressPackageStartupMessages(library(tikzDevice))
tikz("plotmath2.tex", standAlone=TRUE, sanitize=TRUE)
demo(plotmath)
invisible(dev.off())

-->

> draw.plotmath.cell(expression(x %+-% y), i, nr); i <- i + 1
Error in function (string, strip = getOption("tikzSanitizeCharacters"),  : 
  Unable to sanitize string, you may be trying to pass in an unsupported symbol

To be clear, I see this as a feature request rather than a bug, and I do have a workaround (edit all my graphics and replace plotmath with proper TeX math).

@yihui
Copy link

yihui commented Dec 4, 2011

Just out of curiosity: why do you even want (the ugly) plotmath when you can write native (beautiful) LaTeX expressions in your plots with tikzDevice?

@zackw
Copy link
Author

zackw commented Dec 4, 2011

I have a great many plots to convert from the stock PDF device + plotmath to tikzDevice. This is yet another hurdle.

@Sharpie
Copy link
Owner

Sharpie commented Dec 4, 2011

I can see the value of plotmath support for cases where the source code that produces a plot is difficult to change---such as plotting functions buried inside of 3rd party packages.

However, achieving this would take quite a bit of work. One would need to develop a hash that mapped all the Adobe Symbol glyphs used by plotmath to their LaTeX equivalents and then write a function that used the hash to perform substitutions in strings sent to the device---this could probably be worked into the existing sanitization machinery.

Right now I don't have the time to start such a project and, as Yihui mentioned, native LaTeX expressions work so much better that I don't even use plotmath anymore so my motivation for this is low.

However, I would be more than willing to review and merge a pull request that added this functionality.

@zackw
Copy link
Author

zackw commented Dec 6, 2011

Right now I don't have the time to start such a project and, as Yihui mentioned, native LaTeX expressions work so much better that I don't even use plotmath anymore so my motivation for this is low.

However, I would be more than willing to review and merge a pull request that added this functionality.

I'm not familiar with how this stuff works internally. Is there a
point at which the device has an opportunity to inspect and rewrite
the plotmath expression? If so, it seems like it would be fairly
straightforward, although a fair bit of coding, to convert that into
an equivalent TeX math expression. Or does the device only see
plotmath after it's been converted into a sequence of low-level
drawing operations?

@Sharpie
Copy link
Owner

Sharpie commented Dec 6, 2011

I'm not familiar with how this stuff works internally. Is there a
point at which the device has an opportunity to inspect and rewrite
the plotmath expression? If so, it seems like it would be fairly
straightforward, although a fair bit of coding, to convert that into
an equivalent TeX math expression.

I don't know if this is possible---one would have to work their way through the function calls, starting with plot, to see what happens to the expression object and if there is a point where custom code could be hooked on.

Or does the device only see plotmath after it's been converted into a sequence of low-level drawing operations?

All text sent to the device is passed through a sanitization routine. This would be a good place to implement an Adobe Symbol -> LaTeX Math conversion. At the C level, there is a function call that hands the string off to some R-Level code for processing:

https://github.com/Sharpie/RTikZDevice/blob/0.6.2/src/tikzDevice.c#L1958

It wouldn't be hard to extend this function call to pass a flag that indicated if the string to be sanitized contained Adobe Symbol glyphs. I could take care of this part.

The rest of the job would be adding code to:

https://github.com/Sharpie/RTikZDevice/blob/0.6.2/R/sanitizeTexString.R

That swapped the symbol glyphs for LaTeX statements---this is the part that I don't have the time to dig into.

@zackw
Copy link
Author

zackw commented Dec 6, 2011

I'm not familiar with how this stuff works internally.  Is there a
point at which the device has an opportunity to inspect and rewrite
the plotmath expression?  If so, it seems like it would be fairly
straightforward, although a fair bit of coding, to convert that into
an equivalent TeX math expression.

I don't know if this is possible---one would have to work their way through the function calls, starting with plot, to see what happens to the expression object and if there is a point where custom code could be hooked on.

A little poking at the R source tree gives me the impression there is
no such hook. I might ask on r-devel, though, since that seems like
the superior option (taking full advantage of TeX's math engine).

 https://github.com/Sharpie/RTikZDevice/blob/0.6.2/R/sanitizeTexString.R

That swapped the symbol glyphs for LaTeX statements---this is the part that I don't have the time to dig into.

I don't have time right now either, but may try to find time in
January or February.

zw

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants