5

Let's consider very simple kable table for reproducible example:

df <- data.frame("X_1" = c(1, 2), "X_2" =c(3,4))
df <- kable(df, format = 'latex')
df

\begin{tabular}{r|r}
\hline
X\_1 & X\_2\\
\hline
1 & 3\\
\hline
2 & 4\\
\hline
\end{tabular}

Is there any possibility to have this file saved as .tex or .markdown ? I looked for function save_kable but it seems that it only supports .png, .pdf or .jpeg formats.

John
  • 1,849
  • 2
  • 13
  • 23
  • save_kable(df,'df.tex') does the trick for me, although there is an empty line in the beginning and end of the file. Usually if .txt is supported, so is .tex. This might not always be mentioned in the documentation. – Samuel Saari Aug 09 '21 at 13:24

2 Answers2

6

knitr::kable() returns a character vector, which you can definitely write to a file, e.g.,

df <- data.frame("X_1" = c(1, 2), "X_2" =c(3,4))
df <- knitr::kable(df, format = 'latex')
writeLines(df, 'df.tex')
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
4

i am using save_kable and specifying tex with no problems e.g. %>% save_kable(paste(resultspath,"DescTableStroops.tex"),float = FALSE)

deschen
  • 10,012
  • 3
  • 27
  • 50
BlueJ
  • 41
  • 2
  • 1
    `save_kable()` is a function from the {kableExtra} package, if anyone is wondering. Make sure to set `format = "latex"` in your kable for this to work. – fry Jun 13 '22 at 10:56