great_tables
  • Get Started
  • Examples
  • Reference
  • Blog

On this page

  • Parameters
  • Returns
  • Limitations
  • Examples

GT.as_latex

GT.as_latex(self, use_longtable=False, tbl_pos=None)

Output a GT object as LaTeX

The as_latex() method outputs a GT object as a LaTeX fragment. This method is useful for when you need to include a table as part of a LaTeX document. The LaTeX fragment contains the table as a string.

Warning

as_latex() is still experimental.

Parameters

use_longtable : bool = False

An option to use the longtable environment in LaTeX output. This is useful for tables that span multiple pages and don’t require precise positioning.

tbl_pos : str | None = None

The position of the table in the LaTeX output when use_longtable=False. Valid values for positioning include "!t" (top of page), "!b" (bottom of the page), "!h" (here), "!p" (on a separate page), and "!H" (exactly here). If a value is not provided then the table will be placed at the top of the page; if in the Quarto render then the table positioning option will be ignored in favor of any setting within the Quarto rendering environment.

Returns

: str

A LaTeX fragment that contains the table.

Limitations

The as_latex() method is still experimental and has some limitations. The following functionality that is supported in HTML output tables is not currently supported in LaTeX output tables:

  • the rendering of the stub and row group labels (via the =rowname_col and =groupname_col args in the GT() class)
  • the use of the md() helper function to signal conversion of Markdown text
  • units notation within the cols_labels() and tab_spanner() methods
  • the fmt_markdown(), fmt_units(), fmt_image(), and fmt_nanoplot() methods
  • the sub_missing() and sub_zero() methods
  • most options in the tab_options() method, particularly those that are specific to styling text, borders, or adding fill colors to cells

As development continues, we will work to expand the capabilities of the as_latex() method to reduce these limitations and more clearly document what is and is not supported.

Examples

Let’s use a subset of the gtcars dataset to create a new table.

from great_tables import GT
from great_tables.data import gtcars
import polars as pl

gtcars_mini = (
    pl.from_pandas(gtcars)
    .select(["mfr", "model", "msrp"])
    .head(5)
)

gt_tbl = (
    GT(gtcars_mini)
    .tab_header(
        title="Data Listing from the gtcars Dataset",
        subtitle="Only five rows from the dataset are shown here."
    )
    .fmt_currency(columns="msrp")
)

gt_tbl
Data Listing from the gtcars Dataset
Only five rows from the dataset are shown here.
mfr model msrp
Ford GT $447,000.00
Ferrari 458 Speciale $291,744.00
Ferrari 458 Spider $263,553.00
Ferrari 458 Italia $233,509.00
Ferrari 488 GTB $245,400.00

Now we can return the table as string of LaTeX code using the as_latex() method.

gt_tbl.as_latex()
'\\begin{table}\n\\caption*{\n{\\large Data Listing from the gtcars Dataset} \\\\\n{\\small Only five rows from the dataset are shown here.}\n} \n\n\\fontsize{12.0pt}{14.4pt}\\selectfont\n\n\\begin{tabular*}{\\linewidth}{@{\\extracolsep{\\fill}}llr}\n\\toprule\nmfr & model & msrp \\\\ \n\\midrule\\addlinespace[2.5pt]\nFord & GT & \\$447,000.00 \\\\\nFerrari & 458 Speciale & \\$291,744.00 \\\\\nFerrari & 458 Spider & \\$263,553.00 \\\\\nFerrari & 458 Italia & \\$233,509.00 \\\\\nFerrari & 488 GTB & \\$245,400.00 \\\\\n\\bottomrule\n\\end{tabular*}\n\n\\end{table}\n'

The LaTeX string contains the code just for the table (it’s not a complete LaTeX document). This output can be useful for embedding a GT table in an existing LaTeX document.