5

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

c.custer
  • 407
  • 5
  • 13
  • there is a `threeparttable` option – Hao Apr 11 '18 at 16:24
  • @Hao 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. – c.custer Apr 11 '18 at 19:40
  • 1
    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. – Hao Apr 12 '18 at 20:14
  • No luck. It gives me an error that I need to use one of `c('alphabet', 'number', 'symbol')` – c.custer Apr 16 '18 at 13:17
  • “in the dev version” means you need to install from github – Hao Apr 16 '18 at 21:49

2 Answers2

4

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

add_footnote(c(fn1, fn2), threeparttable = TRUE) 
Mnl
  • 787
  • 8
  • 9
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

dfrankow
  • 20,191
  • 41
  • 152
  • 214