avs
avs

Reputation: 678

Is it possible to knit just one code chunk and output the LaTeX code to the console?

I am writing my thesis in LaTeX and doing the data analysis in R. I already have my tex files setup with the formatting I want and a R markdown file for my code. I only use R markdown, because of the improved sectioning and not to generate any sort of report from it. My normal workflow for making tables was to generate a regression table in R using texreg or stargazer and copy the LaTeX code to my tex file. But now I need to make a custom regression table and I have found the kableExtra package to have a simple syntax for making it look good using booktabs.

Is it possible to generate LaTeX code from only a single code chuck with the kableExtra call without knitting my whole document and going into the generated .tex file to copy and paste the part for the table into my thesis?

I see that I am probably misunderstanding the idea behing knitr, but I am hesitant to change my workflow.

Upvotes: 1

Views: 224

Answers (1)

s_baldur
s_baldur

Reputation: 33498

Here is a concrete example:

sink("texy.txt")
iris[1:2, 1:5] %>% 
  kable(format = "latex") %>% 
  kable_styling(font_size = 14) %>%
  column_spec(1, width = "6cm") %>% 
  capture.output() %>%
  cat(sep = "\n")
sink()

Now I have the following in texy.txt:

\begin{table}[H]
\centering\begingroup\fontsize{14}{16}\selectfont

\begin{tabular}{>{\raggedleft\arraybackslash}p{6cm}|r|r|r|l}
\hline
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\hline
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
\hline
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
\hline
\end{tabular}\endgroup{}
\end{table}

Upvotes: 3

Related Questions