Skip to contents

Get the LaTeX content from a gt_tbl object as a knit_asis object. This object contains the LaTeX code and attributes that serve as LaTeX dependencies (i.e., the LaTeX packages required for the table). Using as.character() on the created object will result in a single-element vector containing the LaTeX code.





A table object that is created using the gt() function.


LaTeX packages required to generate tables are: amsmath, booktabs, caption, longtable.

In the event packages are not automatically added during the render phase of the document, please create and include a style file to load them.

Inside the document's YAML metadata, please include:

  pdf_document: # Change to appropriate LaTeX template
      in_header: 'gt_packages.sty'

The gt_packages.sty file would then contain the listed dependencies above:

  \usepackage{amsmath, booktabs, caption, longtable}


Use gtcars to create a gt table. Add a header and then export as an object with LaTeX code.

tab_latex <-
  gtcars %>%
  dplyr::select(mfr, model, msrp) %>%
  dplyr::slice(1:5) %>%
  gt() %>%
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) %>%

What's returned is a knit_asis object, which makes it easy to include in R Markdown documents that are knit to PDF. We can use as.character() to get just the LaTeX code as a single-element vector.

Function ID


See also

Other table export functions: as_raw_html(), as_rtf(), as_word(), extract_cells(), extract_summary(), gtsave()