Search code examples
rlatexr-markdown

render pdf document with the inline R code output number more than 5 digits


Issue

It seems that it can't render the PDF document with the inline R code output number more than 5 digits.

Here is a minimal example highlighting the behaviour:

---
title: "test"
author:
  - test
output: pdf_document
---

# test

```{r}
a <- 12345
```

test: `r a`

This returns the error:

"D:/Ksoftware/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS testpdf2-ctex.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output testpdf2-ctex.tex --template "C:\Users\krup\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" 
! Missing $ inserted.
<inserted text> 
                $
l.137 test: 1.2345\times
                         10\^{}\{4\} 
Here is how much of TeX's memory you used:
 10802 strings out of 494923
 149004 string characters out of 6180912
 210884 words of memory out of 5000000
 14019 multiletter control sequences out of 15000+600000
 23985 words of font info for 22 fonts, out of 8000000 for 9000
 14 hyphenation exceptions out of 8191
 28i,3n,32p,240b,290s stack positions out of 5000i,500n,10000p,200000b,80000s

错误: Failed to compile testpdf2-ctex.tex. See testpdf2-ctex.log for more info.
此外: Warning message:
运行命令'"pdflatex" -halt-on-error -interaction=batchmode "testpdf2-ctex.tex"'的状态是1 
停止执行

Is this an issue?


Solution

  • This issue has previous been noted within the issues of the repository: https://github.com/rstudio/rmarkdown/issues/160

    The problem stems from the scientific notation, not the length of the number. When there are more than 5 digits R will be default convert the number to 1.234 * 10^4, which causes issues in the conversion to the LaTeX file.

    The post shows a few solutions to the problem:

    1. Enclose the inline code in $ symbols

    The $ symbol in RMarkdown is use for mathematical notation:

    ```{r}
    a <- 12345
    ```
    
    test: $`r a`$
    

    2. Prevent Scientific Notation Globally

    You can prevent R from converting numbers by changing the scipen value in options:

    ```{r}
    options(scipen=999)
    ```
    
    `r a`