Changing page orientation in word using Quarto?
Asked Answered
D

1

8

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).

Damnify answered 20/9, 2022 at 9:47 Comment(0)
S
9

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
Spokane answered 20/9, 2022 at 13:51 Comment(6)
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.Thyrotoxicosis
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?Irinairis
@Irinairis 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.Spokane
@Spokane - Thanks for this. When I try this I get a blank page after landscape page. Is there a fix for it?Topsoil
@Topsoil I have the same issue of blank page. Have you resolved it?Archaeopteryx
@AbdullahAbdelaziz - Unfortunately no.Topsoil

© 2022 - 2024 — McMap. All rights reserved.