Markdown code blocks
Show code, commands and configuration files exactly as typed, with colors that make them easy to read. This guide covers inline code, code blocks and syntax highlighting, with examples you can load into the converter and download as a PDF.
Download PDF opens your browser's print dialog. Choose Save as PDF as the destination.
Code inside a sentence
Wrap a word or phrase in single backticks, `like this`, to set it in a monospaced font. Use
it for anything a reader might type or look for exactly: file names, commands, function names, settings
and keyboard input. It tells the reader that config.yaml is a file name, not just a phrase.
Open `settings.json` and set `"autoSave"` to `true`, then run `npm start`.
Code is also left exactly as you typed it. In ordinary text, md2pdf.me turns straight quotes into curly
ones and two hyphens into a dash, which is right for prose and wrong for a command. Inside backticks,
"quotes" and --flags stay as they are, so anyone copying a command from your PDF
gets one that works.
Code blocks
For more than a line, put the code between two lines of three backticks, called fences. Everything between them is shown as it is, with its line breaks and indentation kept, in a box set apart from the text.
```
server {
listen 80;
server_name example.com;
root /var/www/html;
}
```
Three tildes, ~~~, work the same way as backticks. You can also make a code block by
indenting every line by four spaces, an older style you'll see in some documents, but it can't have a
language name, so it never gets syntax highlighting. Fences are clearer and easier to get right.
Syntax highlighting
Name the language straight after the opening fence and md2pdf.me colors the code: keywords in one color, text strings in another, comments in a quieter one. The colors make code much quicker to read, especially on paper.
```python
def total(prices, tax_rate=0.08):
"""Add sales tax to a list of prices."""
subtotal = sum(prices) # before tax
return round(subtotal * (1 + tax_rate), 2)
```
These language names are highlighted, along with the usual short forms, such as js,
ts, py, sh, yml and md:
| Kind | Language names |
|---|---|
| Web | html, xml, css, scss, less, javascript, typescript, php, graphql |
| General purpose | python, java, c, cpp, csharp, go, rust, kotlin, swift, ruby, objectivec, vbnet |
| Scripting and data | bash, shell, perl, lua, r, sql |
| Config and text | json, yaml, toml, ini, makefile, markdown, diff |
A name md2pdf.me doesn't recognize, such as powershell or dockerfile, does no
harm: the block is shown in the same box, just without colors. Use text when you want a
block deliberately left plain, such as program output or a file listing.
One name is special: a block named mermaid is drawn as a diagram instead of shown as code.
The Mermaid to PDF page explains how.
Code that contains backticks
Sometimes the code itself contains backticks, such as a shell command or a piece of Markdown. For inline
code, wrap it in two backticks instead of one, with a space inside each end if the code starts or ends
with a backtick: `` `npm test` `` shows `npm test`.
For a code block, make the fence longer than any run of backticks inside it. A block fenced with four backticks can contain lines of three, which is how you show a Markdown code block inside a code block:
````markdown
To install, run:
```bash
npm install md2pdf
```
````
Terminal sessions and diffs
To show commands together with what they print, name the block console. Lines starting
with a $ prompt are highlighted as commands, and the output is left plain, so readers can
tell at a glance which lines to type.
```console
$ git status
On branch main
nothing to commit, working tree clean
$ git pull
Already up to date.
```
To show what changed in a file, name the block diff and start each line with
+ for an added line, - for a removed one, or a space for one that's unchanged.
Added lines are shown in green and removed ones in red or brown, depending on the style, as in a code
review.
```diff
def greet(name):
- print("Hello " + name)
+ print(f"Hello, {name}!")
```
Long lines and long blocks
On a web page, a long line of code scrolls sideways. Paper can't scroll, so md2pdf.me wraps long lines onto the next line instead of cutting them off at the edge of the page. Nothing is lost, but wrapped code is harder to read, so where you can, break long lines yourself the way the language allows, such as after a comma in a list of arguments.
A code block that fits on one page is never split across two: if it doesn't fit in the space left on a page, it moves to the next one. A block longer than a whole page has to be split, and continues on the next page.
Tab characters are shown eight spaces wide, which can push deeply indented code off to the right. Indent with spaces instead; in md2pdf.me's editor, the Tab key inserts two spaces. Code blocks have no line numbers. If you need to refer to a particular line, mention what's on it, or put a short comment at the end of the line.
How code looks in each style
Every style sets code in small monospaced type, so a line of about 80 characters fits across a Letter or A4 page, and each colors code to suit its look:
| Style | Code blocks look like |
|---|---|
| Classic | A cream box with a thin border and muted, ink-like colors |
| Academic | No box: a line above and below, like a program listing in a paper |
| Modern | A tinted box with rounded corners |
| Report | A light grey box with a thin border |
| Résumé | A compact grey box in slightly smaller type |
| Worksheet | A pale lilac box with a rounded border |
| GitHub | A grey box with the same colors as github.com |
| Gothic | A parchment-colored box with a thin border |
| Sandwake | A white box with a thin outline and a rust edge on the left |
| Elvish | A pale green box with a line above and below |
| Cyberpunk | A framed terminal panel labelled // Terminal |
| Newspaper | A telegram: a white box with a thin border, headed By wire, in typewriter type |
| Fiendish | A white box with a thin border and a heavy line along the top, in typewriter type |
For technical documentation, the GitHub style is the most familiar to developers. For a report or a paper that includes some code, Report or Academic keeps the code looking like part of the document rather than a screenshot of an editor.
Common mistakes
- A fence that isn't closed. Without the closing backticks, the code block runs to the end of the document and swallows everything after it. If the rest of your preview turns into code, look for a missing fence.
- Fences in a list that aren't indented. To keep a code block inside a numbered step, indent the fences and the code to line up with the text of the step. Otherwise the block ends the list, and the steps after it form a new list.
- The language name on its own line. It goes on the same line as the opening fence,
as in
```python. On a line of its own, it's just a stray word and the code isn't highlighted. ChatGPT's copy button adds it on a line above the fence as well; md2pdf.me takes that line out when you paste the copy here. - Code pasted as a picture. A screenshot of code is blurry in print and can't be searched or copied. Paste the text into a code block instead.
Other topics
- Tables: rows, columns and alignment
- Math and LaTeX: formulas and equations
- Footnotes: numbered notes and references
- Task lists: checklists with tick boxes
- Page breaks: controlling where pages end