Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to omit longtable environment in Latex #1310

Closed
jonathandroth opened this issue Apr 17, 2023 · 12 comments
Closed

Option to omit longtable environment in Latex #1310

jonathandroth opened this issue Apr 17, 2023 · 12 comments

Comments

@jonathandroth
Copy link

Thank you for the great package!

I think it would be great if the gtsave function had an option to omit the \begin{longtable} and \end{longtable} parts and only save the the text from \begin{tabular} to \end{tabular}. This would facilitate incorporating the output into Latex documents, and would be analogous to e.g. the contents_only option in xtable.

@spittssj
Copy link

spittssj commented Dec 8, 2023

I agree with this suggestion. Right now I am struggling to add captions and labels to LaTeX tables produced by the gt package and this would be an easy solution.

@jonathandroth
Copy link
Author

@spittssj In case it's helpful as a work-around, I wrote some simple functions here to convert longtable to tabular in a text file. The same link also has a function to extract only the part between the \begin{tabular} and \end{tabular}. This post-processing worked pretty well for me, although it'd be great if it were eventually made internal to gt.

@spittssj
Copy link

@jonathandroth the link you provided doesn't work but the code you describes sounds useful. Can you check the link (or perhaps the security settings)? Thanks.

@jonathandroth
Copy link
Author

@spittssj
Copy link

Thanks @jonathandroth. I tried the code and it works well.

@jonathandroth
Copy link
Author

jonathandroth commented Dec 27, 2023 via email

@olivroy
Copy link
Collaborator

olivroy commented Jul 12, 2024

I think this would be fixed if #1588 is merged?

@jonathandroth
Copy link
Author

I think not quite, because the request was to have the output begin with \begin{tabular}, whereas the new feature appears to output \begin{table} \\n \begin{tabular}.

Just to clarify, the motivation for this request is that I find it much easier to handle creating tables locally in Latex--- because captioning, labeling, and desired placement are specific to the document. So my Latex document would have

\begin{table}[<placement options desired for this doc>]
\input{gt_output.tex}
\caption{<relevant caption for my table>}
\label{<label>}
\end{table} 

Of course, while you're modifying this code, it seems like it should be straightforward to omit the \begin{table} \\n part.

@olivroy
Copy link
Collaborator

olivroy commented Sep 3, 2024

There is a new tab_options(latex.tbl.pos) that can help with positioning. tab_caption() is now kept in LaTeX output also

# dev gt 
# pak::pak("rstudio/gt")
library(gt)
gt(exibble) |>
  tab_caption("caption") |>
  tab_options(latex.tbl.pos = "h!") |>
  as_latex()
\begin{table}[h!]
\caption{caption} 
\fontsize{12.0pt}{14.4pt}\selectfont
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}rlcrrrrll}
\toprule
Num & Char & Fctr & Date & Time & Datetime & Currency & Row & Group \\ 
\midrule\addlinespace[2.5pt]
1.111e-01 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.950 & row\_1 & grp\_a \\ 
2.222e+00 & banana & two & 2015-02-15 & 14:40 & 2018-02-02 14:33 & 17.950 & row\_2 & grp\_a \\ 
3.333e+01 & coconut & three & 2015-03-15 & 15:45 & 2018-03-03 03:44 & 1.390 & row\_3 & grp\_a \\ 
4.444e+02 & durian & four & 2015-04-15 & 16:50 & 2018-04-04 15:55 & 65100.000 & row\_4 & grp\_a \\ 
5.550e+03 & – & five & 2015-05-15 & 17:55 & 2018-05-05 04:00 & 1325.810 & row\_5 & grp\_b \\ 
– & fig & six & 2015-06-15 & – & 2018-06-06 16:11 & 13.255 & row\_6 & grp\_b \\ 
7.770e+05 & grapefruit & seven & – & 19:10 & 2018-07-07 05:22 & – & row\_7 & grp\_b \\ 
8.880e+06 & honeydew & eight & 2015-08-15 & 20:20 & – & 0.440 & row\_8 & grp\_b \\ 
\bottomrule
\end{tabular*}
\end{table}

We may remove the font size shortly #1472 too. #1852

@olivroy
Copy link
Collaborator

olivroy commented Sep 3, 2024

Duplicate of #1549

@olivroy olivroy marked this as a duplicate of #1549 Sep 3, 2024
@olivroy olivroy closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2024
@jonathandroth
Copy link
Author

It's great that you can handle positioning and titles within gt.

However, I personally find it much easier to handle those in the Latex document itself, and not have to run my code again every time I want to change the title or position of my table. That is why it's much easier to have gt output only the \begin{tabular} ... \end{tabular} part without the table environment.

But totally understood if you don't have the time/energy to implement that. I can continue to ex-post process the output to remove the table environment as in the code snippet shared above.

@capnrefsmmat
Copy link

The other option would be to use a Rnw file instead of LaTeX. Rnw files are just LaTeX containing R code chunks, and can be processed by knitr, so you could have the table-generation code directly in the LaTeX source so it is automatically regenerated when you compile to PDF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants