diff --git a/NEWS.md b/NEWS.md index 457e24a621..b7692a7f6a 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) + - Added the returned output from `shiny::runApp()` within `rmarkdown::run()` (thanks, @schloerke, #1760). - YAML header is now correctly parsed in `html_notebook`'s intermediate `.knit.md` file so that features like adding bibliography works again (thanks, @everdark, @cderv, #1747). diff --git a/inst/rmd/lua/pagebreak.lua b/inst/rmd/lua/pagebreak.lua index 0177a4cca9..3ccac6d522 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 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 + -- \newpage or \pagebreak. + if el.format:match 'tex' and is_newpage_command(el.text) then + return newpage(FORMAT, "inline") + end + end }) + end end return { 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}