2017-08-22 58 views
1

我的問題與this one在顯示xtable的腳註時非常相似,但建議的更新解決方案對我無效。我認爲這是因爲我在我自己的設置中錯過了一些東西,因此是單獨的問題。我不認爲這個問題是this question中描述的問題,因爲我在print調用中使用sanitize.text.function而不是xtable調用。在xtable中顯示多個腳註

以下是PDF輸出的圖像。可以看出,即使在渲染兩次之後,實際腳註文本也不會出現在PDF輸出中。然而,出現腳註上標。我正在使用RStudio中的「編織」按鈕進行渲染。

如何獲得實際的腳註文本?

PDF輸出:

PDF output

我從.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) 
     } 
     ) 
``` 

.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} 

回答

0

一個簡單的修正在端部,通過試錯法找到。

爲了使腳註出現,需要使用longtable LaTeX軟件包和環境。我將它們添加到YAML標頭的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) 
    } 
) 
``` 

生成的表看起來是這樣的,用腳註在頁面的底部:

fixed table with footnotes