Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems rendering table using r Markdown, kable and kableExtra

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?

like image 564
ABickford Avatar asked Dec 07 '17 22:12

ABickford


3 Answers

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.

like image 54
timmyjc Avatar answered Oct 26 '22 22:10

timmyjc


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.

like image 39
Seeker for answers meself Avatar answered Oct 26 '22 23:10

Seeker for answers meself


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.

like image 1
Hao Avatar answered Oct 26 '22 23:10

Hao