How to Include a LaTeX Table in R Markdown
At a glance
- 1. Choose the output
Raw LaTeX tables work for PDF output; they are not portable to HTML or Word.
- 2. Prepare the table
Create the table in LaTeX directly or generate it from R with knitr::kable().
- 3. Emit LaTeX as-is
For LaTeX printed by a code chunk, use results='asis' so knitr does not display it as code.
- 4. Render and check
Compile the R Markdown file to PDF and check that required LaTeX packages are available.
R Markdown can include LaTeX table markup when you render the document to PDF. You can either write the table directly in the document or generate LaTeX from an R chunk. The key distinction is that LaTeX markup must reach the PDF renderer as markup, not be printed as ordinary code output.
Generate a LaTeX table from R
For tables built from R data, knitr::kable() can format a data frame as LaTeX. Set the chunk option results='asis' when you use cat() to emit its output. This tells knitr to pass the text through as markup.
Save the following as table.Rmd:
---
title: "A table in R Markdown"
output: pdf_document
header-includes:
- \usepackage{booktabs}
---
```{r measurements, results='asis'}
measurements <- data.frame(
Item = c("A", "B"),
Value = c(12, 18)
)
cat(knitr::kable(
measurements,
format = "latex",
booktabs = TRUE,
caption = "Example measurements"
))
```
The YAML header selects PDF output and loads the booktabs package, which provides the \toprule, \midrule, and \bottomrule commands used for table rules. The chunk creates a data frame, converts it to a LaTeX table, and emits that table as-is. Render it with RStudio’s Knit button or by running rmarkdown::render("table.Rmd") in R. A LaTeX distribution must be installed for PDF rendering.
If you do not need to call cat(), you can often write knitr::kable(measurements) as the final expression in a chunk. Knitr recognizes the returned table object and chooses an output format based on the document. For a PDF-only document, specifying format = "latex" makes the desired format explicit.
Write a table directly in the document
For a fixed table, put the LaTeX environment in the R Markdown body outside an R code chunk. For example:
\begin{table}[ht]
\centering
\begin{tabular}{lr}
\toprule
Item & Value \\
\midrule
A & 12 \\
B & 18 \\
\bottomrule
\end{tabular}
\caption{Example measurements}
\end{table}
Pandoc passes raw LaTeX through when the target is LaTeX/PDF. In this table, {lr} specifies two columns, left-aligned and right-aligned; & separates cells, and \\ ends a row. The booktabs commands require the same package shown in the YAML header. You do not need to put a \documentclass or \begin{document} in an R Markdown file: Pandoc generates the LaTeX document around its contents.
If you want to test the table by itself, here is a complete minimal LaTeX file. It needs the booktabs package:
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{lr}
\toprule
Item & Value \\
\midrule
A & 12 \\
B & 18 \\
\bottomrule
\end{tabular}
\caption{Example measurements}
\end{table}
\end{document}
Rendering to more than one format
Raw LaTeX is intended for LaTeX output. When you render the same R Markdown document to HTML or Word, LaTeX-specific markup may be omitted or fail to produce a table. If you need multiple output formats, use a format-aware table instead. For example, knitr::kable(measurements, caption = "Example measurements") normally selects a format appropriate to the output document. Packages such as kableExtra can add styling, but check their documentation for support in each format.
A raw-TeX table inside the document and LaTeX generated in a code chunk have the same format limitation. The chunk option results='asis' changes how knitr handles output; it does not convert LaTeX into HTML or Word markup.
Common mistakes
- Leaving out
results='asis'when usingcat(): The LaTeX can appear as literal text instead of becoming a table. - Putting table markup in an ordinary code fence: A Markdown code block displays its contents; it does not compile them as LaTeX.
- Forgetting a required package: Commands such as
\topruleneedbooktabs. Add the package to the LaTeX preamble withheader-includes. - Using raw LaTeX for an HTML report: Raw LaTeX is not a reliable way to create an HTML table. Use a format-aware table function for multi-format documents.
- Leaving special characters unescaped: In LaTeX table cells, characters such as
%,_,&, and#have special meanings. Escape them when typing cell contents manually.knitr::kable()escapes content by default; turn escaping off only when you deliberately supply valid LaTeX.
If the PDF build fails, first check that a LaTeX distribution is installed, then look for missing package messages in the render log. If the PDF builds but shows the commands instead of a table, check whether the markup is in a code fence or whether chunk output needs results='asis'.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.