有没有可能在渲染到word的Rmarkdown文档中使用HTML标记?
例如:
---
output: word_document
---
# This is rendered as heading
<h1> But this is not </h1>渲染为html_document时效果很好,但渲染为word_document时效果不佳。
这里提出了一个关于标签的更具体的问题,但没有得到解决:Underline in RMarkdown to Microsoft Word
发布于 2020-10-03 22:08:24
当然,我们开始吧:
---
output:
word_document:
md_extensions: +raw_html-markdown_in_html_blocks
pandoc_args: ['--lua-filter', 'read_html.lua']
---
# This is rendered as heading
<h1> And this is one, too </h1>其中,read_html.lua必须是包含以下内容的同一目录下的文件:
function RawBlock (raw)
if raw.format:match 'html' and not FORMAT:match 'html' then
return pandoc.read(raw.text, raw.format).blocks
end
end让我们解开上面的包,看看它是如何工作的。您首先会注意到的是word_document的附加参数。md_extensions修改了pandoc解析文本的方式,查看here获取完整列表(或在终端中运行pandoc --list-extensions=markdown)。我们启用raw_html以确保pandoc不会丢弃原始的HTML标签,并禁用markdown_in_html_blocks as以确保我们以pandoc的内部格式作为一个块来获取整个HTML标签。
下一个设置是pandoc_args,我们告诉pandoc在转换过程中使用Lua filter来修改文档。过滤器挑选出所有HTML块,将它们解析为HTML而不是Markdown,并用解析结果替换原始HTML。
因此,如果您使用的是pandoc可以读取的原始HTML,那么就可以了。如果您使用的是pandoc无法读取的特殊指令,那么上述设置也不会有任何帮助。您必须在OOXML中重写标记,这是docx中使用的XML格式。
https://stackoverflow.com/questions/64152352
复制相似问题