R Markdown Add Tag to Head of HTML Output

R Markdown Add Tag to Head of HTML Output

You can create a file with the meta tag and add using the following YAML option:

---
title: "mytitle"
output:
html_document:
includes:
in_header: myheader.html
---

You could also create the myheader.html file on the fly in the setup chunck:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE )
#libraries
require(ggplot2)

#Create myheader.html
fileConn <- file("myheader.html")
writeLines('<meta http-equiv="X-UA-Compatible" content="IE=edge" />', fileConn)
close(fileConn)
```

R markdown adds # link behind HTML headers

This is a new feature introduced in the current development version of rmarkdown. See the NEWS file for more info. To disable this feature, you may use:

output:
html_document:
anchor_sections: false

Use internal links in RMarkdown HTML output

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.

R Markdown Hashtag Hover Output

This is a new feature of the latest release of rmarkdown you can disable it by adding anchor_sections: FALSE to the yaml header, it's documented in the release notes

HTML tags in Rmarkdown to word document

Sure, here we go:

---
output:
word_document:
md_extensions: +raw_html-markdown_in_html_blocks
pandoc_args: ['--lua-filter', 'read_html.lua']
---

# This is rendered as heading

<h1> And this is one, too </h1>

where read_html.lua must be a file in the same directory with this content:

function RawBlock (raw)
if raw.format:match 'html' and not FORMAT:match 'html' then
return pandoc.read(raw.text, raw.format).blocks
end
end

Let's unpack the above to see how it works. The first thing you'll notice are the additional parameters to word_document. The md_extensions modify the way that pandoc parses the text, see here for a full list (or run pandoc --list-extensions=markdown) in your terminal. We enable raw_html to make sure that pandoc does not discard raw HTML tags, and disable markdown_in_html_blocks as to ensure that we get the whole HTML tag as one block in pandoc's internal format.

The next setting is pandoc_args, where we tell pandoc to use a Lua filter to modify the document during conversion. The filter picks out all HTML blocks, parses them as HTML instead of Markdown, and replaces the raw HTML with the parsing result.

So if you are using raw HTML that pandoc can read, you'll be fine. If you are using special instructions which pandoc cannot read, then the setup described above won't help either. You'd have to rewrite the markup in OOXML, the XML format used in docx.



Related Topics



Leave a reply



Submit