gt
gt copied to clipboard
Discussion for LaTeX output development
Starting a thread here to discuss the ongoing development of LaTeX output tables. Thanks @annacnev for your Interest and contributions in this space (we’ll ensure that your work does get put into this package).
I have to admit that I don’t have much of a history with LaTeX, so, some terrible bugs got introduced. Development on the LaTeX was also stalled by furthering the HTML part of the package. Now that HTML output is somewhat stable, I think we could definitely move ahead more seriously with the LaTeX part of this package.
I'm reasonably expert with LaTeX and would be happy to help with this if that would be useful. I like this package and would love to see it support LaTeX as well as it supports HTML.
Hey Rich!
Very excited that you guys are ready to move forward on the LaTeX portion of this! Thanks for starting this chain & sorry about the delay on getting back to you here.
I would be more than happy to help with the LaTeX portion of this. I have implemented most of the planned LaTeX features in the mrggt fork- with some additional features that our users at Metrum requested. I have been able to do this without changing any functionality on the HTML end- so there are definitely work arounds for the bugs that were introduced.
I am still doing some code clean up and trying to find better ways to make it all work together, but I am wondering if it might be helpful to provide a write up of the learning I have done so far, the road-blocks I have run into, and decisions that I have made to implement certain features and solve said road-blocks? We could use this to outline a path forward and/or use it as a reference so you guys are not falling down the same rabbit holes I have have already gone down.
@annacnev A write up would be great! I also think we ought to have a video chat or conference call beforehand, bringing together some people from our respective teams (e.g., Joe Cheng on my side). Could you email me at [email protected] to schedule a discussion?
Jumping in to the conversation! The texreg
package has a lot of good options for latex output.
Table Environments
booktabs
, longtable
, threeparttable
, tabular
all let you decide which table environment to use
Other latex options
center
: whether to include \centering
or not
scalebox
: how much to scale the table on the document
label
: label the table for references
Also, I would like to see an option to remove \captionsetup if possible!
Not sure how this is progressing, but is there any chance that fmt_passthrough()
could be extended to allow latex to appear everywhere instead of only by cell? Right now, any kind of $$
environment, even structured within md()
is escaped.
Relevant escapes: https://github.com/rstudio/gt/blob/8f15c1552ffcb17465db68dc3e74ff6d4c635e48/R/helpers.R#L1859-L1866
Function injecting escapes: https://github.com/rstudio/gt/blob/8e5d2d156a2f23cecaff518740e2544fc7eb161f/R/utils.R#L179-L180 Specific escape section: https://github.com/rstudio/gt/blob/8e5d2d156a2f23cecaff518740e2544fc7eb161f/R/utils.R#L219-L240
I've taken to using a custom function at the end of the pipe to address the conversion.
remove_escape_latex <- function(x) {
enable_special_characters = function(x) {
gsub("\\\\([&%$#_{}])", "\\1", x, fixed = FALSE, ignore.case = TRUE)
}
enable_backslash = function(x) {
gsub("\\\\textbackslash([[:space:]])?", "\\\\", x, fixed = FALSE, ignore.case = TRUE)
}
enable_tilde = function(x) {
gsub("\\\\textasciitilde([[:space:]])?", "~", x, fixed = FALSE, ignore.case = TRUE)
}
enable_exponents = function(x) {
gsub("\\\\textasciicircum ", "\\^", x, fixed = FALSE, ignore.case = TRUE)
}
enable_backslash(enable_special_characters(enable_tilde(enable_exponents(x))))
}
my_gt_table() %>% as_latex() %>% as.character() %>% remove_escape_latex()
Relevant issue tickets: #645, #533, #375
Thank you for your work on this @annacnev and @rich-iannone ! Latex will be a very welcome addition to an already wonderful package! 🙌 💯
@annacnev any chance you could point me in the direction of the mrggt
fork? I'm looking to help get the latex integration working and would love to see any work that people have already started. I've found latex-begin, is this still being worked on?