PSA: how to quote code with backticks

This is a short post on using backticks (` and ```) to quote code, with the intention of benefiting users who are confused by this feature of the site. The intention is to make it linkable when necessary.

Why

By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This helps people who read your code and want to help you.

Displayed code looks like this:

function displayed_code(x::Int, y::Int)
    if x < y
        println("x is smaller")
    else
        println("or not")
    end
end

Inline code looks_like_this(c::Float64).

Where

The backtick character (`) is located in the top left of the U.S. keyboard, above the Tab key.
image

It looks similar to but is different from the apostrophe character ('), sometimes also called a single quote.

On mobile, you will typically need to hold down the apostrophe key until a pop-up of similar characters appear for selection. Backtick is usually the option farthest to the left.

How

Discourse uses Markdown. Open and close displayed code with triple backticks ```. The julia after the first ``` specifies the language: on this forum, you can omit it and it will default to Julia.

```julia
function displayed_code(x::Int, y::Int)
    if x < y
        println("x is smaller")
    else
        println("or not")
    end
end
```

The inline syntax uses single backticks: `looks_like_this(c::Float64)` (you can’t specify the language here, no syntax highlighting).

This is all you need to do. When editing your post, the preview window on the right shows the result, and helps you make sure you did it right.

Quotes Contianing Backticks

If you want to quote backticks themselves, enclose them in more backticks: `` ` `` renders as `, and

````julia
"""
This is how you use this function:
```julia
1+1
```
"""
````

produces

"""
This is how you use this function:
```julia
1+1
```
"""

which is useful when quoting code with docstrings that have ``` in them.

65 Likes

7 posts were split to a new topic: Iterative feedback and discussion on the blockquote PSA