rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.88k stars 979 forks source link

FR: Add other orientation control options in lua #2034

Open DanChaltiel opened 3 years ago

DanChaltiel commented 3 years ago

I just realized that Word documents page breaks are now natively supported in Rmarkdown (since v2.4), so I wondered if you would also add support for orientation control.

I've already been using some functions for this from my old gist (and some SO help) for a couple of years and they proved really useful.

Here is some code (but it will obviously only work with the entire gist):

Lua

local function endLandscape(format)
  if format == 'docx' then
    local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:pPr><w:sectPr><w:officersection/><w:pgSz w:orient=\"landscape\" w:w=\"11906\" w:h=\"16838\"/></w:sectPr></w:pPr></w:sectPr></w:pPr></w:p>'
    return pandoc.RawBlock('openxml', pagebreak)
  else
    return pandoc.Para{pandoc.Str '\f'}
  end
end

local function endPortrait(format)
  if format == 'docx' then
    local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:pPr><w:sectPr><w:officersection/><w:pgSz w:orient=\"portrait\" w:w=\"16838\" w:h=\"11906\"/></w:sectPr></w:pPr></w:sectPr></w:pPr></w:p>'
    return pandoc.RawBlock('openxml', pagebreak)
  else
    return pandoc.Para{pandoc.Str '\f'}
  end
end

local function endContinuous(format)
  if format == 'docx' then
    local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:type w:val=\"continuous\"/></w:sectPr></w:pPr></w:p>'
    return pandoc.RawBlock('openxml', pagebreak)
  else
    return pandoc.Para{pandoc.Str '\f'}
  end
end

RMarkdown

---
title: "test"
output: 
  word_document:
    pandoc_args: ["--lua-filter=inst/rmarkdown/page-break.lua"]
---

# Part 1

I am in portrait

\endLandscape

## Part 2

I am in landscape

\endPortrait

## Part 3 

I am in portrait again

The naming comes from an old logic ("end section and start landscape --> endLandscape") and may not make much sense, feel free to use better names.

Also, note that starting directly by \endLandscape will not work, but I think this is a Word limitation.

Hope it helps


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 3 years ago

That is an interesting idea! Thanks for sharing. The name \pagebreak or \newpage is inspired by the LaTeX command and the filter makes it available for different format. This feature is definitely interesting for word output though. Not sure if we could make it available for other printable output.

For reference, this can of Word customization is already easily possible using the great officedown package, which offert landscape mode switch and is compatible with R Markdown pagebreak feature. Try:

---
author: 'Bruce'
date: '0189-12-27'
output: 
  officedown::rdocx_document: default
---

This is in portrait

<!---BLOCK_LANDSCAPE_START--->

This is a landscape section.

\newpage

And another landscape page

<!---BLOCK_LANDSCAPE_STOP--->

We're back in portrait

So, for anything Word specific, I am always wondering what should be in rmarkdown or stay in the custom output format package.

About the naming of commands, another idea would be to use fenced div. This would work the same by adding the correct raw content at start and end of the divs.

---
title: "test"
output: 
  word_document:
    pandoc_args: ["--lua-filter=inst/rmarkdown/word-landscape.lua"]
---

# Part 1

I am in portrait

::: landscape-layout

## Part 2

I am in landscape

:::

## Part 3 

I am in portrait again

Just sharing thoughts and references to think about this feature.