如何在“表格列表"中仅显示一次表格标题用于将表格拆分为多个页面 [英] How to only show table caption once in "list of table" for a table split onto multiple pages

查看:134
本文介绍了如何在“表格列表"中仅显示一次表格标题用于将表格拆分为多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R包(xtableknitr)和Latex包(longtablehyperref)来准备文档.

I am using R packages (xtable and knitr) and Latex packages (longtable and hyperref) to prepare a document.

我的一张桌子很长,分成多页.原来,表列表"显示了该表出现的每个页码,但是所有超链接都将我带到了该表的开头.

One of my tables is very long and splits onto multiple pages. It turned out that the "List of Table" shows every page number at which this table appears, but all hyperlinks bring me to the beginning of this table.

我的问题是,在表格列表"中,如何显示该表格出现的第一个页码.

My question is, in "List of Table" how can I just show the first page number at which this table appears.

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

推荐答案

此问题需要分两部分回答:

This question needs to be answered in two parts:

  1. 需要哪些LaTeX代码在LOF(图中的列表)中仅包含表的第一部分?
  2. 如何使xtable生成该代码?
  1. Which LaTeX code is required to include only the first part of the table in the LOF (List of Figures)?
  2. How to make xtable generate that code?


第一部分已经在 tex.stackexchange上给出了答案:如何使用仅包含以下内容的长表项:表格.归结为在表的第一个标头中使用\caption{…},在随后的标头中使用\caption*{…}.


The first part already has an answer on tex.stackexchange: How to use a longtable with only one entry in the list of tables. It boils down to use \caption{…} in the first header of the table and \caption*{…} in the following headers.

包括问题的页脚,所需的LaTeX如下所示:

Including the footer from the question, the required LaTeX looks like this:

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

(请注意,在\endlastfoot之后的ID & LAB也可以进入第一个标头部分,但是使用xtable易于生成上面的结构.)

(Note that the ID & LAB after \endlastfoot could also go into the first header part, but the structure above is easier to generate using xtable.)

第二部分比较棘手.默认情况下,xtable在表头中包含\caption命令.使用print.xtableadd.to.row选项,我们可以在表主体的前面添加内容,但是我们不能在\caption命令之前添加内容(据我所知).

The second part is a little bit more tricky. By default, xtable includes a \caption command in the table header. Using the add.to.row option of print.xtable, we can add content in front of the table body, but we cannot add content before the \caption command (as far as I know).

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

Therefore, in order to achieve the structure shown above, we suppress as much auto-generated LaTeX code as possible and add the correct table header manually.

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

This can by done with the option only.contents of print.xtable. All arguments concerning metadata of the table (latex.environment, floating and so on) become obsolete because we write the table header on our own:

<<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仅包含一个条目:

As requested, the LOF contains only one entry:

完整代码:

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

附录

要解决

  • 在前言中添加\usepackage{rotating}.
  • 使用paste(paste("\\begin{sideways}", colnames(dTab), "\\end{sideways}"), collapse = " & ")代替paste(colnames(dTab), collapse = " & ").
  • rotate.colnames = TRUE添加到print.xtable调用中.
    • Add \usepackage{rotating} to the preamble.
    • Use paste(paste("\\begin{sideways}", colnames(dTab), "\\end{sideways}"), collapse = " & ") instead of paste(colnames(dTab), collapse = " & ").
    • Add rotate.colnames = TRUE to the print.xtable call.

    这篇关于如何在“表格列表"中仅显示一次表格标题用于将表格拆分为多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆