Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pagebreak from inline command too #1753

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
39 changes: 30 additions & 9 deletions inst/rmd/lua/pagebreak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions vignettes/lua-filters.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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}

Expand Down