Closed mbojan closed 7 years ago
It will be nice if you can provide a minimal example of what you tried exactly so I don't have to guess. Anyway, I think if your table caption is of this form <caption>(#tab:label)
, \\@ref(label)
should work.
Yes, sorry about the lack of example. The original code would be overwhelming with details so I started to distill a simple example. While doing that I was able to get it to work :D
Thanks for the suggestion! I was mislead by the \
in the Markdown code above.
Here is what works for me (essentially):
The code
library(magrittr)
test_table <- function(caption, label) {
require(magrittr)
m <- matrix(1:2, 4, 3)
lab <- paste0("(#tab:", label, ")")
out <- c(
"<table style='width: 50%;'>",
paste0("<caption>", lab, caption, "</caption>"),
structure( paste0("<td>", m, "</td>"), dim=dim(m) ) %>%
apply(1, paste, collapse="") %>%
paste0("<tr>", ., "</tr>"),
"</table>"
)
cat(out, sep="\n")
}
and later in Rmarkdown
Blah blah
```{r tableone, results="asis"}
test_table("This is a table", label="tableone")
```
See \@ref(tab:tableone).
Okay, great!
Well, in fact, I'm still fighting with it. I am trying to hack htmlTable
. See this:
tab <- withr::with_options(
list(htmlTableCompat="html"),
htmlTable::htmlTable(matrix(1:12, 4 ,3), caption="(#tab:tabletwo) A table")
)
cat(unclass(tab))
gives the following HTML:
<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<caption style='caption-side: top'>
(#tab:tabletwo) A table</caption>
<thead>
</thead>
<tbody>
<tr>
<td style='text-align: center;'>1</td>
<td style='text-align: center;'>5</td>
<td style='text-align: center;'>9</td>
</tr>
<tr>
<td style='text-align: center;'>2</td>
<td style='text-align: center;'>6</td>
<td style='text-align: center;'>10</td>
</tr>
<tr>
<td style='text-align: center;'>3</td>
<td style='text-align: center;'>7</td>
<td style='text-align: center;'>11</td>
</tr>
<tr>
<td style='border-bottom: 2px solid grey; text-align: center;'>4</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>8</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>12</td>
</tr>
</tbody>
</table>
When run in RMarkdown \@ref(tab:tabletwo)
seems to give correct hyperlinked reference with number, but the table caption does not work and in browser looks like (#tab:tabletwo) A table
.
Any ideas?
I did a bit of debugging...
It seems that the caption fails to be updated because the regexp in parse_fig_labels
(https://github.com/rstudio/bookdown/blob/4a8a9f5d7955740ab701afd3213ff78eb4f1cbd7/R/html.R#L521) is looking for "^<caption>"
so assumes that there are no tag attributes. This fails in my code because the <caption>
tag has attributes.
Am I correct that the search is done only to check if the (#tab:label)
piece is mentioned within HTML <caption>
?
Perhaps the regexp could just catch "^<caption"
?
I forked, tried, and it fixes the problem. I'm not 100% sure if it does not break anything, but so far so good.
Want a PR?
I don't know if it is going to break anything, either, but I have merged it anyway. If I discover problems in the future, I may revert it. Thanks!
Hi Yihui,
I have a similar issue. Referencing seems to work for html and word but not pdf. Do you know where things might be wrong ?
Thanks
Caroline
---
title: "Test"
output:
bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,warning=FALSE,message=FALSE)
This is a reference to Table \@ref(tab:tableone).
cat("<caption> Table (\\#tab:tableone) A Caption </caption>")
HI, @yihui and @mbojan I have a problem similar to Caroline.
When I use \cite{} in my table in bookdown::pdf_book: like
\begin{tabular}{|l|l|l|l|} \hline reference & Charactersitic 1 & Charactersitic 2 & Charactersitic 3 \ \hline \cite{stefc2009partII} & method xx & method xy & method xz \ \hline \cite{stefc2009partII} & method yx & method yy & method yz \ \hline \end{tabular}
it works only for :word_document2: but not for bookdown::pdf_book:
I also tried with \@cite{stefc2009partII} ; \cite[@stefc2009partII]}; \cite[@stefc2009partII]} ; \@cite(stefc2009partII) no one works...
I'm very new to LaTeX, Rmd ,and bookdown, I'm using those for writing down my PhD Thesis.
Could you help me, please?
.bib @article{stefc2009partII, title={Report of the SGMED-09-03 Working Group on the Mediterranean Part II}, author={Massimiliano Cardinale, Anna Cheilari, Hans-Joachim Rätz}, journal={EUR - Scientific and Technical Research Reports (STEFC) }, year={2009}, publisher={JRC} }
Hi @andreapierucci Can you open a new issue following the issue guide https://yihui.org/issue/ ?
Or ask in one of the QA site before if not already done in case you are not sure this is an issue ? Thanks!
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.
Hi!
I wrote some code that generates LaTeX or HTML code for custom-made tables. I would very much like to take advantage of
\@ref
erencing syntax ofbookdown
.It was fairly simple with LaTeX as I had to simply throw in
\label{}
somewhere in the output.I am trying to understand how does it work with HTML output. In the
md
file generated withbookdown::html_document2
a table produced withknitr::kable
inserts something likeI tried inserting
<caption>
tags with the similar structure as the corresponding HTML output frombookdown::html_document2
but it does not work. I am not sure at which stage of document processingbookdown
resolves the references.Any suggestions how could I make it work?