RStudio failed to compile rmd file to PDF for knitr::kable

I am wondering if anyone has this experience or advise me any solutions. RStudio just failed to knit rmd file to PDF and it took me several hours to find out that the knitr::kable function is the reason that RStudio failed to knit to PDF. If I replace knitr::kable with print(), then RStudio can knit to PDF.

Here are error code:

! Use of \x doesn't match its definition.
\LT@array [#1]#2->\UseTaggingSocket 
                                    {tbl/init}\@kernel@refstepcounter {table...
l.6889 }{}
Error: LaTeX failed to compile

Here are rmarkdown code:

knitr::kable(cars)

Windows TinyTex
RStudio: 2023.12.0 Build 369
R version 4.3.3 (2024-02-29 ucrt)

looks like it should work. It is fine if I use it in a Quarto document.

Where is that error message coming from?

Overall, we probably probably need to more of your code to see what is happening.

See
FAQ Asking Questions

Just copy your code and paste it here between
```

```

Thank jrkrideau for help.

The error code comes from "Render". It used to work, but suddenly it didn't work yesterday, and it still doesn't work today.

To produce a reproducible example, I manually created a new R Markdown file in RStudio, "New File" --> "R Markdown", and then replace summary(cars) with knitr::kable(cars).

Please let me know if you need any more information from my system.

Here are are error codes

processing file: test_kable.Rmd
                                                                                                     
"E:/Software/R/RStudio/resources/app/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS test_kable.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output test_kable.tex --lua-filter "E:\Software\R\R-4.3.3\library\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "E:\Software\R\R-4.3.3\library\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 
output file: test_kable.knit.md

! Use of \x doesn't match its definition.
\LT@array [#1]#2->\UseTaggingSocket 
                                    {tbl/init}\@kernel@refstepcounter {table...
l.6889 }{}

Error: LaTeX failed to compile test_kable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test_kable.log for more info.
Execution halted

Thanks for replying.

I think we actually need to the main body of your code.

I am not sure what this means

! Use of \x doesn't match its definition.

but I looks like it may be something that is preceding the kable() command but which is then affecting kable(). We really need no see "ALL" your code.

Something like my toy example here. Note I am using Quarto not RMarkdown.

---
title: "Wu_gong Cars"
date: today
date-format: iso
format: pdf
---
```{r packages}
#| label: setup
#| include: false
library(data.table)
library(kableExtra)

It was a dark and stormy night

#| echo: false
kable(cars[1:10,])

Thanks jrkrideau. Below is the full code of rmd file.

---
title: "Test Kable"
output: pdf_document
date: "2024-06-25"
---
```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)

Blast it, I think the problem may be that you are missing a bit of syntax in the code chunks but the editor here makes it hard to be sure.

Your raw code chunks should look like this. The text in between is not important.

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

```{r table1}
knitr::kable(cars)
```

```{R pressure}
plot(pressure)
```

Thanks jrkrideau for help. The other codes don't matter, I just got the template from RStudio.

This may not be very helpful, but I just did what you said: used the R Markdown template and changed summary to knitr::kable and it worked fine. That suggests there might be something wrong with your installation rather than your code.

Thanks startz. It could be something within the system. I just run it again today and everything is fine, both for my template and my real work.

Thanks jrkrideau for help. So far it works right now, but I don't know why.

Try updating TinyTeX with tinytex::tlmgr_update() and reinstalling knitr with install.packages("knitr"). Ensure your LaTeX packages are up-to-date. If the issue persists, consider using kableExtra::kbl for more compatibility with LaTeX in RMarkdown.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.