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

Latex caption + cross-referencing in bookdown #1800

Merged
merged 16 commits into from
Aug 29, 2024

Conversation

nielsbock
Copy link
Contributor

This is a fix for #1610 and #818. This PR adds a caption component to the LaTex outputs and enables cross-referencing in bookdown.

Adding a caption with tab_caption()

mtcars[1:5, ] %>%
  gt() %>% 
  tab_caption(md("This is a caption for **mtcars**")) %>% 
  as_latex() %>% 
  as.character()

results in the following Latex code:

"\\begin{table}[!t]\n\\caption{This is a caption for \\textbf{mtcars}} \n\\fontsize{12.0pt}{14.4pt}\\selectfont\n\\begin{tabular*}{\\linewidth}{@{\\extracolsep{\\fill}}rrrrrrrrrrr}\n\\toprule\nmpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\\\ \n\\midrule\\addlinespace[2.5pt]\n21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\ \n21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\ \n22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\ \n21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\ \n18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\ \n\\bottomrule\n\\end{tabular*}\n\\end{table}\n"

I've updated knitr helper functions in knitr-utils.R which enables cross-referencing using bookdown. This only works in HTML if options("htmltools.preserve.raw" = FALSE), which was already commented in the code. For the Latex output in Quarto, I've used the function check_quarto() to disable captions created with tab_captions as it is not compatible with tbl-cap. This is not an issue with HTML in quarto; however, tbl-cap should still be used for cross-referencing.

Copy link
Collaborator

@olivroy olivroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not using bookdown, so can't comment on this, but I made some suggestions in the tests to conform to new test style.

tests/testthat/test-l_table_parts.R Outdated Show resolved Hide resolved
tests/testthat/test-l_table_parts.R Outdated Show resolved Hide resolved
@nielsbock
Copy link
Contributor Author

Thanks @olivroy. I modified the test to match the new style.

@olivroy
Copy link
Collaborator

olivroy commented Aug 28, 2024

Thanks for working on this and addressing the comments!

@rich-iannone I don't have much objection here. If this can help some bookdown users, we may as well go for it!

As long as it doesn't break the Quarto workflow which should still be to use tbl-cap: and label: tbl-*. i.e. tab_caption() is ignored in Quarto output iiuc.

@nielsbock
Copy link
Contributor Author

No problem!

Yes, it shouldn't break Quarto workflow, as it's disabled in Quarto in the same way as the latex.tbl.pos argument in tab_options(), using the check_quarto() function. I've used gt with this code in both Quarto and Rmarkdown for academic work and haven't stumbled upon issues yet.

Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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

Successfully merging this pull request may close these issues.

3 participants