1

I'm trying to knit a rmarkdown file to PDF file which contains kableExtra functions like:

  • kable_styling
  • column_spec
  • add_header_above

However I am always getting the following error:

! Extra alignment tab has been changed to \cr.
<template> \endtemplate 

l.184 \end{tabular}}


pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
Ejecución interrumpida

One easy example that I am executing is:

```
---
output:
 pdf_document:
   keep_tex: yes
classoption: table
header-includes:
  - \usepackage{array}
  - \usepackage{float}
  - \usepackage{xcolor}
---

```{r results='asis'}
options(kableExtra.latex.load_packages = FALSE)
require(kableExtra)
print(kable(head(cars),"latex")%>%kable_styling(latex_options = c("striped", 
"bordered"))
  %>%column_spec(column=1:2,width = "0.5in") %>%
      kable_styling(c("striped", "bordered"),latex_options = 
"scale_down")%>% add_header_above(c(" "=7,
"Absolute"=1,"Relative"=1,"Absolute"=1,"Relative"=1,
"Absolute"=1,"Relative"=1,"Absolute"=1,"Relative"=1,"Absolute"=1,
"Relative"=1))%>%
add_header_above(c(" "= 1,"Non-weighted"=1,"Weighted"=1,"Non- 
weighted"=1,"Weighted"=1,"Non-weighted"=1,
"Weighted"=1,"Weighted"=2,"Non-weighted"=2,"Weighted"=2,"Non- 
weighted"=2,"Weighted"=2))%>%
add_header_above(c("Theoretical Values"= 1,"First-Order Predicted 
Value"=2,"Second-Order Predicted Value"=2,
"Third-Order Predicted Value"=2,
"Non-linearity 1st Order"=2,"Non-linearity 2nd Order"=4,"Non-linearity 3rd 
Order"=4)))

```

Can anyone help me with this issue?

I would appreciate whatever clue!

1 Answers1

1

You are telling kableExtra not to load the LaTeX packages (why?), so you have to do this yourself. The features you are using require array, float and xcolor with the table option. One difficulty is that fancyvrb v3.0 already loads xcolor with other options. You can circumvent this by supplying table as a class option:

---
output: 
  pdf_document:
    keep_tex: yes
classoption: table    
header-includes:
  - \usepackage{array}
  - \usepackage{float}
---


```{r results='asis'}
options(kableExtra.latex.load_packages = FALSE)
require(kableExtra)
print(kable(head(cars),"latex") %>%column_spec(column=1:2,width = "0.5in"))
```

```{r results='asis'} 
print(kable(head(cars),"latex")%>%kable_styling(latex_options = c("striped", "bordered")))
```
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • @LauraSantularioVerdú Please [edit] your question to include such information. – Ralf Stubner Nov 23 '18 at 06:53
  • Thank you very much for your help. It is true that I 'd forgotten the YALM so I 've added now. The thing is that it is that now it works for function column_spec() but not for kable_styling(). I write you the code I have problems with: --- output: pdf_document header-includes: - \usepackage{array} --- ```{r results='asis'} options(kableExtra.latex.load_packages = FALSE) require(kableExtra) print(kable(cars,"latex")%>%kable_styling(latex_options = c("striped", "bordered"))) ``` – Laura Santulario Verdú Nov 23 '18 at 08:56
  • What error(s) do you get? Please add code to your question, since it is unreadable in a comment. Also please build a [mcve] instead of including code and the full YAML header separately. – Ralf Stubner Nov 23 '18 at 09:27
  • @LauraSantularioVerdú Thanks, please see my updated answer. – Ralf Stubner Nov 23 '18 at 10:06
  • thank you very much for your help, I appreciate a lot. Doing your proposals has helped me a lot. Even this, I found some issues again. You can see my updated question. – Laura Santulario Verdú Nov 23 '18 at 10:32
  • @LauraSantularioVerdú Which other issues? The "Unknown float option 'H'" is already handled in my answer. – Ralf Stubner Nov 23 '18 at 10:35
  • Sorry @Ralf Stubner, I am talking about another problem. It is now updated in the question – Laura Santulario Verdú Nov 23 '18 at 10:50
  • @LauraSantularioVerdú The new code does not make sense. You seem to add way more headers than your table has columns. Also, please avoid [chameleon questions](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). By now my answer looks completely wrong, even though it answers your previous questions. – Ralf Stubner Nov 23 '18 at 11:02
  • Thank very much for your help @Ralf Stubner. I have already solve my problem thanks to your help. And I have to apologized for changing the question so many times but I am completely new at Stack Overflow and I do not know how to write code in comments. – Laura Santulario Verdú Nov 23 '18 at 12:04
  • @LauraSantularioVerdú You are welcome. Consider accepting the answer if it hrlped you, c.f. https://stackoverflow.com/help/someone-answers. From my point of view, updated code should always go into the question, since comments do not offer the necessary formating capabilities. However, you should keep the original question intact such that existing answers do not look "out of place". – Ralf Stubner Nov 23 '18 at 12:11
  • You are right @Ralf Stubner, I appreciate your comment and I will improve this for my next time! – Laura Santulario Verdú Nov 23 '18 at 12:53