2015-10-15 38 views
1

如何只在一个表拆分“表的列表”显示表标题一次到我使用R封装(<code>xtable</code>和<code>knitr</code>)和乳胶包(<code>longtable</code>和<code>hyperref</code>)准备一份文件,多页

我的一张桌子非常长,分成多个页面。事实证明,“表格列表”显示了该表格出现的每个页面编号,但是所有的超链接都将我带到了这张表格的开头。

我的问题是,在“表格列表”中,我怎样才能显示此表格出现的第一个页码。

\documentclass{article} 
\usepackage{longtable} 
\usepackage{hyperref} 

<<setup, include=FALSE, cache=FALSE>>= 
library(knitr) 
library(xtable) 
@ 

\begin{document} 
\listoftables 
\newpage 

<<echo=FALSE,results='asis'>>= 
## some customerized settings for "longtable" 
addtorow   <- list() 
addtorow$pos  <- list() 
addtorow$pos[[1]] <- c(0) 
addtorow$command <- c(paste("\\hline \n", 
          "\\endhead \n", 
          "\\hline \n", 
          "{\\footnotesize Continued on next page} \n", 
          "\\endfoot \n", 
          "\\endlastfoot \n",sep="")) 
## create a long table 
d <- data.frame(ID=rep(1:300), LAB=rnorm(300)) 

## execute "xtable" 
dTab <- xtable(d, caption="This is Table 1") 

print(dTab,  
     tabular.environment = "longtable", 
     floating = FALSE, 
     include.colnames = TRUE, 
     include.rownames = FALSE, #addtorow substitute default row names 
     add.to.row = addtorow, # make the substitution here 
     hline.after=c(-1),  # addtorow substitute default hline for first row 
     caption.placement="top" 
) 
@ 

\end{document} 

回答

2

这个问题需要分两部分来回答:

  1. 哪个LaTeX的代码需要包括在LOF(图列表)表中只有第一部分?
  2. 如何让xtable生成该代码?

,第一部分已经对tex.stackexchange: How to use a longtable with only one entry in the list of tables答案。它归结为在表格的第一个标题中使用\caption{…},在以下标题中使用\caption*{…}

包括问题的页脚,所需的乳胶看起来是这样的:

\begin{longtable}{rr} 
    \caption{This is Table 1} \\ \hline 
    \endfirsthead 
    \caption*{This is Table 1} \\ \hline 
    ID & LAB \\ 
    \hline 
    \endhead 
    \hline 
    {\footnotesize Continued on next page} 
    \endfoot 
    \endlastfoot 
ID & LAB \\ 
\hline 
1 & 1.08 \\ 
2 & -0.99 \\ 
3 & 1.64 \\ 

(注意:ID & LAB\endlastfoot也可以进入第一头部分,但上述结构是更容易产生使用xtable。)


第二部分有点棘手。默认情况下,xtable在表头中包含一个\caption命令。使用add.to.row选项print.xtable,我们可以在表格正文前添加内容,但我们无法在\caption命令之前添加内容(据我所知)。

因此,为了实现上面显示的结构,我们尽可能多地抑制自动生成的LaTeX代码并手动添加正确的表头。

这可以通过print.xtable的选项only.contents完成。有关表(latex.environmentfloating等)的元数据,所有参数都已经过时,因为我们写我们自己的表头:

<<echo=FALSE, results='asis'>>= 

    ## create a long table 
    d <- data.frame(ID=rep(1:300), LAB=rnorm(300)) 

    ## execute "xtable" 
    dTab <- xtable(d) 

    cat(sprintf(" 
    \\begin{longtable}{rr} 
    \\caption{%1$s} \\\\ \\hline 
    \\endfirsthead 
    \\caption*{%s} \\\\ \\hline 
    %2$s \\\\ 
    \\hline 
    \\endhead 
    \\hline 
    {\\footnotesize %3$s} 
    \\endfoot 
    \\endlastfoot", 
    "This is Table 1", 
    paste(colnames(dTab), collapse = " & "), 
    "Continued on next page")) 

    print(dTab, 
     include.colnames = TRUE, 
     include.rownames = FALSE, 
     only.contents = TRUE 
) 

    cat("\\end{longtable}") 
@ 

按照要求,LOF的内容只有一条:

Output


全码:

\documentclass{article} 
\usepackage{longtable} 
\usepackage{hyperref} 

<<setup, include=FALSE, cache=FALSE>>= 
    library(knitr) 
    library(xtable) 
@ 

\begin{document} 
\listoftables 

<<echo=FALSE, results='asis'>>= 

    ## create a long table 
    d <- data.frame(ID=rep(1:300), LAB=rnorm(300)) 

    ## execute "xtable" 
    dTab <- xtable(d) 

    cat(sprintf(" 
    \\begin{longtable}{rr} 
    \\caption{%1$s} \\\\ \\hline 
    \\endfirsthead 
    \\caption*{%s} \\\\ \\hline 
    %2$s \\\\ 
    \\hline 
    \\endhead 
    \\hline 
    {\\footnotesize %3$s} 
    \\endfoot 
    \\endlastfoot", 
    "This is Table 1", 
    paste(colnames(dTab), collapse = " & "), 
    "Continued on next page")) 

    print(dTab, 
     include.colnames = TRUE, 
     include.rownames = FALSE, 
     only.contents = TRUE 
) 

    cat("\\end{longtable}") 
@ 

\end{document} 

附录

为了解决如何旋转列名additional question

  • 添加\usepackage{rotating}的序言。
  • 使用paste(paste("\\begin{sideways}", colnames(dTab), "\\end{sideways}"), collapse = " & ")而不是paste(colnames(dTab), collapse = " & ")
  • rotate.colnames = TRUE添加到print.xtable调用。
+0

你给了我一个非常彻底的解释下引擎盖机制!我可能违反了这里的论坛规则,但我一直在为一个扩展到多个页面的图形类似的困难感到困扰。查看我在此[链接]中发布的问题(http://tex.stackexchange.com/questions/191487/sub-figures-in-multiple-pages-but-with-one-figure-caption-and-showing-continued )。你是否能够阐明它呢?我在Latex中的技能还为时过早。 – Yaming

+0

半年后我又回到了这个问题。假设我想旋转表中的列名,然后在'print(dTab)'中添加'rotate.colnames = TRUE'作为额外的控件将不起作用。我怎样才能做到这一点? – Yaming

+0

@Yaming我在答案的最底部添加了一小段。 –

相关问题