0

I create a table in LyX 2.3.6 / TeX Live 2020 as per the following minimal example:

<<results = "asis",echo = F, warning = FALSE, tidy=FALSE>>=
library(dplyr)
library(kableExtra)

df <- data.frame(Female = c(1.52, -0.03), Male   = c(1.34, -0.02) )
rownames(df) <- c("Apples", "Bananas")

kable(df, format = "latex", booktabs = T, linesep = "", row.names = TRUE,
      align = c("l", "r", "r"), escape = FALSE) %>%
      kable_styling("striped", full_width = F, font_size = 10 ) 

@

This prints perfectly (it is centered on the page), but gives me no control over the column width. If I add a single line with a column_spec statement, I get an error that I don't understand. If the chunk is extend to read

<<results = "asis",echo = F, warning = FALSE, tidy=FALSE>>=
library(dplyr)
library(kableExtra)

df <- data.frame(Female = c(1.52, -0.03), Male   = c(1.34, -0.02) )
rownames(df) <- c("Apples", "Bananas")

kable(df, format = "latex", booktabs = T, linesep = "", row.names = TRUE,
      align = c("l", "r", "r"), escape = FALSE) %>%
      kable_styling("striped", full_width = F, font_size = 10 ) %>%
      column_spec(1, width = "6 em") 

@

The table is now left justified, not centered on the page, all columns are centered instead of left and right justified, the letter "c" is printed above the table, and the error log shows the following message:

! Undefined control sequence.
<argument> >{\raggedright \arraybackslash 
                                          }p{6em}lr
l.149 ...}{>{\raggedright\arraybackslash}p{6em}lr}
                                                  
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! LaTeX Error: Illegal character in array arg.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.149 ...}{>{\raggedright\arraybackslash}p{6em}lr}
                                                  
You're in trouble here.  Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

! LaTeX Error:  Illegal character in array arg.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.149 ...}{>{\raggedright\arraybackslash}p{6em}lr}
                                                  
You're in trouble here.  Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

[2] (./Kable_Explorations.aux)
Package rerunfilecheck Info: File `Kable_Explorations.out' has not changed.
(rerunfilecheck)             Checksum: D41D8CD98F00B204E9800998ECF8427E;0.
 )  

I'd be very appreciative for any help and guidance on the root cause of the problem and how it can be fixed.

Sincerely

Thomas Philips

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Thomas Philips
  • 935
  • 2
  • 11
  • 22

1 Answers1

3

The creator of the package provided the solution: he made some changes to column_spec to add conditional formatting support and to allow the insertion of images into columns (see https://haozhu233.github.io/kableExtra/awesome_table_in_html.html#Insert_Images_into_Columns), and column_spec() requires the latex array package to be loaded in the preamble. Updating my Latex preamble as follows solves the problem completely:

\usepackage{array}
\usepackage{booktabs}
\usepackage{threeparttablex}

In fact, he suggested that a user might want to load all the latex packages mentioned on this page (https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf#page=4) but I found the 3 mentioned above to be more than adequate for my relatively simple purposes.

Thomas Philips
  • 935
  • 2
  • 11
  • 22