Using table caption on R markdown file using knitr to use in pandoc to convert to pdf

18,429

Solution 1

If you do not insist on using a LaTeX/HTML-only solution with the otherwise awesome xtable package, you might achieve the same with Pandoc's markdown. One option is to add the caption manually below the table, or use my R Pandoc writer package:

> library(pander)                         # load pkg
> panderOptions('table.split.table', Inf) # not to split table
> set.caption('Hello Fisher!')            # add caption
> pander(head(iris))                      # show (almost) any R object in markdown
-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
     5.1            3.5           1.4            0.2       setosa  

     4.9            3.0           1.4            0.2       setosa  

     4.7            3.2           1.3            0.2       setosa  

     4.6            3.1           1.5            0.2       setosa  

     5.0            3.6           1.4            0.2       setosa  

     5.4            3.9           1.7            0.4       setosa  
-------------------------------------------------------------------

Table: Hello Fisher!

Then use Pandoc to convert this markdown file to HTML, LaTeX, docx, odt or any other popular document formats.

Solution 2

You can insert tables with automatically numbered captions in markdown for processing with pandoc using straight knitr code. Insert this code snippet at the top of your .rmd file:

```{r setup, echo=FALSE}
tn = local({
  i = 0
  function(x) {
    i <<- i + 1
    paste('\n\n:Table ', i, ': ', x, sep = '')
    # The : before Table tells pandoc to wrap your caption in <caption></caption>
  }
})
knit_hooks$set(tab.cap = function(before, options, envir) {
  if(!before)
    tn(options$tab.cap)
})
default_output_hook = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  if (is.null(options$tab.cap) == F)  
    x
  else
    default_output_hook(x,options)
})
```

To insert a numbered table caption:

```{r myirischunk, tab.cap="This is the head of the Iris table"}
kable(head(iris))
```

By overriding the output hook and using tab.cap you don't need to clutter your chunk options with results='asis'.

Thanks Knitr!

PS: If you want to convert to latex/pdf you would probably want latex to number the tables for you. In that case you could change tn(options$tab.cap) to paste('\n\n:', options$tab.cap, sep='') - but I haven't tested this.

Solution 3

You can accomplish this with xtable. Add caption to xtable and comment=FALSE to the print function.

print(
  xtable(
    head(iris),
    caption = 'Iris data'
  ),
  comment = FALSE,
  type = 'latex'
)

See the xtable and print.xtable documentation.

Share:
18,429

Related videos on Youtube

Jd Baba
Author by

Jd Baba

Updated on September 15, 2022

Comments

  • Jd Baba
    Jd Baba over 1 year

    I am wondering if it is possible to use the table captions like figure captions using knitr in .Rmd file ?

    I saw options for figure caption but I couldn't see the option for the table caption. I also want to remove the message such as "% latex table generated in R 2.15.2 by xtable 1.7-0 package % Wed Mar 06 15:02:11 2013" .

    I used X table to create the table: The sample code I used is as follows:

    ```{r table2, results='asis', message=FALSE} 
    library(xtable) 
    print(xtable(head(iris))) 
    ``` 
    

    The table I got after processing through pandoc is as follows:

    enter image description here

    I tried to use message=FALSE in Rmd file to get rid of the message shown above. I also want to know if it is possible to automatically add the caption for table in Rmd ?

    By caption I mean something like below (this is for the figure) and the figure number is automatically updated.

    This output is a snapshot from the pdf generated by pdf using the markdown file created by knitr.

    enter image description here

    Thank you.

    • Yihui Xie
      Yihui Xie about 11 years
      @Jdbaba read the documentation ?print.xtable and see the comment argument
    • Maximilian
      Maximilian over 10 years
      So, here is the solution: print(xtable(yourtable, comment = getOption("xtable.comment", FALSE))
  • jessi
    jessi over 9 years
    I tried this with RStudio running rmarkdown_0.3.3; I did not need the : after \n\n. Because the : was printing. However, I cannot figure out how to get the caption BEFORE (ABOVE) the table. How could I revise the hook to make that work? For figures, I can do it with knit_hooks$set(plot = function(x, options) { paste('<figure><figcaption>', options$fig.cap, '</figcaption><img src="', opts_knit$get('base.url'), paste(x, collapse = '.'), '"></figure>', sep = '')
  • Aleksandr Blekh
    Aleksandr Blekh over 9 years
    I'm trying to use your nice pander package, but couldn't find an option to control the position of a table's caption (by default, the position is below the table, but I need above). I looked through documentation and on the Web, but to no avail. Any advice? (I understand that I can produce caption independently as R Markdown text, but then there is an issue of margin between the caption and the table. It would be nice to be able to specify both the position (above/below) and the margin.)
  • daroczig
    daroczig over 9 years
    @AleksandrBlekh having the caption above the table is not a valid markdown syntax, it should stay below the table. On the other hand, you can render that to be above the table/image in e.g. HTML or LaTeX. For the prior, use JavaScript, for the latter, I suggest the caption and floatrow LaTeX packages, like \floatsetup[table]{capposition=top}
  • Aleksandr Blekh
    Aleksandr Blekh over 9 years
    Thanks for fast reply and nice suggestions! One of the reasons I'm trying to use pander and R Markdown format is to be able to produce multi-format output, as needed. Going the format-specific route is not optimal in this regard (no single codebase); I actually implemented some stuff in a LaTeX-specific way and it took a while just for that. Can't imagine spending more time to support HMTL, etc. Actually, I found a solution: I need pandoc 1.13+, which contains this fix (caption should be on top): search for "table captions above tables" here: johnmacfarlane.net/pandoc/releases.html.
  • Aleksandr Blekh
    Aleksandr Blekh over 9 years
    I can't find pandoc binary for that release or higher for Ubuntu/Debian anywhere. I think nobody has it yet. I've read on some mailing list that even Yihui can't find it. If you will find it, please let me know.
  • daroczig
    daroczig over 9 years
    @AleksandrBlekh This fix sounds great, but it seems to be LaTeX-only to me. RStudio has released some binaries: s3.amazonaws.com/rstudio-buildtools/pandoc-1.13.1.zip Anyway, you can still use markdown with multiple output formats and "captions above tables/images", but this way you have to develop your custom templates for HTML, PDF etc. This is a one-time investment, that you can use for all future reports, so besides the above mentioned one-liner LaTeX call, you'd need also another one-liner JavaScript call in your custom HTML template to move caption above the objects.
  • Aleksandr Blekh
    Aleksandr Blekh over 9 years
    I appreciate your comments and link to the binary. I'm not sure I understand the need for developing templates (unless you meant that in context of using older versions of pandoc without the fix). If I don't need something fancy, aren't the default ones good enough? Sorry, if my question sounds stupid, but I'm not yet fluent in under-the-hood complexities of reproducible research tools (but willing and doing my best to learn as much as I can). P.S. Why do you think that fix is LaTeX-only?
  • daroczig
    daroczig over 9 years
    @AleksandrBlekh the changelog you linked above has the quote part below the "LaTeX writer" section, so I think this update has nothing to do with the other output formats. Also, I think the markdown specification of captions (to have it below the table) has not changed either. About "templates": probably I should have written "stylesheets" instead, see e.g. --template and -H at johnmacfarlane.net/pandoc/README.html#general-writer-options In short: you can override the bundled design of Pandoc created documents.
  • Aleksandr Blekh
    Aleksandr Blekh over 9 years
    Thank you for clarification. Will review the info.
  • Tony Ladson
    Tony Ladson about 9 years
    I changed: paste('\n\n:Table ', i, ': ', x, sep = '') to paste('\n\n<caption>Table ', i, ': ', x, sep = '','</caption><p></p>') to add space after the caption, remove the : which was printed, and retain the caption formatting
  • mavericks
    mavericks almost 4 years
    @DeanK: any recommendations on how this would look like if I want to add a short version of the caption to the TOC, i.e. tab.scap? See also github.com/yihui/knitr/issues/1679