PSA: how to quote code with backticks

This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature.

Why

By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and 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).

How

Discourse uses Markdown. The markdown specification uses triple backticks ``` to open and close displayed code blocks. A language can optionally be specified immediately after the opening ```. On this forum, the default language is julia.

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

Alternatively, on this forum, julia-repl can be specified as the language for better syntax highlighting when copying from the REPL.

```julia-repl
julia> x = 3 + 7
10
``` 

The inline syntax uses single backticks: `looks_like_this(c::Float64)` (You can’t specify a language with inline code, so there is no syntax highlighting.)

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.

When

Always wrap code in backticks so it renders correctly and is distinguished from commentary. When writing your post, the preview window on the right side will show the formatted text. Check that the preview is formatted as you intended before posting.

That’s it! Please take advantage of this feature to make it easier to help you.


Advanced

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.

67 Likes

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