From bed0966c9b3d4ab52c2a66e79f5669b4b2ff0873 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Wed, 1 Jan 2020 12:45:22 +0100 Subject: [PATCH 1/5] also support inline newpage command for docx only until further test --- inst/rmd/lua/pagebreak.lua | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/inst/rmd/lua/pagebreak.lua b/inst/rmd/lua/pagebreak.lua index 0177a4cca9..6696a775c8 100644 --- a/inst/rmd/lua/pagebreak.lua +++ b/inst/rmd/lua/pagebreak.lua @@ -48,19 +48,27 @@ local function pagebreaks_from_config (meta) end --- Return a block element causing a page break in the given format. -local function newpage(format) +local function newpage(format, type) + -- type define if we add block or inline + type = type or 'block' + if type:match 'block' then + add_raw = pandoc.RawBlock + elseif type:match 'inline' then + add_raw = pandoc.RawInline + end + -- the format define the string to add if format == 'docx' then - return pandoc.RawBlock('openxml', pagebreak.ooxml) + return add_raw('openxml', pagebreak.ooxml) elseif format:match 'latex' then - return pandoc.RawBlock('tex', pagebreak.latex) + return add_raw('tex', pagebreak.latex) elseif format:match 'odt' then - return pandoc.RawBlock('opendocument', pagebreak.odt) + return add_raw('opendocument', pagebreak.odt) elseif format:match 'html.*' then - return pandoc.RawBlock('html', pagebreak.html) + return add_raw('html', pagebreak.html) elseif format:match 'epub' then - return pandoc.RawBlock('html', pagebreak.epub) + return add_raw('html', pagebreak.epub) elseif format:match 'context' then - return pandoc.RawBlock('context', pagebreak.context) + return add_raw('context', pagebreak.context) else -- fall back to insert a form feed character return pandoc.Para{pandoc.Str '\f'} @@ -89,12 +97,25 @@ function RawBlock (el) return nil end --- Turning paragraphs which contain nothing but a form feed --- characters into line breaks. +-- Filter function called on each RawBlock element. function Para (el) + -- Turning paragraphs which contains nothing but a form feed + -- characters into line breaks. if #el.content == 1 and el.content[1].text == '\f' then return newpage(FORMAT) end + -- Turning newpage command inside a paragraphs into inline raw + -- working only for docx for now + if FORMAT:match 'docx' then + return pandoc.walk_block(el, { + RawInline = function(el) + -- check that the inline raw is TeX or LaTeX and matches + -- \newpage or \pagebreak. + if el.format:match 'tex' and is_newpage_command(el.text) then + return newpage(FORMAT, "inline") + end + end }) + end end return { From 3c12c745e2b845f421542a70c7f4a534901717d3 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Tue, 14 Jan 2020 07:39:18 +0100 Subject: [PATCH 2/5] use also for odt --- inst/rmd/lua/pagebreak.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/rmd/lua/pagebreak.lua b/inst/rmd/lua/pagebreak.lua index 6696a775c8..3ccac6d522 100644 --- a/inst/rmd/lua/pagebreak.lua +++ b/inst/rmd/lua/pagebreak.lua @@ -105,8 +105,8 @@ function Para (el) return newpage(FORMAT) end -- Turning newpage command inside a paragraphs into inline raw - -- working only for docx for now - if FORMAT:match 'docx' then + -- working only for docx or odt for now + if FORMAT:match 'docx' or FORMAT:match 'odt' then return pandoc.walk_block(el, { RawInline = function(el) -- check that the inline raw is TeX or LaTeX and matches From 1a0387a7e035c09d336b5280461f84e1dffefa6d Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Sun, 19 Jan 2020 22:04:36 +0100 Subject: [PATCH 3/5] add a NEWS bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 0a769a3ef3..3e6d277411 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ rmarkdown 2.1 ================================================================================ +- In word and odt document output, `\newpage` and `\pagebreak` can now be placed at the very end of paragraph to add the page break in the output after the last word and avoid an undesired blank page. (thanks, @atusy, @cderv, #1753) + - `ioslides_presentation` template no longer generates an empty `

` tag when `subtitle` is not specified in YAML (thanks, @jooyoungseo #1735, @cgrudz #1663). - No longer center the `#header` element in the `html_vignette()` output (thanks, @EmilHvitfeldt, #1742). From 4794404a38d35937db81df94c9ea6a1e4e11834b Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Sun, 19 Jan 2020 22:14:16 +0100 Subject: [PATCH 4/5] This PR should not modify the version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 899a5c37d5..6aff4a5cdc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: rmarkdown Type: Package Title: Dynamic Documents for R -Version: 2.0.5 +Version: 2.0.7 Authors@R: c( person("JJ", "Allaire", role = "aut", email = "jj@rstudio.com"), person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), From 9704331fe5cf330ecbe620655606da7a1a071716 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Sun, 19 Jan 2020 22:32:58 +0100 Subject: [PATCH 5/5] update lua-filter vignette to document the feature --- vignettes/lua-filters.Rmd | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/vignettes/lua-filters.Rmd b/vignettes/lua-filters.Rmd index 976b03c3c2..0cb1d6ccad 100644 --- a/vignettes/lua-filters.Rmd +++ b/vignettes/lua-filters.Rmd @@ -120,6 +120,21 @@ output: word_document # First Header ```` +It is also possible to add the pagebreak command at the very end of a paragraph. This can be useful to avoid a blank page with only the pagebreak paragraph. + +````md +--- +title: My main title +output: word_document +--- + +# First Header + +Some text\newpage + +This text will be on a new page. +```` + ### Using with ODT documents {#odt} 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). @@ -141,6 +156,22 @@ output: # First Header ```` +Also like with Word output, it is also possible to add the pagebreak command at the very end of a paragraph in ODT document too. + +````md +--- +title: My main title +output: + odt_document: + reference_odt: reference.odt +--- + +# First Header + +Some text\newpage + +This text will be on a new page. +```` ## About lua filters {#lua-filter}