Problems rendering table using r Markdown, kable and kableExtra
Asked Answered
M

4

10

I am attempting to output a latex table using r markdown, kable and kableExtra. I get an error in the table rendering code that is not part of the latex code produced by R.

The code:

outTab <- m.OutTab %>%
    kable(format='latex',  booktabs=T ,
          #caption = "Population Trend",
          digits=1, 
          row.names=FALSE, 
          align='lccccc', 
          col.names = names_spaced,
          escape = FALSE)

where "m.OutTab" is an matrix that contains the table to be rendered,

The error:

Error producing PDF.
! Misplaced \noalign.
\addlinespace ->\noalign 
                         {\ifnum 0=`}\fi \@ifnextchar [{\@addspace }{\@addsp...
l.116    \addlinespace

Error: pandoc document conversion failed with error 43

These codes ("\noalign ...") is not part of "outTab".

Any idea how to work around this error?

Masque answered 7/12, 2017 at 22:27 Comment(3)
Hi, you are not giving enough information in your code, I tried but cannot reproduce your problem. I have to guess what is names_spaced or the format of your table at the beginning. Possibly add header-includes: - \usepackage{booktabs} but hard to know if that' s your problemFooted
I have this same problem. I have a simple pdf markdown with a single chart. When I knit it without kableExtra, using only knitr::kable(chart), no problem. When I knit it having loaded kableExtra, even if I don't use any functions from that package to alter my chart, I get the following error: Error producing PDF. ! Misplaced \noalign. \hline ->\noalign {\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let... l.109 \hline Error: pandoc document conversion failed with error 43Ubiquitous
I have discovered that I only get the error when I have "escape = T" in the kable call. escape = F does not give the error, although the pdf renders the chart with a bunch of html code in the table cells instead of the actual values.Ubiquitous
U
6

If you are using bookdown, this could be caused by using non-alphanumeric characters in your code chunk label. I had a similar problem which was solved by removing an underscore.

Unrig answered 25/3, 2019 at 13:12 Comment(2)
I have been fighting with strange errors like this for hours! This was the solution: Knitr doesn't handle underscore in captions correctly, so don't use them.Fefeal
Also caused by using non-alphanumeric characters in the caption if you are using caption = "..." instead of the code chunk to caption your table :)Hex
J
4

I have encountered this problem. I seem to be able to fix it by specifying format="pandoc" or format="markdown". If seems to be some issue with how latex output from kable is handled.

Joinery answered 27/12, 2018 at 10:32 Comment(0)
A
3

It seems like this question is getting a lot of traffic. If you see an error like that, that means that there are something wrong with the raw latex you wrote. Check special symbols like < \ / [] and make sure they are properly escaped by yourself.

Due to running mechanism, a lot of places with kableExtra requires double escape, which means that you need to type \\\\ to get a \. You should be able to get it work after a few attempts.

Aleph answered 26/4, 2019 at 21:35 Comment(2)
The error cannot mean that 'there [is] something wrong with the raw LaTeX you wrote' because the question relates to LaTeX generated by knitr and kableExtra from an RMarkdown source.Chase
I had a '%' symbol in a column with cell_spec() which screwed the whole thing up and took me hours to find this solution. Apparently '%' needs to be escaped ('escape = TRUE') in order for knittr to work, but cell_spec() needs 'escape = FALSE' in order to work. So I had to rewrite '%' to '\\\\%' and it finally worked. The problem is that this isn't a bug in kableExtra. kableExtra doesn't know if you have other columns needing to be escaped. I think the real problem here is the completely useless error messages.Hemelytron
A
0

I had a similar issue, although my error message was slightly different:

! Misplaced \noalign.
\addlinespace ->\noalign 
                     {\ifnum 0=`}\fi \@ifnextchar [{\@addspace }{\@addsp...
l.376 \end{tabu}
Error: Failed to compile

In my case, adding

format = "latex"

and

full_width = FALSE

solved it.

Alfieri answered 24/4, 2019 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.