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.

Markdown
Preview

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:

KindLanguage names
Webhtml, xml, css, scss, less, javascript, typescript, php, graphql
General purposepython, java, c, cpp, csharp, go, rust, kotlin, swift, ruby, objectivec, vbnet
Scripting and databash, shell, perl, lua, r, sql
Config and textjson, 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:

StyleCode blocks look like
ClassicA cream box with a thin border and muted, ink-like colors
AcademicNo box: a line above and below, like a program listing in a paper
ModernA tinted box with rounded corners
ReportA light grey box with a thin border
RésuméA compact grey box in slightly smaller type
WorksheetA pale lilac box with a rounded border
GitHubA grey box with the same colors as github.com
GothicA parchment-colored box with a thin border
SandwakeA white box with a thin outline and a rust edge on the left
ElvishA pale green box with a line above and below
CyberpunkA framed terminal panel labelled // Terminal
NewspaperA telegram: a white box with a thin border, headed By wire, in typewriter type
FiendishA 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

Other topics

Back to the Markdown guide