Skip to content

pdf footnotes #366

@ggrothendieck

Description

@ggrothendieck

In attempting to get pdf footnotes without using the last row of the table for that, the following seems to work but had to descend to latex level and also there are some gsub's that are pretty ugly. After trying many variations this was the one that worked the best. I wonder if huxtable could make this a bit easier. For example, maybe to_latex could have an argument that instructs it not to generate the junk that the gsub's remove.

To run this put it in a file myfile.Rmd and then run render("myfile.Rmd")

---
title: "test footnotes"
output:
  pdf_document:
    keep_tex: true
header-includes:
  - \usepackage{threeparttable}
  - \usepackage{caption}
  - \usepackage{calc}
  - \usepackage{hhline}
  - \usepackage{colortbl}
  - \usepackage{tabularx}
---

\newcommand{\htwithnotes}[3]{%
\begin{center}
\begin{threeparttable}
\captionof{table}{#3}
#1
\begin{tablenotes}[flushleft]
\footnotesize
#2
\end{tablenotes}
\end{threeparttable}
\end{center}
}

```{r setup, include=FALSE}
library(huxtable)
options(huxtable.latex_use_fontspec = FALSE)

ht_pdf <- function(ht, notes = "", caption = "") {
  
  safe_caption <- sanitize(caption, type = "latex")
  safe_notes   <- sanitize(notes, type = "latex")
  
  escape_contents(ht) <- TRUE
  
  latex_float(ht) <- "none"
  table_environment(ht) <- NA
  
  ltx_body <- to_latex(ht, tabular_only = TRUE)
  
  ltx_body <- gsub("```\\{=latex\\}", "", ltx_body)
  ltx_body <- gsub("```", "", ltx_body)
  
  formatted_notes <- paste0("\\item ", safe_notes, collapse = "\n")
  
  res <- sprintf("\\htwithnotes{%s}{%s}{%s}", ltx_body, formatted_notes, safe_caption)
  knitr::asis_output(res)
}
```

## Table

```{r generate_table, echo = FALSE, results = 'asis'}

ht <- as_hux(BOD, add_colnames = TRUE) |>
  set_align(everywhere, everywhere, "center") |>
  set_all_borders(0.5) |>
  set_bold(1, everywhere, TRUE) |>
  set_background_color(1, everywhere, "grey90") |>
  set_width(0.9)

notes <- c("Some note", "Some other note")

ht_pdf(ht, notes = watchlist_notes, caption = "My caption")
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions