stefano-meschiari / latex2exp

Use LaTeX in R graphics.
Other
186 stars 10 forks source link

white space causes unexpected behaviour #2

Closed goldingn closed 9 years ago

goldingn commented 9 years ago

Awesome work, this package is going to be really useful!

I noticed that whitespace in equations caused some unexpected behaviour. In the following code, the top two panels are as expected and the bottom two aren't:

par(mfrow = c(2, 2))

# as expected
plot.new()
text(0.5, 0.5, latex2exp('$\\frac{1}{x^2}$'), cex = 5)

# as expected
plot.new()
text(0.5, 0.5, latex2exp('$2^{\\frac{1}{x^2}}$'), cex = 5)

# power dies not appear
plot.new()
text(0.5, 0.5, latex2exp('$\\frac{1}{x ^ 2}$'), cex = 5)

# power appears outside the fraction
plot.new()
text(0.5, 0.5, latex2exp('$2^{\\frac{1}{x ^ 2}}$'), cex = 5)

A simple fix might be to remove any whitespace within equations. I'd have a bash and submit a PR, but I'm unfamiliar with stringr so would probably take me forever

stefano-meschiari commented 9 years ago

Unfortunately you can't just remove whitespace within $$, otherwise $\alpha 2$ would become $\alpha2$ (which is an error in LaTeX). The issue is that space can signal a new LaTeX token, unless it's preceded by ^ or _.

I just fixed this in 0.3.1 (to be installed using devtools — see the README). It should now be more compliant with how LaTeX treats whitespace. If you find more strange behavior, don't hesitate to file a bug!

goldingn commented 9 years ago

Yup, that works just fine - thanks!