1

I am working on a Rmarkdown book with the package bookdown. At some point, I am using a R chunk to create some HTML code (with the option "asis" in the chunk header). In that HTML, I would like to add a reference to a chunk in an other part of the book. Note that this part is created in a separated html file by pandoc.

The function generating the HTML works well and the HTML is rendered as expected, but the references added with @ref(chunkid) are not converted to links. They are just plain text in the HTML.

I tried to add the function cat to print the HTML code, but if I do so, the HTML is not rendered and is shown as plain text.

This is what I have currently:

```{r results = 'asis'}
"<h2>An html title</h2>
<div>\\@ref(titi22)</div>
"
```\

This will render the expected HTML when the book is knitted but the link in the div is not created. @ref(titi22) is written in the div.

On the real case, this is what I get The real case

If I add cat:

```{r results = 'asis'}
cat("<h2>An html title</h2>
<div>\\@ref(titi22)</div>
")
```\

The HTML is not rendered nor the reference.

How can I render the reference and the HTML?

Thank you

2 Answers2

1

In the bookdown:

output:
  bookdown::html_document2:

... it works right:

<h2>An html title</h2>
<div> Here is my first table: \@ref(tab:tab1) <div>

Tab:

```{r tab1}

#any tab

```
manro
  • 3,529
  • 2
  • 9
  • 22
  • Thank you for your answer, I tried to switch from gitbook to html2 in the yml. It changes a lot the style of the document and does not solve the issue. The cross reference is not formatted when the HTML code is produced **inside** a R chunk with the parameter results = "asis". In your example, the HTML code is written directly in the markdown. Do you have another idea? – jérémy Gelb Nov 11 '21 at 17:03
  • @jérémyGelb you should tell before, that you work in the gitbook. Which version of the bookdown do you have? – manro Nov 11 '21 at 18:57
  • @jérémyGelb look there: https://stackoverflow.com/questions/59635579/bookdown-chapter-section-cross-referencing-not-working-gitbook – manro Nov 11 '21 at 19:09
  • 1
    Thank you for the links, it helped me finding the real issue. I also posted the answer below. – jérémy Gelb Nov 16 '21 at 19:50
  • @jérémyGelb good luck and "+1" ;) – manro Nov 16 '21 at 20:08
1

I found where the problem was.

The html code I am using was produced with the library htmltools and converted to text with the function doRenderTags. This function do a nice job but the indentation is no read as expected by pandoc (see here : R markdown asis breaks valid html code). So the solution was just to use the parameter indent = FALSE in doRenderTags. After that, the HTML code was not indented and rendered as expected by pandoc.