[Tex/LaTex] Selectively suppress caption numbering when using xtable

captionsrsweavetablesxtable

I have a chunk of code which is producing table output using Xtable and I would like to suppress the table numbering. I know I can suppress numbering using \caption* outside of Xtable but I'm stumped as to how to use \caption* or it's equivalent in Xtable.

I'm afraid I'm only a few days in to using KnitR, Sweave and LaTeX so forgive my ignorance and thanks for the assistance. The working example is below, I'd like to remove the Table 1.

disclaimer – the code below is a modification of http://christophj.github.io/replicating/r/how-to-produce-nice-tables-in-pdfs-using-knitr-sweave-and-r/

edit: I suppose I could use \captionsetup[table]{labelformat=empty} in the preamble as in this document I don't think I will be numbering any tables, using \captionsetup within the specific table environment would be preferable though.

\documentclass[draft]{article}
\usepackage{booktabs}

\begin{document}

<<results='asis', echo=FALSE>>=
library(xtable)
strCaption<- paste0("Sample table")
df<-data.frame(Area=1:5, n=seq(0,50, length.out=5))
print(xtable(df, digits=1, caption=strCaption, label="Test_table"),
      size="footnotesize",
      include.rownames=FALSE,
      include.colnames = FALSE,
      caption.placement = "top",
      hline.after=NULL,
      add.to.row = list(pos=list(-1,
                                 nrow(df)),
                        command = c(paste("\\toprule \n",
                                          "Stratum & Sets \\\\\n",
                                          "\\midrule \n"),
                                    "\\bottomrule \n")
                        )
      )

@


\end{document}

Best Answer

Ok, sorry to have asked this. It did take me a fair amount of searching to figure this out though (as I mentioned I'm just learning!) so hopefully this answer will be useful to someone else.

The caption package provides an answer to my question.

Using \captionsetup{labelformat=empty} before the table and captionsetup{lableformat=default} after the table seems to allow me to selectively remove table numbering from the caption.

I see I can also set this option right in the \usepackage declaration as so: \usepackage[labelformat=empty]{caption}.

A working example is below:

\documentclass[draft]{article}
\usepackage{booktabs}
\usepackage[font=small, justification=justified, singlelinecheck=false]{caption}

\begin{document}

\captionsetup{labelformat=empty}
<<echo=FALSE, results='asis'>>=
library(xtable)
strCaption<- paste0("Sample table")
df<-data.frame(Area=1:5, n=seq(0,50, length.out=5))
print(xtable(df, digits=1, caption=strCaption, label="Test_table"),
      latex.environments = "flushleft",
      size="footnotesize",
      include.rownames=FALSE,
      include.colnames = FALSE,
      caption.placement = "top",
      hline.after=NULL,
      add.to.row = list(pos=list(-1,
                                 nrow(df)),
                        command = c(paste("\\toprule \n",
                                          "Stratum & Sets \\\\\n",
                                          "\\midrule \n"),
                                    "\\bottomrule \n")
                        )
      )

@

\captionsetup{labelformat=default}
<<echo=FALSE, results='asis'>>=
print(xtable(df, digits=1, caption=strCaption, label="Test_table2"),
      latex.environments = "flushleft",
      size="footnotesize",
      include.rownames=FALSE,
      include.colnames = FALSE,
      caption.placement = "top",
      hline.after=NULL,
      add.to.row = list(pos=list(-1,
                                 nrow(df)),
                        command = c(paste("\\toprule \n",
                                          "Stratum & Sets \\\\\n",
                                          "\\midrule \n"),
                                    "\\bottomrule \n")
                        )
      )
@

\end{document}

Thanks for looking though!