2016-04-21 13:36:55 +02:00
# Using Weave
2019-03-04 14:42:16 +01:00
You can write your documentation and code in input document using Markdown, Noweb or script
2020-03-26 09:35:43 +01:00
syntax and use [`weave` ](@ref ) function to execute to document to capture results and figures.
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
## `weave`
2016-04-21 13:36:55 +02:00
2018-08-13 14:51:08 +02:00
Weave document with markup and julia code using `Plots.jl` for plots,
2016-04-21 13:36:55 +02:00
`out_path = :pwd` makes the results appear in the current working directory.
2020-03-26 09:35:43 +01:00
> A prepared example
2016-04-21 13:36:55 +02:00
```julia
2020-03-26 17:49:48 +01:00
# First add depencies for the example
2018-08-13 14:51:08 +02:00
using Pkg; Pkg.add.(["Plots", "DSP"])
2016-04-21 13:36:55 +02:00
using Weave
2018-08-13 14:51:08 +02:00
weave(joinpath(dirname(pathof(Weave)), "../examples", "FIR_design.jmd"), out_path=:pwd)
2016-04-21 13:36:55 +02:00
```
2016-05-18 22:12:01 +02:00
```@docs
2020-03-26 09:35:43 +01:00
weave
2016-05-18 22:12:01 +02:00
```
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
## `tangle`
2016-04-21 13:36:55 +02:00
Tangling extracts the code from document:
2016-05-18 22:12:01 +02:00
```@docs
2020-03-26 17:49:48 +01:00
tangle
2016-05-18 22:12:01 +02:00
```
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
## Supported Output Formats
2016-04-21 13:36:55 +02:00
2020-03-27 12:34:06 +01:00
Weave automatically detects the output format based on the file extension.
The auto output format detection is handled by `detect_doctype(path::AbstractString)` :
2016-04-22 15:16:50 +02:00
```julia
2020-03-27 12:34:06 +01:00
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
2016-04-22 15:16:50 +02:00
```
2020-03-27 12:34:06 +01:00
You can also manually specify it using the `doctype` keyword option.
2016-04-21 13:36:55 +02:00
You can get a list of supported output formats:
2020-03-26 09:35:43 +01:00
```@docs
list_out_formats
2016-04-21 13:36:55 +02:00
```
2020-03-26 09:35:43 +01:00
```@example
using Weave # hide
2016-05-18 22:12:01 +02:00
list_out_formats()
```
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
## [Document Syntax](@id document-syntax)
2016-04-21 13:36:55 +02:00
2019-03-04 14:42:16 +01:00
Weave uses markdown, Noweb or script syntax for defining the code chunks and
2016-12-12 13:05:26 +01:00
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.
2016-04-22 15:16:50 +02:00
The rules for autodetection are:
2016-12-12 20:40:05 +01:00
```julia
2016-04-22 15:16:50 +02:00
ext == ".jl" & & return "script"
ext == ".jmd" & & return "markdown"
2016-12-12 13:05:26 +01:00
ext == ".ipynb" & & return "notebook"
2016-04-22 15:16:50 +02:00
return "noweb"
```
2020-05-16 10:52:17 +02:00
## Documentation Chunks
2016-04-21 13:36:55 +02:00
2020-05-16 13:33:48 +02:00
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.
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
## Code Chunks
2016-04-21 13:36:55 +02:00
2020-05-16 10:52:17 +02:00
### Markdown Format
2016-12-15 20:08:49 +01:00
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:
2019-03-10 22:09:27 +01:00
```
2019-09-28 09:41:54 +02:00
```julia; echo=false
2019-03-10 22:09:27 +01:00
```
2016-12-15 20:08:49 +01:00
2019-03-04 14:42:16 +01:00
[Sample document ]( https://github.com/mpastell/Weave.jl/blob/master/examples/FIR_design.jmd )
2020-05-16 10:52:17 +02:00
## [Inline Code](@id inline-code)
2019-03-10 18:23:18 +01:00
You can also add inline code to your documents using
```
`j juliacode`
```
or
```
! juliacode
```
2020-03-26 09:35:43 +01:00
syntax. Using the `j code` syntax you can insert code anywhere in a line and with
2019-03-10 21:53:00 +01:00
the `!` syntax the whole line after `!` will be executed. The code will be replaced
with captured output in the weaved document.
2019-03-10 18:23:18 +01:00
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 )`)
```
2019-03-10 21:53:00 +01:00
or to produce any html output:
2019-03-10 18:23:18 +01:00
```
! display("text/html", "Header from julia");
```
2019-03-04 14:42:16 +01:00
2020-05-16 10:52:17 +02:00
### Noweb Format
2019-03-04 14:42:16 +01:00
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. See [chunk options ](../chunk_options/ ).
2016-12-15 20:08:49 +01:00
2020-05-16 10:52:17 +02:00
### Script Format
2016-12-15 20:08:49 +01:00
Weave also support script input format with a markup in comments.
2020-05-16 10:52:17 +02:00
These scripts can be executed normally using Julia or published with Weave.
2016-12-15 20:08:49 +01:00
2020-05-16 10:52:17 +02:00
Lines starting with `#'` , `#%%` or `# %%` are treated as document.
2016-12-15 20:08:49 +01:00
2020-05-16 10:52:17 +02:00
All non-document lines are treated as code.
You can set chunk options using lines starting with `#+` just before code e.g:
```julia
#+ term=true
hoge # some code comes here
```
2016-12-15 20:08:49 +01:00
2020-05-16 10:52:17 +02:00
The format is identical to [Pweave ](http://mpastell.com/pweave/pypublish.html ) and the concept is similar to publishing documents with MATLAB or using Knitr's [spin ](http://yihui.name/knitr/demo/stitch/ ).
Weave will remove the first empty space from each line of documentation.
2016-04-21 13:36:55 +02:00
2016-12-15 20:08:49 +01:00
[See sample document: ](https://github.com/mpastell/Weave.jl/blob/master/examples/FIR_design.jl )
2016-12-29 15:34:39 +01:00
2019-03-04 10:10:02 +01:00
2020-05-16 10:52:17 +02:00
## Configuration via YAML Header
2019-03-04 10:10:02 +01:00
2020-05-16 13:49:52 +02:00
When `weave` ing markdown files, you can use YAML header to provide additional metadata and configuration options.
2020-05-16 10:52:17 +02:00
See [Header Configuration ](@ref ) section for more details.
2019-03-04 10:10:02 +01:00
2019-03-05 15:44:10 +01:00
2020-05-16 10:52:17 +02:00
## Passing Runtime Arguments to Documents
2016-12-29 16:06:29 +01:00
2020-03-26 09:35:43 +01:00
You can pass arguments as `Dict` to the weaved document using the `args` argument
to `weave` . The arguments will be available as `WEAVE_ARGS` variable in the document.
2016-12-29 16:06:29 +01:00
2018-08-13 14:51:08 +02:00
This makes it possible to create the same report easily for e.g. different
2016-12-29 16:06:29 +01:00
date ranges of input data from a database or from files with similar format giving the
2018-08-13 14:51:08 +02:00
filename as input.
2016-12-29 16:06:29 +01:00
In order to pass a filename to a document you need call `weave` using:
```julia
weave("mydoc.jmd", args = Dict("filename" => "somedata.h5"))
```
and you can access the filename from document as follows:
```
2020-05-16 10:52:17 +02:00
```julia
print(WEAVE_ARGS["filename"])
```
2016-12-29 16:06:29 +01:00
```
2018-08-13 14:51:08 +02:00
You can use the `out_path` argument to control the name of the
2018-01-07 11:31:06 +01:00
output document.
2020-05-16 10:52:17 +02:00
## `include_weave`
You can call `include_weave` on a Weave document and run all code chunks within in the current session.
2018-01-07 11:31:06 +01:00
```@docs
2020-03-06 02:14:21 +01:00
include_weave
2018-08-13 14:51:08 +02:00
```