【问题标题】:R: xtable caption (or comment)R:xtable 标题(或评论)
【发布时间】:2011-05-28 19:07:42
【问题描述】:

我想在xtable. 打印的表格下发表评论,我认为最好的选择是使用“标题”选项:xtable(tablename, caption="This is a caption")。但这不知何故会自动放入“表 1”,因此输出如下所示:

表 1:这是一个标题。

有没有什么方法可以抑制这种情况,或者有任何更简单的方法可以简单地将注释作为表格中的最后一行添加?

【问题讨论】:

  • xtable 输出代码(LaTeX 或 html),因此您必须表示 LaTeX 将“表 1:”放在标题文本前面。这是正常的 LaTeX 行为;我建议在那里寻找解决方案。
  • 尝试在 tex.stackexchange.com 上搜索“抑制标题标签”
  • 这个帖子应该对你有所帮助:tex.stackexchange.com/questions/15282/…
  • 这个问题似乎没有得到更多答案,以下任何一个解决了您的问题吗?如果是这样,您可以将其选择为接受,这将提高您在 SO 上的声誉。

标签: r xtable


【解决方案1】:

首先,一些模拟数据:

x <- sample(LETTERS, 5, replace = TRUE)
y <- sample(LETTERS, 5, replace = TRUE)
z <- table(x, y)

现在这是一个有点笨拙的解决方案,使用 print.xtableadd.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]] &lt;- c(0) 而不是 comment$pos[[1]] &lt;- 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}

【讨论】:

  • 不幸的是,这会将所有标题文本放在第一列,这会产生很多空白。
【解决方案2】:

这基本上是重新利用this 答案,但这是使用xtable 执行此操作的最程序化的方式。这很丑,主要是因为我讨厌xtableadd.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}

如果我用\documentclass{article}\begin{document}\end{document} 包装它,.pdf 结果:

当然,要让它为出版做好准备,还有更多花里胡哨的东西要添加,但这是关键,你应该一切顺利。

【讨论】:

    【解决方案3】:

    如果您使用的是 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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2020-01-21
      • 1970-01-01
      • 2012-04-29
      相关资源
      最近更新 更多