8. Example

This is an example generated with Literate based on this source file: example.jl. You are seeing the generated notebook output. The corresponding markdown (HTML) output can be found here: example.html, and the plain script output can be found here: example.jl.

To view this notebook as a slideshow, install the RISE plugin and press alt-r to start. Use spacebar to advance.

It is recommended to have the source file available when reading this, to better understand how the syntax in the source file corresponds to the output you are seeing.

Basic syntax

The basic syntax for Literate is simple, lines starting with # is interpreted as markdown, and all the other lines are interpreted as code. Here is some code:

In [1]:
x = 1//3
y = 2//5
Out[1]:
2//5

In markdown sections we can use markdown syntax. For example, we can write text in italic font, text in bold font and use links.

It is possible to filter out lines depending on the output using the #md, #nb, #jl and #src tags (see Filtering lines):

  • This line starts with #nb and is thus only visible in the notebook output.

The source file is parsed in chunks of markdown and code. Starting a line with #- manually inserts a chunk break. For example, if we want to display the output of the following operations we may insert #- in between. These two code blocks will now end up in different @example-blocks in the markdown output, and two different notebook cells in the notebook output.

In [2]:
x + y
Out[2]:
11//15
In [3]:
x * y
Out[3]:
2//15

Output capturing

Code chunks are by default placed in Documenter @example blocks in the generated markdown. This means that the output will be captured in a block when Documenter is building the docs. In notebooks the output is captured in output cells, if the execute keyword argument is set to true. Output to stdout/stderr is also captured.

In [4]:
function foo()
    println("This string is printed to stdout.")
    return [1, 2, 3, 4]
end

foo()
This string is printed to stdout.
Out[4]:
4-element Vector{Int64}:
 1
 2
 3
 4

Just like in the REPL, outputs ending with a semicolon hides the output:

In [5]:
1 + 1;

Both Documenter's @example block and notebooks can display images. Here is an example where we generate a simple plot using the Plots.jl package

In [6]:
using Plots
x = range(0, stop=6π, length=1000)
y1 = sin.(x)
y2 = cos.(x)
plot(x, [y1, y2])
Out[6]:

Custom processing

It is possible to give Literate custom pre- and post-processing functions. For example, here we insert a placeholder value y = 321 in the source, and use a preprocessing function that replaces it with y = 321 in the rendered output.

In [7]:
x = 123
Out[7]:
123

In this case the preprocessing function is defined by

In [8]:
function pre(s::String)
    s = replace(s, "x = 123" => "y = 321")
    return s
end
Out[8]:
pre (generic function with 1 method)

Documenter.jl interaction

In the source file it is possible to use Documenter.jl style references, such as @ref and @id. These will be filtered out in the notebook output. For example, here is a link, but it is only visible as a link if you are reading the markdown output. We can also use equations:

$$ \int_\Omega \nabla v \cdot \nabla u\ \mathrm{d}\Omega = \int_\Omega v f\ \mathrm{d}\Omega $$

using Documenters math syntax. Documenters syntax is automatically changed to \begin{equation} ... \end{equation} in the notebook output to display correctly.


This notebook was generated using Literate.jl.