rstudio / rmarkdown

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

Math formula error in rendering pdf_document #2318

Open ghost opened 2 years ago

ghost commented 2 years ago

Hi,

I'm struggling to render this .Rmd file to a pdf_latex, although the LaTeX appears to be correct

---
title: "Untitled"
author: "TEST"
date: "2/28/2022"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

This code does not run

$$lift_{i,c}=\frac{\frac{\sum \text{nsales}{i,c}}{\sum \text{nimpressions}{i,c}}}{\frac{\sum \text{nsales}{i,FR}}{\sum \text{nimpressions}{i,FR}}}-1$$


The output is

<...>

/usr/lib/rstudio/bin/pandoc/pandoc +RTS -K512m -RTS test.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.tex --lua-filter /home/pc/R/x86_64-pc-linux-gnu-library/4.1/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/pc/R/x86_64-pc-linux-gnu-library/4.1/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable 'geometry:margin=1in' output file: test.knit.md

! Missing $ inserted.

$ l.75 ...{i,FR}}{\sum \text{n_impressions}_{i,FR}}} -1\] Try to find the following text in test.Rmd: ...{i,FR}}{\sum \text{n_impressions}_{i,FR}}} You may need to add $ $ around a certain inline R expression `r ` in test.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info. Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info. Execution halted ```` Session info: ```` R version 4.1.2 (2021-11-01) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Linux Mint 20.3, RStudio 2021.9.2.382 Locale: LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=lt_LT.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=lt_LT.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=lt_LT.UTF-8 LC_IDENTIFICATION=C Package version: base64enc_0.1.3 bslib_0.3.1 digest_0.6.29 evaluate_0.15 fastmap_1.1.0 fs_1.5.2 glue_1.6.2 graphics_4.1.2 grDevices_4.1.2 highr_0.9 htmltools_0.5.2 jquerylib_0.1.4 jsonlite_1.8.0 knitr_1.37 magrittr_2.0.2 methods_4.1.2 R6_2.5.1 rappdirs_0.3.3 rlang_1.0.1 rmarkdown_2.11.22 sass_0.4.0 stats_4.1.2 stringi_1.7.6 stringr_1.4.0 tinytex_0.37 tools_4.1.2 utils_4.1.2 xfun_0.29 yaml_2.3.5 Pandoc version: 2.14.0.3 ```` OS: Linux Mint 20.3 x86_64 with texlive-full package: ```` pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) kpathsea version 6.3.1 Copyright 2019 Han The Thanh (pdfTeX) et al. There is NO warranty. Redistribution of this software is covered by the terms of both the pdfTeX copyright and the Lesser GNU General Public License. For more information about these matters, see the file named COPYING and the pdfTeX source. Primary author of pdfTeX: Han The Thanh (pdfTeX) et al. Compiled with libpng 1.6.37; using libpng 1.6.37 Compiled with zlib 1.2.11; using zlib 1.2.11 Compiled with xpdf version 4.01 ```` - [x] installed and tested your bug with the development version of the rmarkdown package using `remotes::install_github("rstudio/rmarkdown")`? Any suggestions were I might have done an error? Or what parameters for pdf should I select to render this correctly?
cderv commented 2 years ago

although the LaTeX appears to be correct

Did you test the equation already ? It seems using \text{} with \sum does not work in LaTeX. I tried this smaller reprex

---
title: "Untitled"
author: "TEST"
date: "2/28/2022"
output: 
  pdf_document:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

This code does not run

$$ \sum \text{nsales}{i,c} $$


If you escape the underscore `_` using `\_`, your equation seems to render ok. 

````markdown
$$
lift_{i,c}=\frac{\frac{\sum n\_sales_{i,c}}{\sum n\_impressions_{i,c}}}{\frac{\sum n\_sales_{i,FR}}{\sum n\_impressions_{i,FR}}}-1
$$

I don't know if this is a LaTeX math limitation or conflict. Maybe there is another way to write a sum with \text{}