I’m working on a book project that has to be delivered to the publisher in Word. I compile for Pandoc → Microsoft Word (.docx). I use a couple of prefixing, suffixing and replacement strategies to directly manipulate the OpenXML code in the internal document.xml inside the Word ZIP file (= the .docx).
I add index keys and add some font and color details to a couple of styles. Not easy, but does work.
Next, I wanted to add some formatting details to a paragraph/character style named “Tabelle” (German for “table”), like borders.
The current XML looks like that, here an excerpt with the first lines for a table:
as the first child element inside of <w:tblPr>...</w:tblPr>.
I tried to replace <w:tblPr> with <w:tblPr> <w:tblBorders>... </w:tblBorders> but to no avail. If I use (paragraph) prefixes/suffixes for the style itself to replace those later on, I cannot target the right spot, the prefixes/suffixes are placed in other elements than <w:tblPr>.
Is it even possible to access the final XML like that and replace strings there? Or is there any other way?
As Pandoc + Scrivener are so flexible, there are multiple places[1] you can make modifications:
Use custom styles (Pandoc - Pandoc User’s Guide) and a reference document (simplest way to get specific styling).
Use Compiler regex replacements with some unique editor tags.
Manually injecting XML with Compiler prefix/suffix/formats, as you are doing automatically, in the .md using Scrivener.
Manually injecting the XML, using a post-processing script run after Scrivener generates the markdown, but before Pandoc is called. This may give you a bit more control…
Pandoc filter: this takes an abstract document and provides quite precise targeting of elements. Pandoc forum is good for getting help with filters.
Have you had tried to use custom styles, as that is much simpler (though how easy it is to trigger Word’s table styling I don’t know, I know table styles are not easy to work with). It may help to make a small sample markdown file of a minimal table, and try with pandoc directly to simplify the task before using Scrivener to bake in a final solution.
If I had your task, I would try the custom styles first, then Lua filter, then prefix/suffix or replacements, then scripting.
As an aside, I have a Pandoc filter for indexing: Making an index? - #32 by nontroppo – this demonstrates syntax injection using a filter and works across multiple outputs so is more flexible than manual XML injection in Scrivener…
By the way, ODT XML is much easier to work with in general than DOCX XML, and using LibreOffice instead of Word to generate the final DOCX may make some of the steps a bit easier…
[1] I use pandocomatic precisely because it gives me lots of control in running extra tools at every stage of the pandoc process, keeping all steps in a single recipe.
Thanks again, I got most of it working, with custom styles, and your index filter convertIndex.lua.
Tables in Word can only be styled with paragraph settings in the custom.docx, not with Word table formats, but that’s secondary - the final typesetting is not my job, I used paragraph styling to style them different.
I still struggle a bit with headings and a way to get internal document cross links working …
(I’m in a Windows environment and did without pandocomatic … It’s already complicated enough … I’ve never worked with Pandoc before … )
In my example project shared on the other thread, I demonstrate how to cross-reference titles of other sections. This uses normal Scrivener document links (drag a Binder document onto a word in the editor) which Scrivener converts to markdown links (make sure you enable this in the compiler settings), and Pandoc generates the correct anchors for you.
I’m not sure if this is entirely true. Pandoc does apply a Table style called “Table” to tables, but at least on macOS there is no way to modify it directly in the reference.docx; this seems like a bug in Word rather than Pandoc:
Actually while you cannot select the “Table” style for a new table you can edit it in the reference.docx as it includes an existing table you can select and use Table Design > Modify Table Style…
Here is my reference.docx where I edited the “table” style to add bold header line with top+bottom border: custom.docx.zip (34.7 KB) (the template uses the free and wonderful Alegreya and Alegreya Sans fonts, it may fallback to other fonts on your system).
So if we convert a simple table with pandoc and the reference-doc:
➪ pandoc -s -o out.docx --reference-doc $E:PD/templates/custom.docx
Right Left Center Default
------- ------ ---------- -------
12 12 12 12
123 123 123 123
1 1 1 1
Table: Demonstration of simple table syntax.
I finally found the problem: Although I based my custom.docx (the reference) on one of your examples, I managed to fiddle inadvertently with the Table style in Word so that it wasn’t a real table style anymore but a “normal” format. So I couldn’t use the style manager to change the table design.
Took me a while to get acquainted with that, I usually do not much formatting in Word, but your question
You cannot assign the “Table” style in the table format editor in the reference.docx to modify as you can for other styles!?