Debian Codegraph @

inst/doc/lua-filters.html

raw symbols SCIP
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13
14
15 <title>Lua filters in R Markdown</title>
16
17 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
18 // be compatible with the behavior of Pandoc < 2.8).
19 document.addEventListener('DOMContentLoaded', function(e) {
20 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
21 var i, h, a;
22 for (i = 0; i < hs.length; i++) {
23 h = hs[i];
24 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
25 a = h.attributes;
26 while (a.length > 0) h.removeAttribute(a[0].name);
27 }
28 });
29 </script>
30
31 <style type="text/css">
32 code{white-space: pre-wrap;}
33 span.smallcaps{font-variant: small-caps;}
34 span.underline{text-decoration: underline;}
35 div.column{display: inline-block; vertical-align: top; width: 50%;}
36 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
37 ul.task-list{list-style: none;}
38 </style>
39
40
41
42 <style type="text/css">
43 code {
44 white-space: pre;
45 }
46 .sourceCode {
47 overflow: visible;
48 }
49 </style>
50 <style type="text/css" data-origin="pandoc">
51 html { -webkit-text-size-adjust: 100%; }
52 pre > code.sourceCode { white-space: pre; position: relative; }
53 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
54 pre > code.sourceCode > span:empty { height: 1.2em; }
55 .sourceCode { overflow: visible; }
56 code.sourceCode > span { color: inherit; text-decoration: inherit; }
57 div.sourceCode { margin: 1em 0; }
58 pre.sourceCode { margin: 0; }
59 @media screen {
60 div.sourceCode { overflow: auto; }
61 }
62 @media print {
63 pre > code.sourceCode { white-space: pre-wrap; }
64 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
65 }
66 pre.numberSource code
67 { counter-reset: source-line 0; }
68 pre.numberSource code > span
69 { position: relative; left: -4em; counter-increment: source-line; }
70 pre.numberSource code > span > a:first-child::before
71 { content: counter(source-line);
72 position: relative; left: -1em; text-align: right; vertical-align: baseline;
73 border: none; display: inline-block;
74 -webkit-touch-callout: none; -webkit-user-select: none;
75 -khtml-user-select: none; -moz-user-select: none;
76 -ms-user-select: none; user-select: none;
77 padding: 0 4px; width: 4em;
78 color: #aaaaaa;
79 }
80 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
81 div.sourceCode
82 { }
83 @media screen {
84 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
85 }
86 code span.al { color: #ff0000; font-weight: bold; }
87 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
88 code span.at { color: #7d9029; }
89 code span.bn { color: #40a070; }
90 code span.bu { color: #008000; }
91 code span.cf { color: #007020; font-weight: bold; }
92 code span.ch { color: #4070a0; }
93 code span.cn { color: #880000; }
94 code span.co { color: #60a0b0; font-style: italic; }
95 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
96 code span.do { color: #ba2121; font-style: italic; }
97 code span.dt { color: #902000; }
98 code span.dv { color: #40a070; }
99 code span.er { color: #ff0000; font-weight: bold; }
100 code span.ex { }
101 code span.fl { color: #40a070; }
102 code span.fu { color: #06287e; }
103 code span.im { color: #008000; font-weight: bold; }
104 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
105 code span.kw { color: #007020; font-weight: bold; }
106 code span.op { color: #666666; }
107 code span.ot { color: #007020; }
108 code span.pp { color: #bc7a00; }
109 code span.sc { color: #4070a0; }
110 code span.ss { color: #bb6688; }
111 code span.st { color: #4070a0; }
112 code span.va { color: #19177c; }
113 code span.vs { color: #4070a0; }
114 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
115 </style>
116 <script>
117 // apply pandoc div.sourceCode style to pre.sourceCode instead
118 (function() {
119 var sheets = document.styleSheets;
120 for (var i = 0; i < sheets.length; i++) {
121 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
122 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
123 var j = 0;
124 while (j < rules.length) {
125 var rule = rules[j];
126 // check if there is a div.sourceCode rule
127 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
128 j++;
129 continue;
130 }
131 var style = rule.style.cssText;
132 // check if color or background-color is set
133 if (rule.style.color === '' && rule.style.backgroundColor === '') {
134 j++;
135 continue;
136 }
137 // replace div.sourceCode by a pre.sourceCode rule
138 sheets[i].deleteRule(j);
139 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
140 }
141 }
142 })();
143 </script>
144
145
146
147
148 <style type="text/css">body {
149 background-color: #fff;
150 margin: 1em auto;
151 max-width: 700px;
152 overflow: visible;
153 padding-left: 2em;
154 padding-right: 2em;
155 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
156 font-size: 14px;
157 line-height: 1.35;
158 }
159 #TOC {
160 clear: both;
161 margin: 0 0 10px 10px;
162 padding: 4px;
163 width: 400px;
164 border: 1px solid #CCCCCC;
165 border-radius: 5px;
166 background-color: #f6f6f6;
167 font-size: 13px;
168 line-height: 1.3;
169 }
170 #TOC .toctitle {
171 font-weight: bold;
172 font-size: 15px;
173 margin-left: 5px;
174 }
175 #TOC ul {
176 padding-left: 40px;
177 margin-left: -1.5em;
178 margin-top: 5px;
179 margin-bottom: 5px;
180 }
181 #TOC ul ul {
182 margin-left: -2em;
183 }
184 #TOC li {
185 line-height: 16px;
186 }
187 table {
188 margin: 1em auto;
189 border-width: 1px;
190 border-color: #DDDDDD;
191 border-style: outset;
192 border-collapse: collapse;
193 }
194 table th {
195 border-width: 2px;
196 padding: 5px;
197 border-style: inset;
198 }
199 table td {
200 border-width: 1px;
201 border-style: inset;
202 line-height: 18px;
203 padding: 5px 5px;
204 }
205 table, table th, table td {
206 border-left-style: none;
207 border-right-style: none;
208 }
209 table thead, table tr.even {
210 background-color: #f7f7f7;
211 }
212 p {
213 margin: 0.5em 0;
214 }
215 blockquote {
216 background-color: #f6f6f6;
217 padding: 0.25em 0.75em;
218 }
219 hr {
220 border-style: solid;
221 border: none;
222 border-top: 1px solid #777;
223 margin: 28px 0;
224 }
225 dl {
226 margin-left: 0;
227 }
228 dl dd {
229 margin-bottom: 13px;
230 margin-left: 13px;
231 }
232 dl dt {
233 font-weight: bold;
234 }
235 ul {
236 margin-top: 0;
237 }
238 ul li {
239 list-style: circle outside;
240 }
241 ul ul {
242 margin-bottom: 0;
243 }
244 pre, code {
245 background-color: #f7f7f7;
246 border-radius: 3px;
247 color: #333;
248 white-space: pre-wrap;
249 }
250 pre {
251 border-radius: 3px;
252 margin: 5px 0px 10px 0px;
253 padding: 10px;
254 }
255 pre:not([class]) {
256 background-color: #f7f7f7;
257 }
258 code {
259 font-family: Consolas, Monaco, 'Courier New', monospace;
260 font-size: 85%;
261 }
262 p > code, li > code {
263 padding: 2px 0px;
264 }
265 div.figure {
266 text-align: center;
267 }
268 img {
269 background-color: #FFFFFF;
270 padding: 2px;
271 border: 1px solid #DDDDDD;
272 border-radius: 3px;
273 border: 1px solid #CCCCCC;
274 margin: 0 5px;
275 }
276 h1 {
277 margin-top: 0;
278 font-size: 35px;
279 line-height: 40px;
280 }
281 h2 {
282 border-bottom: 4px solid #f7f7f7;
283 padding-top: 10px;
284 padding-bottom: 2px;
285 font-size: 145%;
286 }
287 h3 {
288 border-bottom: 2px solid #f7f7f7;
289 padding-top: 10px;
290 font-size: 120%;
291 }
292 h4 {
293 border-bottom: 1px solid #f7f7f7;
294 margin-left: 8px;
295 font-size: 105%;
296 }
297 h5, h6 {
298 border-bottom: 1px solid #ccc;
299 font-size: 105%;
300 }
301 a {
302 color: #0033dd;
303 text-decoration: none;
304 }
305 a:hover {
306 color: #6666ff; }
307 a:visited {
308 color: #800080; }
309 a:visited:hover {
310 color: #BB00BB; }
311 a[href^="http:"] {
312 text-decoration: underline; }
313 a[href^="https:"] {
314 text-decoration: underline; }
315
316 code > span.kw { color: #555; font-weight: bold; }
317 code > span.dt { color: #902000; }
318 code > span.dv { color: #40a070; }
319 code > span.bn { color: #d14; }
320 code > span.fl { color: #d14; }
321 code > span.ch { color: #d14; }
322 code > span.st { color: #d14; }
323 code > span.co { color: #888888; font-style: italic; }
324 code > span.ot { color: #007020; }
325 code > span.al { color: #ff0000; font-weight: bold; }
326 code > span.fu { color: #900; font-weight: bold; }
327 code > span.er { color: #a61717; background-color: #e3d2d2; }
328 </style>
329
330
331
332
333 </head>
334
335 <body>
336
337
338
339
340 <h1 class="title toc-ignore">Lua filters in R Markdown</h1>
341
342
343
344 <p>This vignettes gives some detailed information regarding the Lua
345 filters including in the <strong>rmarkdown</strong> package. You don’t
346 have to do anything to use them usually as they just works usaully. This
347 document presents the features they power.</p>
348 <p>To know more about Lua filters in general, you can jump directly to
349 <a href="#lua-filter">last section</a></p>
350 <div id="pagebreaks" class="section level2">
351 <h2>Pagebreaks</h2>
352 <p>This filter is available since <strong>rmarkdown</strong> 1.15 and
353 works with Pandoc 2.1+ (shipped in RStudio &gt;= 1.2).</p>
354 <p>Adding a pagebreak in document was always possible using custom
355 output specific syntax in a rmarkdown file but one drawback was the
356 compatibility with several output format.</p>
357 <p>With this Lua filter, it is possible to add a <code>\newpage</code>
358 or <code>\pagebreak</code> command in a new line to include a pagebreak
359 in any of these formats: <code>pdf_document()</code>,
360 <code>html_document()</code>, <code>word_document()</code> and
361 <code>odt_document()</code>.</p>
362 <div class="sourceCode" id="cb1"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu"># Header 1</span></span>
363 <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a></span>
364 <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a>Some text</span>
365 <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a></span>
366 <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a>\newpage</span>
367 <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a></span>
368 <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="fu"># Header 2 on a new page</span></span>
369 <span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a></span>
370 <span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a>Some other text</span>
371 <span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a></span>
372 <span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a>\pagebreak</span>
373 <span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a></span>
374 <span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="fu"># Header 3 on a third page</span></span></code></pre></div>
375 <p>rmarkdown will convert those commands in the correct output format
376 syntax using a <a href="#lua-filter">Lua filter</a> during pandoc
377 conversion.</p>
378 <div id="pdf" class="section level3">
379 <h3>Using with PDF/ latex documents</h3>
380 <p>As the commands are the ones already used in latex syntax, this works
381 as expected in a tex output document, and thus with pdf. Adding a
382 pagebreak was already possible with rmarkdown when output is
383 <code>pdf_document()</code> or <code>latex_document()</code>, without
384 any restriction about the version of pandoc.</p>
385 </div>
386 <div id="html" class="section level3">
387 <h3>Using with HTML documents</h3>
388 <p>A <code>\newpage</code> or <code>\pagebreak</code> command in a
389 rmarkdown document with output as HTML will be converted by default in
390 this html code with inline style using CSS rule <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after"><code>page-break-after</code></a></p>
391 <div class="sourceCode" id="cb2"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="dt">&lt;</span><span class="kw">div</span><span class="ot"> style</span><span class="op">=</span><span class="st">&quot;page-break-after: always;&quot;</span><span class="dt">&gt;&lt;/</span><span class="kw">div</span><span class="dt">&gt;</span></span></code></pre></div>
392 <p>This will always insert a pagebreak after this div.</p>
393 <p>To get more flexibility, you can use a HTML class and some custom CSS
394 instead of an inline style. You need to add a metadata field
395 <code>newpage_html_class</code> in your yaml header to set the
396 class.</p>
397 <p>Then you can control the behavior using custom CSS as in this
398 example</p>
399 <div class="sourceCode" id="cb3"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="co">---</span></span>
400 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="an">output:</span><span class="co"> </span></span>
401 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co"> html_document: default</span></span>
402 <span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="an">newpage_html_class:</span><span class="co"> page-break</span></span>
403 <span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">---</span></span>
404 <span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a></span>
405 <span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="in">```{css, echo = FALSE}</span></span>
406 <span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="in">// display the pagebreak only when printing the html page</span></span>
407 <span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="in">@media all {</span></span>
408 <span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="in"> .page-break { display: none; }</span></span>
409 <span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="in">}</span></span>
410 <span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="in">@media print {</span></span>
411 <span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a><span class="in"> .page-break { display: block; break-after: page; }</span></span>
412 <span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a><span class="in">}</span></span>
413 <span id="cb3-15"><a href="#cb3-15" tabindex="-1"></a><span class="in">```</span></span>
414 <span id="cb3-16"><a href="#cb3-16" tabindex="-1"></a></span>
415 <span id="cb3-17"><a href="#cb3-17" tabindex="-1"></a><span class="fu"># Header 1</span></span>
416 <span id="cb3-18"><a href="#cb3-18" tabindex="-1"></a></span>
417 <span id="cb3-19"><a href="#cb3-19" tabindex="-1"></a>Some text</span>
418 <span id="cb3-20"><a href="#cb3-20" tabindex="-1"></a></span>
419 <span id="cb3-21"><a href="#cb3-21" tabindex="-1"></a>\newpage</span>
420 <span id="cb3-22"><a href="#cb3-22" tabindex="-1"></a></span>
421 <span id="cb3-23"><a href="#cb3-23" tabindex="-1"></a><span class="fu"># Header 2 on a new page</span></span>
422 <span id="cb3-24"><a href="#cb3-24" tabindex="-1"></a></span>
423 <span id="cb3-25"><a href="#cb3-25" tabindex="-1"></a>Some other text</span></code></pre></div>
424 <p><code>\newpage</code> will be converted here to</p>
425 <div class="sourceCode" id="cb4"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="dt">&lt;</span><span class="kw">div</span><span class="ot"> class</span><span class="op">=</span><span class="st">&quot;page-break&quot;</span><span class="dt">&gt;&lt;/</span><span class="kw">div</span><span class="dt">&gt;</span></span></code></pre></div>
426 <p>and the style will be applied to this class from the CSS included in
427 the chunk.</p>
428 <p>This customisation can also be achieved by setting the environnement
429 variable <code>PANDOC_NEWPAGE_HTML_CLASS</code> in the R session that
430 will render the document (or in <code>.Renviron</code> file for
431 example)</p>
432 <p>Let’s note that in this example we use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/break-after"><code>break-after</code></a>
433 property instead of <code>page-break-after</code> as it is recommended
434 now to use the former which is the replacement. The latter is kept
435 around for compatibility reason with browsers.</p>
436 </div>
437 <div id="word" class="section level3">
438 <h3>Using with Word documents</h3>
439 <p>A <code>\newpage</code> or <code>\pagebreak</code> command in a
440 rmarkdown document with output as Word document will be converted in a
441 pagebreak for word document. Manually, this would mean adding this in
442 your rmarkdown</p>
443 <div class="sourceCode" id="cb5"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="in">```{=openxml}</span></span>
444 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="in">&lt;w:p&gt;&lt;w:r&gt;&lt;w:br w:type=&quot;page&quot;/&gt;&lt;/w:r&gt;&lt;/w:p&gt;</span></span>
445 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
446 <p>For example, using the pagebreak feature, this will add the first
447 header in the second page of the work document</p>
448 <div class="sourceCode" id="cb6"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="co">---</span></span>
449 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="an">title:</span><span class="co"> My main title</span></span>
450 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="an">output:</span><span class="co"> word_document</span></span>
451 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">---</span></span>
452 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a></span>
453 <span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a>\newpage</span>
454 <span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a></span>
455 <span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="fu"># First Header</span></span></code></pre></div>
456 </div>
457 <div id="odt" class="section level3">
458 <h3>Using with ODT documents</h3>
459 <p>To use the pagebreak feature with <code>odt_document()</code>, you
460 need to provide a reference document that includes a paragraph style
461 with, by default, the name <em>Pagebreak</em>. This named paragraph
462 style should have no extra space before or after and have a pagebreak
463 after it. (see <a href="https://help.libreoffice.org/Writer/Text_Flow">libre office
464 documentation</a> on how to create a style).</p>
465 <p>The name of the named paragrah style could be customized using
466 <code>newpage_odt_style</code> metadata in yaml header or
467 <code>PANDOC_NEWPAGE_ODT_STYLE</code> environment variable (as in <a href="#html">html document</a>).</p>
468 <p>As the previous one, this example will lead to a two pages document,
469 with first header on the second page.</p>
470 <div class="sourceCode" id="cb7"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="co">---</span></span>
471 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="an">title:</span><span class="co"> My main title</span></span>
472 <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="an">output:</span><span class="co"> </span></span>
473 <span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co"> odt_document:</span></span>
474 <span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co"> reference_odt: reference.odt</span></span>
475 <span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">---</span></span>
476 <span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a></span>
477 <span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a>\newpage</span>
478 <span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a></span>
479 <span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="fu"># First Header</span></span></code></pre></div>
480 </div>
481 </div>
482 <div id="number-sections" class="section level2">
483 <h2>Number sections</h2>
484 <p>This filter is available since <strong>rmarkdown</strong> 2.4 and
485 works with Pandoc 2.1+ (shipped in RStudio &gt;= 1.2).</p>
486 <p>Numbering sections are supported by Pandoc for limited formats (e.g.,
487 html and pdf). The rmarkdown package adds
488 <code>number_sections.lua</code> to support this feature in other
489 formats (e.g., docx, odt, and so on). Users do not have to know which
490 formats use the Pandoc’s feature or the Lua filter.</p>
491 <div class="sourceCode" id="cb8"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="co">---</span></span>
492 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="an">title:</span><span class="co"> My main title</span></span>
493 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="an">output:</span><span class="co"> </span></span>
494 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co"> md_document:</span></span>
495 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co"> number_sections: true # implemented by Lua filter</span></span>
496 <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co"> html_document</span></span>
497 <span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co"> number_sections: true # implemented by Pandoc</span></span>
498 <span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">---</span></span>
499 <span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a></span>
500 <span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="fu"># First Header</span></span>
501 <span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a></span>
502 <span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="fu">## A Header Belonging the First Header</span></span>
503 <span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a></span>
504 <span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="fu"># Second Header</span></span></code></pre></div>
505 <p>In general, numbers and titles of sections are separated by a space.
506 An exception is the <code>word_document</code> function, which separates
507 them by a tab in order to be consistent with Pandoc’s number sections
508 for docx format in Pandoc &gt;= 2.10.1. If one wants to have fine
509 controls on the format of section numbers, prepare customized docx file
510 and specify it to the <code>reference_docx</code> argument of the
511 <code>word_document</code> function.</p>
512 </div>
513 <div id="custom-environment-for-latex" class="section level2">
514 <h2>Custom environment for LaTeX</h2>
515 <p>This filter is available since <strong>rmarkdown</strong> 1.15 and
516 works with Pandoc 2.1+ (shipped in RStudio &gt;= 1.2).</p>
517 <p>Pandoc supports a syntax called <a href="https://pandoc.org/MANUAL.html#divs-and-spans">Fenced Divs</a>
518 that allows to create <code>&lt;div&gt;</code> in HTML. For example, the
519 syntax is</p>
520 <div class="sourceCode" id="cb9"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>::: {#special .sidebar}</span>
521 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>Here is a paragraph.</span>
522 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a></span>
523 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a>And another.</span>
524 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a>:::</span></code></pre></div>
525 <p>By default, this special syntax will be ignored in LaTeX conversion
526 for PDF output and the content of the block will be used as-is. However,
527 this syntax could be used to create LaTeX environment.</p>
528 <p>That is why <strong>rmarkdown</strong> uses a custom Lua filter to
529 allow any Fenced Divs to be converted as a LaTeX environment
530 (<code>\begin{env}</code> … <code>\end{env}</code>) when desired by
531 adding a special attribute.</p>
532 <div class="sourceCode" id="cb10"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>::: {.verbatim data-latex=&quot;&quot;}</span>
533 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>We show some _verbatim_ text here.</span>
534 <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a>:::</span></code></pre></div>
535 <p>Its LaTeX output will be:</p>
536 <div class="sourceCode" id="cb11"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="kw">\begin</span>{<span class="ex">verbatim</span>}</span>
537 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="vs">We show some \emph{verbatim} text here.</span></span>
538 <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="kw">\end</span>{<span class="ex">verbatim</span>}</span></code></pre></div>
539 <p>This feature we call <em>Custom blocks</em> is documented in details
540 in the <a href="https://yihui.org/rmarkdown-cookbook/custom-blocks.html">R
541 Markdown Cookbook</a></p>
542 </div>
543 <div id="lua-filter" class="section level2">
544 <h2>About Lua filters</h2>
545 <p>Since pandoc 2.0, it is possible to use Lua filters to add some extra
546 functionality to pandoc document conversion. Adding a pagebreak command
547 in markdown to be compatible with several output documents is one of
548 them. You can find some more informations about Lua filters in <a href="https://pandoc.org/lua-filters.html">pandoc’s documentation</a>
549 and also some examples in <a href="https://github.com/pandoc/lua-filters">a collection of Lua filters
550 for pandoc</a>. These examples, and any other Lua filters, can be use in
551 your Rmarkdown document directly by adding <a href="https://yihui.org/rmarkdown/html-document.html">a pandoc
552 argument</a> in yaml header</p>
553 <div class="sourceCode" id="cb12"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>---</span>
554 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>output:</span>
555 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> html_document:</span>
556 <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a> pandoc_args: [&quot;--lua-filter=filter.lua&quot;]</span>
557 <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a>---</span></code></pre></div>
558 <p>You can also use a special helper using a <code>!expr</code> syntax
559 for yaml in your header</p>
560 <p>The package <a href="https://github.com/crsh/rmdfiltr">rmdfiltr</a>
561 provides a collection of Lua filters and helpers functions to use
562 them.</p>
563 <p>Before pandoc 2.0, <a href="https://pandoc.org/filters.html">using
564 filter</a> with pandoc was already available through programs that
565 modifies the AST. <code>pandoc-citeproc</code> is an example used to
566 deal with citations. The package <a href="https://cran.r-project.org/package=pandocfilters">pandocfilter</a>
567 is useful to create filters using R.</p>
568 <div id="using-filters-with-output-format" class="section level3">
569 <h3>Using filters with output format</h3>
570 <p>If you want to create a format that uses a special filter, you can
571 add it as a pandoc option if the output format.</p>
572 <p>Either by modifying an existing format:</p>
573 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>custom_format <span class="ot">&lt;-</span> <span class="cf">function</span>(...) {</span>
574 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> base_format <span class="ot">&lt;-</span> rmarkdown<span class="sc">::</span><span class="fu">html_document</span>(...)</span>
575 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="co"># prepending a new Lua filter to html_document() ones</span></span>
576 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a> base_format<span class="sc">$</span>pandoc<span class="sc">$</span>lua_filters <span class="ot">&lt;-</span> <span class="fu">c</span>(</span>
577 <span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a> rmarkdown<span class="sc">::</span><span class="fu">pandoc_path_arg</span>(<span class="st">&quot;new.lua&quot;</span>), </span>
578 <span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a> base_format<span class="sc">$</span>pandoc<span class="sc">$</span>lua_filters)</span>
579 <span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a> base_format</span>
580 <span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a>}</span>
581 <span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a></span>
582 <span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="fu">basename</span>(<span class="fu">custom_format</span>()<span class="sc">$</span>pandoc<span class="sc">$</span>lua_filters)</span>
583 <span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#&gt; [1] &quot;new.lua&quot; &quot;pagebreak.lua&quot; &quot;latex-div.lua&quot; </span></span>
584 <span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co">#&gt; [4] &quot;table-classes.lua&quot;</span></span></code></pre></div>
585 <p>Or by creating a new format :</p>
586 <div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>custom_format <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">toc =</span> <span class="cn">TRUE</span>, ...) {</span>
587 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a> rmarkdown<span class="sc">::</span><span class="fu">output_format</span>(</span>
588 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a> <span class="at">knitr =</span> rmarkdown<span class="sc">::</span><span class="fu">knitr_options</span>(),</span>
589 <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a> <span class="co"># a new filter will be appended to base_format ones</span></span>
590 <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a> <span class="at">pandoc =</span> rmarkdown<span class="sc">::</span><span class="fu">pandoc_options</span>(<span class="at">to =</span> <span class="st">&quot;html&quot;</span>, <span class="at">lua_filters =</span> <span class="st">&quot;new.lua&quot;</span>),</span>
591 <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a> <span class="at">base_format =</span> rmarkdown<span class="sc">::</span><span class="fu">html_document</span>(<span class="at">toc =</span> toc, ...)</span>
592 <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a> )</span>
593 <span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a>}</span>
594 <span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a></span>
595 <span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="fu">basename</span>(<span class="fu">custom_format</span>()<span class="sc">$</span>pandoc<span class="sc">$</span>lua_filters)</span>
596 <span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co">#&gt; [1] &quot;pagebreak.lua&quot; &quot;latex-div.lua&quot; &quot;table-classes.lua&quot;</span></span>
597 <span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="co">#&gt; [4] &quot;new.lua&quot;</span></span></code></pre></div>
598 <p>As this is an advanced feature, if you want to go further, we
599 encourage you to look at the examples of formats in packages such as
600 <strong>rmarkdown</strong>, <strong>bookdown</strong> or
601 <strong>pagedown</strong>.</p>
602 </div>
603 </div>
604
605
606
607 <!-- code folding -->
608
609
610 <!-- dynamically load mathjax for compatibility with self-contained -->
611 <script>
612 (function () {
613 var script = document.createElement("script");
614 script.type = "text/javascript";
615 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
616 document.getElementsByTagName("head")[0].appendChild(script);
617 })();
618 </script>
619
620 </body>
621 </html>