gpt4 book ai didi

ms-word - pandoc markdown to docx - 将列表保留在一页上

转载 作者:行者123 更新时间:2023-12-05 01:17:38 24 4
gpt4 key购买 nike

我有一个这样的 Markdown 列表:

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

最佳答案

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

值得注意的是,Pandoc 的 documentation州(强调):

Because pandoc’s intermediate representation of a document is less expressive than many of the formats it converts between, one should not expect perfect conversions between every format and every other. Pandoc attempts to preserve the structural elements of a document, but not formatting details...

当然,Markdown 没有“分页”或“分页符”的概念,所以这不是 Pandoc 默认可以处理的。但是,Pandoc 知道 docx 样式。作为documentation解释:

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.

如果您不想手动定义样式,但希望它自动应用于每个列表(或者可能应用于每个遵循特定模式的列表),您可以定义自定义 filter它将样式应用于文档中的每个匹配元素。

当然,这只会将样式名称添加到输出中。您仍然需要定义样式(告诉 Word 如何显示分配了这些样式的元素)。作为documentation --reference-doc 选项说明:

For best results, the reference docx should be a modified version of a docx file produced using pandoc. The contents of the reference docx are ignored, but its stylesheets and document properties (including margins, page size, header, and footer) are used in the new docx. If no reference docx is specified on the command line, pandoc will look for a file reference.docx in the user data directory (see --data-dir). If this is not found either, sensible defaults will be used.

To produce a custom reference.docx, first get a copy of the default reference.docx: pandoc --print-default-data-file reference.docx >
custom-reference.docx
. Then open custom-reference.docx in Word, modify the styles as you wish, and save the file.

当然,在 Word 中修改 custom-reference.docx 时,您可以添加您在 Markdown 中使用的新自定义样式。正如@CindyMeister 在评论中指出的那样:

Word would handle this using styles, where the Question style would have the paragraph setting "Keep with Next". the Answer style would have this as well. A third style, for the last entry, would NOT have the setting activated. In addition, all three styles would have the paragraph setting "Keep together" activated.

最后,当使用 pandoc 将 Markdown 转换为 Word docx 文件时,使用选项 --reference-doc=custom-reference.docx 并且您的自定义样式定义将包含在生成的.docx 文件。只要您还正确识别 Markdown 文档中的哪些元素获得哪些样式,您就应该有一个列表,只要整个列表适合一页,就不会因分页符而被破坏。

关于ms-word - pandoc markdown to docx - 将列表保留在一页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49441137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com