You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<!DOCTYPE html> |
|
|
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>8. Example · Literate.jl</title><meta name="title" content="8. Example · Literate.jl"/><meta property="og:title" content="8. Example · Literate.jl"/><meta property="twitter:title" content="8. Example · Literate.jl"/><meta name="description" content="Documentation for Literate.jl."/><meta property="og:description" content="Documentation for Literate.jl."/><meta property="twitter:description" content="Documentation for Literate.jl."/><meta property="og:url" content="https://fredrikekre.github.io/Literate.jl/v2/generated/example/"/><meta property="twitter:url" content="https://fredrikekre.github.io/Literate.jl/v2/generated/example/"/><link rel="canonical" href="https://fredrikekre.github.io/Literate.jl/v2/generated/example/"/><script data-outdated-warner src="../../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="../.."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../../assets/documenter.js"></script><script src="../../search_index.js"></script><script src="../../siteinfo.js"></script><script src="../../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../../assets/themeswap.js"></script><link href="../../assets/custom.css" rel="stylesheet" type="text/css"/><link href="../../assets/favicon.ico" rel="icon" type="image/x-icon"/></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../../"><img src="../../assets/logo.png" alt="Literate.jl logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href="../../">Literate.jl</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li><a class="tocitem" href="../../"><strong>1.</strong> Introduction</a></li><li><a class="tocitem" href="../../fileformat/"><strong>2.</strong> File format</a></li><li><a class="tocitem" href="../../pipeline/"><strong>3.</strong> Processing pipeline</a></li><li><a class="tocitem" href="../../outputformats/"><strong>4.</strong> Output formats</a></li><li><a class="tocitem" href="../../customprocessing/"><strong>5.</strong> Custom pre- and post-processing</a></li><li><a class="tocitem" href="../../documenter/"><strong>6.</strong> Interaction with Documenter.jl</a></li><li><a class="tocitem" href="../../tips/"><strong>7.</strong> Tips and tricks</a></li><li class="is-active"><a class="tocitem" href><strong>8.</strong> Example</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href><strong>8.</strong> Example</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href><strong>8.</strong> Example</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/fredrikekre/Literate.jl" title="View the repository on GitHub"><span class="docs-icon fa-brands"></span><span class="docs-label is-hidden-touch">GitHub</span></a><a class="docs-navbar-link" href="https://github.com/fredrikekre/Literate.jl/blob/master/examples/example.jl" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="**8.**-Example"><a class="docs-heading-anchor" href="#**8.**-Example"><strong>8.</strong> Example</a><a id="**8.**-Example-1"></a><a class="docs-heading-anchor-permalink" href="#**8.**-Example" title="Permalink"></a></h1><p><a href="https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v2.16.0/generated/example.ipynb"><img src="https://mybinder.org/badge_logo.svg" alt/></a> <a href="https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v2.16.0/generated/example.ipynb"><img src="https://img.shields.io/badge/show-nbviewer-579ACA.svg" alt/></a></p><p>This is an example generated with Literate based on this source file: <a href="https://github.com/fredrikekre/Literate.jl/blob/master/examples/example.jl"><code>example.jl</code></a>. You are seeing the HTML-output which Documenter has generated based on a markdown file generated with Literate. The corresponding notebook can be viewed in <a href="http://nbviewer.jupyter.org/">nbviewer</a> here: <a href="https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v2.16.0/generated/example.ipynb"><code>example.ipynb</code></a>, and opened in <a href="https://mybinder.org/">binder</a> here: <a href="https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v2.16.0/generated/example.ipynb"><code>example.ipynb</code></a>, and the plain script output can be found here: <a href="../example.jl"><code>example.jl</code></a>.</p><p>It is recommended to have the <a href="https://github.com/fredrikekre/Literate.jl/blob/master/examples/example.jl">source file</a> available when reading this, to better understand how the syntax in the source file corresponds to the output you are seeing.</p><h3 id="Basic-syntax"><a class="docs-heading-anchor" href="#Basic-syntax">Basic syntax</a><a id="Basic-syntax-1"></a><a class="docs-heading-anchor-permalink" href="#Basic-syntax" title="Permalink"></a></h3><p>The basic syntax for Literate is simple, lines starting with <code>#</code> is interpreted as markdown, and all the other lines are interpreted as code. Here is some code:</p><pre><code class="language-julia hljs">x = 1//3 |
|
|
y = 2//5</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2//5</code></pre><p>In markdown sections we can use markdown syntax. For example, we can write <em>text in italic font</em>, <strong>text in bold font</strong> and use <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">links</a>.</p><p>It is possible to filter out lines depending on the output using the <code>#md</code>, <code>#nb</code>, <code>#jl</code> and <code>#src</code> tags (see <a href="../../fileformat/#Filtering-lines">Filtering lines</a>):</p><ul><li>This line starts with <code>#md</code> and is thus only visible in the markdown output.</li></ul><p>The source file is parsed in chunks of markdown and code. Starting a line with <code>#-</code> manually inserts a chunk break. For example, if we want to display the output of the following operations we may insert <code>#-</code> in between. These two code blocks will now end up in different <code>@example</code>-blocks in the markdown output, and two different notebook cells in the notebook output.</p><pre><code class="language-julia hljs">x + y</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">11//15</code></pre><pre><code class="language-julia hljs">x * y</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2//15</code></pre><h3 id="Output-capturing"><a class="docs-heading-anchor" href="#Output-capturing">Output capturing</a><a id="Output-capturing-1"></a><a class="docs-heading-anchor-permalink" href="#Output-capturing" title="Permalink"></a></h3><p>Code chunks are by default placed in Documenter <code>@example</code> 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 <code>execute</code> keyword argument is set to true. Output to <code>stdout</code>/<code>stderr</code> is also captured.</p><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>Note that Documenter currently only displays output to <code>stdout</code>/<code>stderr</code> if there is no other result to show. Since the vector <code>[1, 2, 3, 4]</code> is returned from <code>foo</code>, the printing of <code>"This string is printed to stdout."</code> is hidden.</p></div></div><pre><code class="language-julia hljs">function foo() |
|
|
println("This string is printed to stdout.") |
|
|
return [1, 2, 3, 4] |
|
|
end |
|
|
|
|
|
foo()</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">4-element Vector{Int64}: |
|
|
1 |
|
|
2 |
|
|
3 |
|
|
4</code></pre><p>Just like in the REPL, outputs ending with a semicolon hides the output:</p><pre><code class="language-julia hljs">1 + 1;</code></pre><p>Both Documenter's <code>@example</code> block and notebooks can display images. Here is an example where we generate a simple plot using the <a href="https://github.com/JuliaPlots/Plots.jl">Plots.jl</a> package</p><pre><code class="language-julia hljs">using Plots |
|
|
x = range(0, stop=6π, length=1000) |
|
|
y1 = sin.(x) |
|
|
y2 = cos.(x) |
|
|
plot(x, [y1, y2])</code></pre><img src="795ca914.svg" alt="Example block output"/><h3 id="Custom-processing"><a class="docs-heading-anchor" href="#Custom-processing">Custom processing</a><a id="Custom-processing-1"></a><a class="docs-heading-anchor-permalink" href="#Custom-processing" title="Permalink"></a></h3><p>It is possible to give Literate custom pre- and post-processing functions. For example, here we insert a placeholder value <code>y = 321</code> in the source, and use a preprocessing function that replaces it with <code>y = 321</code> in the rendered output.</p><pre><code class="language-julia hljs">x = 123</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">123</code></pre><p>In this case the preprocessing function is defined by</p><pre><code class="language-julia hljs">function pre(s::String) |
|
|
s = replace(s, "x = 123" => "y = 321") |
|
|
return s |
|
|
end</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">pre (generic function with 1 method)</code></pre><h3 id="documenter-interaction"><a class="docs-heading-anchor" href="#documenter-interaction">Documenter.jl interaction</a><a id="documenter-interaction-1"></a><a class="docs-heading-anchor-permalink" href="#documenter-interaction" title="Permalink"></a></h3><p>In the source file it is possible to use Documenter.jl style references, such as <code>@ref</code> and <code>@id</code>. These will be filtered out in the notebook output. For example, <a href="#documenter-interaction">here is a link</a>, but it is only visible as a link if you are reading the markdown output. We can also use equations:</p><p class="math-container">\[\int_\Omega \nabla v \cdot \nabla u\ \mathrm{d}\Omega = \int_\Omega v f\ \mathrm{d}\Omega\]</p><p>using Documenters math syntax. Documenters syntax is automatically changed to <code>\begin{equation} ... \end{equation}</code> in the notebook output to display correctly.</p><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../tips/">« <strong>7.</strong> Tips and tricks</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="auto">Automatic (OS)</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.1.2 on <span class="colophon-date" title="Wednesday 8 November 2023 09:42">Wednesday 8 November 2023</span>. Using Julia version 1.9.3.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
|
|
|
|