jérémy Gelb
jérémy Gelb

Reputation: 166

Adding cross reference in html in Rmarkdown

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

Upvotes: 1

Views: 266

Answers (2)

j&#233;r&#233;my Gelb
j&#233;r&#233;my Gelb

Reputation: 166

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.

Upvotes: 1

manro
manro

Reputation: 3677

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

```

Upvotes: 1

Related Questions