9

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?

Adrian Martin
  • 780
  • 7
  • 21
ABickford
  • 109
  • 1
  • 2
  • 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 problem – Cedric Dec 09 '17 at 17:29
  • 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 43 – Adrian Martin Jun 08 '18 at 12:46
  • 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. – Adrian Martin Jun 08 '18 at 14:31

4 Answers4

5

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.

timmyjc
  • 51
  • 1
  • 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. – jeppeb Sep 15 '20 at 09:12
  • 1
    Also caused by using non-alphanumeric characters in the caption if you are using `caption = "..."` instead of the code chunk to caption your table :) – QAsena Sep 28 '20 at 20:25
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.

1

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.

Hao
  • 7,476
  • 1
  • 38
  • 59
  • 1
    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. – Westcroft_to_Apse Apr 26 '21 at 16:11
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.

ethanweed
  • 33
  • 7