Wrapping footnote to second line to fit table in kable (LaTeX .Rmd)
Asked Answered
B

2

6

I'm wondering if there is a way I can force a footnote to fit the width of a table (and wrap to a second line) using kable in R (I'm knitting .Rmd to PDF so the format is latex). I've used both add_footnote() and footnote(). The add_footnote function will actually wrap a footnote to a second line if it extends beyond the width of the table, but it also forces the use of superscripts (which for this sake is something I can't have in my table). footnote gives me the option to remove the superscript but I'm not sure how to get it to match the formatting of add_footnote and wrap footnotes that are wider than the table to a second line. Another solution would be to remove the superscripts from add_footnote

\captionsetup[table]{labelformat=empty}
```{r packs}
library(pacman)
p_load(tidyverse,knitr,kableExtra,janitor)

mydf <-data_frame(x=1:4,y=2:5,z=3:6)

fn1='This the footnote 1'
fn2='This is footnote 2 and is much longer'

mydf %>%
  kable(format='latex',booktabs=T,
        col.names=c('X','Y','Z'),
        caption=c('This method stretches', 'my table out in an ugly way')) %>%
  kable_styling(latex_options = c('hold_position')) %>%
  footnote(general=c(fn1, fn2),general_title="")

mydf %>%
  kable(format='latex',booktabs=T,
        col.names=c('X','Y','Z'),
        caption=c('This method ruins my title', 'and left justifies my table')) %>%
  kable_styling(latex_options = c('hold_position')) %>%
  footnote(general=c(fn1, fn2),general_title="",threeparttable = T)

mydf %>%
  kable(format='latex',booktabs=T,
        col.names=c('X','Y','Z'),
        caption=c('This is pretty close to perfect', 'if I could remove the superscripts')) %>%
  kable_styling(latex_options = c('hold_position')) %>%
  add_footnote(c(fn1, fn2))
```

A screenshot of the knitted PDF: enter image description here

Ballplayer answered 11/4, 2018 at 16:8 Comment(5)
there is a threeparttable optionValse
@Valse Yeah but that ruines my captions/titles and also seems to force my table to be left-justified in the PDF. I'm admittedly bad at LaTex so I'm guessing fixing the lest justification is simple enough but it just removes parts of my title. I've edited/attached a MWE above.Ballplayer
I almost forgot I have such a hack in add_footnote as it was actually using multi-column to do the footnote... Well, in the dev version, for add_footnote I added "none" to notation. You can try it out.Valse
No luck. It gives me an error that I need to use one of c('alphabet', 'number', 'symbol')Ballplayer
“in the dev version” means you need to install from githubValse
N
5

setting threeparttable = TRUE as @hao suggested worked for me:

add_footnote(c(fn1, fn2), threeparttable = TRUE) 
Nehemiah answered 8/7, 2021 at 17:50 Comment(0)
F
1

Below is the notation="none" option @hao suggested. It is center-justified.

mydf %>%
  kable(format='latex',booktabs=T,
        col.names=c('X','Y','Z'),
        caption=c('Notation="none"') %>%
  kable_styling(latex_options = c('hold_position')) %>%
  add_footnote(c(fn1, fn2), notation="none")

enter image description here

Forecast answered 5/5, 2020 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.