Rmd
SyntaxItalics
Can be producted by either _text1_
or *text1*
which will look this text1 or text2.
Bold
Produced using a pair of double asterisks **text3**
or __text4__
which will look like this text3 or text4.
Headers
# Header 1
## Header 2
### Header 3
etc...
Numbered list
1. Item 1
2. Item 2
3. Item 3
+ part a
+ part b
Unnumbered list
* Item 1
* Item 2
* Item 3
+ part a
+ part b
Script
A pair of tildes (~
) turn text to a subscript (e.g., H~3~PO~4~
renders H3PO4). A pair of carets (^
) produce a superscript (e.g., Cu^2+^
renders Cu2+). To get strike through ~~strike~~
renders to this strike
Images
![](http://example.com/logo.png)
![optional caption text](figures/img.png)
Hyperlinks
http://example.com
[linked phrase](http://example.com)
Horizontal rule
Three or more asterisks or dashes ***
or ---
Tables
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
$x = y $
$x = y $ $x < y $
$x < y $ $x \le y $
$x y $ $x^{n}$
\(x^{n}\) $\hat{x}$
\(\hat{x}\) $\tilde{x}$
\(\tilde{x}\) $\frac{a}{b}$
\(\frac{a}{b}\) $\mu M$
\(\mu M\) $\alpha A$
\(\alpha A\) $\beta B$
\(\beta B\) $\pi \Pi$
\(\pi \Pi\) $^\circ C$
\(^\circ C\)
Below are some useful bits of code that you can use to change font. For basic Rmd syntax see offical Reference Guide.
It is also handy to have colour guides/convertors at the ready, checkout the my section on colour in Plot Aesthetics & Colour.
Rmd
-> PDF
docsFont colour
The quickest option is this \textcolor{olive}{This text is olive}
. It will look like this “This text is olive” in your PDF document.
If you want a more customisable option you can use the following to first choose your font colour, and then apply it. Define text colour (this won’t appear in your Knitted PDF)
\definecolor{fancyTextColor}{HTML}{FF0000}
Then use the following syntax to change font colour \colorbox{hightlightColor}{This text is red}
It will look like this “This text is red” in your PDF document.
Rmd
-> html
docsFont colour and text
the following html code
Roses are <span style="color:red; font-family:Georgia; font-size:2em;">red.</span>
Will give you this output
Roses are red.
For coloured text blocks.
<style>
div.blue { background-color:#e6f0ff; border-radius: 5px; padding: 20px;}
</style>
<div class = "blue">
- This is my first conclusion
- This is my second conclusion
</div>
Custom Fonts in R Markdown PDF Font Formatting - coloring and emphasis
Underlink links in PDF
documents By default hyperlinks on PDF knitted documents are not underlined. Use the following to underlink the link.
[\underline{R Markdown: The Definitive Guide}](https://bookdown.org/yihui/rmarkdown/)
Tips and tricks for working with images and figures in R Markdown documents.
Simple add for image
![image caption](path/to/image.png)
Embed video
vebmedr
is a package that makes embedding video into Rmarkdown simple.
Install the package from CRAN
install.packages("vembedr")
Example
library("htmltools")
library("vembedr")
embed_url("https://www.youtube.com/watch?v=uV4UpCq2azs")
Depending on the type of RMarkdown document you are working with (i.e. what are you “knitting” to), will depend on which code works for you. As always there is more than one way to this.
To start with the most commonly used icons are from font awesome
suite - so head here to pick your (free) icons.
HTML docs
For HTML docs (including webpages), I have found the following works best <i class="fab fa-r-project"></i>
this will give you a beautiful RStudio icon .
If you want to use Ionicons
then include you will first need to make sure the following line appears within your Rmarkdown doc (don’t worry it wont appear in the knitted product) <script src="https://unpkg.com/ionicons@5.0.0/dist/ionicons.js"></script>
. Then use the following code to insert your icons, for example this <ion-icon name="bug-outline"></ion-icon>
produces a lovely bug for you
This code <span class="badge badge-info">Info</span>
will produce this badge Info in html document, more details found here
Webpages (HTML) - nav bar
You can add icons to the nav bar on your webpage, just like on this one. In the YAML doc add the following bit of code under the relevent section icon: fa-icon
e.g. for the rstudio icon use fa-r-project
for this icon
Example - partial YAML doc, for more info on RMarkdown webpages/YAML head to RMarkdown Webpages section
navbar:
title: "Code"
left:
- text: "Home"
href: index.html
- text: "RStudio"
icon: fa-file-alt
PDF docs
When knitting to PDF I have found the easiest way using the icon
package.
Install developer version from github
# install.packages("devtools")
devtools::install_github("ropenscilabs/icon")
Then add the following bit of code in your RMarkdown document.
library(icon)
icon::fa("rocket") # equivalent to icon::fa_rocket()
and you’ll get this . Got some extra tips and trick see here.
In additional to using basic RMarkdown syntax for tables
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
Which look like this
First Header | Second Header |
---|---|
Content Cell | Content Cell |
Content Cell | Content Cell |
You can use KableExtra
Work by Siobhon Egan
siobhon.egan@murdoch.edu.au