Strange Rmd knitting behavior

Note that the following toy Rmd file will successfully compile to a pdf using the Knit button in Rstudio:


title: "toy_example"
output: pdf_document

```{r cars}
kable(head(mtcars), format = "latex", booktabs = T)

It fails, however, using rmarkdown::render("toy_ex.Rmd") from the console, producing this error at the end of attempted compliation:

! Undefined control sequence.
l.121 \toprule

Error: LaTeX failed to compile toy_ex.tex.

This seems to be some problem with the intermediate .tex file that the call to kable() is generating, but why woud it knit successfully from the Rstudio IDE and not from the console?

I've followed Yihui's debugging steps here; updated packages, reinstalled tinytex, etc.

rstudio: 1.2.1335
knitr: 1.28
rmarkdown: 2.3
tinytex: 0.22
kableExtra: 1.1.0

I'm on a mac running 10.15.4 Catalina

Any answers are appreciated-


Thanks for the reproducible example. That made it very easy to investigate.

I was able to reproduce this behavior on Windows 10.

  1. I knit the Rmd file using the RStudio Knit button. This caused TinyTeX to install lots of TeX packages. The call to pandoc is below:

    "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output toy_ex.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in" --include-in-header "C:\Users\john\AppData\Local\Temp\RtmpOObyVC\rmarkdown-strcc20ea115d.html"
  2. I then ran rmarkdown::render() in the R console:

    processing file: toy_ex.Rmd
      |..........................................                                         |  50%
      ordinary text without R code
      |...................................................................................| 100%
    label: cars
    output file:
    "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output toy_ex.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in" 
    ! Undefined control sequence.
    l.117 \toprule
    Error: LaTeX failed to compile toy_ex.tex. See for debugging tips. See toy_ex.log for more info.

Comparing the two calls to pandoc, there was only one difference. The RStudio Knit button version included --include-in-header "C:\Users\john\AppData\Local\Temp\RtmpOObyVC\rmarkdown-strcc20ea115d.html".

However, after I restarted my R session and re-ran rmarkdown::render("toy_ex.Rmd"), it suddenly worked. And it additionally included the --include-in-header argument to pandoc. Thus my guess is that somehow the recently installed TeX files weren't immediately available to the R console, and thus required a restart first.

After you restart the session (Cmd-Shift-F10 on macOS), do you still see the error with rmarkdown::render()?

In case it could be helpful, here's my session information:

R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041), RStudio 1.3.959

  LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
  LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
  LC_TIME=English_United States.1252    

Package version:
  askpass_1.1          assertthat_0.2.1     base64enc_0.1.3      BH_1.72.0.3         
  callr_3.4.3          cli_2.0.2            clipr_0.7.0          colorspace_1.4-1    
  compiler_4.0.1       crayon_1.3.4         curl_4.3             digest_0.6.25       
  ellipsis_0.3.0       evaluate_0.14        fansi_0.4.1          farver_2.0.3        
  glue_1.4.1           graphics_4.0.1       grDevices_4.0.1      highr_0.8           
  hms_0.5.3            htmltools_0.4.0.9003 httr_1.4.1           jsonlite_1.6.1      
  kableExtra_1.1.0     knitr_1.28           labeling_0.3         lifecycle_0.2.0     
  magrittr_1.5         markdown_1.1         methods_4.0.1        mime_0.9            
  munsell_0.5.0        openssl_1.4.1        pillar_1.4.3         pkgconfig_2.0.3     
  processx_3.4.2       ps_1.3.3             R6_2.4.1             RColorBrewer_1.1.2  
  Rcpp_1.0.4.6         readr_1.3.1          rlang_0.4.6          rmarkdown_2.2       
  rstudioapi_0.11      rvest_0.3.5          scales_1.1.0         selectr_0.4.2       
  stats_4.0.1          stringi_1.4.6        stringr_1.4.0        sys_3.3             
  tibble_3.0.1         tinytex_0.23         tools_4.0.1          utf8_1.1.4          
  utils_4.0.1          vctrs_0.3.1          viridisLite_0.3.0    webshot_0.5.2       
  xfun_0.14            xml2_1.3.2           yaml_2.2.1         

Thank you for helping me troubleshoot- I'm very grateful.

As for the issue: that is very interesting. Like you, I was able to fix the issue with an R restart.

But I updated to R 4.0.2 (from 4.0.0) and rstudio to the daily build :grimacing:, and all seems well.

Again, thanks.

1 Like

I think this is something due to the way kableExtra tries to help the user by automatically adding the required latex dependencies.

In your case you need booktabs. It seems that kableExtra only manage to pass the information when first use in a clean session. (so first rendering at console - with knit button it is always a clean session)

If you pass the required Tex package yourself, it is working fine I think

title: "toy_example"
output: pdf_document
    - \usepackage{booktabs}

```{r cars}
kable(head(mtcars), format = "latex", booktabs = T)

See kableExtra vignette about this (page 3):

I answered with more details in the related GH issue:

Hope it helps understand