wilkelab / ggtext

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

Add support to all basic Markdown syntax elements and fix superscript-related issue #12

Closed GegznaV closed 4 years ago

GegznaV commented 4 years ago

As a user, I expect all basic Markdown syntax (see bullets 1, 2, and 3 below) to be supported correctly. Making new lines, bold, and italic text as well as escaping symbols (\* \_" \^ \~ \\ $) works as expected.

But there are the following issues:

1) Superscript tags do not close correctly (see examples below). 2) Making subscripts, endashes, and emdashes does not work at all (see examples below). 3) If I try to make verbatim code and strikethrough text, the error messages state that tags <code> and <del> are not supported (see examples below). Most probably, it's an issue in gridtext package. 4) There is an issue in rendering LaTeX equations, but as understood, work is in progress (#4).

The examples of issues in rendering Markdown syntax elements (sorry for the large images: I'm not sure how to control the size of them with reprex)

library(ggplot2)
library(ggtext)

gg_example <- function(str) {
  ggplot() +
  ggtitle(str) +
  theme(plot.title = element_markdown(lineheight = 1.1))
}
# NOT OK (does not close correctly)
gg_example("normal ^superscript^ normal")

# NOT OK (does not close correctly)
gg_example("Length (cm^2^)")

# Does not work:
gg_example("normal ~subscript~ normal")

# Does not work:
gg_example("(CO~2~) ")

# Does not work:
gg_example("endash: --")

# Does not work:
gg_example("emdash: ---")

# Not OK (not interpreted as Equation):
gg_example("equation: $A = \\pi*r^{2}$")


# Error
gg_example("normal `verbatim code` normal")
#> Error: gridtext has encountered a tag that isn't supported yet: <code>
#> Only a very limited number of tags are currently supported.

# Error
gg_example("normal ~~strikethrough~~ normal")
#> Error: gridtext has encountered a tag that isn't supported yet: <del>
#> Only a very limited number of tags are currently supported.

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

The examples of code with Markdown syntax elements that are rendered as expected.

# OK
gg_example(
"End a line with two spaces  
to start a new paragraph."
)

# OK
gg_example("normal *italics* normal")
gg_example("normal _italics_ normal")

# OK
gg_example("normal **bold** normal")  
gg_example("normal __bold__ normal") 

# OK
gg_example("normal **_bold italics_** normal")  

# OK
gg_example("escaped: \\*")
gg_example("escaped: \\_")
gg_example("escaped: \\^")
gg_example("escaped: \\~")
gg_example("escaped: \\\\")
gg_example("escaped: \\$")
clauswilke commented 4 years ago

This is a gridtext issue.

kongdd commented 2 years ago

Just wondering whether gridtext has plan to support this feature?

kongdd commented 2 years ago

This my solution for superscript and subscript. Hope that helps.

#' @importFrom stringr str_replace_all
#' @export
str_mk <- function(x) {
    replacement <- c(
        "(\\^\\{?)([\\w\\+\\-\\*\\\\]*)(\\}?)" = "<sup>\\2</sup>",
        "(\\_\\{?)([\\w\\+\\-\\*\\\\]*)(\\}?)" = "<sub>\\2</sub>",
        "\n" = "<br>"
    )
    str_replace_all(x, replacement)
}

x = c(
    "gC m^{-2} d^{-1}",
    "gC m^-2 d^-1",
    "gC m_{-2} d_{-1}",
    "gC m_-2 d_-1", 
    "gC \n mm/d"
)
str_mk(x)
#> [1] "gC m<sup>-2</sup> d<sup>-1</sup>" "gC m<sup>-2</sup> d<sup>-1</sup>"
#> [3] "gC m<sub>-2</sub> d<sub>-1</sub>" "gC m<sub>-2</sub> d<sub>-1</sub>"
#> [5] "gC <br> mm/d"

https://rpkgs.github.io/gg.layers/reference/str_mk.html