2020-05-24 19:05:34 +02:00
# [Chunk Options](@id chunk-options)
2016-04-21 13:36:55 +02:00
2020-05-24 19:05:34 +02:00
You can use chunk options to configure how each chunk is evaluated, rendered, etc.
Most of the ideas came from [chunk options in RMarkdown ](http://yihui.name/knitr/options ).
2016-04-21 13:36:55 +02:00
2019-03-04 14:42:16 +01:00
2020-05-24 19:05:34 +02:00
## Syntax
2016-04-21 13:36:55 +02:00
2020-05-25 05:22:33 +02:00
Chunk options come after [code chunk ](@ref code-chunks ) header.
2020-05-24 19:05:34 +02:00
There are two (slightly) different syntax to write them:
- (Julia's toplevel expression) options are separated by semicolon (`;`)
- (RMarkdown style) options are separated by comma (`,`)
2016-04-21 13:36:55 +02:00
2020-05-24 19:05:34 +02:00
Let's take a look at examples. All the following code chunk header are valid,
and so configured to hide the source code from generated output (`echo = false`)
and displays figures with 12cm width (`out_width = "12cm"`):
```md
```julia; echo = false; out_width = "12cm"
2020-05-16 10:52:17 +02:00
2020-05-24 19:05:34 +02:00
```{julia; echo = false; out_width = "12cm"}
```julia, echo = false, out_width = "12cm"
```{julia, echo = false, out_width = "12cm"}
```
## Weave Chunk Options
Weave currently supports the following chunk options:
we've mostly followed [RMarkdown's namings ](http://yihui.name/knitr/options ), but not all options are implemented.
### Evaluation
- `eval = true` : Evaluate the code chunk. If `false` the chunk won’ t be executed.
2020-06-13 16:09:51 +02:00
- `error = true` : If `true` [`weave` ](@ref ) won't stop on errors and rather they will be included in output document. If `false` , [`weave` ](@ref ) will halt on any of un-caught errors.
2020-05-24 19:05:34 +02:00
- `cache = false` : Cache results, depending on `cache` parameter on [`weave` ](@ref ) function.
- `tangle = true` : Set tangle to `false` to exclude chunk from tangled code.
### Rendering
2016-04-21 13:36:55 +02:00
2020-03-26 13:41:54 +01:00
- `echo = true` : Echo the code in the output document. If `false` the source code will be hidden.
- `results = "markup"` : The output format of the printed results. `"markup"` for literal block, `"hidden"` for hidden results, or anything else for raw output (I tend to use `"tex"` for Latex and `"rst"` for rest). Raw output is useful if you want to e.g. create tables from code chunks.
- `term = false` : If `true` the output emulates a REPL session. Otherwise only stdout and figures will be included in output.
- `wrap = true` : Wrap long lines from output.
- `line_width = 75` : Line width for wrapped lines.
- `hold = false` : Hold all results until the end of the chunk.
2016-04-21 13:36:55 +02:00
2020-05-24 19:05:34 +02:00
### Figures
2020-05-16 10:52:17 +02:00
2020-05-24 19:05:34 +02:00
- `label = nothing` : Chunk label, will be used for figure labels in Latex as `fig:label` .
2020-03-26 13:41:54 +01:00
- `fig_width = 6` : Figure width passed to plotting library.
- `fig_height = 4` : Figure height passed to plotting library.
- `out_width` : Width of saved figure in output markup e.g. `"50%"` , `"12cm"` , `0.5\linewidth`
- `out_height` : Height of saved figure in output markup
- `dpi = 96` : Resolution of saved figures.
- `fig_cap` : Figure caption.
- `fig_ext` : File extension (format) of saved figures.
- `fig_pos = "!h"` : Figure position in Latex, e.g.: `"ht"` .
- `fig_env = "figure"` : Figure environment in Latex.
2016-04-21 13:36:55 +02:00
2020-05-24 19:05:34 +02:00
## Default Chunk Options
2016-04-21 13:36:55 +02:00
2020-05-18 15:10:26 +02:00
You can set the default chunk options (and `weave` arguments) for a document using `weave_options` key in YAML [Header Configuration ](@ref ).
E.g. to set the default `out_width` of all figures you can use:
2016-04-21 13:36:55 +02:00
2019-03-05 18:36:27 +01:00
```yaml
---
2020-05-18 15:10:26 +02:00
weave_options:
2020-05-16 10:52:17 +02:00
out_width : 50%
2019-03-05 18:36:27 +01:00
---
```
2016-04-21 13:36:55 +02:00
2020-03-06 01:20:32 +01:00
You can also set or change the default chunk options for a document either before weave using the `set_chunk_defaults` function.
2016-04-21 13:36:55 +02:00
2016-05-18 22:12:01 +02:00
```@docs
2020-05-17 05:23:41 +02:00
set_chunk_defaults!
2020-03-06 01:20:32 +01:00
get_chunk_defaults
2020-05-17 05:23:41 +02:00
restore_chunk_defaults!
2016-05-18 22:12:01 +02:00
```