Using Weave
You can write your documentation and code in input document using Markdown, Noweb or script syntax and use weave
function to execute to document to capture results and figures.
weave
Weave document with markup and julia code using Plots.jl
for plots, out_path = :pwd
makes the results appear in the current working directory.
A prepared example:
Weave.SAMPLE_JL_DOC
# First add depencies for the example
using Pkg; Pkg.add.(["Plots", "DSP"])
using Weave
weave(Weave.SAMPLE_JL_DOC; out_path=:pwd)
Weave.weave
— Functionweave(source::AbstractString; kwargs...)
Weave an input document to output file.
Keyword options
doctype::Union{Nothing,AbstractString} = nothing
: Output document format. By default (i.e. givennothing
), Weave will set it automatically based on file extension. You can also manually specify it; seelist_out_formats()
for the supported formatsinformat::Union{Nothing,AbstractString} = nothing
: Input document format. By default (i.e. givennothing
), Weave will set it automatically based on file extension. You can also specify either of"script"
,"markdown"
,"notebook"
, or"noweb"
out_path::Union{Symbol,AbstractString} = :doc
: Path where the output is generated can be either of::doc
: Path of the source document (default):pwd
: Julia working directory"somepath"
:String
of output directory e.g."~/outdir"
, or of filename e.g."~/outdir/outfile.tex"
args::Any = Dict()
: A runtime object that is available asWEAVE_ARGS
whileweave
ingmod::Union{Module,Nothing} = nothing
: Module where Weaveeval
s code. You can pass aModule
object, otherwise create an new sandbox module.fig_path::Union{Nothing,AbstractString} = nothing
: Where figures will be generated, relative toout_path
. By default (i.e. givennothing
), Weave will automatically createfigures
directory.fig_ext::Union{Nothing,AbstractString} = nothing
: Extension for saved figures e.g.".pdf"
,".png"
. Default setting depends ondoctype
cache_path::AbstractString = "cache"
: Where of cached output will be savedcache::Symbol = :off
: Controls caching of code::off
means no caching (default):all
caches everything:user
caches based on chunk options:refresh
runs all code chunks and save new cache
template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing
: Template (file path) orMustache.MustacheTokens
s formd2html
ormd2tex
formatscss::Union{Nothing,AbstractString} = nothing
: Path of a CSS file used for md2html formathighlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing
: Theme used for syntax highlighting (defaults toHighlights.Themes.DefaultTheme
)pandoc_options::Vector{<:AbstractString} = String[]
:String
s of options to pass to pandoc forpandoc2html
andpandoc2pdf
formats, e.g.["--toc", "-N"]
latex_cmd::Vector{<:AbstractString} = ["xelatex", "-shell-escape", "-halt-on-error"]
: The command used to make PDF file from .texkeep_unicode::Bool = false
: Iftrue
, do not convert unicode characters to their respective latex representation. This is especially useful if a font and tex-engine with support for unicode characters are used
Run Weave from terminal and try to avoid weaving from IJulia or ESS; they tend to mess with capturing output.
tangle
Tangling extracts the code from document:
Weave.tangle
— Functiontangle(source::AbstractString; kwargs...)
Tangle source code from input document to .jl file.
Keyword options
informat::Union{Nothing,AbstractString} = nothing
: Input document format. By default (i.e. givennothing
), Weave will set it automatically based on file extension. You can also specify either of"script"
,"markdown"
,"notebook"
, or"noweb"
out_path::Union{Symbol,AbstractString} = :doc
: Path where the output is generated can be either of::doc
: Path of the source document (default):pwd
: Julia working directory"somepath"
:String
of output directory e.g."~/outdir"
, or of filename e.g."~/outdir/outfile.tex"
Supported Output Formats
Weave automatically detects the output format based on the file extension. The auto output format detection is handled by detect_doctype(path::AbstractString)
:
function detect_doctype(path::AbstractString)
_, ext = lowercase.(splitext(path))
match(r"^\.(jl|.?md|ipynb)", ext) !== nothing && return "md2html"
ext == ".rst" && return "rst"
ext == ".tex" && return "texminted"
ext == ".txt" && return "asciidoc"
return "pandoc"
end
You can also manually specify it using the doctype
keyword option. You can get a list of supported output formats:
Weave.list_out_formats
— Functionlist_out_formats()
List supported output formats with its description.
list_out_formats()
13-element Array{Pair{String,String},1}: "github" => "GitHub Markdown" "md2tex" => "Weave-styled LaTeX" "pandoc2html" => "HTML via intermediate Pandoc Markdown (requires Pandoc 2)" "pandoc" => "Pandoc Markdown" "pandoc2pdf" => "PDF via intermediate Pandoc Markdown" "texminted" => "LaTeX using minted package for code highlighting" "md2html" => "Weave-style HTML" "rst" => "reStructuredText and Sphinx" "multimarkdown" => "MultiMarkdown" "md2pdf" => "PDF via LaTeX" "asciidoc" => "AsciiDoc" "minted2pdf" => "PDF via LaTeX" "hugo" => "Hugo Markdown (using shortcodes)"
Document Syntax
Weave uses markdown, Noweb 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 informat
parameter.
The rules for autodetection are:
ext == ".jl" && return "script"
ext == ".jmd" && return "markdown"
ext == ".ipynb" && return "notebook"
return "noweb"
Documentation Chunks
In markdown and Noweb input formats documentation chunks are the parts that aren't inside code delimiters. Documentation chunks can be written with several different markup languages.
Code Chunks
Code chunks are written in different ways in different formats.
Markdown Format
Weave code chunks are defined using fenced code blocks, same as with common markdown:
```julia
code
...
```
Weave code chunks can optionally be followed by chunk options on the same line. E.g. the chunk below will hide code itself from generated output:
```julia, echo = false
code
...
```
Noweb Format
Code chunks start with a line marked with <<>>=
or <<options>>=
and end with line marked with @
. The code between the start and end markers is executed and the output is captured to the output document.
Inline Code
You can also add inline code to your documents using
`j juliacode`
or
! juliacode
syntax.
The former syntax allows you to insert code anywhere in a line while the !
syntax treats the whole line as code, and the code will be replaced with captured output in the weaved document.
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:
![A plot](`j plot(1:10)`)
or to produce any HTML output:
! display("text/html", "Header from julia");
Script Format
Weave also supports script input format with a markup in comments. These scripts can be executed normally using Julia or published with Weave.
Lines starting with #'
, #%%
or # %%
are treated as document.
All non-document lines are treated as code. You can set chunk options using lines starting with #+
just before code e.g:
#+ term=true
hoge # some code comes here
The format is identical to Pweave and the concept is similar to publishing documents with MATLAB or using Knitr's spin. Weave will remove the first empty space from each line of documentation.
- Here are sample documents:
- Details about chunk options
Configuration via YAML Header
When weave
ing markdown files, you can use YAML header to provide additional metadata and configuration options. See Header Configuration section for more details.
Passing Runtime Arguments to Documents
You can pass arbitrary object to the weaved document using weave
's optional argument args
. It will be available as WEAVE_ARGS
variable in the weave
d document.
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.
E.g. if you call weave("weavefile.jmd", args = (datalocation = "somedata.h5",))
, and then you can retrieve the datalocation
in weavefile.jmd
as follows: WEAVE_ARGS.datalocation
include_weave
You can call include_weave
on a Weave document and run all code chunks within in the current session.
Weave.include_weave
— Functioninclude_weave(source::AbstractString, informat::Union{Nothing,AbstractString} = nothing)
include_weave(m::Module, source::AbstractString, informat::Union{Nothing,AbstractString} = nothing)
Include code from Weave document calling include_string
on all code from doc. Code is run in the path of the include document.