Search code examples
rlatexknitrkable

Let knitr/kable display latex code for further editing (.rnw file)


I'm using knitr to compile a .rnw file in a pdf. In the file I use a kbl() function to plot a table in RStudio. I'd like to see the LaTeX code produced by kable and kableExtra to realize further editing to the table, but I don't know how to do that. I saw a question about this topic but it was a Rmarkdown document and not a rnw (Let knitr/kable display latex code for further editing).

My question: Is there any option to let me see the full LaTeX code produced by kable/kableExtra in order to take it and copy it into my pure LaTeX document?


Solution

  • You should run your code with knitr in your console. Here is an example:

    Example data:

    df <- data.frame(x = 1:10, y = 11:20)
    
        x  y
    1   1 11
    2   2 12
    3   3 13
    4   4 14
    5   5 15
    6   6 16
    7   7 17
    8   8 18
    9   9 19
    10 10 20
    

    Run this code in your console:

    knitr::kable(df, format="latex")
    

    Output in console:

    \begin{tabular}{r|r}
    \hline
    x & y\\
    \hline
    1 & 11\\
    \hline
    2 & 12\\
    \hline
    3 & 13\\
    \hline
    4 & 14\\
    \hline
    5 & 15\\
    \hline
    6 & 16\\
    \hline
    7 & 17\\
    \hline
    8 & 18\\
    \hline
    9 & 19\\
    \hline
    10 & 20\\
    \hline
    \end{tabular}
    

    Which gives you the latex code so you can copy this and paste in your latex document.