rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.87k stars 977 forks source link

Math rendered with github_document changed between 2.7 and 2.11 #2003

Closed cderv closed 2 years ago

cderv commented 3 years ago

This is following observation on https://github.com/rstudio/rmarkdown/issues/1886 and it is related. Opening the issue just to track the change than happened between Pandoc version

# pandoc 2.7.3
$ ./pandoc -t gfm <<< '$\frac{e^{i \pi}}{10}$'
\(\frac{e^{i \pi}}{10}\)
$ ./pandoc -t gfm+tex_math_dollars <<< '$\frac{e^{i \pi}}{10}$'
\(\frac{e^{i \pi}}{10}\)
# pandoc 2.11.3.2
$ pandoc -t gfm <<< '$\frac{e^{i \pi}}{10}$'
[WARNING] Could not convert TeX math '\frac{e^{i \pi}}{10}', rendering as TeX
$\\frac{e^{i \\pi}}{10}$
$ pandoc -t gfm+tex_math_dollars <<< '$\frac{e^{i \pi}}{10}$'
$\frac{e^{i \pi}}{10}$
$ pandoc -t gfm+tex_math_single_backslash <<< '$\frac{e^{i \pi}}{10}$'
The extension tex_math_single_backslash is not supported for gfm

How does it look with github_document ?

dir.create(tmp_dir <- tempfile())
owd <- setwd(tmp_dir)
xfun::write_utf8(c(
  "---",
  "title: test",
  "---",
  "",
  "$e^{i \\pi}$",
  "",
  "$\\frac{e^{i \\pi}}{10}$",
  ""
), "test.Rmd")

# With Pandoc 2.7 (RStudio 1.3)
pandoc_2.7 <- fs::path_home("Logiciel/pandoc/pandoc-2.7.3")
rmarkdown::find_pandoc(cache = FALSE, dir = pandoc_2.7)
#> $version
#> [1] '2.7.3'
#> 
#> $dir
#> [1] "C:/Users/chris/Logiciel/pandoc/pandoc-2.7.3"
res <- rmarkdown::render("test.Rmd", "github_document")
#> processing file: test.Rmd
#> output file: test.knit.md
#> "C:/Users/chris/Logiciel/pandoc/pandoc-2.7.3/pandoc" +RTS -K512m -RTS test.utf8.md --to gfm --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.md --standalone --atx-headers --template "C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\default.md" 
#> "C:/Users/chris/Logiciel/pandoc/pandoc-2.7.3/pandoc" +RTS -K512m -RTS test.md --to html4 --from gfm --output test.html --standalone --self-contained --highlight-style pygments --template "C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\preview.html" --variable "github-markdown-css:C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\github.css" --metadata pagetitle=PREVIEW
#> 
#> Preview created: test.html
#> 
#> Output created: test.md
xfun::file_string(res)
#> test
#> ================
#> 
#> \(e^{i \pi}\)
#> 
#> \(\frac{e^{i \pi}}{10}\)

# With Pandoc 2.11 (RStudio 1.4)
rmarkdown::find_pandoc(cache = FALSE, version = "2.11.3.2")
#> $version
#> [1] '2.11.3.2'
#> 
#> $dir
#> [1] "C:/Users/chris/scoop/shims"
res <- rmarkdown::render("test.Rmd", "github_document")
#> processing file: test.Rmd
#> output file: test.knit.md
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS test.utf8.md --to gfm --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.md --standalone --template "C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\default.md" 
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS test.md --to html4 --from gfm --output test.html --standalone --self-contained --highlight-style pygments --template "C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\preview.html" --variable "github-markdown-css:C:\Users\chris\Documents\R\win-library\4.0\rmarkdown\rmarkdown\templates\github_document\resources\github.css" --metadata pagetitle=PREVIEW
#> 
#> Preview created: test.html
#> 
#> Output created: test.md
xfun::file_string(res)
#> test
#> ================
#> 
#> *e*<sup>*i**p*</sup>
#> 
#> $\\frac{e^{i \\pi}}{10}$

# We can keep the dollar 
res <- rmarkdown::render("test.Rmd", 
                         rmarkdown::md_document("gfm+tex_math_dollars"), 
                         quiet = TRUE)
xfun::file_string(res)
#> $e^{i \pi}$
#> 
#> $\frac{e^{i \pi}}{10}$

# clean
setwd(owd)
unlink(tmp_dir, recursive = TRUE)

Created on 2021-01-07 by the reprex package (v0.3.0.9001)

I don't think there is an extension so that the new gfm output as the old one, because tex_math_single_backslash is not supported by gfm, or commonmark or even commonmark_x.

It seems that the default behavior Pandoc 2.7 was to transform the math to single backslash even if the extension was already deactivated.

$ ./pandoc --version
pandoc.exe 2.7.3
Compiled with pandoc-types 1.17.5.4, texmath 0.11.2.2, skylighting 0.8.1
Default user data directory: C:\Users\chris\AppData\Roaming\pandoc
Copyright (C) 2006-2019 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

$ ./pandoc --list-extensions=gfm
-abbreviations
+all_symbols_escapable
-amuse
-angle_brackets_escapable
-ascii_identifiers
+auto_identifiers
+autolink_bare_uris
+backtick_code_blocks
-blank_before_blockquote
-blank_before_header
-bracketed_spans
-citations
-compact_definition_lists
-definition_lists
-east_asian_line_breaks
+emoji
-empty_paragraphs
-epub_html_exts
-escaped_line_breaks
-example_lists
-fancy_lists
-fenced_code_attributes
+fenced_code_blocks
-fenced_divs
-footnotes
-four_space_rule
+gfm_auto_identifiers
-grid_tables
-hard_line_breaks
-header_attributes
-ignore_line_breaks
-implicit_figures
-implicit_header_references
-inline_code_attributes
-inline_notes
+intraword_underscores
-latex_macros
-line_blocks
-link_attributes
+lists_without_preceding_blankline
-literate_haskell
-markdown_attribute
-markdown_in_html_blocks
-mmd_header_identifiers
-mmd_link_attributes
-mmd_title_block
-multiline_tables
-native_divs
-native_spans
-ntb
-old_dashes
-pandoc_title_block
+pipe_tables
-raw_attribute
+raw_html
-raw_tex
+shortcut_reference_links
-simple_tables
-smart
+space_in_atx_header
-spaced_reference_links
-startnum
+strikeout
-subscript
-superscript
-styles
+task_lists
-table_captions
-tex_math_dollars
-tex_math_double_backslash
-tex_math_single_backslash
-yaml_metadata_block

$ ./pandoc -t gfm-tex_math_single_backslash <<< '$\frac{e^{i \pi}}{10}$'
\(\frac{e^{i \pi}}{10}\)

Possible option I see:

cderv commented 3 years ago

We could also try to recreate a "gfm_format". There is one unused internally in rmarkdown:::gfm_format() but as GFM does not really handle math, it does not either

xfun::write_utf8(c(
  "---",
  "title: test",
  "---",
  "",
  "$e^{i \\pi}$",
  "",
  "$\\frac{e^{i \\pi}}{10}$",
  ""
), "test.Rmd")

# With Pandoc 2.7 (RStudio 1.3)
pandoc_2.7 <- fs::path_home("Logiciel/pandoc/pandoc-2.7.3")
rmarkdown::find_pandoc(cache = FALSE, dir = pandoc_2.7)
#> $version
#> [1] '2.7.3'
#> 
#> $dir
#> [1] "C:/Users/chris/Logiciel/pandoc/pandoc-2.7.3"
res <- rmarkdown::render("test.Rmd", rmarkdown::md_document(rmarkdown:::gfm_format()),
                         quiet = TRUE)
xfun::file_string(res)
#> *e*<sup>*i**p*</sup>
#> 
#> $\\frac{e^{i \\pi}}{10}$

# With Pandoc 2.11 (RStudio 1.4)
rmarkdown::find_pandoc(cache = FALSE, version = "2.11.3.2")
#> $version
#> [1] '2.11.3.2'
#> 
#> $dir
#> [1] "C:/Users/chris/scoop/shims"
res <- rmarkdown::render("test.Rmd", 
                         rmarkdown::md_document(rmarkdown:::gfm_format()), 
                         quiet = TRUE)
xfun::file_string(res)
#> *e*<sup>*i**p*</sup>
#> 
#> $\\frac{e^{i \\pi}}{10}$
cderv commented 3 years ago

I think the best way will be to support --webtex for github_document format. This should be an extension of the work in #1940

jmbuhr commented 3 years ago

It seems that pandoc now wants to really enforce the use of gfm over markdown_github, as I am seeing a warning (e.g. in pkgdown vignettes):

[WARNING] Deprecated: markdown_github. Use gfm instead.
cderv commented 3 years ago

Yes markdown_github will probably disappear in future versions of Pandoc.

pkgdown will switch to gfmto fix the warning (https://github.com/r-lib/pkgdown/pull/1473).

rmarkdown already use gfm for pandoc 2.0+ https://github.com/rstudio/rmarkdown/blob/44d7b1093b46bd3cee217ba13cabdf8bc60e1783/R/github_document.R#L41-L46

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.