Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kableExtra addfootnote general spanning multiple lines with PDF (LaTeX) output

Problem

Code

# Toy Data
ID <- c(paste("G0", as.character(1:9), sep = ""),"G10","G11","Mean")
V1 <- c(10.06,11.06,12.06,13.06,14.06,15.06,16.06,17.07,18.07,19.07,6.88,13.86)
V2 <- c(0.21,0.03,0.09,0.03,0.09,0.03,0.09,0.03,0.09,0.21,0.31,NA)
tbl <- data.frame(ID, V1, V1, V2, V1, V2, V1, V2, V2)
colnames(tbl) <- c('ID','Get. \\%','Get. \\%','K','Get. \\%','K','Get. \\%','K','P')

# Specify kable NA value and load kableExtra
options(knitr.kable.NA = '--')
require(kableExtra)

# Generate table for PDF output (LaTeX)
kbl(tbl, format = 'latex', align = 'l', booktabs = T, escape = F, digits = 2,
    linesep = "", caption = "This is a table caption.") %>%
  add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
  column_spec(1, width = '1.15cm') %>%
  row_spec(11, hline_after = T) %>%
  row_spec(12, bold = T) %>%
  kable_styling(position = "center", latex_options = "hold_position") %>%
  footnote(general_title = "Note.", footnote_as_chunk = T, 
           general = "Relatively long footnote that I would like to span 
                     a couple of lines. Relatively long footnote that I
                     would like to span a couple of lines.")

Output

enter image description here

Comments

Issue 1: The output displays 'makecell[1]' in the footnote, which I obviously do not want included. Adding the argument escape = T did not resolve this problem as I expected it might have.

N.B. By setting footnote_as_chunk = F, this issue was resolved, but with the unwanted effect of introducing a line break before the caption starts. This is demonstrated by Peter's answer below.

Issue 2 The footnote does not want to be constrained to the length of the table. I suppose one might be able to manually add line breaks in the footnote string, but this seems like tedious work-around, and I'm hoping there is a method for achieving this more efficiently. The documentation shows (see Table 4, p. 25) an example of how one might circumvent this problem, but the code is absent.

EDIT: This issue (#2) was resolved by setting threeparttable = T when calling kbl.

Compiling with pdflatex or xelatex does not seem to make any difference. Any insight would be much appreciated.

like image 252
Speleo4Life Avatar asked Sep 06 '25 03:09

Speleo4Life


1 Answers

Try this:


library(kableExtra)
library(magrittr)

kbl(tbl, 
    format = 'latex',
    longtable = TRUE,
    align = 'l', 
    booktabs = T, 
    escape = F, 
    digits = 2,
    linesep = "", 
    caption = "This is a table caption.") %>%
  add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
  column_spec(1, width = '1.15cm') %>%
  row_spec(11, hline_after = T) %>%
  row_spec(12, bold = T) %>%
  kable_styling(position = "center", latex_options = "hold_position", full_width = FALSE) %>%
  footnote(general_title = "Note.", 
           footnote_as_chunk = TRUE,
           threeparttable = TRUE,
           general = "Relatively long footnote that I would like to span a couple of lines. Relatively long footnote that I would like to span a couple of lines.")

With footnote_as_chunk = TRUE using the "general" footnote option "Note." and the "Footnote...." text start on the same line. As in this example, image below.

enter image description here

like image 142
Peter Avatar answered Sep 07 '25 22:09

Peter