7. Example
This is an example generated with Literate based on this source file: example.jl. You are seeing the HTML-output which Documenter have generated based on a markdown file generated with Literate. The corresponding notebook can be viewed in nbviewer here: example.ipynb, and opened in binder here: example.ipynb, and the plain script output can be found here: example.jl.
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:
x = 1//3
+7. Example · Literate.jl
7. Example
This is an example generated with Literate based on this source file: example.jl. You are seeing the HTML-output which Documenter have generated based on a markdown file generated with Literate. The corresponding notebook can be viewed in nbviewer here: example.ipynb, and opened in binder here: example.ipynb, and the plain script output can be found here: example.jl.
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:
x = 1//3
y = 2//5
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
#md and is thus only visible in the markdown 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.
x + y
11//15
x * y
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.
function foo()
println("This string is printed to stdout.")
return [1, 2, 3, 4]
@@ -16,114 +16,114 @@ y2 = cos.(x)
plot(x, [y1, y2])
diff --git a/dev/outputformats/index.html b/dev/outputformats/index.html
index 58423dc..73f8495 100644
--- a/dev/outputformats/index.html
+++ b/dev/outputformats/index.html
@@ -31,8 +31,8 @@ When adding `x` and `y` together we obtain a new rational number:
```@example name
z = x + y
-```We note that lines starting with # are printed as regular markdown, and the code lines have been wrapped in @example blocks. We also note that an @meta block have been added, that sets the EditURL variable. This is used by Documenter to redirect the "Edit on GitHub" link for the page, see Interaction with Documenter.
Some of the output rendering can be controlled with keyword arguments to Literate.markdown:
Literate.markdown — Function.Literate.markdown(inputfile, outputdir; kwargs...)Generate a markdown file from inputfile and write the result to the directoryoutputdir.
Keyword arguments:
name: name of the output file, excluding.md.nameis also used to name all the@exampleblocks, and to replace@__NAME__. Defaults to the filename ofinputfile.preprocess,postprocess: custom pre- and post-processing functions, see the Custom pre- and post-processing section of the manual. Defaults toidentity.documenter: boolean that tells if the output is intended to use with Documenter.jl. Defaults totrue. See the the manual section on Interaction with Documenter.codefence: APairof opening and closing code fence. Defaults to
if"```@example $(name)" => "```"documenter = trueand
if"```julia" => "```"documenter = false.credit: boolean that controls the addition ofThis file was generated with Literate.jl ...to the bottom of the page. If you find Literate.jl useful then feel free to keep this to the default, which istrue.
4.2. Notebook Output
The (default) notebook output of the source snippet can be seen here: notebook.ipynb.
We note that lines starting with # are placed in markdown cells, and the code lines have been placed in code cells. By default the notebook is also executed and output cells populated. The current working directory is set to the specified output directory the notebook is executed. Some of the output rendering can be controlled with keyword arguments to Literate.notebook:
Literate.notebook — Function.Literate.notebook(inputfile, outputdir; kwargs...)Generate a notebook from inputfile and write the result to outputdir.
Keyword arguments:
name: name of the output file, excluding.ipynb.nameis also used to replace@__NAME__. Defaults to the filename ofinputfile.preprocess,postprocess: custom pre- and post-processing functions, see the Custom pre- and post-processing section of the manual. Defaults toidentity.execute: a boolean deciding if the generated notebook should also be executed or not. Defaults totrue. The current working directory is set tooutputdirwhen executing the notebook.documenter: boolean that says if the source contains Documenter.jl specific things to filter out during notebook generation. Defaults totrue. See the the manual section on Interaction with Documenter.credit: boolean that controls the addition ofThis file was generated with Literate.jl ...to the bottom of the page. If you find Literate.jl useful then feel free to keep this to the default, which istrue.
4.3. Script Output
The (default) script output of the source snippet above is as follows
x = 1//3
+```We note that lines starting with # are printed as regular markdown, and the code lines have been wrapped in @example blocks. We also note that an @meta block have been added, that sets the EditURL variable. This is used by Documenter to redirect the "Edit on GitHub" link for the page, see Interaction with Documenter.
Some of the output rendering can be controlled with keyword arguments to Literate.markdown:
Literate.markdown — Function.Literate.markdown(inputfile, outputdir; kwargs...)Generate a markdown file from inputfile and write the result to the directoryoutputdir.
Keyword arguments:
name: name of the output file, excluding.md.nameis also used to name all the@exampleblocks, and to replace@__NAME__. Defaults to the filename ofinputfile.preprocess,postprocess: custom pre- and post-processing functions, see the Custom pre- and post-processing section of the manual. Defaults toidentity.documenter: boolean that tells if the output is intended to use with Documenter.jl. Defaults totrue. See the the manual section on Interaction with Documenter.codefence: APairof opening and closing code fence. Defaults to
if"```@example $(name)" => "```"documenter = trueand
if"```julia" => "```"documenter = false.credit: boolean that controls the addition ofThis file was generated with Literate.jl ...to the bottom of the page. If you find Literate.jl useful then feel free to keep this to the default, which istrue.
4.2. Notebook Output
The (default) notebook output of the source snippet can be seen here: notebook.ipynb.
We note that lines starting with # are placed in markdown cells, and the code lines have been placed in code cells. By default the notebook is also executed and output cells populated. The current working directory is set to the specified output directory the notebook is executed. Some of the output rendering can be controlled with keyword arguments to Literate.notebook:
Literate.notebook — Function.Literate.notebook(inputfile, outputdir; kwargs...)Generate a notebook from inputfile and write the result to outputdir.
Keyword arguments:
name: name of the output file, excluding.ipynb.nameis also used to replace@__NAME__. Defaults to the filename ofinputfile.preprocess,postprocess: custom pre- and post-processing functions, see the Custom pre- and post-processing section of the manual. Defaults toidentity.execute: a boolean deciding if the generated notebook should also be executed or not. Defaults totrue. The current working directory is set tooutputdirwhen executing the notebook.documenter: boolean that says if the source contains Documenter.jl specific things to filter out during notebook generation. Defaults totrue. See the the manual section on Interaction with Documenter.credit: boolean that controls the addition ofThis file was generated with Literate.jl ...to the bottom of the page. If you find Literate.jl useful then feel free to keep this to the default, which istrue.
4.3. Script Output
The (default) script output of the source snippet above is as follows
x = 1//3
y = 2//5
-z = x + yWe note that lines starting with # are removed and only the code lines have been kept. Some of the output rendering can be controlled with keyword arguments to Literate.script:
Literate.script — Function.Literate.script(inputfile, outputdir; kwargs...)Generate a plain script file from inputfile and write the result to outputdir.
Keyword arguments:
name: name of the output file, excluding.jl.nameis also used to replace@__NAME__. Defaults to the filename ofinputfile.preprocess,postprocess: custom pre- and post-processing functions, see the Custom pre- and post-processing section of the manual. Defaults toidentity.documenter: boolean that says if the source contains Documenter.jl specific things to filter out during script generation. Defaults totrue. See the the manual section on Interaction with Documenter.keep_comments: boolean that, if set totrue, keeps markdown lines as comments in the output script. Defaults tofalse.credit: boolean that controls the addition ofThis file was generated with Literate.jl ...to the bottom of the page. If you find Literate.jl useful then feel free to keep this to the default, which istrue.