Debian Codegraph @

inst/doc/lua-filters.Rmd

raw
1 ---
2 title: "Lua filters in R Markdown"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Lua filters in R Markdown}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r setup, include = FALSE}
11 knitr::opts_chunk$set(
12 collapse = TRUE,
13 comment = "#>"
14 )
15 ```
16
17 This vignettes gives some detailed information regarding the Lua filters including in the **rmarkdown** package. You don't have to do anything to use them usually as they just works usaully. This document presents the features they power.
18
19 To know more about Lua filters in general, you can jump directly to [last section](#lua-filter)
20
21 ## Pagebreaks
22
23 This filter is available since **rmarkdown** 1.15 and works with Pandoc 2.1+ (shipped in RStudio >= 1.2).
24
25 Adding a pagebreak in document was always possible using custom output specific syntax in a rmarkdown file but one drawback was the compatibility with several output format.
26
27 With this Lua filter, it is possible to add a `\newpage` or `\pagebreak` command in a new line to include a pagebreak in any of these formats: `pdf_document()`, `html_document()`, `word_document()` and `odt_document()`.
28
29 ```md
30 # Header 1
31
32 Some text
33
34 \newpage
35
36 # Header 2 on a new page
37
38 Some other text
39
40 \pagebreak
41
42 # Header 3 on a third page
43
44 ```
45
46 rmarkdown will convert those commands in the correct output format syntax using a [Lua filter](#lua-filter) during pandoc conversion.
47
48 ### Using with PDF/ latex documents {#pdf}
49
50 As the commands are the ones already used in latex syntax, this works as expected in a tex output document, and thus with pdf. Adding a pagebreak was already possible with rmarkdown when output is `pdf_document()` or `latex_document()`, without any restriction about the version of pandoc.
51
52 ### Using with HTML documents {#html}
53
54 A `\newpage` or `\pagebreak` command in a rmarkdown document with output as HTML will be converted by default in this html code with inline style using CSS rule [`page-break-after`](https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after)
55
56 ```html
57 <div style="page-break-after: always;"></div>
58 ```
59
60 This will always insert a pagebreak after this div.
61
62 To get more flexibility, you can use a HTML class and some custom CSS instead of an inline style. You need to add a metadata field `newpage_html_class` in your yaml header to set the class.
63
64 Then you can control the behavior using custom CSS as in this example
65
66 ````md
67 ---
68 output:
69 html_document: default
70 newpage_html_class: page-break
71 ---
72
73 ```{css, echo = FALSE}`r ''`
74 // display the pagebreak only when printing the html page
75 @media all {
76 .page-break { display: none; }
77 }
78 @media print {
79 .page-break { display: block; break-after: page; }
80 }
81 ```
82
83 # Header 1
84
85 Some text
86
87 \newpage
88
89 # Header 2 on a new page
90
91 Some other text
92 ````
93
94 `\newpage` will be converted here to
95
96 ```html
97 <div class="page-break"></div>
98 ```
99
100 and the style will be applied to this class from the CSS included in the chunk.
101
102 This customisation can also be achieved by setting the environnement variable `PANDOC_NEWPAGE_HTML_CLASS` in the R session that will render the document (or in `.Renviron` file for example)
103
104 Let's note that in this example we use [`break-after`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-after) property instead of `page-break-after` as it is recommended now to use the former which is the replacement. The latter is kept around for compatibility reason with browsers.
105
106 ### Using with Word documents {#word}
107
108 A `\newpage` or `\pagebreak` command in a rmarkdown document with output as Word document will be converted in a pagebreak for word document. Manually, this would mean adding this in your rmarkdown
109
110 ````md
111 ```{=openxml}
112 <w:p><w:r><w:br w:type="page"/></w:r></w:p>
113 ```
114 ````
115
116 For example, using the pagebreak feature, this will add the first header in the second page of the work document
117
118 ````md
119 ---
120 title: My main title
121 output: word_document
122 ---
123
124 \newpage
125
126 # First Header
127 ````
128
129 ### Using with ODT documents {#odt}
130
131 To use the pagebreak feature with `odt_document()`, you need to provide a reference document that includes a paragraph style with, by default, the name _Pagebreak_. This named paragraph style should have no extra space before or after and have a pagebreak after it. (see [libre office documentation](https://help.libreoffice.org/Writer/Text_Flow) on how to create a style).
132
133 The name of the named paragrah style could be customized using `newpage_odt_style` metadata in yaml header or `PANDOC_NEWPAGE_ODT_STYLE` environment variable (as in [html document](#html)).
134
135 As the previous one, this example will lead to a two pages document, with first header on the second page.
136
137 ````md
138 ---
139 title: My main title
140 output:
141 odt_document:
142 reference_odt: reference.odt
143 ---
144
145 \newpage
146
147 # First Header
148 ````
149
150
151
152 ## Number sections
153
154 This filter is available since **rmarkdown** 2.4 and works with Pandoc 2.1+ (shipped in RStudio >= 1.2).
155
156 Numbering sections are supported by Pandoc for limited formats (e.g., html and pdf).
157 The rmarkdown package adds `number_sections.lua` to support this feature in other formats (e.g., docx, odt, and so on).
158 Users do not have to know which formats use the Pandoc's feature or the Lua filter.
159
160 ```md
161 ---
162 title: My main title
163 output:
164 md_document:
165 number_sections: true # implemented by Lua filter
166 html_document
167 number_sections: true # implemented by Pandoc
168 ---
169
170 # First Header
171
172 ## A Header Belonging the First Header
173
174 # Second Header
175 ```
176
177 In general, numbers and titles of sections are separated by a space.
178 An exception is the `word_document` function, which separates them by a tab in order to be consistent with Pandoc's number sections for docx format in Pandoc >= 2.10.1.
179 If one wants to have fine controls on the format of section numbers, prepare customized docx file and specify it to the `reference_docx` argument of the `word_document` function.
180
181 ## Custom environment for LaTeX
182
183 This filter is available since **rmarkdown** 1.15 and works with Pandoc 2.1+ (shipped in RStudio >= 1.2).
184
185 Pandoc supports a syntax called [Fenced Divs](https://pandoc.org/MANUAL.html#divs-and-spans) that allows to create `<div>` in HTML. For example, the syntax is
186
187 ```markdown
188 ::: {#special .sidebar}
189 Here is a paragraph.
190
191 And another.
192 :::
193 ```
194 By default, this special syntax will be ignored in LaTeX conversion for PDF output and the content of the block will be used as-is. However, this syntax could be used to create LaTeX environment.
195
196 That is why **rmarkdown** uses a custom Lua filter to allow any Fenced Divs to be converted as a LaTeX environment (`\begin{env}` ... `\end{env}`) when desired by adding a special attribute.
197
198 ```markdown
199 ::: {.verbatim data-latex=""}
200 We show some _verbatim_ text here.
201 :::
202 ```
203 Its LaTeX output will be:
204
205 ```latex
206 \begin{verbatim}
207 We show some \emph{verbatim} text here.
208 \end{verbatim}
209 ```
210
211 This feature we call _Custom blocks_ is documented in details in the [R Markdown Cookbook](https://yihui.org/rmarkdown-cookbook/custom-blocks.html)
212
213 ## About Lua filters {#lua-filter}
214
215 Since pandoc 2.0, it is possible to use Lua filters to add some extra functionality to pandoc document conversion. Adding a pagebreak command in markdown to be compatible with several output documents is one of them. You can find some more informations about Lua filters in [pandoc's documentation](https://pandoc.org/lua-filters.html) and also some examples in [a collection of Lua filters for pandoc](https://github.com/pandoc/lua-filters). These examples, and any other Lua filters, can be use in your Rmarkdown document directly by adding [a pandoc argument](https://yihui.org/rmarkdown/html-document.html) in yaml header
216
217 ```html
218 ---
219 output:
220 html_document:
221 pandoc_args: ["--lua-filter=filter.lua"]
222 ---
223 ```
224
225 You can also use a special helper using a `!expr` syntax for yaml in your header
226
227 The package [rmdfiltr](https://github.com/crsh/rmdfiltr) provides a collection of Lua filters and helpers functions to use them.
228
229 Before pandoc 2.0, [using filter](https://pandoc.org/filters.html) with pandoc was already available through programs that modifies the AST. `pandoc-citeproc` is an example used to deal with citations. The package [pandocfilter](https://cran.r-project.org/package=pandocfilters) is useful to create filters using R.
230
231 ### Using filters with output format
232
233 If you want to create a format that uses a special filter, you can add it as a pandoc option if the output format.
234
235 Either by modifying an existing format:
236
237 ```{r modifying-format}
238 custom_format <- function(...) {
239 base_format <- rmarkdown::html_document(...)
240 # prepending a new Lua filter to html_document() ones
241 base_format$pandoc$lua_filters <- c(
242 rmarkdown::pandoc_path_arg("new.lua"),
243 base_format$pandoc$lua_filters)
244 base_format
245 }
246
247 basename(custom_format()$pandoc$lua_filters)
248 ```
249 Or by creating a new format :
250
251 ```{r creating-format}
252 custom_format <- function(toc = TRUE, ...) {
253 rmarkdown::output_format(
254 knitr = rmarkdown::knitr_options(),
255 # a new filter will be appended to base_format ones
256 pandoc = rmarkdown::pandoc_options(to = "html", lua_filters = "new.lua"),
257 base_format = rmarkdown::html_document(toc = toc, ...)
258 )
259 }
260
261 basename(custom_format()$pandoc$lua_filters)
262 ```
263
264 As this is an advanced feature, if you want to go further, we encourage you to look at the examples of formats in packages such as **rmarkdown**, **bookdown** or **pagedown**.