将Pandoc Markdown转换为docx格式 - 保持列表在一页上

3

我有一个像这样的Markdown列表:

* 问题 A - 答案 1 - 答案 2 - 答案 3 当我使用Pandoc将Markdown文档转换为docx时,我需要确保所有答案(1-3)与问题A出现在同一页上。我该怎么做?


3
我对Pandoc一无所知,但我知道理想情况下,Word可以使用样式来处理这个问题。在此方案中,"问题"样式应该具有段落设置"与下一段在同一页","答案"样式也应该如此。第三个样式,用于最后一项,不应该激活该设置。另外,所有三个样式都应该激活段落设置"保持在同一页"。另一种可能的方法是将整个集合放在一个表格单元格中,并禁止"允许跨页断行"设置。也许你可以找到与Pandoc对应的方式,以便成功地迁移到Word... - Cindy Meister
1个回答

7

使用自定义样式在您的Markdown中,然后在自定义docx模板中定义这些样式。

值得注意的是,Pandoc的文档(强调添加):

因为pandoc对文档的中间表示不如它转换之间的许多格式表达力强,所以不应期望每种格式和其他格式之间都有完美的转换。 Pandoc试图保留文档的结构元素,但不保留格式细节...

当然,Markdown没有“页面”或“分页符”的概念,因此这不是Pandoc默认处理的内容。但是,Pandoc知道docx样式。如文档所解释的那样:

By default, pandoc’s docx output applies a predefined set of styles for blocks such as paragraphs and block quotes, and uses largely default formatting (italics, bold) for inlines. This will work for most purposes, especially alongside a reference.docx file. However, if you need to apply your own styles to blocks, or match a preexisting set of styles, pandoc allows you to define custom styles for blocks and text using divs and spans, respectively.

If you define a div or span with the attribute custom-style, pandoc will apply your specified style to the contained elements. So, for example using the bracketed_spans syntax,

[Get out]{custom-style="Emphatically"}, he said.

would produce a docx file with “Get out” styled with character style Emphatically. Similarly, using the fenced_divs syntax,

Dickinson starts the poem simply:

::: {custom-style="Poetry"}
| A Bird came down the Walk---
| He did not know I saw---
:::

would style the two contained lines with the Poetry paragraph style.

If the styles are not yet in your reference.docx, they will be defined in the output file as inheriting from normal text. If they are already defined, pandoc will not alter the definition.

如果您不想手动定义样式,但希望它自动应用于每个列表(或者可能是每个遵循特定模式的列表),您可以定义一个自定义过滤器,将样式应用于文档中的每个匹配元素。
当然,这只是将样式名称添加到输出中。您仍然需要定义样式(告诉Word如何显示分配了这些样式的元素)。正如--reference-doc选项的文档所解释的那样:
为了获得最佳结果,参考文档应该是使用 pandoc 生成的 docx 文件的修改版本。参考文档的内容将被忽略,但它的样式表和文档属性(包括边距、页面大小、页眉和页脚)将用于新的 docx 文件。如果在命令行上没有指定参考文档,pandoc 将查找用户数据目录中的文件 reference.docx(参见 --data-dir)。如果也找不到该文件,则会使用合理的默认值。
要生成自定义的 reference.docx,首先获取默认的 reference.docx 的副本:pandoc --print-default-data-file reference.docx > custom-reference.docx。然后在 Word 中打开 custom-reference.docx 文件,按照您的意愿修改样式,并保存该文件。
当然,在 Word 中修改 custom-reference.docx 文件时,您可以添加您在 Markdown 中使用的新自定义样式。正如 @CindyMeister 在评论中指出的那样:
Word可以使用样式来处理此问题,其中“问题”样式将具有段落设置“与下一段保持在一起”,“答案”样式也将具有此设置。第三个样式,用于最后一个条目,将不会激活该设置。此外,所有三种样式都将激活段落设置“保持在一起”。
最后,在使用pandoc将您的Markdown转换为Word docx文件时,请使用选项--reference-doc=custom-reference.docx,您的自定义样式定义将包含在生成的docx文件中。只要您还正确标识了Markdown文档中哪些元素获取哪些样式,您应该获得一个列表,该列表不会在跨页断开时被打破,只要整个列表适合一页即可。

我还没有想明白关于Word样式的问题。可能因为我们正在处理列表,所以它更加复杂?我修改了样式,使问题样式是“与下一段保持在一起”,答案样式也是如此,并且最后一个答案选项已经取消了“与下一段保持在一起”。所有这些都有“保持行在一起”的设置,但我仍然遇到问题,即问题和答案块位于不同的页面上。 - Andrew Barr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接