Weave.jl/doc/src/usage.md

157 lines
4.5 KiB
Markdown
Raw Normal View History

# Using Weave
2016-12-15 20:08:49 +01:00
You can write your documentation and code in input document using Noweb, Markdown or script
syntax and use `weave` function to execute to document to capture results and figures.
## Weave
2018-08-13 14:51:08 +02:00
Weave document with markup and julia code using `Plots.jl` for plots,
`out_path = :pwd` makes the results appear in the current working directory.
```julia
2018-08-13 14:51:08 +02:00
#First add depencies for the example
using Pkg; Pkg.add.(["Plots", "DSP"])
using Weave
2018-08-13 14:51:08 +02:00
weave(joinpath(dirname(pathof(Weave)), "../examples", "FIR_design.jmd"), out_path=:pwd)
```
```@docs
weave(source)
```
## Tangle
Tangling extracts the code from document:
```@docs
tangle(source)
```
## Supported formats
2016-04-22 15:16:50 +02:00
Weave sets the output format based on the file extension, but you can also set
it using `doctype` option. The rules for detecting the format are:
```julia
ext == ".jl" && return "md2html"
contains(ext, ".md") && return "md2html"
contains(ext, ".rst") && return "rst"
contains(ext, ".tex") && return "texminted"
contains(ext, ".txt") && return "asciidoc"
return "pandoc"
```
You can get a list of supported output formats:
2016-12-16 23:16:40 +01:00
```@example
using Weave # hide
2016-12-15 20:08:49 +01:00
list_out_formats()
```
```@docs
list_out_formats()
```
## Document syntax
2016-04-22 15:16:50 +02:00
Weave uses noweb, markdown 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"
```
2016-12-15 20:08:49 +01:00
## Noweb format
### Code chunks
2016-12-15 20:08:49 +01:00
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/).
### Documentation chunks
Are the rest of the document (between `@` and `<<>>=` lines and the first chunk be default) and can be written with several different markup languages.
[Sample document]( https://github.com/mpastell/Weave.jl/blob/master/examples/julia_sample.mdw)
2016-12-15 20:08:49 +01:00
## Markdown format
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:
` ```julia; echo=false`
[See sample document:](https://github.com/mpastell/Weave.jl/blob/master/examples/gadfly_md_sample.jmd)
## Script format
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
`#'`, `#%%` or `# %%`, and code is executed and results are included
in the weaved document.
All lines that are not documentation are treated as code. You can set chunk options
using lines starting with `#+` just before code e.g. `#+ term=true`.
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-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
## Inline code
You can also add inline code to your documents using
```
`j juliacode`
2018-08-13 14:51:08 +02:00
```
2016-12-29 15:34:39 +01:00
2018-08-13 14:51:08 +02:00
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
2016-12-29 15:34:39 +01:00
added to output e.g. to include a Plots figure in markdown you can use:
```
![A plot](`j plot(1:10)`)
```
2016-12-29 16:06:29 +01:00
## Passing arguments to documents
2018-08-13 14:51:08 +02:00
You can pass arguments as dictionary to the weaved document using the `args` argument
2016-12-29 16:06:29 +01:00
to `weave`. The dictionary will be available as `WEAVE_ARGS` variable in the document.
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:
```
```julia
print(WEAVE_ARGS["filename"])
```
```
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.
## Include Weave document in Julia
You can call `include_weave` on a Weave document to run the contents
of all code chunks in Julia.
```@docs
include_weave(doc, informat=:auto)
2018-08-13 14:51:08 +02:00
```