如何在bookdown slidy中实现定理环境?v0.25版本原有方法失效
bookdown slidy幻灯片定理环境问题解答
是Bug还是有意移除?
这是有意的设计调整。bookdown v0.25版本统一采用Pandoc标准的围栏式环境语法(::: {.theorem})替代了旧版的代码块式声明,目的是对齐Pandoc原生语法规范。但bookdown::slidy_presentation2的渲染逻辑暂未适配新语法,导致幻灯片中无法生效,属于功能适配滞后,并非代码Bug。
当前在slidy中配置定理环境的可行方法
方法1:自定义CSS+HTML标签
- 在文档YAML头部指定自定义CSS文件:
output: bookdown::slidy_presentation2: css: custom.css
- 新建
custom.css文件,添加定理样式:
.theorem { border-left: 4px solid #2c3e50; padding: 10px 15px; margin: 15px 0; background-color: #f8f9fa; } .theorem .title { font-weight: bold; color: #2c3e50; margin-right: 8px; }
- 在幻灯片中用HTML标签实现定理:
<div class="theorem"> <span class="title">定理</span> my theorem </div>
方法2:用Lua过滤器适配围栏式语法
编写Lua过滤器让slidy识别围栏式定理环境:
- 新建
theorem-filter.lua文件:
function Div(el) if el.classes:includes("theorem") then -- 添加定理标题 local title = pandoc.Span(pandoc.Strong(pandoc.Str("定理")), {class = "title"}) table.insert(el.content, 1, title) -- 应用样式 el.attributes.style = "border-left:4px solid #2c3e50; padding:10px 15px; margin:15px 0; background-color:#f8f9fa;" return el end end
- 在YAML中引用过滤器:
output: bookdown::slidy_presentation2: pandoc_args: ["--lua-filter=theorem-filter.lua"]
- 之后就能正常使用围栏式语法:
::: {.theorem} my theorem :::
方法3:临时回退旧语法(不推荐)
若需快速临时解决,可继续使用旧版代码块语法,但该语法已被官方弃用,后续版本可能完全移除支持,不建议长期依赖。
内容的提问来源于stack exchange,提问作者fe2084
相关产品推荐
相关产品推荐

