I am running in an odd result. I have inserted a number of tables in my document in Rmarkdown. This is the YAML:
---
output:
pdf_document:
toc: true
toc_depth: 2
number_sections: true
header-includes:
\usepackage{float}
\floatplacement{figure}{H}
\floatplacement{table}{H}
---
And the first chunk:
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.pos = "H",
fig.width=8,
fig.height=7,
tab.pos = "H",
collapse = TRUE,
message = FALSE,
warning = FALSE,
comment = "#>"
)
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
options(knitr.kable.NA = "-")
The tables are inserted in this manner:
kable(tableX, caption = "Caption", booktabs = TRUE) %>%
row_spec(0, bold=TRUE) %>%
kable_styling(bootstrap_options = "condensed") %>%
column_spec(1, italic = TRUE)
Now, most tables are inserted correctly, but there is just one which is printed with the latex code, \begin{table}, then \caption{}, then the table, and at the end \end{table}. How is it possible that one table is being printed in this manner with the code being the same?
Thank you
As the author of rmarkdown Yihui Xie noted here, you can disable the character-escaping argument in kable()
to include LaTeX's spacial characters in the table caption, or column/row names, or both. Then you need to manually escape such characters by adding \\
before each special character.
```{r tab1, echo=F}
tableX=data.frame(col1 = "Empty data frame")
kable(
tableX,
caption = "Caption 20\\%",
booktabs = TRUE,
escape = FALSE
) |>
row_spec(0, bold=TRUE) |>
kable_styling(bootstrap_options = "condensed") %>%
column_spec(1, italic = TRUE)
```