Weave.jl/v0.4.0/usage/index.html

599 lines
27 KiB
HTML

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="no-js ie6"><![endif]-->
<!--[if IE 7 ]><html class="no-js ie7"><![endif]-->
<!--[if IE 8 ]><html class="no-js ie8"><![endif]-->
<!--[if IE 9 ]><html class="no-js ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1">
<title>Using Weave - Weave.jl</title>
<meta name="author" content="Matti Pastell">
<meta property="og:url" content="None">
<meta property="og:title" content="Weave.jl">
<meta property="og:image" content="None/../">
<meta name="apple-mobile-web-app-title" content="Weave.jl">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="shortcut icon" type="image/x-icon" href="../assets/images/favicon-e565ddfa3b.ico">
<link rel="icon" type="image/x-icon" href="../assets/images/favicon-e565ddfa3b.ico">
<style>
@font-face {
font-family: 'Icon';
src: url('../assets/fonts/icon.eot?52m981');
src: url('../assets/fonts/icon.eot?#iefix52m981')
format('embedded-opentype'),
url('../assets/fonts/icon.woff?52m981')
format('woff'),
url('../assets/fonts/icon.ttf?52m981')
format('truetype'),
url('../assets/fonts/icon.svg?52m981#icon')
format('svg');
font-weight: normal;
font-style: normal;
}
</style>
<link rel="stylesheet" href="../assets/stylesheets/application-a422ff04cc.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,700|Ubuntu+Mono">
<style>
body, input {
font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
}
pre, code {
font-family: 'Ubuntu Mono', 'Courier New', 'Courier', monospace;
}
</style>
<link rel="stylesheet" href="../assets/Documenter.css">
<script src="../assets/javascripts/modernizr-4ab42b99fd.js"></script>
</head>
<body class=" ">
<div class="backdrop">
<div class="backdrop-paper"></div>
</div>
<input class="toggle" type="checkbox" id="toggle-drawer">
<input class="toggle" type="checkbox" id="toggle-search">
<label class="toggle-button overlay" for="toggle-drawer"></label>
<header class="header">
<nav aria-label="Header">
<div class="bar default">
<div class="button button-menu" role="button" aria-label="Menu">
<label class="toggle-button icon icon-menu" for="toggle-drawer">
<span></span>
</label>
</div>
<div class="stretch">
<div class="title">
<span class="path">
</span>
Using Weave
</div>
</div>
<div class="button button-twitter" role="button" aria-label="Twitter">
<a href="https://twitter.com/mpastell" title="@mpastell on Twitter" target="_blank" class="toggle-button icon icon-twitter"></a>
</div>
<div class="button button-search" role="button" aria-label="Search">
<label class="toggle-button icon icon-search" title="Search" for="toggle-search"></label>
</div>
</div>
<div class="bar search">
<div class="button button-close" role="button" aria-label="Close">
<label class="toggle-button icon icon-back" for="toggle-search"></label>
</div>
<div class="stretch">
<div class="field">
<input class="query" type="text" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck>
</div>
</div>
<div class="button button-reset" role="button" aria-label="Search">
<button class="toggle-button icon icon-close" id="reset-search"></button>
</div>
</div>
</nav>
</header>
<main class="main">
<div class="drawer">
<nav aria-label="Navigation">
<a href="https://github.com/mpastell/Weave.jl" class="project">
<div class="banner">
<div class="name">
<strong>
Weave.jl
<span class="version">
v0.4.0
</span>
</strong>
<br>
mpastell/Weave.jl
</div>
</div>
</a>
<div class="scrollable">
<div class="wrapper">
<ul class="repo">
<li class="repo-download">
<a href="https://github.com/mpastell/Weave.jl/archive/v0.4.0.zip" target="_blank" title="Download" data-action="download">
<i class="icon icon-download"></i> Download
</a>
</li>
<li class="repo-stars">
<a href="https://github.com/mpastell/Weave.jl/stargazers" target="_blank" title="Stargazers" data-action="star">
<i class="icon icon-star"></i> Stars
<span class="count">&ndash;</span>
</a>
</li>
</ul>
<hr>
<div class="toc">
<ul>
<li>
<a class="" title="Home" href="..">
Home
</a>
</li>
<li>
<a class="" title="Getting started" href="../getting_started/">
Getting started
</a>
</li>
<li>
<a class="current" title="Using Weave" href="./">
Using Weave
</a>
<ul>
<li class="anchor">
<a title="Weave" href="#weave">
Weave
</a>
</li>
<li class="anchor">
<a title="Weave from shell" href="#weave-from-shell">
Weave from shell
</a>
</li>
<li class="anchor">
<a title="Tangle" href="#tangle">
Tangle
</a>
</li>
<li class="anchor">
<a title="Supported formats" href="#supported-formats">
Supported formats
</a>
</li>
<li class="anchor">
<a title="Document syntax" href="#document-syntax">
Document syntax
</a>
</li>
<li class="anchor">
<a title="Noweb format" href="#noweb-format">
Noweb format
</a>
</li>
<li class="anchor">
<a title="Markdown format" href="#markdown-format">
Markdown format
</a>
</li>
<li class="anchor">
<a title="Script format" href="#script-format">
Script format
</a>
</li>
<li class="anchor">
<a title="Inline code" href="#inline-code">
Inline code
</a>
</li>
<li class="anchor">
<a title="Passing arguments to documents" href="#passing-arguments-to-documents">
Passing arguments to documents
</a>
</li>
</ul>
</li>
<li>
<a class="" title="Publishing scripts" href="../publish/">
Publishing scripts
</a>
</li>
<li>
<a class="" title="Chunk options" href="../chunk_options/">
Chunk options
</a>
</li>
<li>
<a class="" title="Working with Jupyter notebooks" href="../notebooks/">
Working with Jupyter notebooks
</a>
</li>
<li>
<a class="" title="Function index" href="../function_index/">
Function index
</a>
</li>
</ul>
<hr>
<span class="section">The author</span>
<ul>
<li>
<a href="https://twitter.com/mpastell" target="_blank" title="@mpastell on Twitter">
@mpastell on Twitter
</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
<article class="article">
<div class="wrapper">
<p><a id='Using-Weave-1'></a></p>
<h1 id="using-weave">Using Weave</h1>
<p>You can write your documentation and code in input document using Noweb, Markdown or script syntax and use <code>weave</code> function to execute to document to capture results and figures.</p>
<p><a id='Weave-1'></a></p>
<h2 id="weave">Weave</h2>
<p>Weave document with markup and julia code using Gadfly for plots, <code>out_path = :pwd</code> makes the results appear in the current working directory.</p>
<div class="code"><pre><span></span><span class="k">using</span> <span class="n">Weave</span>
<span class="n">weave</span><span class="p">(</span><span class="n">Pkg</span><span class="o">.</span><span class="n">dir</span><span class="p">(</span><span class="s">&quot;Weave&quot;</span><span class="p">,</span><span class="s">&quot;examples&quot;</span><span class="p">,</span><span class="s">&quot;gadfly_sample.mdw&quot;</span><span class="p">),</span> <span class="n">out_path</span> <span class="o">=</span> <span class="o">:</span><span class="n">pwd</span><span class="p">)</span>
</pre></div>
<p>Using PyPlot:</p>
<div class="code"><pre><span></span><span class="n">weave</span><span class="p">(</span><span class="n">Pkg</span><span class="o">.</span><span class="n">dir</span><span class="p">(</span><span class="s">&quot;Weave&quot;</span><span class="p">,</span><span class="s">&quot;examples&quot;</span><span class="p">,</span><span class="s">&quot;julia_sample.mdw&quot;</span><span class="p">),</span> <span class="n">plotlib</span><span class="o">=</span><span class="s">&quot;PyPlot&quot;</span><span class="p">,</span> <span class="n">out_path</span> <span class="o">=</span> <span class="o">:</span><span class="n">pwd</span><span class="p">)</span>
</pre></div>
<p><a id='Weave.weave-Tuple{Any}' href='#Weave.weave-Tuple{Any}'>#</a>
<strong><code>Weave.weave</code></strong> &mdash; <em>Method</em>.</p>
<div class="code"><pre><span></span>weave(source ; doctype = :auto, plotlib=:auto,
informat=:auto, out_path=:doc, args = Dict(),
fig_path = &quot;figures&quot;, fig_ext = nothing,
cache_path = &quot;cache&quot;, cache=:off,
template = nothing, highlight_theme = nothing, css = nothing
latex_cmd = &quot;pdflatex&quot;)
</pre></div>
<p>Weave an input document to output file.</p>
<ul>
<li><code>doctype</code>: :auto = set based on file extension or specify one of the supported formats. See <code>list_out_formats()</code></li>
<li><code>plotlib</code>: <code>"PyPlot"</code>, <code>"Gadfly"</code> or <code>nothing</code></li>
<li><code>informat</code>: :auto = set based on file extension or set to <code>"noweb"</code>, <code>"markdown"</code> or <code>script</code></li>
<li><code>out_path</code>: Path where the output is generated. Can be: <code>:doc</code>: Path of the source document, <code>:pwd</code>: Julia working directory, <code>"somepath"</code>: output directory as a String e.g <code>"/home/mpastell/weaveout"</code> or filename as string e.g. ~/outpath/outfile.tex.</li>
<li><code>args</code>: dictionary of arguments to pass to document. Available as WEAVE_ARGS</li>
<li><code>fig_path</code>: where figures will be generated, relative to out_path</li>
<li><code>fig_ext</code>: Extension for saved figures e.g. <code>".pdf"</code>, <code>".png"</code>. Default setting depends on <code>doctype</code>.</li>
<li><code>cache_path</code>: where of cached output will be saved.</li>
<li><code>cache</code>: controls caching of code: <code>:off</code> = no caching, <code>:all</code> = cache everything, <code>:user</code> = cache based on chunk options, <code>:refresh</code>, run all code chunks and save new cache.</li>
<li><code>template</code> : Template (file path) for md2html or md2tex formats.</li>
<li><code>highlight_theme</code> : Theme (Highlights.AbstractTheme) for used syntax highlighting</li>
<li><code>css</code> : CSS (file path) used for md2html format</li>
<li><code>latex_cmd</code> the command used to make pdf from .tex</li>
</ul>
<p><strong>Note:</strong> Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.</p>
<p><a id='Weave-from-shell-1'></a></p>
<h2 id="weave-from-shell">Weave from shell</h2>
<p>You can also use the <code>weave.jl</code> script under bin directory to weave documents from the shell:</p>
<div class="code"><pre><span></span>$ ./weave.jl
usage: weave.jl <span class="o">[</span>--doctype DOCTYPE<span class="o">]</span> <span class="o">[</span>--plotlib PLOTLIB<span class="o">]</span>
<span class="o">[</span>--informat INFORMAT<span class="o">]</span> <span class="o">[</span>--out_path OUT_PATH<span class="o">]</span>
<span class="o">[</span>--fig_path FIG_PATH<span class="o">]</span> <span class="o">[</span>--fig_ext FIG_EXT<span class="o">]</span> source...
</pre></div>
<p><a id='Tangle-1'></a></p>
<h2 id="tangle">Tangle</h2>
<p>Tangling extracts the code from document:</p>
<p><a id='Weave.tangle-Tuple{Any}' href='#Weave.tangle-Tuple{Any}'>#</a>
<strong><code>Weave.tangle</code></strong> &mdash; <em>Method</em>.</p>
<p><code>tangle(source ; out_path=:doc, informat="noweb")</code></p>
<p>Tangle source code from input document to .jl file.</p>
<ul>
<li><code>informat</code>: <code>"noweb"</code> of <code>"markdown"</code></li>
<li><code>out_path</code>: Path where the output is generated. Can be: <code>:doc</code>: Path of the source document, <code>:pwd</code>: Julia working directory, <code>"somepath"</code>, directory name as a string e.g <code>"/home/mpastell/weaveout"</code></li>
</ul>
<p>or filename as string e.g. ~/outpath/outfile.jl.</p>
<p><a id='Supported-formats-1'></a></p>
<h2 id="supported-formats">Supported formats</h2>
<p>Weave sets the output format based on the file extension, but you can also set it using <code>doctype</code> option. The rules for detecting the format are:</p>
<div class="code"><pre><span></span><span class="n">ext</span> <span class="o">==</span> <span class="s">&quot;.jl&quot;</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;md2html&quot;</span>
<span class="n">contains</span><span class="p">(</span><span class="n">ext</span><span class="p">,</span> <span class="s">&quot;.md&quot;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;md2html&quot;</span>
<span class="n">contains</span><span class="p">(</span><span class="n">ext</span><span class="p">,</span> <span class="s">&quot;.rst&quot;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;rst&quot;</span>
<span class="n">contains</span><span class="p">(</span><span class="n">ext</span><span class="p">,</span> <span class="s">&quot;.tex&quot;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;texminted&quot;</span>
<span class="n">contains</span><span class="p">(</span><span class="n">ext</span><span class="p">,</span> <span class="s">&quot;.txt&quot;</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;asciidoc&quot;</span>
<span class="k">return</span> <span class="s">&quot;pandoc&quot;</span>
</pre></div>
<p>You can get a list of supported output formats:</p>
<div class="code"><pre><span></span><span class="n">list_out_formats</span><span class="p">()</span>
</pre></div>
<div class="code"><pre><span></span><span class="n">github</span><span class="o">:</span> <span class="n">Github</span> <span class="n">markdown</span>
<span class="n">md2tex</span><span class="o">:</span> <span class="n">Julia</span> <span class="n">markdown</span> <span class="n">to</span> <span class="n">latex</span>
<span class="n">pandoc2html</span><span class="o">:</span> <span class="n">Markdown</span> <span class="n">to</span> <span class="n">HTML</span> <span class="o">(</span><span class="n">requires</span> <span class="n">Pandoc</span><span class="o">)</span>
<span class="n">pandoc</span><span class="o">:</span> <span class="n">Pandoc</span> <span class="n">markdown</span>
<span class="n">pandoc2pdf</span><span class="o">:</span> <span class="n">Pandoc</span> <span class="n">markdown</span>
<span class="n">tex</span><span class="o">:</span> <span class="n">Latex</span> <span class="k">with</span> <span class="n">custom</span> <span class="n">code</span> <span class="n">environments</span>
<span class="n">texminted</span><span class="o">:</span> <span class="n">Latex</span> <span class="n">using</span> <span class="n">minted</span> <span class="k">for</span> <span class="n">highlighting</span>
<span class="n">md2html</span><span class="o">:</span> <span class="n">Julia</span> <span class="n">markdown</span> <span class="n">to</span> <span class="n">html</span>
<span class="n">rst</span><span class="o">:</span> <span class="n">reStructuredText</span> <span class="n">and</span> <span class="n">Sphinx</span>
<span class="n">multimarkdown</span><span class="o">:</span> <span class="n">MultiMarkdown</span>
<span class="n">md2pdf</span><span class="o">:</span> <span class="n">Julia</span> <span class="n">markdown</span> <span class="n">to</span> <span class="n">latex</span>
<span class="n">asciidoc</span><span class="o">:</span> <span class="n">AsciiDoc</span>
</pre></div>
<p><a id='Weave.list_out_formats-Tuple{}' href='#Weave.list_out_formats-Tuple{}'>#</a>
<strong><code>Weave.list_out_formats</code></strong> &mdash; <em>Method</em>.</p>
<p><code>list_out_formats()</code></p>
<p>List supported output formats</p>
<p><a id='Document-syntax-1'></a></p>
<h2 id="document-syntax">Document syntax</h2>
<p>Weave uses noweb, markdown or script syntax for defining the code chunks and documentation chunks. You can also weave Jupyter notebooks. The format is detected based on the file extension, but you can also set it manually using the <code>informat</code> parameter.</p>
<p>The rules for autodetection are:</p>
<div class="code"><pre><span></span><span class="n">ext</span> <span class="o">==</span> <span class="s">&quot;.jl&quot;</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;script&quot;</span>
<span class="n">ext</span> <span class="o">==</span> <span class="s">&quot;.jmd&quot;</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;markdown&quot;</span>
<span class="n">ext</span> <span class="o">==</span> <span class="s">&quot;.ipynb&quot;</span> <span class="o">&amp;&amp;</span> <span class="k">return</span> <span class="s">&quot;notebook&quot;</span>
<span class="k">return</span> <span class="s">&quot;noweb&quot;</span>
</pre></div>
<p><a id='Noweb-format-1'></a></p>
<h2 id="noweb-format">Noweb format</h2>
<p><a id='Code-chunks-1'></a></p>
<h3 id="code-chunks">Code chunks</h3>
<p>start with a line marked with <code>&lt;&lt;&gt;&gt;=</code> or <code>&lt;&lt;options&gt;&gt;=</code> and end with line marked with <code>@</code>. The code between the start and end markers is executed and the output is captured to the output document. See <a href="../../chunk_options/">chunk options</a>.</p>
<p><a id='Documentation-chunks-1'></a></p>
<h3 id="documentation-chunks">Documentation chunks</h3>
<p>Are the rest of the document (between <code>@</code> and <code>&lt;&lt;&gt;&gt;=</code> lines and the first chunk be default) and can be written with several different markup languages.</p>
<p><a href="https://github.com/mpastell/Weave.jl/blob/master/examples/julia_sample.mdw">Sample document</a></p>
<p><a id='Markdown-format-1'></a></p>
<h2 id="markdown-format">Markdown format</h2>
<p>Markdown code chunks are defined using fenced code blocks with options following on the same line. e.g. to hide code from output you can use:</p>
<p><code>```julia; echo=false</code></p>
<p><a href="https://github.com/mpastell/Weave.jl/blob/master/examples/gadfly_md_sample.jmd">See sample document:</a></p>
<p><a id='Script-format-1'></a></p>
<h2 id="script-format">Script format</h2>
<p>Weave also support script input format with a markup in comments. These scripts can be executed normally using Julia or published with Weave. Documentation is in lines starting with <code>#'</code>, <code>#%%</code> or <code># %%</code>, and code is executed and results are included in the weaved document.</p>
<p>All lines that are not documentation are treated as code. You can set chunk options using lines starting with <code>#+</code> just before code e.g. <code>#+ term=true</code>.</p>
<p>The format is identical to <a href="http://mpastell.com/pweave/pypublish.html">Pweave</a> and the concept is similar to publishing documents with MATLAB or using Knitr's <a href="http://yihui.name/knitr/demo/stitch/">spin</a>. Weave will remove the first empty space from each line of documentation.</p>
<p><a href="https://github.com/mpastell/Weave.jl/blob/master/examples/FIR_design.jl">See sample document:</a></p>
<p><a id='Inline-code-1'></a></p>
<h2 id="inline-code">Inline code</h2>
<p>You can also add inline code to your documents using</p>
<div class="code"><pre><span></span>`j juliacode`
</pre></div>
<p>syntax. The code will be replaced with the output of running the code. If the code produces figures the filename or base64 encoded string will be added to output e.g. to include a Plots figure in markdown you can use:</p>
<div class="code"><pre><span></span><span class="o">!</span><span class="p">[</span><span class="n">A</span> <span class="n">plot</span><span class="p">](</span><span class="err">`</span><span class="n">j</span> <span class="n">plot</span><span class="p">(</span><span class="mi">1</span><span class="o">:</span><span class="mi">10</span><span class="p">)</span><span class="err">`</span><span class="p">)</span>
</pre></div>
<p><a id='Passing-arguments-to-documents-1'></a></p>
<h2 id="passing-arguments-to-documents">Passing arguments to documents</h2>
<p>You can pass arguments as dictionary to the weaved document using the <code>args</code> argument to <code>weave</code>. The dictionary will be available as <code>WEAVE_ARGS</code> variable in the document.</p>
<p>This makes it possible to create the same report easily for e.g. different date ranges of input data from a database or from files with similar format giving the filename as input. </p>
<p>In order to pass a filename to a document you need call <code>weave</code> using:</p>
<div class="code"><pre><span></span><span class="n">weave</span><span class="p">(</span><span class="s">&quot;mydoc.jmd&quot;</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="kt">Dict</span><span class="p">(</span><span class="s">&quot;filename&quot;</span> <span class="o">=&gt;</span> <span class="s">&quot;somedata.h5&quot;</span><span class="p">))</span>
</pre></div>
<p>and you can access the filename from document as follows:</p>
<div class="code"><pre><span></span> ```julia
print(WEAVE_ARGS[&quot;filename&quot;])
```
</pre></div>
<p>You can use the <code>out_path</code> argument to control the name of the output document.</p>
<aside class="copyright" role="note">
Matti Pastell 2016 &ndash;
Documentation built with
<a href="http://www.mkdocs.org" target="_blank">MkDocs</a>
using the
<a href="http://squidfunk.github.io/mkdocs-material/" target="_blank">
Material
</a>
theme.
</aside>
<footer class="footer">
<nav class="pagination" aria-label="Footer">
<div class="previous">
<a href="../getting_started/" title="Getting started">
<span class="direction">
Previous
</span>
<div class="page">
<div class="button button-previous" role="button" aria-label="Previous">
<i class="icon icon-back"></i>
</div>
<div class="stretch">
<div class="title">
Getting started
</div>
</div>
</div>
</a>
</div>
<div class="next">
<a href="../publish/" title="Publishing scripts">
<span class="direction">
Next
</span>
<div class="page">
<div class="stretch">
<div class="title">
Publishing scripts
</div>
</div>
<div class="button button-next" role="button" aria-label="Next">
<i class="icon icon-forward"></i>
</div>
</div>
</a>
</div>
</nav>
</footer>
</div>
</article>
<div class="results" role="status" aria-live="polite">
<div class="scrollable">
<div class="wrapper">
<div class="meta"></div>
<div class="list"></div>
</div>
</div>
</div>
</main>
<script>
var base_url = '..';
var repo_id = 'mpastell/Weave.jl';
</script>
<script src="../assets/javascripts/application-997097ee0c.js"></script>
<script src="../assets/mathjaxhelper.js"></script>
<script>
(function(i,s,o,g,r,a,m){
i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||
[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;
m.parentNode.insertBefore(a,m)
})(window, document,
'script', 'https://www.google-analytics.com/analytics.js', 'ga');
/* General initialization */
ga('create', 'UA-6474352-10', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
/* Track outbound links */
var buttons = document.querySelectorAll('a');
Array.prototype.map.call(buttons, function(item) {
if (item.host != document.location.host) {
item.addEventListener('click', function() {
var action = item.getAttribute('data-action') || 'follow';
ga('send', 'event', 'outbound', action, item.href);
});
}
});
/* Register handler to log search on blur */
var query = document.querySelector('.query');
query.addEventListener('blur', function() {
if (this.value) {
var path = document.location.pathname;
ga('send', 'pageview', path + '?q=' + this.value);
}
});
</script>
</body>
</html>