2011-05-28 20 views
11

Quiero poner un comentario debajo de una tabla impresa por xtable. Pensé que la mejor opción sería usar la opción "título": xtable(tablename, caption="This is a caption"). Pero esto de alguna manera está poniendo en una "Tabla 1" automáticamente, de modo que la salida se vea como:R: subtítulo xtable (o comentario)

Tabla 1: Esto es un subtítulo.

¿Hay alguna forma de suprimir esta o cualquier otra forma más sencilla de poner un comentario simplemente como una última fila adicional en la tabla?

+0

código de salida xtable (LaTeX o html), por lo que debe significar que LaTeX está poniendo 'Tabla 1:' delante de su texto de leyenda. Este es el comportamiento normal de LaTeX; Sugiero buscar allí una solución. – joran

+0

¿Alguna idea de dónde buscar? – user702432

+1

Intenta buscar "suprimir etiqueta de título" en tex.stackexchange.com – joran

Respuesta

12

primer lugar, algunos datos de simulacro:

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

Ahora he aquí una solución un tanto torpe, utilizando print.xtable 's add.to.row argumento.

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) 

Si desea que su comentario que se presentarán los datos, utilice comment$pos[[1]] <- c(0) en lugar de comment$pos[[1]] <- c(nrow(z)) y ajustar hline.after en consecuencia.

Aquí está mi salida:

% 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

Desafortunadamente, esto pone todo el texto de los subtítulos en la primera columna, lo que crea una gran cantidad de espacio en blanco. – MichaelChirico

0

Esto es, básicamente, readapta this respuesta, pero esta es la forma más programática de hacer esto con xtable. Es feo, principalmente porque odio la forma en que funciona el argumento xtable de add.to.row.

datos

muestra:

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

Aquí está la salida 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} 

Y el resultado .pdf si yo envuelvo con \documentclass{article}, \begin{document} y \end{document}:

enter image description here

Por supuesto, hay muchas cosas más que agregar para que esté listo para la publicación, pero este es el punto crucial y debes estar en camino.

2

Si está utilizando RMarkdown, añadir esto a la cabecera:

--- 
(other configs here, like title, author, etc.) 

header-includes: 
    - \usepackage{caption} 
    - \captionsetup{labelformat=empty} 
--- 

Editar:

Después de hablar con el responsable del paquete xtable, David (que era muy accesible), vino con esta solución I publicar a continuación:

Creo que esto se puede resolver con xtableList. Cree algunos datos y convierta el marco de datos a 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") 

Entonces xtable produce lo siguiente:

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} 

Para hacer frente a las leyendas tiempo necesitará para dividir líneas:

attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines") 
Cuestiones relacionadas