rstudio / rmarkdown

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

trouble escaping raw latex #2277

Open bbolker opened 2 years ago

bbolker commented 2 years ago

This is related to https://github.com/rstudio/rmarkdown/issues/1449, but the standard solution (identify a raw LaTeX block) isn't working for me.

HTML output looks like this:

Screenshot from 2022-01-11 16-11-13

(note that "\small" is interpreted literally rather than executed as a LaTeX command)

PDF output is fine:

Screenshot from 2022-01-11 16-11-47

Here's the whole Rmd file:

---
title: "test of raw latex"
---

```{=latex}
\begin{equation}
S  =  \underbrace{b}_{\text{\small birth}}
    - \underbrace{\beta S I }_{\text{\small infection}}
\end{equation}
## there is a triple-back-quote ending delimiter here that I'm having trouble formatting

Session info:


R Under development (unstable) (2022-01-09 r81462)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 21.04

Locale:
  LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
  LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
  LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
  LC_PAPER=en_US.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3  bslib_0.3.1      digest_0.6.29    evaluate_0.14   
  fastmap_1.1.0    fs_1.5.2         glue_1.6.0       graphics_4.2.0  
  grDevices_4.2.0  highr_0.9        htmltools_0.5.2  jquerylib_0.1.4 
  jsonlite_1.7.2   knitr_1.37       magrittr_2.0.1   methods_4.2.0   
  R6_2.5.1         rappdirs_0.3.3   rlang_0.4.12     rmarkdown_2.11.6
  sass_0.4.0       stats_4.2.0      stringi_1.7.6    stringr_1.4.0   
  tinytex_0.36     tools_4.2.0      utils_4.2.0      xfun_0.29       
  yaml_2.2.1      

Pandoc version: 2.9.2.1
cderv commented 2 years ago

Hi @bbolker,

For HTML output, the math rendering will by default be supported by MathJax. Here, you are trying to pass raw LaTeX for an HTML output, so this is not full proof. Using the Math markdown syntax would be easier for multi format output. Know that passing raw LaTeX will not always work as MathJaX is not 100% porting LaTeX syntax.

In this case of your example, MathJaX will recognize the \begin{equation} I believe because it is among the few supportted environment so it will work. Also it seems \small and \text are among the supported command. However, I think you need to invert the use of \text{} and \small{}. Try:

---
title: "test of raw latex"
output:
  html_document: default
  pdf_document: default
---

```{=latex}
\begin{equation}
S  =  \underbrace{b}_{\small \text{birth}}
    - \underbrace{\beta S I }_{\small \text{infection}}
\end{equation}

![image](https://user-images.githubusercontent.com/6791940/149117855-5886c51d-4baf-4b38-b945-ab72e3220755.png)

That being said, with R Markdown, it is often better to Markdown syntax which has support for Math Syntax (https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#math-expressions) from Pandoc's Markdown: https://pandoc.org/MANUAL.html#math

So using `$$` block

````markdown
---
title: "test of raw latex"
output:
  pdf_document: default
  html_document: default
---

Equation

$$
S  =  \underbrace{b}_{\small \text{birth}}
    - \underbrace{\beta S I }_{\small \text{infection}}
$$

It depends if you want numbering equation support or not in the PDF I guess.

Anyway, both works as expected I believe. Does it help ?

there is a triple-back-quote ending delimiter here that I'm having trouble formatting

See the issue guide about how to format https://yihui.org/issue/#please-format-your-issue-correctly