5

Is there a way of changing the page orientation for specific segments of a document when using Quarto and rendering to Word?

The ideal approach would be something similar to BLOCK_LANDSCAPE_START/BLOCK_LANDSCAPE_STOP from officedown.

But also interested in other approaches (for example using the reference-doc).

edvinsyk
  • 160
  • 6

1 Answers1

7

There's no unified solution for this yet, but you can get around by using a custom Lua filter. Store the filter given below to a file, say docx-landscape.lua, and then use it by listing it under filters in the document's YAML section. Use a fenced div with class landscape to mark content that should appear in landscape mode.

E.g.:

---
title: "Nullus"
filters:
  - docx-landscape.lua
---

This is in portrait mode.

::: landscape
This should appear in landscape mode.
:::

Things should be back to normal here.

where the filter docx-landscape.lua contains

local ooxml = function (s)
  return pandoc.RawBlock('openxml', s)
end

local end_portrait_section = ooxml
  '<w:p><w:pPr><w:sectPr></w:sectPr></w:pPr></w:p>'

local end_landscape_section = ooxml [[
<w:p>
  <w:pPr>
    <w:sectPr>
      <w:pgSz w:h="11906" w:w="16838" w:orient="landscape" />
    </w:sectPr>
  </w:pPr>
</w:p>
]]

function Div (div)
  if div.classes:includes 'landscape' then
    div.content:insert(1, end_portrait_section)
    div.content:insert(end_landscape_section)
    return div
  end
end

The filter takes a few shortcuts, but should work ok in most cases. Please let me know about any issues with it.


Addendum: if you prefer officedown commands, then append the following to the filter to make those commands work:

function RawBlock (raw)
  if raw.text:match 'BLOCK_LANDSCAPE_START' then
    return end_portrait_section
  elseif raw.text:match 'BLOCK_LANDSCAPE_STOP' then
    return end_landscape_section
  end
end
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • 1
    This is a great answer. I wonder if it is worth clarifying that the lua file can be stored in the same directory as the qmd file and doesn't need to be installed into quarto like other filters. I couldn't find information regarding how to store the file but tried it anyway. – hisspott Sep 29 '22 at 21:50
  • The filter works as the page I desired is indeed put in landscape mode! However, a few details from the custom reference docx I used were gone. Specifically: the margins before the landscape page are modified and the header and footer are removed. Do you have a fix for this perhaps? – PLY Mar 16 '23 at 14:20
  • @PLY Not yet, sorry. I plan to pickup work on docx support again soon, but it'll be a while before we'll get tangible results. – tarleb Mar 16 '23 at 14:22
  • @tarleb - Thanks for this. When I try this I get a blank page after landscape page. Is there a fix for it? – Selva Jul 06 '23 at 06:07