latex \textbackslash does not work, rmarkdown html - html

I want to include the \textbackslash in a latex math formula in a rmarkdown html document. I am working with bookdown. here is a sample code:
---
title: "Probabilidad"
author: "Nicolás Molano Gonzalez"
date: "7 de Abril de 2020"
output:
bookdown::html_document2: default
---
here is some useless text
now I want to do this $A \textbackslash B$ but it does not work properly
The $A \textbackslash B$ is not working

You can use \setminus, \smallsetminus or \backslash instead. The problem is that bookdown::html_document2 is using MathJax, and by design it only handles math-mode macros. See here for a discussion of the differences.

Related

Quarto: Howto use a custom (Web)font?

I'm trying to get a Quarto home page of the ground and one requirement is a custom font (free webfont; Tex-Gyre-Adventor).
Following the documentation, I have played around with the html format specific mainfont tag in various iterations (see header example below), but cannot make this work - including if the font is additionally defined as a #font-face in style.css (and many other attempts).
Can anyone point me in the right direction how to use mainfont with a non-standard font?
Thanks for any pointers!
---
title: "Page"
title-block-banner: false
format:
html:
theme: vapor
mainfont: `https://fontlibrary.org/en/font/tex-gyre-adventor`
code-fold: true
toc: false
number-sections: false
link-citations: yes
---
To use a custom webfont as the mainfont, just add font stylesheet file in document header using header-includes and set the font name in mainfont yaml key.
---
title: "Page"
title-block-banner: false
format:
html:
theme: vapor
code-fold: true
toc: false
number-sections: false
header-includes: |
<link rel="stylesheet" media="screen" href="https://fontlibrary.org//face/tex-gyre-adventor" type="text/css"/>
link-citations: yes
mainfont: TeXGyreAdventorRegular
---
## Quarto
Quarto enables you to weave together content and executable code into a
finished document. To learn more about Quarto see <https://quarto.org>.
## Running Code
When you click the **Render** button a document will be generated that
includes both content and the output of embedded code. You can embed
code like this:
```{r}
1 + 1
```

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
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
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
```
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.

How to embed external HTML in r distill rmd

Question:
How do I place HTML files in place within an R Distill Markdown file?
Background:
I would like to embed / incorporate an external html file within a Distill Rmd file. I don't want to show the link to the html in the document, but the content of the html file. Something like include_graphics only for html.
I wasn't able to find something via google search. I only found Include HTML files in R Markdown file? where OP writes, he never figured out how to do it :(
From that post I tried htmltools::includeHTML("file.html") which didn't work,
and also shiny::includeHTML("file.html") which didn't work,
and even (it didn't work):
includes:
in_header: file.html
With htmltools::includeHTML I at least got the error message Error loading script: https://cdn.jsdelivr.net/npm//vega#5?noext which I couldn't make sense of :(
Though I can't reproduce this error-message now (a couple of weeks later). Maybe it is because I included library(htmltools). Though there still is no html embedded :(
However the Distill package apparently is able to do embed html, as the _footer.html is included correctly, but automatically (such that i wasn't able to find the command in the code).
Thanks a lot!
You might want to double check your R chunk arguments, does it have include=FALSE? because then you would be calling the HTML file, but then telling it not to be included, here is my distill Rmd file and the output with the included HTML code.
Rmarkdown
---
title: "Untitled"
output: distill::distill_article
---
```{r setup}
knitr::opts_chunk$set(echo = FALSE)
library(htmltools)
htmltools::includeHTML("test2.html")
```
HTML: titled test2.html located in same directory as .Rmd
<html>
<head>
</style>
<title>Title</title>
</head>
<body>
<p>This is an R HTML document. When you click the <b>Knit HTML</b> button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:</p>
<div class="chunk" id="unnamed-chunk-1"><div class="rcode"><div class="source"><pre class="knitr r" ><span class="hl kwd">summary</span><span class="hl std">(cars)</span>
</pre></div>
<div class="output"><pre class="knitr r">## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
</pre></div>
</div></div>
<p>You can also embed plots, for example:</p>
<div class="chunk" id="unnamed-chunk-2"><div class="rcode"><div class="source"><pre class="knitr r" ><span class="hl kwd">plot</span><span class="hl std">(cars)</span>
</pre></div>
</div><div class="rimage default"><img src="figure/unnamed-chunk-2-1.png" title="plot of chunk unnamed-chunk-2" alt="plot of chunk unnamed-chunk-2" class="plot" /></div></div>
</body>
</html>
output rendered to html

Justify text in Rmarkdown html output in YAML header

I want to justify text in an Rmarkdown html file on both sides. I know how to do this using <style> body {text-align: justify} </style> after the YAML header (as per this answer):
---
output: html_document
---
<style> body {text-align: justify} </style> <!-- Justify text. -->
# Text that is justified on both sides
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this. This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this.
However, I want to find a way to specify this in the YAML header directly, instead of after the YAML header, but without having to resort to additional .css files in the same repository. In other words, I don't want to put a tiny bit of html code in a separate .css file and call it via
output:
html_document:
css: justify.css
and I don't want it in the main body of the Rmarkdown file. How can I do this?
Using plain pandoc, one would use the include-headers field. Citing from pandoc's manual:
Raw content to include in the document's header may be specified using header-includes; however, it is important to mark up this content as raw code for a particular output format, using the raw_attribute extension), or it will be interpreted as markdown. For example:
header-includes:
- |
```{=latex}
\let\oldsection\section
\renewcommand{\section}[1]{\clearpage\oldsection{#1}}
```
My interpretation of this RMarkdown issue is that this won't work in RMarkdown. Only files can be included. However, one of the linked issues therein offers a workaround, in which the file is generated through an R snippet in the YAML header:
---
output:
html_document:
includes:
in_header: header.html
dummy: "`<style>body {text-align: justify; color: green}</style>`{cat, engine.opts=list(file='header.html')}"
---
Not exactly pretty, but works as desired.

Using the Author field of R Markdown in footer.html

R Markdown allows to add a footer to your html output. The YAML header allows to give an author name using a specific field.
I would like to use this author name in my footer.html file, but cannot figure out how to achieve that.
Here is a minimal example:
fic.rmd:
---
title: "title"
author: "Mister-A"
output:
html_document:
include:
after_body: footer.html
---
content
And in the same folder the footer.html file:
I am - #author-name-field-that-I-don't-konw-how-to-get -
Any help or advice would me much appreciated. Thank you very much.
If you want to be able to use the YAML parameters within sections of the report, you need to alter the base pandoc template. You can find all of them here
The basic structure of making this work is to put the variable surrounded by dollar signs to use the YAML variable in the output document. So for example $author$ is required in this case.
Solution
We can create a copy of the pandoc template for HTML in our local directory using the following command. This is the same file as here.
# Copies the RMkarkdown template to the local directory so we can edit it
file.copy(rmarkdown:::rmarkdown_system_file("rmd/h/default.html"), to = "template.html")
In the template.html, we need to add the pandoc tags. To add a footer, we want to add code to the buttom of the document. This is line 457 in the current template but this may change in future versions, so we want to put it after the include-after tag:
$for(include-after)$
$include-after$
$endfor$
<hr />
<p style="text-align: center;">I am $author$</p>
$if(theme)$
$if(toc_float)$
</div>
</div>
$endif$
Finally, the R Markdown file looks like:
---
title: "title"
author: "Mister-A"
output:
html_document:
template: template5.html
---
This is some text
As a possible extension of this, you may want to check out this post on designing a stylish footer.