5

I am attempting to output a latex table using r markdown, kable and kableExtra. When i use the option row.names=FALSE instead of row.names=TRUE the latex code generates \vphantom code which produce an error to create the pdf . It seems the problem is linked to the row_spec option.

Here is Rmarkdown code (.Rmd File):

---
title: "Test"
output:
pdf_document: 
fig_caption: true
keep_tex: true
---

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


{r}
library(knitr)
library(kableExtra)

temp <- mtcars[1:5,1:5]

kable(temp, format = "latex", booktabs = F,row.names=F)  %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, background = "red")

The error is:

! Forbidden control sequence found while scanning use of \check@nocorr@. \par l.105 ...color{red} \textbf{21.0 &\vphantom{1} 6} & \textbf{160} & \textbf{...

What is happening?

1
  • It works on my computer. Have you tried with bold=F to see what happens? Commented Feb 27, 2018 at 23:54

1 Answer 1

4

This is caused by the duplicated rows in the dataframe, as both rows 1 and 2 are the same.

Reviewing the code for row_spec_latex, when kableExtra is used against a kable table, it checks for duplicated rows. If it finds one, it inserts the vphantom argument within the fix_duplicated_rows_latex internal function. This vphantom insertion is then messing up the formatting of the textbf function.

This seems like a slight bug, so it may be worth reporting it as an issue in kableExtra: https://github.com/haozhu233/kableExtra . I am sure that the vphantom is added for a good reason though, but doubt this was an intended consequence.

Supporting code:

---
output: pdf_document
---

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

library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
```

```{r}
# Keeping the row names (means all rows are unique)
kable(temp, format = "latex", booktabs = F)  %>%
  kable_styling(position = "center") %>%
  row_spec(1, bold = T, color = "red")
```

```{r}
# Highlighting second row (which doesn't have the vphantom statement)
kable(temp, format = "latex", booktabs = F, row.names=F)  %>%
  kable_styling(position = "center") %>%
  row_spec(2, bold = T, color = "red")
```

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I reported the error to github in kableExtra. Thanks a lot for your help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.