ABickford
ABickford

Reputation: 111

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?

Upvotes: 10

Views: 7805

Answers (5)

robertspierre
robertspierre

Reputation: 4431

I had this problem with Quarto and modelsummary.

Don't use the title option of modelsummary.

Use the tbl-cap: cell metadata to specify the caption of the table.

Upvotes: 1

Hao
Hao

Reputation: 7856

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.

Upvotes: 3

ethanweed
ethanweed

Reputation: 33

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.

Upvotes: 0

timmyjc
timmyjc

Reputation: 61

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.

Upvotes: 6

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.

Upvotes: 4

Related Questions