Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc Markdown to Docx with Cover Page and TOC in separated pages

I followed the instructions in this answer: https://stackoverflow.com/a/52131435/510024 and I could manage to create and use a filter to create a new page whenever I want in a conversion from markdown to docx. With that I could manage to get a 'Cover/Front Page' in the docx file. However when using --toc option the filter doesn't work. The newpage is not respected and the 'TOC' appears right after the document title i.e in the same page.

There is a way to convert from markdown to docx having as result a docx document with a 'Cover/Front Page' and the 'TOC' (i.e. the 'TOC' being displayed in a page after the 'Cover/Front Page')

Thanks!

like image 965
avaz Avatar asked Jan 28 '23 07:01

avaz


1 Answers

There are (at least) two possible methods: one which requires changes to the input document, and another where one modifies the reference document.

Adding a page break to the abstract

The abstract is the last element before the table of contents. Ending the abstract with a page break will cause the toc to start on a new page:

---
title: MWE
abstract: ' `<w:p><w:r><w:br w:type="page"/></w:r></w:p>`{=openxml}'
---

Note that the abstract may not be empty, but adding a single no-break space, as demonstrated in above example, is enough.

Modifying the reference document

Pandoc uses a reference document when creating docx files. It is possible to create a custom reference doc and to modify the TOCHeader style.

Creating a custom reference doc requires the following steps:

  1. Create a new docx based on pandoc's default reference file:

    pandoc --print-default-data-file reference.docx > custom-reference.docx
    
  2. Open custom-reference.docx and modify the styles in it to your liking.

  3. Pass the result via the --reference-doc option each time when invoking pandoc.

    pandoc --reference-doc=custom-reference.docx …
    

    Alternatively, rename the file to reference.docx and place it in pandoc's data directory, so it is chosen as default.

See the documentation on how to modify a custom style for details.

like image 111
tarleb Avatar answered Feb 07 '23 13:02

tarleb