rpandocquarto

Changing page orientation in word using Quarto?


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


Solution

  • 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