I am trying to add a section before the table of contents in a docx generated from a markdown file. My table of content is generated with the --toc option of the pandoc command and my markdown does not have a table of contents field. I have tried to use the --include-before-body option to add a section before the table of contents but without success.
pandoc test.md -f markdown -t docx -s -o test.docx --reference-doc=./custom-reference.docx --number-sections=true --lof=true --toc=true --include-before-body=cover.docx
The --include-before-body option :
-B FILE, --include-before-body=FILE|URL
Include contents of FILE, verbatim, at the beginning of the document body (e.g. after the <body> tag in HTML, or the \begin{document} command in LaTeX). This can be used to include navigation bars or banners in HTML documents.
This option can be used repeatedly to include multiple files. They will be included in the order specified. Implies --standalone. Note that if the output format is odt,
this file must be in OpenDocument XML format suitable for insertion into the body of the document, and if the output is docx, this file must be in appropriate OpenXML format.
But when I try to pass a simple cover.docx file, it fails with the following error
UTF-8 decoding error in cover.docx at byte offset 10 (b2). The input must be a UTF-8 encoded text
I have tried to generate cover.doc, cover.docx (strict office XML), cover.docx, and it still does not work.
Is it the correct way to add a section before the table of contents and if so, what did I do wrong ?
Most variables only work if there's a pandoc template with the corresponding variable. But docx output doesn't use templates, but reference-doc instead.
But for include-before-body
you should be able to supply a file with an XML snippet (which pandoc will insert here in this XML file). That means a file that you can open with any plain text editor. Be sure not to supply a .docx
file itself (which is basically a .zip
file).
However, if you don't need special formatting, it's maybe easier to write a pandoc lua filter similar to this filter.