How can I add custom formatting to docx files generated by Pandoc from, say, Markdown? For example, can I highlight text or center it? Can I restyle links, etc?
I know that I can change styles for default elements, like headings, by using a --reference-doc
(generated with pandoc -o custom-reference.docx --print-default-data-file reference.docx
), but I'd like to introduce additional styles.
There are basically 3 ways to customize docx output in Pandoc:
Generate a reference doc (as mentioned in the question: pandoc -o custom-reference.docx --print-default-data-file reference.docx
). It will have a bunch of content, representing common Pandoc components like headings and blockquotes (see picture at the end).
In the Styles
menu at the top of Word, find the style you want to change and edit it to be what you'd like (I'm not sure if you can simply edit the text's style directly).
There are more styles than the ones that appear in the reference document text and in the "Style gallery." For example, you can customize hyperlinks (e.g. color).
Generate a reference doc (or use the one you have from above).
Click the expand button in the Styles section to show the Styles pane (see below).
Find the style you want, then edit it.
Also:
This is probably the most relevant to the question. If you want to introduce custom elements beyond the default ones Pandoc provides and Word provides, you can simply add custom styles to the reference document. For example, you can create a style for highlighted text or centered text, or a style for large code blocks.
Generate a reference doc (or use the one you have from above).
Add some text to the document using the base style you want---the style you want to base your new custom style on. For example, if you want "normal text but highlighted," write some new text that uses the "Normal" or "Body text" style.
Click the dropdown in the Styles gallery.
Click Create a Style
Name the style, whatever you'd like.
Click Modify...
and customize the style however you'd like. You can always get to this menu later by right-clicking the style in the gallery and clicking "Modify..."
Save the document.
In the text you will convert to docx, add div
s and span
s with the appropriate custom-style
attribute whenever you want to consume it. For example, if you have a paragraph style called Super big
and a character style called Highlighted text
:
<div custom-style="Super big">My super big text</div>
Normal text. <span custom-style="Highlighted text">This is highlighted</span>.
Pandoc will apply those custom styles for you!