0

I am trying to print a fairly simple table using Rmarkdown LaTex through kable and the kableExtra functions that allow me to bold the rows and columns. Below is an example of my issue:

df <- data_frame(grp=c("<1","1-5","5+"),n=c(3,4,5))

#This works
df %>%
  kable(format="latex",booktabs=T)

#This works
df %>%
  kable(format="latex",booktabs=T) %>%
  kable_styling(latex_options = c("hold_position"))

#This works
df %>%
  kable(format="latex",booktabs=T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1,bold=T)

#This works
df %>%
  kable(format="latex",booktabs=T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  row_spec(1 ,bold=T)

#This does not work
df %>%
  kable(format="latex",booktabs=T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  row_spec(0 ,bold=T) %>%
  column_spec(1,bold=T) 

Now I was able to figure out it is because my group starts with the less than symbol which apparently is causing chaos when attempting to use both column_spec and row_spec. The error message is:

! Misplaced \noalign.
\toprule ->\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\abovetopsep \global \@b...
l.129 \toprule

In my actual data frame (which itself isn't much more complicated than this one), my main error is actually Error: \caption outside float (but I can't seem to replicate it with my MVE, admittedly I left out a few of the other options for this example, though).

I've tried just renaming the group along the lines of \<1 up through \\\\<1 and that either 'works' but prints a slash, or it gives me an error about an unrecognized escape. Now I know that the easiest option is to simply rename the group in some way but I'm interested to see if there is some other escape trick or work around so that I can keep my group name.

Thanks!

c.custer
  • 407
  • 5
  • 13
  • You may be able to work around this if you explicitly use the booktabs package (although for me the problem crops up only when I'm using `render()` directly and the error was slightly different). You can add this to the YAML header under `header-includes: - \usepackage{booktabs}`. The `header-includes:` is on the first line with the packages code on a separate line. I found I also needed `- \usepackage{array} ` for your example. – aosmith Jun 12 '18 at 20:19
  • As a follow-up, if you need to use `header-includes` it sounds like the best approach is to put those in an external file and then use `includes: in_header`. See [the comment here](https://github.com/rstudio/rmarkdown/issues/1368#issuecomment-396402560) – aosmith Jun 13 '18 at 18:12
  • For me it works. I'm using `kableExtra 0.9.0`. – Stéphane Laurent Aug 28 '18 at 09:56

0 Answers0