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:
- keep the tex file generated by quarto by adding
keep-tex: true
to your YAML preamble
- 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
- 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)
