Pandoc升级后自动为HTML表格添加<br />标签,如何禁用该行为?
问题描述
我使用Rmarkdown结合Pandoc自定义模板生成带审批签名表格的报告,签名块放在模板的<body>元素中,但无论放在<head>、<header>还是其他位置,都会出现相同问题。该功能在Pandoc 2.11.2中可正常工作,升级至3.1.11后,Pandoc会在表格的每个标签末尾添加换行标签<br />,导致显示效果异常。请问是否有Pandoc选项可以禁用此行为?
(注:此为上周问题重发,已确认问题源于Pandoc版本,尚未找到解决方法)
报告显示效果对比

HTML源码对比

模板文件(RMtemplate.html)
<!DOCTYPE html> <html> <head> </head> <body style = "background-color:#C0C0C0"> $if(title)$ <font style = 'font-size:2.074em'>$title$</font> <br/><br/> $endif$ $body$ $if(signature)$ $signature$ $endif$ </body> </html>
Rmd文件
--- output: html_document: self_contained: true template: RMtemplate.html --- ```{r, echo = FALSE} cat(R.Version()$version.string) cat(sprintf("rmarkdown version: %s", packageVersion("rmarkdown"))) cat(sprintf("pandoc version: %s", rmarkdown::find_pandoc()$version)) ``` ```{r, echo = FALSE} TITLE <- "Report Title" SIGNATURE <- "<table cellpadding = \"2\"><tr><td style = \"width:75%;border-bottom:solid black 1px;height:1in;\"></td><td style = \"width:5%;\"></td><td style = \"width:20%;border-bottom:solid black 1px;vertical-align:bottom\"></td></tr><tr><td>Approver 1 Role</td><td></td><td>Date</td></tr><tr><td style = \"width:75%;border-bottom:solid black 1px;height:1in;\"></td><td style = \"width:5%;\"></td><td style = \"width:20%;border-bottom:solid black 1px;vertical-align:bottom\">14 Jan 2025 13:18</td></tr><tr><td>User Name, Approver 2 Role</td><td></td><td>Date</td></tr></table>" ``` --- title: '`r TITLE`' signature: '`r SIGNATURE`' ---
解决方法
这个问题源于Pandoc 3.x版本对模板变量的处理逻辑变更:默认会对模板中的纯HTML变量内容进行自动段落拆分和换行转换。以下两种方案可禁用该行为:
方案1:使用原始变量输出
在自定义HTML模板中,将$signature$替换为$raw.signature$,Pandoc会直接输出变量中的原始HTML内容,不做任何格式化处理:
$if(signature)$ $raw.signature$ $endif$
这是最直接的解决方案,专门用于保留变量中的原始代码内容。
方案2:通过Pandoc参数强制禁用格式化
若不想修改模板,可在Rmarkdown的YAML配置中添加Pandoc参数,指定变量以原始格式输出:
--- output: html_document: self_contained: true template: RMtemplate.html pandoc_args: ["--variable", "signature=raw:$SIGNATURE$"] ---
此方式需确保变量传递格式正确,优先推荐方案1。
内容的提问来源于stack exchange,提问作者Benjamin
相关产品推荐
相关产品推荐

