Search code examples
rlatexknitrcaptionxtable

Is it possible to set fig.scap for table (instead for figure) genrated by a code chunk?


I have a table generated by knitr code chunk. The table is printed using xtable. Its caption is set inside the xtable function. This label is too long to be displayed in the list of tables in the beginning of the document. Therefore I want to set a short label which will be used instead the long label in this list.

For plots generated this label can be set by the chunk option fig.scap, but when I tried to use it for a table, the long label remains in list of tables.

I think I would be able to deal with this using \captionof{table}[short label]{long label} outside the code chunk, but it is not a very straightforward way and I'm afraid I would get problems to stuck table and its caption together. Is there some better way to do it?

The code (with artificial data):

\documentclass[a4paper,12pt, english]{article}

\usepackage{capt-of}

\begin{document}
\listoffigures
\listoftables
\section{Intoduction}
<<Chunk1, results="asis", echo=FALSE, fig.scap= "short caption - tab">>=
library(xtable)
print(xtable(head(iris), caption="long caption of the table"))
@

<<Chunk2, results="asis", echo=FALSE,fig.align="center", fig.cap = "Long caption of the figure", fig.scap= "short caption - fig">>=
plot(iris[,1:2])
@

\end{document}

Solution

  • To define a "short caption" to be used in the List of Figures, pass a character vector of length 2 to the caption argument of xtable. From ?xtable:

    caption: Character vector of length 1 or 2 containing the table's caption or title. If length is 2, the second item is the "short caption" used when LaTeX generates a "List of Tables"

    Example:

    \documentclass{article}
    \begin{document}
    \listoftables
    <<echo = FALSE, results = "asis">>=
    xtable::xtable(head(iris), caption = c("Long Caption", "Short"))
    @
    \end{document}