Skip to content
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

Theorem environments no longer work in LaTeX/PDF output with Pandoc >= 2.7.3 #883

Closed
yihui opened this issue May 6, 2020 · 10 comments
Closed
Labels
bug an unexpected problem or unintended behavior

Comments

@yihui
Copy link
Member

yihui commented May 6, 2020

Because Pandoc converts <div class='foo'></div> to \begin{foo}\end{foo} now.

I'm a little surprised that no one has reported it so far. Perhaps this feature is rarely used by anyone... Anyway, this is a nasty bug and needs to be fixed soon.

https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#theorems

@yihui yihui added the bug an unexpected problem or unintended behavior label May 6, 2020
@yihui
Copy link
Member Author

yihui commented Jun 18, 2020

For anyone reading this issue and has the same problem, please:

remotes::install_github('yihui/knitr')

@tchevri
Copy link

tchevri commented Jun 19, 2020

Sorry to trouble you - I tried your fix and it did not work for me (just to confirm and make sure I did not screw up anything, my knitr version is 1.28.9 after running your command) . As per your earlier request, I posted as a comment back on SO.

XiangyunHuang added a commit to XiangyunHuang/ElegantBookdown that referenced this issue Jun 20, 2020
@cderv
Copy link
Collaborator

cderv commented Jun 21, 2020

Thanks for the report @tchevri. I can reproduce. As it bugged me, I spend all my spare time from the last two days trying to understand what is wrong with the fix.

After some times, I started again from the beginning not considering the current fix and I think I found the issue.

Because Pandoc converts <div class='foo'> to \begin{foo}\end{foo} now.

@yihui I did a few checks, and I think this comes from our recent latex-div.lua filter. That would mean it happens with every pandoc version.

See this reprex: the environment is created from a Div, only if I activate our filter.

test_in <- tempfile()
test_out <- tempfile(fileext = ".tex")
xfun::write_utf8(
  '<div class="definition">something</div>',
  test_in
)
# using last pandoc
rmarkdown::pandoc_version()
#> [1] '2.9.2.1'
# by default, pandoc does not create an environment from a Div
rmarkdown::pandoc_convert(test_in, to = 'latex', from = "html", 
                          output = test_out, verbose = TRUE)
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS "C:\Users\chris\AppData\Local\Temp\RtmpG6BPN2\file5e8052eb5c38" --to latex --from html --output "C:\Users\chris\AppData\Local\Temp\RtmpG6BPN2\file5e8089c27a9.tex"
xfun::read_utf8(test_out)
#> [1] "something"
# with our filter, an environment is created from a div
rmarkdown::pandoc_convert(test_in, to = 'latex', from = "html", 
                          output = test_out, verbose = TRUE,
                          options = rmarkdown:::pandoc_lua_filters("latex-div.lua"))
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS "C:\Users\chris\AppData\Local\Temp\RtmpG6BPN2\file5e8052eb5c38" --to latex --from html --output "C:\Users\chris\AppData\Local\Temp\RtmpG6BPN2\file5e8089c27a9.tex" --lua-filter "C:/Users/chris/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua"
xfun::read_utf8(test_out)
#> [1] "\\begin{definition}" ""                    "something"          
#> [4] ""                    "\\end{definition}"
unlink(c(test_in, test_out))

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

Reprex using pandoc 2.6
test_in <- tempfile()
test_out <- tempfile(fileext = ".tex")
xfun::write_utf8(
  '<div class="definition">something</div>',
  test_in
)
# using last pandoc
rmarkdown::find_pandoc(version = "2.6")
#> $version
#> [1] '2.6'
#> 
#> $dir
#> [1] "C:/Users/chris/scoop/shims"
rmarkdown::pandoc_version()
#> [1] '2.6'
# by default, pandoc does not create an environment from a Div
rmarkdown::pandoc_convert(test_in, to = 'latex', from = "html", 
                          output = test_out, verbose = TRUE)
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS "C:\Users\chris\AppData\Local\Temp\RtmpwzE4uh\file3abc7b856d08" --to latex --from html --output "C:\Users\chris\AppData\Local\Temp\RtmpwzE4uh\file3abc420185c.tex"
xfun::read_utf8(test_out)
#> [1] "something"
# with our filter, an environment is created from a div
rmarkdown::pandoc_convert(test_in, to = 'latex', from = "html", 
                          output = test_out, verbose = TRUE,
                          options = rmarkdown:::pandoc_lua_filters("latex-div.lua"))
#> "C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS "C:\Users\chris\AppData\Local\Temp\RtmpwzE4uh\file3abc7b856d08" --to latex --from html --output "C:\Users\chris\AppData\Local\Temp\RtmpwzE4uh\file3abc420185c.tex" --lua-filter "C:/Users/chris/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua"
xfun::read_utf8(test_out)
#> [1] "\\begin{definition}" ""                    "something"          
#> [4] ""                    "\\end{definition}"
unlink(c(test_in, test_out))

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

Reading the filter, I can confirm that the filter add a latex environment for each div with a class.
https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/lua/latex-div.lua

I think the fix should be either

  • in our filter so that we do really nothing if no data-latex attribute is provided.
  • or we should apply your fix for all pandoc version and consider that rmarkdown will convert Div to latex environment. (What pandoc does not want to do I think (reading at fenced span/div for latex output jgm/pandoc#5880 (comment))

What do you think ?

yihui added a commit to yihui/knitr that referenced this issue Jun 21, 2020
…e Pandoc version, just don't write the <div> when the output format is LaTeX, because rmarkdown will apply its Pandoc filter to convert any div to a LaTeX environment
@yihui
Copy link
Member Author

yihui commented Jun 21, 2020

@cderv I'm still hesitating on if the data-latex attribute should be mandatory (rstudio/rmarkdown#1779). Now I feel it probably should, since I have been surprised a few times that <div> is also converted to LaTeX environments (I was only expecting ::: {} to be converted). Anyway, yihui/knitr@3f08505 should fix @tchevri's problem. Please try:

remotes::install_github('yihui/knitr')

Thanks!

clrpackages pushed a commit to clearlinux-pkgs/R-knitr that referenced this issue Jun 25, 2020
Aaron A King (1):
      add engine for c++ to be compiled by 'R CMD SHLIB' (#1832)

Carson Sievert (2):
      Support for ragg::agg_png device (#1834)
      Pass options to showtext() (#1847)

Christophe Dervieux (1):
      fix #1845: correctly pass the dev.args option to the ragg_png device (#1846)

Ellis Hughes (1):
      fix #1838: format() columns individually for non-matrix objects in kable() (#1827)

Randy Lai (1):
      sql engine: check if there is more than one element in the first column (#1837)

Salim B (1):
      improve documentation (#1835)

Yihui Xie (44):
      start the next version
      forgot to update news in 4a50ee68
      the argument `error` of `include_graphics()` takes value from the global R option `knitr.graphics.error` by default
      fix the issue reported by @XiangyunHuang at https://d.cosx.org/d/421249
      allow users to customize the `sep` argument of the cat engine
      only use the echo option to control whether a chunk is included in the output via the `asis` engine
      for the dot and asy engines, first create the output file under the current working directory, then move it to the actual figure path
      roxygenize and bump version
      don't update texlive
      when there are multiple plots in a chunk, create labels by separateing labels and numbers with dashes
      roxygenize (after #1835) and bump version
      suggest ragg (amend #1834)
      support the chunk option dev.close = FALSE, and plan to drop the package option global.device = TRUE in future
      always restore par() if the package option global.par = TRUE
      indicate if a device is open by the package option dev.open
      set the package option dev.open outside chunk_device(), and after chunk_device() has been called successfully
      use a single argument `options` in chunk_device() since all other arguments are already in it
      don't deprecate the package option global.device, because it is actually quite complicated to implement a global device with a chunk option... the dev.close option won't work for child documents (e.g. this example failed: https://travis-ci.org/github/yihui/knitr/jobs/677962281)
      put the on.exit() call after when a new device has been opened (no need to store the device number in opts_knit)
      on exit, store par() in global.pars before the device is closed in a later on.exit() call
      pass the dpi option to showtext::showtext_opts(): yixuan/showtext#33 (comment)
      not sure if it matters, but set the dpi before showtext_begin()
      fix rstudio/bookdown#883: for LaTeX output in bookdown, do not write <div class="environment"> for Pandoc >= 2.7.3, because such a div would be converted to a LaTeX environment
      travis-ci.org -> .com
      add a markdown_mode argument to parse_block(), so parse_block() does not have to rely on opts_knit$get('out.format'), and users can specify if the document is in the markdown mode
      do not cache texlive
      not sure why LaTeX fails; try my own version of TeX Live instead
      include the content of the code chunk in the error message, to make it easier to find the chunk label that was duplicated in the document
      add a new function kables() to create a table containing multiple tables that are individually created from kable()
      c() all ... arguments in create_label(), instead of assuming that each of ... is of length 1 or 0
      amend ea7f3070ef42989d0ccc945eadfdd3c2454d0740: use dashes before fig numbers in the labels consistently in other output formats besides HTML
      move the R implementation of base64 encoding to the xfun package, and use xfun::base64_uri() instead of the unexported markdown:::.b64EncodeFile()
      a news item for 76fef5b9c535afa244989732b53cc8dfb7ffb471
      the first step for #1864: warn if markdown is not a dependency of the package containing vignettes based on the knitr::knitr engine
      amend 6907b428572c130982f6b3d6c91a164a15b94a30 to fix rstudio/bookdown#883 (comment): regardless of the Pandoc version, just don't write the <div> when the output format is LaTeX, because rmarkdown will apply its Pandoc filter to convert any div to a LaTeX environment
      close #1862: add an empty line after each bib entry in write_bib()
      factor out the code to get the env var _R_CHECK_PACKAGE_NAME_, and add a function to check if the package being checked is lower or equal to a version
      make sure the current knitr submission is safe on CRAN; will remove the hacks once arsenal and skimr are updated on CRAN
      check against oldrel, release, and devel versions of R on Travis
      do not use matrix and jobs together, but only use jobs
      change \link[pkg]{function} to pkg::\link{function} because R-devel now warns against the former usage:
      rename the job
      for help links to packages that are not hard dependencies, both the package name and the topic name have to be specified in \link[pkg:topic]{function}
      CRAN release v1.29

atusy (1):
      fix #1788 and also fix #1817: change the default table format from Pandoc's simple tables to pipe tables for R Markdown documents (#1830)
@cderv
Copy link
Collaborator

cderv commented Jun 29, 2020

I am still searching if there is a way to apply lua filer only on fenced div, and not explicit <div>.
It seems we may need to make the data-latex mandatory, or make the filter latex-div optional so that people can choose to opt-in, with the known consequences. Currently it applies to each rendering with any tools that uses rmarkdown and convert to PDF.

@yihui
Copy link
Member Author

yihui commented Jun 29, 2020

I just reverted rstudio/rmarkdown@14127ac with rstudio/rmarkdown@b2cff70. I'll update the R Markdown Cookbook soon.

I am still searching if there is a way to apply lua filer only on fenced div, and not explicit <div>.

I feel this may not be possible. Pandoc's parser treats them as identical:

 % echo '<div class="foo">abc</div>' | pandoc -f markdown -t native
[Div ("",["foo"],[])
 [Plain [Str "abc"]]]

% echo '::: foo\nabc\n:::' | pandoc -f markdown -t native
[Div ("",["foo"],[])
 [Para [Str "abc"]]]

@cderv
Copy link
Collaborator

cderv commented Jun 30, 2020

Shouldn't we also revert the change in knitr that closed this issue ?

<div class="environment">

won't be converted to latex environment anymore.

@RLesur
Copy link
Collaborator

RLesur commented Jun 30, 2020

I feel this may not be possible. Pandoc's parser treats them as identical:

Pandoc can treat them differently if one disables the native_divs extension (this extension is enabled by default for markdown input).

@yihui
Copy link
Member Author

yihui commented Jun 30, 2020

@cderv It doesn't matter. I mean yihui/knitr@6907b42 doesn't hurt, although it is no longer necessary.

@RLesur Thanks for the tip!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants