PSA: how to quote code with backticks


#1

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).

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.

Pro tip

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.


Regression using Categorical variables
Gadfly problem plotting
Error that I would like help reading- about creating matrices
Evaluate multi-variable anonymous function for one variable
Evaluate multi-variable anonymous function for one variable
f(t+1) in JuMP
Constraint optimization without gradient
Improving Speed for a Novice
Why doesn't an argument of Vector{Vector{T} where T<:Real} accept a variable of Vector{Vector{Int64}}?
How to compute expoents using JuMP?
Fisher's test p-value results appear to differ from matlab, R
Help on optimization
How to read a system of equations?
Local function redefinition: feature or bug?
Emacs users should know about julia-max-block-lookback
getting the types right for transformed distributions
Learning Plots... now: marker_z?
Problem in cloning or adding OpenCV.jl
Sorting custom types
Command that calls functions without the need of arguments
Pre-allocated return arguments
LoadError using julia
Nonlinear Constraints with Splatting
Function hist() not found
Can't create animations
using constructor to compute new field
Pmap inexact error int128
What can and can I not put into an expression
SomeType{T} where T:>SomeOtherType
What is the difference between `Threads.@threads' and `Threads@threads'?
Sparse matrix-vector product: much more slow than Matlab
Problem with permuting dimensions of multi dimensional arrays
Trouble with ControlSystems.jl
New topic template text
Pkg.update error, Base.LibGit2 test fails
Fail Testing Mosek 8 -> ERROR: LoadError: ReadOnlyMemoryError()
FactorAnalysis, KernelPCA in MultivariateStats.jl broken?
Looking for feedback on package type design - tree log volumes
Help: I get this error whenever I try to run my code
Problem with split.(...)
Running simple loop in parallel
#2

I want more likes to give to this post

:heart: :heart: :heart: :heart: :heart: :heart:


#3
maybe pin it?

#4

Isn’t the julia in ```julia optional?


#5

I think so, yes. Do you think I should remove it from the example? I like to write things free of context, as I use Markdown in many places.


#6

Also note that julia is the default setting for this site. So be mindful
of you want to post code in a different language.


#7

Yea, if we don’t need to write julia in the julia discourse, then remove it. Your goal is to guide people for this specific site, not every possible forum.


#8

Minor edits: most importantly, emphasized that specifying julia after the first ``` is optional on this forum.

Thanks to everyone who pointed this out.