wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
651 stars 37 forks source link

Possible bug in `element_markdown()` #40

Closed fmmattioni closed 3 years ago

fmmattioni commented 4 years ago

I have been trying to place the following label on my axis: [La-] (mmol·L-1).

The problem is that it seems the use of [ ] and ( ) is causing a bug.

reprex with bug:

library(ggplot2)
library(ggtext)

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  labs(x = "[La<sup>-</sup>] (mmol·L<sup>-1</sup>)") +
  theme(axis.title.x = element_markdown())
#> Error: gridtext has encountered a tag that isn't supported yet: <a>
#> Only a very limited number of tags are currently supported.

Created on 2020-06-06 by the reprex package (v0.3.0)

When I use ( ) instead of [ ], the bug seems to disappear:

library(ggplot2)
library(ggtext)

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  labs(x = "(La<sup>-</sup>) (mmol·L<sup>-1</sup>)") +
  theme(axis.title.x = element_markdown())

Created on 2020-06-06 by the reprex package (v0.3.0)

Any thoughts on this?

clauswilke commented 4 years ago

This happens because the pattern [...] (...) is interpreted as a link in markdown. One workaround is to use the html entities representing [ and ]:

library(ggplot2)
library(ggtext)

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  labs(x = "&#91;La<sup>-</sup>&#93; (mmol·L<sup>-1</sup>)") +
  theme(axis.title.x = element_markdown())

Created on 2020-06-06 by the reprex package (v0.3.0)

The other is to use a non-breaking space. While element_markdown() doesn't actually handle non-breaking spaces correctly, it's sufficient here to stop the markdown interpreter from inserting a link:

library(ggplot2)
library(ggtext)

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  labs(x = "[La<sup>-</sup>]&nbsp;(mmol·L<sup>-1</sup>)") +
  theme(axis.title.x = element_markdown())

Created on 2020-06-06 by the reprex package (v0.3.0)

fmmattioni commented 4 years ago

Oohh, that is true.. I am so sorry, I have completed overlooked this.. thank you, @clauswilke!