-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
protect_math() fails for "\\", "\," and "\;" #61
Comments
Also, display math seems to require newlines after the first
R code: x <- tinkr::yarn$new("examples/example.md")
x$protect_math()
x$show()
## $$
## \exp(x)
## $$
##
## $$\\exp(x)$$ |
Thank you for the report! Sorry I haven't gotten to it until now (been swamped with releasing a large project). The inline block math behaviour was known, but I did not know about the slashes disappearing. I think I may have some time this week to address both of them. |
No worries! Thank you for looking into it! |
Just a follow up, I have been looking into this and it's a problem with the fact that commonmark sees everything in a commonmark context and it doesn't understand math mode (which is why we needed to create The problem is that when commonmark reads in markdown, it helpfully removes any escapes so we don't know which characters in the original document were escaped 😩. Take for example: f <- textConnection("a\\; b; c \\\\ d")
y <- tinkr::yarn$new(f)
writeLines(as.character(y$body))
#> <?xml version="1.0" encoding="UTF-8"?>
#> <!DOCTYPE document SYSTEM "CommonMark.dtd">
#> <document xmlns="http://commonmark.org/xml/1.0">
#> <paragraph>
#> <text xml:space="preserve">a; b; c \ d</text>
#> </paragraph>
#> </document> Created on 2022-03-14 by the reprex package (v2.0.1) This has a semicolon and a backslash that are both escaped in the original document, but in the XML representation, those escape characters are just not there and unfortunately, things like That being said, until I get this fixed, if your escaped math symbols will have consistent formatting (e.g. any escaped symbols will always be surrounded by spaces), then you can post-process after protecting the math by re-adding in the escapes with regex: ex <- r"{
Example 1: $a = \begin{pmatrix} b \\ c \end{pmatrix}$
Example 2: $f(x, \, y)$
Example 3: $f(x, \; y)$
}"
y <- tinkr::yarn$new(textConnection(ex))$protect_math()
asis <- xml2::xml_find_all(y$body, ".//md:text[@asis]", y$ns)
txt <- xml2::xml_text(asis)
xml2::xml_set_text(asis, gsub("\\s([,;\\])\\s", "\\\\\\1", txt))
#> {xml_nodeset (3)}
#> [1] <text asis="true">$a = \\begin{pmatrix} b\\\\c \\end{pmatrix}$</text>
#> [2] <text asis="true">$f(x,\\,y)$</text>
#> [3] <text asis="true">$f(x,\\;y)$</text>
y$show()
#> Example 1: $a = \begin{pmatrix} b\\c \end{pmatrix}$
#>
#> Example 2: $f(x,\,y)$
#>
#> Example 3: $f(x,\;y)$ Created on 2022-03-14 by the reprex package (v2.0.1) |
And I forgot to add in the spaces around the output in the gsub command, but you get the gist 😅 |
Thank you for this great package! I'm using it to format Python code chunks in RMarkdown with black, isort, etc.
I think I've found a small issue with
protect_math()
, which seems to fail for\\
,\,
and\;
. Here's my reprex: Create a fileexample.md
with the following content:Then run:
Would be cool to see a fix for this!
Session Info
The text was updated successfully, but these errors were encountered: