简体   繁体   中英

kableExtra latex code not working for bold face

I am trying to create some tables with kableExtra to use them later on on an external latex document, so I would like to export the table as a .tex document. This works fine except when I try to use boldface in some of the rows.

This is a MWE of the code I'm using:

require(kableExtra)
require(dplyr)

bold_letters = c(1,0,1)
df = cbind.data.frame("Noms" = c("A", "B", "C"), "var1" = c(1,2,3))

df %>% 
  mutate(Noms = cell_spec(Noms, bold = ifelse(bold_letters==0,FALSE,TRUE))) %>%
  kable(format = "latex", escape = F, row.names = F, align = "c")

The issue is with the boldface. If I put it in a markdown and knit it it works fine, but if I try to export the tex code I get something like:

\begin{tabular}{c|c}
\hline
Noms & var1\\
\hline
<span style=" font-weight: bold;    " >A</span> & 1\\
\hline
<span style="     " >B</span> & 2\\
\hline
<span style=" font-weight: bold;    " >C</span> & 3\\
\hline
\end{tabular}

Which, as far as I know, is not latex code.

PS: I could create the table and export it as an image, but the quality is quite poor and furthermore it includes the white bands at either side of the table as part of the image, thus making it impractical.

Maybe you'll just do it without dplyr . You also need format="latex" within cell_spec , otherwise it formats HTML.

library(kableExtra)
bold_letters <- c(1,0,1)
df <- cbind.data.frame(Noms=c("A", "B", "C"), var1=c(1, 2, 3))
df$Noms <- cell_spec(df$Noms, format="latex", 
                     bold=ifelse(bold_letters == 0, FALSE, TRUE))

kable(df, format="latex", escape=FALSE, row.names=FALSE, align="c")
# \begin{tabular}{c|c}
# \hline
# Noms & var1\\
# \hline
# \textbf{A} & 1\\
# \hline
# B & 2\\
# \hline
# \textbf{C} & 3\\
# \hline
# \end{tabular}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM