Gt: Exporting as LaTeX

Created on 17 Dec 2018  Â·  12Comments  Â·  Source: rstudio/gt

First, thank you for this nice package. I could not find a way to export a gt table to a .tex file. Is it possible? I was not able to use the as_latex() to have a valid LaTeX output.

library(tidyverse)
library(gt)

mtcars %>% 
  gt() %>% 
  as_latex() %>% 
  as.character()
#> [1] "\\captionsetup[table]{labelformat=empty,skip=1pt}\n\\begin{longtable}{rrrrrrrrrrr}\n\\toprule\nmpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\\\ \n\\midrule\n21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\ \n21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\ \n22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\ \n21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\ \n18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\ \n18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\\\ \n14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4 \\\\ \n24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2 \\\\ \n22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2 \\\\ \n19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4 \\\\ \n17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4 \\\\ \n16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3 \\\\ \n17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3 \\\\ \n15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3 \\\\ \n10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4 \\\\ \n10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4 \\\\ \n14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4 \\\\ \n32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1 \\\\ \n30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2 \\\\ \n33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1 \\\\ \n21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1 \\\\ \n15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2 \\\\ \n15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2 \\\\ \n13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4 \\\\ \n19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2 \\\\ \n27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1 \\\\ \n26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2 \\\\ \n30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2 \\\\ \n15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4 \\\\ \n19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6 \\\\ \n15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8 \\\\ \n21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2 \\\\ \n\\bottomrule\n\\end{longtable}\n"

Exporting seems to not be supported

mtcars %>% 
  gt() %>% 
  as_latex(file = "mytable.tex")
#> Error in as_latex(., file = "mytable.tex"): unused argument (file = "mytable.tex")

Created on 2018-12-17 by the reprex package (v0.2.1)

[2] Intermediate [3] High [2] Medium ★ Enhancement

Most helpful comment

@PMassicotte @MikeKSmith @kuriwaki @vincentarelbundock

We have a file-based exporting function (gtsave()) in the works: https://github.com/rstudio/gt/pull/130. That function is supposed to be a single place to export the gt object as a file (analogous to the ggplot2 ggsave() function). That would be the place to export as HTML, LaTeX, and RTF.

The argument that @kuriwaki brought up (floating) is something I would like to implement in as_latex(). Thanks for that suggestion!

For gtsave(), I'm thinking it would be good to have a standalone argument, for both for the HTML and LaTeX output types. A value of TRUE would, in the HTML case, place the table within a minimal HTML document (i.e., with HTML declaration, head, and body tags); FALSE would write an HTML document just with the <table>...</table>.

There are certainly a few inconsistencies within all of these exporting functions and I'd like to fix that with better defaults. @vincentarelbundock , your comment about the asis object makes me think that should be a secondary option. And, yes! I do value your opinion.

We could and should continue to discuss issues around the as_*() and the WIP gtsave() functions here.

All 12 comments

@PMassicotte I'm glad you brought this issue up. It's true that the as_latex() function is not very useful currently as an exported function (it's main use is in the internal knit_print.gt_tbl() function, which converts the gt_tbl object to LaTeX for knitr).

I think we just need to add a few things to as_latex() (or some other function, more on this later...) to make it useful for exporting to a standalone .tex file.

If we take your as.character() output ([1] "\\captionsetup[table]{labelformat=empty,skip=1pt}\n...) and pipe that into writeLines(con = "latex_out.tex") we do get a file with a valid chunk of LaTeX code. However, it's not a standalone file that you can expect to run... it's just the table part. It does run in Overleaf, for example, if you also include three \usepackage{} statements (for longtable, booktabs, and caption) within a template document. Here's a screenshot of that:

gt_latex_output_in_overleaf

To make this better for the user, there are some options that can be considered:

  • include a self_contained argument, where TRUE would include all code to make the file a valid document (with the necessary \usepackage{} statements, \documentclass{}, begin{}/end{}, ...) and FALSE would just be the table part.
  • make a separate function to get the packages that should be included for the table part (this would be useful if someone wanted to integrate the table part into a larger LaTeX document, but needed the correct LaTeX packages as well)
  • make an export_latex() function (and possibly export_html() and export_rtf() functions with the file argument); this would leave as_latex() alone to do it's job and the user-facing export_latex() can then issue any necessary messages and warnings and not affect the print method

Let me know what you think of these proposed ideas! This would be all be part of a larger effort to make exporting gt objects to images and files possible across the different output formats.

Thanks for the reply. This is a good workaround I can use at the moment!

I think your suggestions are nice. The export_ look promising in my opinion and should not be too difficult to implement.

I would be in favour of the export_latex(), export_html() and export_rtf() functions to separate functionality as you describe.

xtable::print.xtable has an argument floating that either generates .tex with a float environment with captions \begin{table}... (the default), or without the floating environment and starts directly with \begin{tabular} (if set to FALSE). Both options would be great to have with the file export argument.

library(xtable)
library(ggplot2)

tab <- xtabs(~ cyl + drv, data = mpg)
xtbl <- xtable(tab)

print.xtable(xtbl, floating = TRUE)
#> % latex table generated in R 3.5.1 by xtable 1.8-3 package
#> % Sun Jan 13 15:28:24 2019
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{rrrr}
#>   \hline
#>  & 4 & f & r \\ 
#>   \hline
#> 4 &  23 &  58 &   0 \\ 
#>   5 &   0 &   4 &   0 \\ 
#>   6 &  32 &  43 &   4 \\ 
#>   8 &  48 &   1 &  21 \\ 
#>    \hline
#> \end{tabular}
#> \end{table}
print.xtable(xtbl, floating = FALSE)
#> % latex table generated in R 3.5.1 by xtable 1.8-3 package
#> % Sun Jan 13 15:28:24 2019
#> \begin{tabular}{rrrr}
#>   \hline
#>  & 4 & f & r \\ 
#>   \hline
#> 4 &  23 &  58 &   0 \\ 
#>   5 &   0 &   4 &   0 \\ 
#>   6 &  32 &  43 &   4 \\ 
#>   8 &  48 &   1 &  21 \\ 
#>    \hline
#> \end{tabular}

Created on 2019-01-13 by the reprex package (v0.2.1)

Not that you asked for my opinion, but having separate as_latex and export_latex functions seems a bit confusing to me. Why wouldn't a couple new arguments suffice? as_latex(self_contained = TRUE, file = 'table.tex')

Also, it seems a bit inconsistent that the other two as_* functions return raw character strings containing the end result, while as_latex returns an "asis" object.

@PMassicotte @MikeKSmith @kuriwaki @vincentarelbundock

We have a file-based exporting function (gtsave()) in the works: https://github.com/rstudio/gt/pull/130. That function is supposed to be a single place to export the gt object as a file (analogous to the ggplot2 ggsave() function). That would be the place to export as HTML, LaTeX, and RTF.

The argument that @kuriwaki brought up (floating) is something I would like to implement in as_latex(). Thanks for that suggestion!

For gtsave(), I'm thinking it would be good to have a standalone argument, for both for the HTML and LaTeX output types. A value of TRUE would, in the HTML case, place the table within a minimal HTML document (i.e., with HTML declaration, head, and body tags); FALSE would write an HTML document just with the <table>...</table>.

There are certainly a few inconsistencies within all of these exporting functions and I'd like to fix that with better defaults. @vincentarelbundock , your comment about the asis object makes me think that should be a secondary option. And, yes! I do value your opinion.

We could and should continue to discuss issues around the as_*() and the WIP gtsave() functions here.

I will try it as soon as the PR is accepted. Thank you very much for your work.

The gtsave() function is now available in the package (https://github.com/rstudio/gt/pull/130) and that covers the original scope of this issue. That function doesn't replace any of the as_<format>() functions. The @kuriwaki and @vincentarelbundock brought up are still quite relevant, so, separate issues will be created from those recent comments.

FWIW, I tried gtsave and it works really nicely. Thanks for all your work on this!

@vincentarelbundock great! And you’re quite welcome.

Thank you @rich-iannone !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jenniferp96 picture jenniferp96  Â·  4Comments

r2evans picture r2evans  Â·  5Comments

higgi13425 picture higgi13425  Â·  4Comments

ddsjoberg picture ddsjoberg  Â·  3Comments

DickStartz picture DickStartz  Â·  4Comments