Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Pandoc monospace font size or style in DOCX output

Tags:

pandoc

docx

When using markdown code blocks the resulting monospace font size is too large in DOCX documents.

I can adjust the font size of paragraphs by specifying a custom template.docx file, but for some reason the generated code blocks do not use a paragraph style, as opposed to most other generated output.

Is there any way to:

  • Make code blocks use a specific style so that I can override the style in the template.docx

  • Override the monospace font used in the DOCX representation of code blocks?

Updated to clarify: I am using an external reference.docx based on a previously generated docx as described in the comments. By modifying the styles for heading1 etc I have reasonable control over the output. The problem is that generated monospace text does not use a named style, it is just "normal" with some changes. So I have no way to change it in the template unless I also change the size of all "normal" text.

like image 391
Karl Ivar Dahl Avatar asked Feb 05 '15 14:02

Karl Ivar Dahl


2 Answers

Using Pandoc 1.17.2 and Word 2013 I have finally found a solution, it seems later versions of Pandoc uses a linked style that is by default hidden in Word.

Step 1: Generate a custom template file using

pandoc -o template_1.17.2.docx test.md

Where test.md includes source code and all other styles you may want to modify. For example:

~~~~
this is preformatted source using style "Source Code"
~~~~

~~~ xml
<this> is preformatted source using "KeyworkTok" and "NormalTok"</this>
~~~

Open template_1.17.2.docx in Word. The preformatted source is now formatted using the hidden linked style "Source Code". This style is NOT displayed in the styles preview pane by default, you can add it by configuring the styles preview pane by clicking the tiny square-with-arrow in the bottom right of the styles preview panel.

Modify this style as you wish and save the template. Then generate your document based on this template:

pandoc --reference-docx=template_1.17.2.docx -o mydoc.docx mydoc.md

You should now see the source properly formatted in mydoc.

@LinusR suggests that different source styles uses different Layout styles. I have added XML as an example. The formatted XML will use "KeywordTok" and "NormalTok".

like image 93
Karl Ivar Dahl Avatar answered Oct 08 '22 19:10

Karl Ivar Dahl


Pandoc, when creating DOCX (MS Word) documents uses a reference.docx file. This has to be given on the Pandoc command line. Pandoc will then extract all default styles and formatting settings (unless they use custom names) from this reference DOCX and apply them on the generated DOCX:

    pandoc -t docx -o out.docx in-markdown.txt --reference-docx=my.docx

The best way to arrive at a Pandoc-usable reference DOCX is to generate a first simple DOCX with the help of Pandoc, then take it to a Word installation, open it and change the styles to be used by you to your liking. Then save it, take it back to Pandoc and use it as a reference.


For ODT (LibreOffice/OpenOffice/OpenDocument) in addition to the reference.odt (which you can use with the --reference-odt flag), there's also a template. You can print the default template with pandoc -D odt, then modify it and use it with pandoc -o out.odt --template=modifiedTemplate.odt


Last advice: use the latest Pandoc version! (Current is 1.13.2.1. For end of this month a 1.14 is expected.) Its DOCX support improved considerably in recent releases.

like image 41
Kurt Pfeifle Avatar answered Oct 08 '22 19:10

Kurt Pfeifle