xtable.
私は、「キャプション」オプションを使用するのが最良の選択肢であると考えた によって印刷された表の下にコメントを入れたい: xtable(tablename, caption="This is a caption")
. しかし、これはどういうわけか自動的に「表 1」を入れているため、出力は次のようになります。
表 1: これはキャプションです。
これを抑制する方法、または単にテーブルの最後の行としてコメントを追加する簡単な方法はありますか?
まず、いくつかのモック データ:
x <- sample(LETTERS, 5, replace = TRUE)
y <- sample(LETTERS, 5, replace = TRUE)
z <- table(x, y)
ここで、print.xtable
のadd.to.row
引数を使用したやや不器用なソリューションを示します。
comment <- list()
comment$pos <- list()
comment$pos[[1]] <- c(nrow(z))
comment$command <- c(paste("\\hline \n", # we`ll replace all default hlines with this and the ones below
"your footnote, caption or whatever. \n",
sep = ""))
print(xtable(z),
add.to.row = comment,
hline.after = c(-1, 0)) # indicates rows that will contain hlines (the last one was defined up there)
コメントをデータの前に配置する場合は、comment$pos[[1]] <- c(0)
代わりに使用し、それに応じcomment$pos[[1]] <- c(nrow(z))
て調整しhline.after
ます。
ここに私の出力があります:
% latex table generated in R 2.14.1 by xtable 1.7-0 package
% Mon Feb 20 02:17:58 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& B & C & P & V \\
\hline
A & 0 & 0 & 0 & 1 \\
D & 1 & 0 & 0 & 0 \\
I & 0 & 0 & 0 & 1 \\
P & 0 & 0 & 1 & 0 \\
Z & 0 & 1 & 0 & 0 \\
\hline
your footnote, caption or whatever.
\end{tabular}
\end{center}
\end{table}
これは基本的にこの回答を転用していますが、これは でこれを行う最もプログラム的な方法xtable
です。醜いのは、主にxtable
のadd.to.row
引数の動作が嫌いだからです。
サンプルデータ:
set.seed(230)
DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))
#of course, we can pass this directly below; I'm just saving
# horizontal space for this answer
comm <- paste0("\\hline \n \\multicolumn{4}{l}",
"{\\scriptsize{Check out these random numbers!}} \n")
print.xtable(xtable(DF, caption = "Describe the table"),
#adjusting hline.after so that our comment appears
# "outside" the table, as defined by its border
hline.after=c(-1, 0),
#**NOTE: the first argument to add.to.row must be
# a list -- don't ask me why since it strikes me as odd**
add.to.row = list(pos = list(5),
command = comm))
TeX出力は次のとおりです。
% latex table generated in R 3.2.4 by xtable 1.8-2 package
% Mon May 23 18:25:14 2016
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
\hline
& a & b & c \\
\hline
1 & -0.23 & 0.04 & 1.34 \\
2 & 0.10 & 0.57 & -1.62 \\
3 & 0.33 & -0.14 & 0.83 \\
4 & 0.36 & -0.75 & 0.20 \\
5 & 0.44 & 0.13 & -0.49 \\
\hline
\multicolumn{4}{l}{\scriptsize{Check out these random numbers!}}
\end{tabular}
\caption{Describe the table}
\end{table}
そして、 、 、および でラップすると、.pdf の結果が得\documentclass{article}
られ\begin{document}
ます\end{document}
。
もちろん、出版の準備が整うまでには、さらに多くの機能を追加する必要がありますが、これが核心であり、順調に進んでいるはずです。
RMarkdown を使用している場合は、これをヘッダーに追加します。
---
(other configs here, like title, author, etc.)
header-includes:
- \usepackage{caption}
- \captionsetup{labelformat=empty}
---
編集:
xtable パッケージのメンテナーである David (非常にアクセスしやすかった) と話をした後、彼は私が以下に投稿するこのソリューションにたどり着きました。
これは xtableList で解決できると思います。データを作成し、データ フレームを xtableList に変換します。
set.seed(230)
DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))
library(xtable)
dfList <- list(DF)
attr(dfList, "message") <- c("A caption", "Which can have multiple lines")
次に、xtable は以下を生成します。
print(xtableList(dfList))
## % latex table generated in R 3.2.5 by xtable 1.8-3 package
## % Sat Jul 09 21:52:53 2016
## \begin{table}[ht]
## \centering
## \begin{tabular}{rrrr}
## \hline
## & a & b & c \\
## \hline
## 1 & -0.23 & 0.04 & 1.34 \\
## 2 & 0.10 & 0.57 & -1.62 \\
## 3 & 0.33 & -0.14 & 0.83 \\
## 4 & 0.36 & -0.75 & 0.20 \\
## 5 & 0.44 & 0.13 & -0.49 \\
## \hline
## \multicolumn{4}{l}{A caption}\\
##
## \multicolumn{4}{l}{Which can have multiple lines}\\
## \end{tabular}
## \end{table}
長いキャプションを処理するには、行を分割する必要があります。
attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines")