I tried several methods that did not work for me. The steps suggested in Tarleb's comment were the only ones that worked. I adapted the code to change the output from normal margins to narrow margins. To modify other things than margins, documentation can be found here.
- Create a .lua document (I personally use Visual Studio Code to do so)(and save it in the same directory as your Quarto/RMarkdown document) containing the following code:
local ooxml = function (s)
return pandoc.RawBlock('openxml', s)
end
local end_narrow_section = ooxml [[
<w:p>
<w:pPr>
<w:sectPr>
<w:pgMar w:top="722" w:right="722" w:bottom="722" w:left="722" w:header="567" w:footer="510" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:pPr>
</w:p>
]]
function Div (div)
if div.classes:includes 'narrow' then
div.content:insert(end_narrow_section)
return div
end
end
- Then add a filter in the YAML header at the top of your Quarto/RMarkdown document, with the name of the file you have just created :
---
filters:
- example.lua
---
- Wrap the part of the document that you want to have modified margins with the following code block:
::: narrow
This is an example
:::
- Render your document as a Word document as usual.
EDIT: Another way to proceed is to create a reference docx document with the desired margins. It is easier to implement, but the whole document will be formatted instead of being able to format only some parts. Just add the docx to the same folder and add the code below to the YAML header.
---
format :
docx :
reference-doc: example.docx
---