14

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.

Karl Ivar Dahl
  • 1,023
  • 2
  • 11
  • 23
  • Can you post (a link to) your custom *template.docx* file? Are you aware that there is an additional option (different from the *template.docx* used by Pandoc), which can be invoked with `--reference-docx=my.docx`? You can create the `my.docx` file to include the styles you want, and Pandoc will steal all styles to be used from that reference.docx... – Kurt Pfeifle May 24 '15 at 22:02
  • 1
    Did you find a solution? I've noticed that, for fenced code blocks where I specify a syntax (e.g. ```php...), the code block does have assigned styles once in docx, but it has many different styles, depending on the code syntax token type. I could change the styles for all of those in the reference doc. They inherit from a style I can't find called "Verbatim Char". But for regular code blocks without syntax specified, still no luck. – LinusR Aug 22 '16 at 16:45

2 Answers2

9

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

Karl Ivar Dahl
  • 1,023
  • 2
  • 11
  • 23
6

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 1
    I am using an external reference.docx based on a previously generated docx as you describe. By modifying the styles for heading1 etc I have reasonable control over the output. The problem is that generated monospace text do 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 "normal". – Karl Ivar Dahl May 26 '15 at 14:05
  • @mb21: I appreciate that you tried to improve my answer. You seem to know more about Pandoc's internals than I do. But... could you please be meticulous enough to improve the following points of your edit: ***(1)*** Don't refer to ODT when you *only* talk about the *reference.docx* file. ***(2)*** Also update the reference to *"latest Pandoc version"*, which no longer is 1.13.2.1, but now is 1.17. – Kurt Pfeifle Mar 21 '16 at 21:28
  • @mb21: Could you please also have a look at https://github.com/jgm/pandoc/issues/2797 ? – Kurt Pfeifle Mar 21 '16 at 21:48
  • @KurtPfeifle I'm sorry, I missed the ODT part since the question asked only about Docx. Added it in again... please feel to revert any of my changes or improve them further... – mb21 Mar 22 '16 at 09:28