1

I am trying to include a table in an Rmarkdown document, which is going to be rendered both in PDF and in HTML. I would like the table to float in the PDF version, and have a running number and a caption, and be linkable using \@ref.

Everything I have found in the interwebs is about how to produce a dynamic table based on some structure, using knitr and variants. I don't want that. I want a static, hardcoded table, and I have not been able to get this to work.

As an example, I have the following in my file tabletest.Rmd:

---
output: pdf_document
---

Lorem ipsum

|             | Covered   | Not covered   |
| :---------- | :-------: | ------------: |
| Observed    | $k$       | $n-k$         |
| Expected    | $qn$      | $(1-q)n$      |

See Table X.

This is roughly the output I would like in PDF:

desired output

I created the desired output by LaTeXing a file with these contents:

\documentclass{article}
\begin{document}
Lorem ipsum

\begin{table}
  \begin{tabular}{|c|c|c|}
    & Covered & Not covered \\ \hline
    Observed & $k$ & $n-k$ \\
    Expected & $qn$ & $(1-q)n$
  \end{tabular}
  \caption{A table}\label{ref: table}
\end{table}
See Table \ref{ref: table}.
\end{document}
Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48

1 Answers1

1

You have to use bookdown::pdf_document2, rather than (rmarkdown::)pdf_document, to use cross-references within a document, as explained in 4.7 Cross-referencing within documents of R Markdown Cookbook. You can make a cross-reference to a table by inserting (\#tab:label) to the table caption and by refering the table using \@ref(tab:label). See also the second paragraph from the last in 2.5 Tables of bookdown: Authoring Books and Technical Documents with R Markdown.

---
output:
  bookdown::pdf_document2: default
---

Lorem ipsum

|             | Covered   | Not covered   |
| :---------- | :-------: | ------------: |
| Observed    | $k$       | $n-k$         |
| Expected    | $qn$      | $(1-q)n$      |

Table: (\#tab:ref2tab-first) The caption for the first table


See Table \@ref(tab:ref2tab-first)
and compare it with Table \@ref(tab:ref2tab-second).

|             | purchased   | Not purchased   |
| :---------- | :---------: | :-------------: |
| food        | sausage     | bread           |
| book        | bookdown    | cookbook        |

Table: (\#tab:ref2tab-second) The caption for the second table
Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • I should have specified that I am compiling using `bookdown::render_book("index.Rmd")` and `bookdown::render_book("index.Rmd", bookdown::pdf_book(toc = TRUE))`. Funnily enough, your proposal works for me in PDF with both `pdf_book` as-is and with `pdf_document2`, yielding a floating table with a linkable number and a caption... But the HTML version does not have a number for the table and only gives "Table #{tab:ref2tab-first}" in front of the caption. Any thoughts on how to make this work for the html version, too? – Stephan Kolassa Jan 08 '23 at 15:52
  • Please accept my answer if it works for solving your original LaTeX problem. For the HTML issue, have you tried `bookdown::html_document2`, not `(rmarkdown::)html_document`? In my environment, `bookdown::html_document2` makes correct cross-references with numbering. – Carlos Luis Rivera Jan 09 '23 at 04:49
  • @StephanKolassa What is your version of Pandoc, knitr, and rmarkdown? Could you check the versions by running `rmarkdown::pandoc_version(); packageVersion("knitr"); packageVersion("rmarkdown")`? I have Pandoc 2.19.2, knitr 1.39, and rmarkdown 2.14. – Carlos Luis Rivera Jan 09 '23 at 04:52
  • 1
    Actually, it turns out your proposed change did already result in a html just as I wanted. There was some confusion on my part due to coauthors renaming files and me looking at outdated versions... apologies, and thank you! – Stephan Kolassa Jan 09 '23 at 07:56