[Tex/LaTex] include latex table in rmarkdown

markdownthreeparttable

I try to include latex code (a table) into my Rmarkdown document. I need to use latex instead of other options because the table has to be in a certain format.

The problem I encounter is the following error:

! LaTeX Error: Environment threeparttable undefined.
Error: Failed to compile confounder_table.tex. See
confounder_table.log for more info. Execution halted

This is the table I include in the .Rmd document. I want to render it to pdf and/or docx and I use knitr for that (all in Rstudio). After working on that for 2 hours I hope that you can give me a hint how to solve this. It works for docx and html but not for the pdf format.

This is table code:

\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{Descriptive statistics included in the present study.}
\begin{tabular}{ll}
\toprule
cyl & \multicolumn{1}{c}{Some columnname}\\
\midrule
4.00 & 26.66 \± 4.51\\
6.00 & 19.74 \± 1.45\\
8.00 & 15.1 \± 2.56\\
\bottomrule
\addlinespace
\end{tabular}
\begin{tablenotes}[para]
\textit{Note.} There were no signnificant differences in the means between the groups.
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{table}

Best Answer

If you are using bookdown, there is explicit functionality to handle this. You just need to handle the LaTeX with the following:

```{=latex}

         [PUT LATEX HERE]      

```

This feature requires a Pandoc version higher than 2.0 (check markdown::pandoc_version()).

If you have the right pandoc version, you would do:

```{=latex}

\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{Descriptive statistics included in the present study.}
\begin{tabular}{ll}
\toprule
cyl & \multicolumn{1}{c}{Some columnname}\
\midrule
4.00 & 26.66 ± 4.51\
6.00 & 19.74 ± 1.45\
8.00 & 15.1 ± 2.56\
\bottomrule
\addlinespace
\end{tabular}
\begin{tablenotes}[para]
\textit{Note.} There were no signnificant differences in the means between the groups.
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{table}

```

Here's an example using the bookdown package for longer documents: https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html