2.5 Markdown syntax | R Markdown: The Definitive Guide (2025)

2.5 Markdown syntax

The text in an R Markdown document is written with the Markdown syntax. Precisely speaking, it is Pandoc’s Markdown. There are many flavors of Markdown invented by different people, and Pandoc’s flavor is the most comprehensive one to our knowledge. You can find the full documentation of Pandoc’s Markdown at https://pandoc.org/MANUAL.html. We strongly recommend that you read this page at least once to know all the possibilities with Pandoc’s Markdown, even if you will not use all of them. This section is adapted from Section 2.1 of Xie (2016), and only covers a small subset of Pandoc’s Markdown syntax.

2.5.1 Inline formatting

Inline text will be italic if surrounded by underscores or asterisks, e.g., _text_ or *text*. Bold text is produced using a pair of double asterisks (**text**). 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 mark text as inline code, use a pair of backticks, e.g., `code`. To include \(n\) literal backticks, use at least \(n+1\) backticks outside, e.g., you can use four backticks to preserve three backtick inside: ```` ```code``` ````, which is rendered as ```code```.

Hyperlinks are created using the syntax [text](link), e.g., [RStudio](https://www.rstudio.com). The syntax for images is similar: just add an exclamation mark, e.g., ![alt text or image title](path/to/image). Footnotes are put inside the square brackets after a caret ^[], e.g., ^[This is a footnote.].

There are multiple ways to insert citations, and we recommend that you use BibTeX databases, because they work better when the output format is LaTeX/PDF. Section 2.8 of Xie (2016) has explained the details. The key idea is that when you have a BibTeX database (a plain-text file with the conventional filename extension .bib) that contains entries like:

@Manual{R-base, title = {R: A Language and Environment for Statistical Computing}, author = {{R Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2017}, url = {https://www.R-project.org/},}

You may add a field named bibliography to the YAML metadata, and set its value to the path of the BibTeX file. Then in Markdown, you may use @R-base (which generates “R Core Team (2023)”) or [@R-base] (which generates “(R Core Team 2023)”) to reference the BibTeX entry. Pandoc will automatically generate a list of references in the end of the document.

2.5.2 Block-level elements

Section headers can be written after a number of pound signs, e.g.,

# First-level header## Second-level header### Third-level header

If you do not want a certain heading to be numbered, you can add {-} or {.unnumbered} after the heading, e.g.,

# Preface {-}

Unordered list items start with *, -, or +, and you can nest one list within another list by indenting the sub-list, e.g.,

- one item- one item- one item - one more item - one more item - one more item

The output is:

  • one item
  • one item
  • one item
    • one more item
    • one more item
    • one more item

Ordered list items start with numbers (you can also nest lists within lists), e.g.,

1. the first item2. the second item3. the third item - one unordered item - one unordered item

The output does not look too much different with the Markdown source:

  1. the first item
  2. the second item
  3. the third item
    • one unordered item
    • one unordered item

Blockquotes are written after >, e.g.,

> "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him.">> --- Mark Twain

The actual output (we customized the style for blockquotes in this book):

“I thoroughly disapprove of duels. If a man should challenge me,I would take him kindly and forgivingly by the hand and lead himto a quiet place and kill him.”

— Mark Twain

Plain code blocks can be written after three or more backticks, and you can also indent the blocks by four spaces, e.g.,

```This text is displayed verbatim / preformatted```Or indent by four spaces: This text is displayed verbatim / preformatted

In general, you’d better leave at least one empty line between adjacent but different elements, e.g., a header and a paragraph. This is to avoid ambiguity to the Markdown renderer. For example, does “#” indicate a header below?

In R, the character# indicates a comment.

And does “-” mean a bullet point below?

The result of 5- 3 is 2.

Different flavors of Markdown may produce different results if there are no blank lines.

2.5.3 Math expressions

Inline LaTeX equations can be written in a pair of dollar signs using the LaTeX syntax, e.g., $f(k) = {n \choose k} p^{k} (1-p)^{n-k}$ (actual output: \(f(k)={n \choose k}p^{k}(1-p)^{n-k}\)); math expressions of the display style can be written in a pair of double dollar signs, e.g., $$f(k) = {n \choose k} p^{k} (1-p)^{n-k}$$, and the output looks like this:

\[f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k}\]

You can also use math environments inside $ $ or $$ $$, e.g.,

$$\begin{array}{ccc}x_{11} & x_{12} & x_{13}\\x_{21} & x_{22} & x_{23}\end{array}$$

\[\begin{array}{ccc}x_{11} & x_{12} & x_{13}\\x_{21} & x_{22} & x_{23}\end{array}\]

$$X = \begin{bmatrix}1 & x_{1}\\1 & x_{2}\\1 & x_{3}\end{bmatrix}$$

\[X = \begin{bmatrix}1 & x_{1}\\1 & x_{2}\\1 & x_{3}\end{bmatrix}\]

$$\Theta = \begin{pmatrix}\alpha & \beta\\\gamma & \delta\end{pmatrix}$$

\[\Theta = \begin{pmatrix}\alpha & \beta\\\gamma & \delta\end{pmatrix}\]

$$\begin{vmatrix}a & b\\c & d\end{vmatrix}=ad-bc$$

\[\begin{vmatrix}a & b\\c & d\end{vmatrix}=ad-bc\]

References

R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

———. 2016. Bookdown: Authoring Books and Technical Documents with R Markdown. Boca Raton, Florida: Chapman; Hall/CRC. https://github.com/rstudio/bookdown.

2.5 Markdown syntax | R Markdown: The Definitive Guide (2025)

FAQs

What is syntax Markdown? ›

Markdown language simplifies formatting plain text by using keyboard strokes. For example, to display a HEADING 1 the Markdown syntax is to precede the plain text word with the Markdown hash tag #HEADING 1. When the text displays in a browser, browsers recognize the Markdown syntax and display the text as HEADING 1.

What does $$ do in Markdown? ›

Everything written between two $$ markup will be interpreted as tex math (here is some documentation). Note that you can also use a single $ sign. $$ will produce a block element (a paragraph) while $ will produce an inline element.

What is the Markdown code in R? ›

R Markdown is a format for writing reproducible, dynamic reports with R. The output from R Markdown is a markdown file that contains chunks of embedded R code. With R Markdown you can create different types of files: HTML documents, PDFs, Word Documents, slideshows, and more.

How to bold in R Markdown? ›

Markdown provides simple tags to format text for emphasis as well as super- and subscripting: *italics* produces italics. **bold** produces bold. ~~strikethrough~~ produces strikethrough.

What is an example of a Markdown? ›

For example, to denote a heading, you add a number sign before it (e.g., # Heading One ). Or to make a phrase bold, you add two asterisks before and after it (e.g., **this text is bold** ). It may take a while to get used to seeing Markdown syntax in your text, especially if you're accustomed to WYSIWYG applications.

How to write Markdown code? ›

To create inline code, wrap with backticks ` . To create a code block, either indent each line by 4 spaces, or place 3 backticks ``` on a line above and below the code block. A code block or span displays every character inside exactly as it was typed.

What is ### in Markdown? ›

To create a heading, add number signs ( # ) in front of a word or phrase. The number of number signs you use should correspond to the heading level. For example, to create a heading level three ( <h3> ), use three number signs (e.g., ### My Header ).

Why Markdown is so good? ›

It's a very fast format to compose in since you don't have to bother with formatting. Plain text is typically considered future-proof. In the computing timeline, plain text has been in use for well over 50 years. You can actually edit Markdown with any plain text editor and are not tied to a particular application.

Can I use HTML in Markdown? ›

Span-level HTML tags — e.g. <span> , <cite> , or <del> — can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you'd prefer to use HTML <a> or <img> tags instead of Markdown's link or image syntax, go right ahead.

What is a Markdown script? ›

Markdown is a shorthand that lets you quickly format text. With the Markdown component, you can use Markdown in scripts to quickly write lists, headings, and any other element that Markdown supports.

What is R Markdown good for? ›

R Markdown documents are fully reproducible and support dozens of output formats, like PDFs, Word files, slideshows, and more. R Markdown files are designed to be used in three ways: For communicating to decision makers, who want to focus on the conclusions, not the code behind the analysis.

What is Markdown VS code? ›

Markdown is a lightweight markup language for creating formatted text using a plain-text editor. It is widely used for creating README files, documentation, and other forms of text.

What is $$ in Markdown? ›

Math inside RMarkdown

In side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”.

What is ``` in r? ›

Alternatively, if you start with a line with a sequence of three backticks ``` , this starts a separated code block.

How to break a line in R Markdown? ›

To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .

What is the Markdown meaning? ›

Meaning of markdown in English

a reduction in the price of something: We're offering a ten percent markdown on selected items.

What is the Markdown syntax for a link? ›

In Markdown, a link is created by enclosing the link text in square brackets, [Link text] , and then following it immediately with the URL in parentheses, (https://website-name.com) . For example: We just updated our [Docs Contribution Guide](https://www.codecademy.com/pages/contribute-docs)!

What is markdown vs. HTML? ›

Markdown offers simplicity and ease of use for content creation, while HTML provides structure and control for building robust web applications. By understanding the strengths of each language, developers can leverage them effectively to streamline their workflow and create compelling online experiences.

What is the difference between Markdown and markup syntax? ›

Syntax Complexity: Markdown has a simpler syntax compared to Markup languages. It uses plain text and specific characters to format text, making it easier to learn and use. Markup languages, on the other hand, have more complex syntax with opening and closing tags, requiring more coding knowledge and experience.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5451

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.