Search code examples
rr-markdownbookdown

Label a figure as a table in book down


I have a table that I've designed in Excel and exported as an image, and now I want to include it into my R Markdown document. I would like to treat it similarly to a table generated with knit::kable, in that the caption should sit above the table and the numbering should be part of the table numbering, not the figure numbering. Is there any way to achieve this? I would then also like the be able to reference it, although I don't care if the reference format is @ref(tab:mylabel) or @ref(fig:mylabel)


Solution

  • Have a look here about the tables https://bookdown.org/yihui/bookdown/tables.html

    You can use any types of Markdown tables in your document. To be able to cross-reference a Markdown table, it must have a labeled caption of the form Table: (#label) Caption here, where label must have the prefix tab:, e.g., tab:simple-table.

    So it might be an option, to put the image in a markdown table (that just contains the image and nothing else).

    Like this:

    |  <!-- -->   | 
    | :------------ | 
    | ![image](figure/test.jpg)    |
    Table: (\#tab:label1) My table
    

    The last line manages, that it gets indexed correctly.

    But honestly, I would myself would convert the Excel table to a data.frame and use kable/kableExtra ...should be quite easy and probably saves trouble in the long run.