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.
as_latex(data)
data | A table object that is created using the |
---|
13-3
Other Export Functions:
as_raw_html()
,
as_rtf()
,
extract_summary()
,
gtsave()
# 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() # `tab_latex` 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 tab_latex %>% as.character() %>% cat()#> \makeatletter #> \def\getLTwidth#1{% #> \begingroup #> #1=0pt% #> \def\LT@entry##1##2{\advance#1 by ##2}% #> \LT@save@row #> \edef\X{\noexpand#1=\the#1\relax}% #> \expandafter\endgroup\X} #> \makeatother #> \newlength\LTwidth #> #> #> \normalsize #> \setlength{\tabcolsep}{3pt} #> \captionsetup[table]{labelformat=empty, justification=raggedright, width =\textwidth} #> \begin{longtable}{p{2.18cm}p{2.18cm}p{2.18cm}} #> #> #> \\ #> \getLTwidth\LTwidth #> \begin{minipage}{\LTwidth} #> \centering #> \smallskip #> \large #> Data listing from \textbf{gtcars} \\ #> \footnotesize #> \texttt{gtcars} is an R dataset \\ #> \bigskip #> \end{minipage} \\ #> #> \toprule #> mfr & model & msrp\\ #> #> \hline #> \addlinespace #> \endfirsthead #> #> #> \bottomrule #> #> \endlastfoot #> #> #> #> Ford & GT & 447000 \\ #> Ferrari & 458 Speciale & 291744 \\ #> Ferrari & 458 Spider & 263553 \\ #> Ferrari & 458 Italia & 233509 \\ #> Ferrari & 488 GTB & 245400 \\ #> #> \end{longtable} #>