Show multiple footnotes in xtable

廉价感情. 提交于 2019-12-11 06:19:50

问题


My question is very similar to this one on showing footnotes in xtable, although the suggested updated solution isn't working for me. I presume this is because I am missing something in my own setup, hence the separate question. I don't think the issue is what is described in this question, as I am using the sanitize.text.function in the print call rather than the xtable call.

Below is an image of the PDF output. As can be seen, the actual footnote text does not appear in the PDF output, even after rendering twice. The footnote superscripts do appear, however. I am rendering using the "Knit" button in RStudio.

How can I get the actual footnote text to show?

PDF output:

My code from the .Rmd file is below.

---
title: "xtable footnotes"
output: 
    pdf_document:
        keep_tex: true
        fig_caption: true

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r results = "asis"}
library(xtable)

x <- matrix(rnorm(60), ncol = 10)

x.big <- xtable(x,label = 'tabbig', caption = 'Example of xtable footnotes')
names(x.big) <- LETTERS[1:10]

names(x.big)[1] <- paste('A','footnote1')    # I put the tag on A letter 
names(x.big)[9] <- paste('I','footnote2')    # I put the tag on I letter 
print(x.big, 
      type = "latex",
      sanitize.text.function = function(str){
        str <- gsub("footnote1","\\footnote{my tricky footnote 1 !!}", str, fixed = TRUE)
        str <- gsub("footnote2","\\footnote{my tricky footnote 2 !!}", str, fixed = TRUE)
      }
        )
```

The .tex output for the table is as follows:

\% latex table generated in R 3.4.1 by xtable 1.8-2 package \% Tue Aug
22 09:45:33 2017

\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrr}
  \hline
 & A \footnote{my tricky footnote 1 !!} & B & C & D & E & F & G & H & I \footnote{my tricky footnote 2 !!} & J \\ 
  \hline
1 & 1.13 & -0.00 & -0.14 & 0.83 & 0.58 & -0.65 & -1.12 & -2.04 & -0.64 & 0.50 \\ 
  2 & 0.13 & -0.65 & -1.11 & 0.06 & -1.32 & -0.28 & 0.96 & 1.19 & -0.41 & -0.51 \\ 
  3 & -0.73 & 0.16 & -0.26 & -1.50 & -1.34 & 0.84 & -0.28 & -0.02 & -0.98 & 1.13 \\ 
  4 & 0.33 & 0.89 & -1.08 & -0.89 & 1.16 & 1.70 & -0.77 & -0.21 & 1.01 & 0.22 \\ 
  5 & 0.86 & 0.19 & -0.94 & -1.36 & -2.49 & 0.62 & 0.87 & -1.17 & -0.24 & 0.17 \\ 
  6 & 0.19 & -0.15 & 0.20 & -0.56 & 0.04 & 1.20 & -0.72 & -1.39 & -1.30 & 0.03 \\ 
   \hline
\end{tabular}
\caption{Example of xtable footnotes} 
\label{tabbig}
\end{table}

回答1:


A simple fix in the end, found through trial-and-error.

To make the footnotes appear, the longtable LaTeX package and environment need to be used. I added them into the header-includes part of the YAML header and then the code ran as expected.

Using the following code in the .Rmd file made the footnotes appear:

---
title: "xtable footnotes"
output: 
    pdf_document:
        keep_tex: true
        fig_caption: true
header-includes:
    - \usepackage{longtable}  

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r results = "asis"}
library(xtable)

x <- matrix(rnorm(60), ncol = 10)

x.big <- xtable(x,label = 'tabbig', caption = 'Example of xtable footnotes')
names(x.big) <- LETTERS[1:10]

names(x.big)[1] <- paste('A','footnote1')
names(x.big)[9] <- paste('I','footnote2') 

# tabular.environment needs to be 'longtable'
# \usepackage{longtable} needs to be in the YAML header at the start of the .Rmd file

print(x.big,
      tabular.environment = 'longtable',
      floating = FALSE,
      sanitize.text.function = function(str){
        str <- gsub("footnote1","\\footnote{my tricky footnote 1 !!}", str, fixed = TRUE)
        str <- gsub("footnote2","\\footnote{my tricky footnote 2 !!}", str, fixed = TRUE)
    }
  )
```

The generated table looks like this, with footnotes at the bottom of the page:



来源:https://stackoverflow.com/questions/45813247/show-multiple-footnotes-in-xtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!