Use internal links in RMarkdown HTML output

Joe Parzival

I am using R Studio to create a markdown document. I tried:

Jump to [Header 1](#anchor)

I would like to set up a link so when a reader clicks it, can jump to a specific point on the page.
Let's say the point I want them to be directed to has a header "##Test".

Martin Schmelzer

Here is a solution for HTML documents using jQuery:

---
title: "Internal Links"
output: html_document
---

# First Section

## Second Section

### Third Section


<script type="text/javascript">
  // When the document is fully rendered...
  $(document).ready(function() {
    // ...select all header elements...
    $('h1, h2, h3, h4, h5').each(function() {
      // ...and add an id to them corresponding to their 'titles'
      $(this).attr('id', $(this).html());
    });
  });
</script>


<a href="#First Section">Go to first section</a><br>
<a href="#Second Section">Go to second section</a><br>
<a href="#Third Section">Go to third section</a>

As the comments point out, we simply select all headers, read out their content (e.g. "First Section") and add the attribute id with the value corresponding to the specific content to each header. Now you can link to any header using #HEADER (e.g. #First Section).


This is of course extendable to all other elements you wish to put an anchor on. So if you want to link to any of your chunks, simply add this script to your document:

<script type="text/javascript">
  $(document).ready(function() {
    $('pre.r').each(function(i) {
      $(this).attr('id', 'Chunk_' + i);
    });
  });
</script>

Now you can link to the chunks by using <a href="Chunk_i">My Chunk</a> where i goes from 0, the first chunk, to N, the very last chunk in your document.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Internal links in rmarkdown don't work

use of tikzdevice in RMarkdown with beamer output

Centering a Xtable in Rmarkdown html output

Nav bar links to internal html pages not working

Using SED to fix internal links in HTML documents?

What are internal and external links on HTML pages?

Rmarkdown: Indentation of TOC items in HTML output

Hyperlink a pdf on local drive to rmarkdown html output

Rmarkdown html matrix output width too narrow

Suppress alt text for GIFs in rmarkdown HTML output

dfSummary - formatting factor levels in html output in Rmarkdown?

How to use internal mic for input and bluetooth for output

How to use Docker to convert Rmarkdown to HTML?

Use Regex to highlight a text in HTML generated with Rmarkdown

Html links in dumped formatted output from links 2.21 browser

Can HTML anchor tags ever be used for internal links in Gatsby?

Access Internal PDF links from html text block inside pdf

Why are some of my HTML internal links working and others not?

XSLT transformation XML to HTML to generate navigation section and internal links

Links to sections in DT in rmarkdown

Changing the font size of figure captions in RMarkdown HTML output

Change color of error messages in RMarkdown code output (HTML, PDF)

How to add code folding to output chunks in rmarkdown html documents

How to render HTML from RMarkdown without javascript in output

How to show significance stars in R Markdown (rmarkdown) html output notes?

Suppress running messages from RStan in Rmarkdown HTML output

How to remove white space at the end of an Rmarkdown HTML output

Avoid text-wrapping of code in Rmarkdown html output

Using nest and tidy to create formatted html regression output in RMarkdown