1 R Markdown Basics: The Markdown syntax
Here is a brief introduction to using R Markdown. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents and much, much more. R Markdown provides the flexibility of Markdown with the implementation of R input and output. For more details on using R Markdown see http://rmarkdown.rstudio.com.
Be careful with your spacing in Markdown documents. While whitespace largely is ignored, it does at times give Markdown signals as to how to proceed. As a habit, try to keep everything left aligned whenever possible, especially as you type a new paragraph. In other words, there is no need to indent basic text in the Rmd document (in fact, it might cause your text to do funny things if you do).
1.1 Markdown basic syntax
1.1.1 Italics and bold
- Italics are done like *this* or _this_
- Bold is done like **this** or __this__
- Bold and italics is done like ***this***, ___this___, or (the most transparent solution, in my opinion) **_this_**
1.1.5 ‘Escaping’ (aka “What if I need an actual asterisk?”)
- To include an actual *, _ or \, add another \ in front of them: \*, \_, \\
1.1.8 Headings
- are done with #’s of increasing number, i.e.
- # First-level heading
- ## Second-level heading
- ### Etc.
In PDF output, a level-five heading will turn into a paragraph heading, i.e. \paragraph{My level-five heading}
, which appears as bold text on the same line as the subsequent paragraph.
1.1.9 Lists
Unordered list by starting a line with an * or a -:
- Item 1
- Item 2
Ordered lists by starting a line with a number:
- Item 1
- Item 2
Notice that you can mislabel the numbers and Markdown will still make the order right in the output.
To create a sublist, indent the values a bit (at least four spaces or a tab):
- Item 1
- Item 2
- Item 3
- Item 3a
- Item 3b
1.1.10 Line breaks
The official Markdown way to create line breaks is by ending a line with more than two spaces.
Roses are red. Violets are blue.
This appears on the same line in the output, because we didn’t add spaces after red.
Roses are red.
Violets are blue.
This appears with a line break because I added spaces after red.
I find this is confusing, so I recommend the alternative way: Ending a line with a backslash will also create a linebreak:
Roses are red.
Violets are blue.
To create a new paragraph, you put a blank line.
Therefore, this line starts its own paragraph.
1.1.11 Hyperlinks
-
This is a hyperlink created by writing the text you want turned into a clickable link in
[square brackets followed by a](https://hyperlink-in-parentheses)
1.1.12 Footnotes
- Are created1 by writing either ^[my footnote text] for supplying the footnote content inline, or something like
[^a-random-footnote-label]
and supplying the text elsewhere in the format shown below:2
[^a-random-footnote-label]: This is a random test.
1.1.14 Math
The syntax for writing math is stolen from LaTeX. To write a math expression that will be shown inline, enclose it in dollar signs. - This: $A = \pi*r^{2}$ Becomes: \(A = \pi*r^{2}\)
To write a math expression that will be shown in a block, enclose it in two dollar signs.
This: $$A = \pi*r^{2}$$
Becomes: \[A = \pi*r^{2}\]
To create numbered equations, put them in an ‘equation’ environment and give them a label with the syntax (\#eq:label)
, like this:
\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
(\#eq:binom)
\end{equation}
Becomes: \[\begin{equation} f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k} \tag{1.1} \end{equation}\]
For more (e.g. how to theorems), see e.g. the documentation on bookdown.org
1.2 Additional resources
R Markdown: The Definitive Guide - https://bookdown.org/yihui/rmarkdown/
R for Data Science - https://r4ds.had.co.nz
1.1.13 Comments
To write comments within your text that won’t actually be included in the output, use the same syntax as for writing comments in HTML. That is, <!-- this will not be included in the output -->.