在 xtable 中显示多个脚注

Show multiple footnotes in xtable

我的问题与 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 非常相似,因为我在 print 调用中使用 sanitize.text.function 而不是 xtable 调用。

下面是 PDF 输出的图像。可以看出,实际脚注文本 没有出现在 PDF 输出中,即使在渲染两次之后也是如此。然而,脚注上标 do 出现了。我正在使用 RStudio 中的 "Knit" 按钮进行渲染。

如何显示实际的脚注文本?

PDF 输出:

我在 .Rmd 文件中的代码如下。

---
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)
      }
        )
```

table 的 .tex 输出如下:

\% 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}

最后一个简单的修复,通过 trial-and-error 找到。

要显示脚注,需要使用longtable LaTeX 包和环境。我将它们添加到 YAML header 的 header-includes 部分,然后按预期添加代码 运行。

.Rmd 文件中使用以下代码使脚注出现:

---
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)
    }
  )
```

生成的table是这样的,页面底部有脚注: