3

R パッケージ (xtableおよびknitr) と Latex パッケージ (longtableおよびhyperref) を使用してドキュメントを準備しています。

テーブルの 1 つが非常に長く、複数のページに分割されています。「表のリスト」には、この表が表示されるすべてのページ番号が表示されますが、すべてのハイパーリンクでこの表の先頭に移動することがわかりました。

私の質問は、「テーブルのリスト」で、このテーブルが表示される最初のページ番号を表示するにはどうすればよいかということです。

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

1 に答える 1

4

この質問には、次の 2 つの部分で回答する必要があります。

  1. 表の最初の部分のみを LOF (図のリスト) に含めるために必要な LaTeX コードはどれですか?
  2. xtableそのコードを生成する方法は?

最初の部分には、tex.stackexchange: How to use a longtable with only one entry in the list of tablesに関する回答が既にあります。要約する\caption{…}と、テーブルの最初のヘッダーと次のヘッダーで使用され\caption*{…}ます。

質問のフッターを含めると、必要な LaTeX は次のようになります。

\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 & LABafter\endlastfootは最初のヘッダー部分にも入る可能性がありますが、上記の構造は を使用して生成する方が簡単であることに注意してくださいxtable。)


2 番目の部分はもう少しトリッキーです。デフォルトでは、テーブル ヘッダーにコマンドがxtable含まれます。のオプションを\caption使用すると、テーブル本体の前にコンテンツを追加できますが、コマンドの前にコンテンツを追加することはできません(私の知る限り)。add.to.rowprint.xtable\caption

したがって、上記の構造を実現するために、自動生成された LaTeX コードを可能な限り抑制し、正しいテーブル ヘッダーを手動で追加します。

これは、 のオプションonly.contentsで実行できますprint.xtable。テーブルのメタデータに関するすべての引数 (latex.environmentなどfloating) は、独自にテーブル ヘッダーを記述するため、廃止されます。

<<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 には 1 つのエントリのみが含まれます。

出力


完全なコード:

\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 = TRUEprint.xtableます。
于 2015-10-16T09:06:41.037 に答える