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