1

Trying to get to grips with the {gt} package, v0.9.

Outputting the following in pdf (html works) results in displaced footnotes/source_notes. I seem to fail to identify in the documentation how to ensure that (both) footnotes and source_notes are shown underneath the table.

library(tidyverse)
library(gt)

Building the table and it's {gt}-output

# taken from vignette
# take build-in island dataset and coerce to tibble and take top 10
islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) |>
  arrange(desc(size)) |>
  slice(1:10)

# add header and footnote
# footnotes

tbl_experiment <- islands_tbl |> 
  gt() |> 
  tab_header(
      title = "Large Landmasses of the World",
      subtitle = "The top ten largest are presented"
  ) |>
  tab_footnote(
    footnote = "The Americas.",
    locations = cells_body(columns = name, rows = 3:4)
  ) |>  
  tab_source_note(
    source_note = md("(I am a source_note) Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
  )

tbl_experiment

This yield in pdf output:

displaced footnote and source_note in pdf output

Obviously, I could use {gt}'s gtsave() to store the table as a png (for example) and then include it again as an image in a qmd/Rmd file. While that workaround works ...

{gt} table saved out as png and re-included in qmd/Rmd file

... I am trying to find out how to do this natively. Thanks for pointing me in the right direction.

Ray
  • 2,008
  • 14
  • 21
  • This is how it's supposed to look with the pdf output. It's not a translation of the table's appearance in html. It puts the tables content inside a latex `longtable` and the footnotes/source notes inside a `minipage`. – jkd Aug 05 '23 at 19:20
  • @jdk - thanks for that. Not sure that I would agree with that, but it helps to understand that this is the expected output. How could I control the width of the minipage footer part from Quarto/Rmd? I.e. How could this be made narrower/matching ideally the width of the table? – Ray Aug 06 '23 at 07:42

1 Answers1

0

The output you get is the expected output because quarto or rather the underlying pandoc relies on the longtable latex package and puts the footnotes inside a minipage.

There are many questions about footnotes being below tables in latex on tex.stackexchange.com but they generally rely on other table environments/packages or on complex tricks. The same is true regarding matching the width of the minipage and the table.

So the simplest solution would be to:

  1. keep the tex file generated by quarto by adding keep-tex: true to your YAML preamble
  2. match the width of table and minipage by:
    • setting the table to full width (which will match minipage width), OR
    • adapting the width of the minipage and table manually
  3. compile the modified tex file (e.g., call tinytex::latexmk("filename.tex") from R)

To set the table to full width, replace \begin{longtable*}{lr} in your tex file by (source):

\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable*}{@{\extracolsep{\fill}}lr@{}}

To adapt the width of the minipage and table you must center the minipage by enclosing in in \centerline{} and replace its width (\linewidth) by an absolute or relative width (e.g., 5cm or 0.5\linewidth, respectively). Then you may want to increase column width in the table (to get a wider table) by adding a specific width besides each column defined at the beginning of the longtable environment, so \begin{longtable*}{lr} would become \begin{longtable*}{l{2cm}r{50pt}} for example. Since this requires manual tweaking I would rather recommend the full-width approach.

Other solutions include:

Edit 08-08-2023: switching from gt to kableExtra

I just found a not-so-old Reddit thread where someone recommended kableExtra when using a pdf output and indeed it seems that kableExtra got you covered:

library(dplyr)
library(kableExtra)

islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) |>
  arrange(desc(size)) |>
  slice(1:10)

# there is no subcaption so we have to define caption and subcaption as one
cap_text <- paste("\\large{Large Landmasses of the World} \\\\",
                  "\\small{The top ten largest are presented}")
# some wild escaping going on here to get proper formatting
# note that I add a manual linebreak, otherwise the table would be much wider
# to accommodate the long footnote
ref_text <- paste("(I am a source\\\\_note) Reference: McNeil, D. R. (1977) \n",
                  "\\\\textit{Interactive Data Analysis}. Wiley.")

islands_tbl |>
  mutate(name = ifelse(grepl("America",name,ignore.case = T),
                       paste0(name,footnote_marker_number(1, "latex")),
                       name)) |> # adding the footnote markers where necessary
  kbl(caption = cap_text,
      booktabs = T,
      linesep = "", # if unspecified booktabs adds spacing after every 5th line
      escape = F) |> # you need to set escape to FALSE so that the latex commands work
  kable_styling(latex_options = "hold_position") |>
  footnote(general = linebreak(ref_text, double_escape = T),
           number = "The Americas.",
           footnote_order = c("number","general"),
           general_title = "",
           escape = F)

Table produced by kableExtra

jkd
  • 1,327
  • 14
  • 29
  • hi @jdk - thanks a lot for the extensive answer and research going into the workaround. Highly appreciated. I accept this answer. My goal was to get to grips with {gt}. Thanks for pointing out {kable}{kableExtra}. While it would be nice to have one set of packages to handle multiple outputs, this feels like making use of conditional (table) output in quarto when outputting to pdf or other formats. Thanks again for all the time you invested in this. – Ray Aug 28 '23 at 06:01
  • Hi @Ray, you're welcome. The issue was of interest to me and I learned quite a bit working on the answer. I agree that mutliple output formats are rather tedious to work with as soon as you need special formating/layouting. On the other hand, it's also a lot of work to implement new output formats and I am still very happy with the possibilites offered by quarto and pandoc. – jkd Aug 29 '23 at 06:32