How to only show table caption once in “list of table” for a table split onto multiple pages

此生再无相见时 提交于 2019-12-04 19:08:46
CL.

This question needs to be answered in two parts:

  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?

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.

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

(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.)


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

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.

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}")
@

As requested, the LOF contains only one entry:


Full code:

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

Addendum

To address the additional question of how to rotate the column names:

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