yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 873 forks source link

pdf asis_output heisenbug: TeX is sometimes escaped #1622

Closed hughjonesd closed 5 years ago

hughjonesd commented 5 years ago

When knitting the attached file, the \begin{table} and \end{table} lines are escaped, even though the chunk has results="asis".

The relevant chunk is:

htl_raw <- "
\\begin{table}[h]
\\providecommand{\\somecommand}[2][default]{Print something}
\\somecommand{}
\\end{table}
"

cat(htl_raw)

The result looks like:

\textbackslash{}begin\{table\}{[}h{]}

\providecommand{\somecommand}[2][default]{Print something}

\textbackslash{}somecommand\{\} \textbackslash{}end\{table\}

This is as small a MWE as I could get. I think it has something to do with the TeX being parsed, because very similar examples don't do the same thing. For example, if I change \\somecommand{} to \\somecommand{}{}, then the TeX is not escaped.

Apologies if this is pandoc (on one side) or rmarkdown (on the other). The toolchain becomes long here, and I'm not even sure I understand how these tools interact.

I guess the underlying question is, why is the TeX being escaped ever?

asis-output-heisenbug.zip

Sessioninfo:


> devtools::session_info()
Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.456)           
 language (EN)                        
 collate  en_GB.UTF-8                 
 tz       Europe/London               
 date     2018-10-23                  

Packages ---------------------------------------------------------------------------------------------
 package    * version    date       source                         
 assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                 
 backports    1.1.2      2017-12-13 CRAN (R 3.5.0)                 
 base       * 3.5.1      2018-07-05 local                          
 bindr        0.1.1      2018-03-13 CRAN (R 3.5.0)                 
 bindrcpp     0.2.2      2018-03-29 CRAN (R 3.5.0)                 
 broom        0.5.0      2018-07-17 CRAN (R 3.5.0)                 
 compiler     3.5.1      2018-07-05 local                          
 crayon       1.3.4      2017-09-16 CRAN (R 3.5.0)                 
 datasets   * 3.5.1      2018-07-05 local                          
 devtools     1.13.6     2018-06-27 CRAN (R 3.5.0)                 
 digest       0.6.16     2018-08-22 CRAN (R 3.5.0)                 
 dplyr        0.7.6      2018-06-29 CRAN (R 3.5.1)                 
 evaluate     0.11       2018-07-17 CRAN (R 3.5.0)                 
 glue         1.3.0      2018-08-01 Github (tidyverse/glue@a292148)
 graphics   * 3.5.1      2018-07-05 local                          
 grDevices  * 3.5.1      2018-07-05 local                          
 grid         3.5.1      2018-07-05 local                          
 htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                 
 huxtable   * 4.2.0.9000 2018-10-23 local                          
 knitr      * 1.20       2018-02-20 CRAN (R 3.5.0)                 
 lattice      0.20-35    2017-03-25 CRAN (R 3.5.1)                 
 magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                 
 memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                 
 methods    * 3.5.1      2018-07-05 local                          
 nlme         3.1-137    2018-04-07 CRAN (R 3.5.1)                 
 pillar       1.3.0      2018-07-14 CRAN (R 3.5.0)                 
 pkgconfig    2.0.2      2018-08-16 CRAN (R 3.5.0)                 
 purrr        0.2.5      2018-05-29 CRAN (R 3.5.0)                 
 R6           2.2.2      2017-06-17 CRAN (R 3.5.0)                 
 Rcpp         0.12.18    2018-07-23 CRAN (R 3.5.0)                 
 rlang        0.2.2      2018-08-16 CRAN (R 3.5.0)                 
 rmarkdown    1.10       2018-06-11 CRAN (R 3.5.0)                 
 rprojroot    1.3-2      2018-01-03 CRAN (R 3.5.0)                 
 rstudioapi   0.7        2017-09-07 CRAN (R 3.5.0)                 
 stats      * 3.5.1      2018-07-05 local                          
 stringi      1.2.4      2018-07-20 CRAN (R 3.5.0)                 
 stringr      1.3.1      2018-05-10 CRAN (R 3.5.0)                 
 tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                 
 tidyr        0.8.1      2018-05-18 CRAN (R 3.5.0)                 
 tidyselect   0.2.4      2018-02-26 CRAN (R 3.5.0)                 
 tools        3.5.1      2018-07-05 local                          
 utils      * 3.5.1      2018-07-05 local                          
 withr        2.1.2      2018-03-15 CRAN (R 3.5.0)                 
 yaml         2.2.0      2018-07-25 CRAN (R 3.5.0) 
yihui commented 5 years ago

It might be this issue https://github.com/jgm/pandoc/issues/2493

With Pandoc 2.x (currently shipped with the RStudio preview version), there is a simple solution, which is to use raw blocks: http://pandoc.org/MANUAL.html#generic-raw-attribute

hughjonesd commented 5 years ago

Is there a way to do that programmatically? raw_output seems to do something different.

On Tue, 23 Oct 2018 at 14:35, Yihui Xie notifications@github.com wrote:

It might be this issue jgm/pandoc#2493 https://github.com/jgm/pandoc/issues/2493

With Pandoc 2.x (currently shipped with the RStudio preview version), there is a simple solution, which is to use raw blocks: http://pandoc.org/MANUAL.html#generic-raw-attribute

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yihui/knitr/issues/1622#issuecomment-432248346, or mute the thread https://github.com/notifications/unsubscribe-auth/AFjJ92WHd4LNd6vjLUfU0gUrDSnUTcKJks5unxsxgaJpZM4X1f1Y .

-- Sent from Gmail Mobile

yihui commented 5 years ago

That sounds like a good idea, but I don't have time for it.

hughjonesd commented 5 years ago

I sympathize! I will try and find a hack that does not trigger pandoc's hissy fits.

hughjonesd commented 5 years ago

Am I right in thinking that knitr could take advantage of this pandoc feature, by emitting raw blocks around raw_output? Is this a substitute for the current raw_output mechanism? (Or does it address a different use case?)

I don’t mind trying to fix this myself if you’re open to it, because the situation at the moment makes TeX output unpredictable for huxtable (or indeed other solutions like xtable, as I discovered yesterday...).

David -- Sent from Gmail Mobile

fedelibre commented 5 years ago

It might be this issue jgm/pandoc#2493

With Pandoc 2.x (currently shipped with the RStudio preview version), there is a simple solution, which is to use raw blocks: http://pandoc.org/MANUAL.html#generic-raw-attribute

The explicit raw blocks (```{=latex}) helped me to solve my problem, which is not related at all with issue #2493, as no $ is present in my file. I'm using a Markdown file, where I insert some Latex raw blocks. Since I've upgraded Pandoc to a more recent version (currently 2.1.2), there's some crazy escaping.

This is the source:

\begin{figure}[H]
\centering
\begin{lilypond}[nofragment]
\include "lilypond-book-preamble.ly"
\include "custom.ly"
musica = {
  \time 2/4
  c'8-1 e' d'16-3 c'-1 a-2 g |
  c'2-1 %\bar "|." |
}
\score {
  \new StaffGroup <<
    \new Staff { \clef "treble_8" \musica }
    \new TabStaff { \clef "moderntab" \new TabVoice \musica }
  >>
}
\paper { line-width = 8\cm }
\end{lilypond}
\end{figure}

and this is the latex output:

\textbackslash{}begin\{figure\}{[}H{]} \centering
\textbackslash{}begin\{lilypond\}{[}nofragment{]}
\include ``lilypond-book-preamble.ly'' \include ``custom.ly'' musica =
\{ \time 2/4 c'8-1 e' d'16-3 c'-1 a-2 g \textbar{} c'2-1
\%\bar ``\textbar{}.'' \textbar{} \} \score {
  \new StaffGroup <<
    \new Staff { \clef "treble_8" \musica }
    \new TabStaff { \clef "moderntab" \new TabVoice \musica }
  >>
} \paper { line-width = 8\cm } \textbackslash{}end\{lilypond\}
\textbackslash{}end\{figure\}

Which change in Pandoc might have caused this?

(I'm using lyluatex to process music fragments with Lilypond)

fedelibre commented 5 years ago

Sorry, I thought I was on Pandoc issue tracker

fedelibre commented 5 years ago

My problem is probably this issue. Sorry for the noise!

yihui commented 5 years ago

If you are using Pandoc 2.x, you could use knitr::raw_latex(htl_raw) instead of cat() + results='asis'.

github-actions[bot] commented 3 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.