在DOCX输出中更改Pandoc等宽字体大小或样式

时间:2015-02-05 14:21:55

标签: docx pandoc

使用markdown代码块时,在DOCX文档中生成的等宽字体大小太大。

我可以通过指定自定义 template.docx 文件来调整段落的字体大小,但由于某种原因,生成的代码块不使用段落样式,而不是大多数其他生成的输出。

有没有办法:

  • 使代码块使用特定样式,以便我可以覆盖 template.docx

  • 中的样式
  • 是否覆盖代码块DOCX表示中使用的等宽字体?

更新以澄清: 我正在使用基于先前生成的docx的外部reference.docx,如评论中所述。通过修改heading1等的样式,我可以合理地控制输出。问题是生成的等宽文本不使用命名样式,它只是“正常”有一些变化。所以除非我也改变所有“普通”文本的大小,否则我无法在模板中更改它。

2 个答案:

答案 0 :(得分:4)

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.

答案 1 :(得分:4)

使用Pandoc 1.17.2和Word 2013我终于找到了一个解决方案,似乎后来的Pandoc版本使用默认隐藏在Word中的链接样式。

步骤1:使用

生成自定义模板文件
pandoc -o template_1.17.2.docx test.md

test.md包含源代码和您可能想要修改的所有其他样式。例如:

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

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

在Word中打开template_1.17.2.docx。预格式化的源现在使用隐藏链接样式“源代码”进行格式化。默认情况下,样式预览窗格中不显示此样式,您可以通过单击样式预览面板右下角的小方框箭头来配置样式预览窗格来添加样式。

根据需要修改此样式并保存模板。然后根据此模板生成您的文档:

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

您现在应该在mydoc中看到源格式正确。

@LinusR建议不同的源样式使用不同的布局样式。我添加了XML作为示例。格式化的XML将使用“KeywordTok”和“NormalTok”。