Search code examples
rlatexr-markdownsymbolstabular

Formula's or symbols in footnotes using knitr and kableExtra


Does anyone know how to place a formula, a (weird) character, or words in italic within a sentence of a footnote of a table?

I'm creating a pdf file with Rmarkdown and kableExtra. But stuff like $Y_{t-1}$ or $p < .001$ (since I want the p to be italic) does not work. Or should I really learn xtable?


Solution

  • The trick is 1. to escape latex code and special characters four times, e.g. \\\\frac, 2. to set option escape=FALSE in footnote().

    ---
    title: "Untitled"
    output: pdf_document
    ---
    
    ```{r tab}
    library(knitr)
    library(kableExtra)
    df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33), 
                     row.names=LETTERS[1:6])
    kable(df, "latex", align="c", booktabs=TRUE) %>%
    footnote(general=c("$a^2+b^2=c^2,$",     
                       "$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$", 
                       "1,000 \\\\$;", "100\\\\%."),
             number=c("Hello\ there! \\\\textit{Hello\ there!}"),
             footnote_as_chunk=TRUE, 
             escape=FALSE)
    ```
    

    Yields: enter image description here