Learn R Programming

gt (version 0.7.0)

as_latex: Output a gt object as LaTeX

Description

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.

Usage

as_latex(data)

Arguments

data

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

Examples

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() %>%
  tab_header(
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) %>%
  as_latex()

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

13-3

Details

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:


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

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


  \usepackage{amsmath, booktabs, caption, longtable}

See Also

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