如何在Rmarkdown+reveal.js中创建增量表格类incremental_table
实现reveal.js表格逐行增量显示的方法
要实现和:::incremental列表用法一致的逐行显示表格,需要借助Pandoc Lua过滤器修改表格的HTML输出,给每行添加reveal.js的增量显示类。具体步骤如下:
1. 编写Lua过滤器文件
创建一个名为incremental-table.lua的文件,内容如下:
function Div(el) -- 识别incremental_table类的块 if el.classes:includes("incremental_table") then -- 遍历块内元素,找到表格 for _, block in ipairs(el.content) do if block.t == "Table" then -- 给表格的每一行数据添加fragment类(表头保持常驻) for _, row in ipairs(block.content.body) do row.attributes.class = (row.attributes.class or "") .. " fragment" end end end return el end return nil end
2. 在Rmarkdown中配置过滤器
在你的Rmarkdown文档的YAML头部,添加Lua过滤器的引用:
--- title: "你的演示文稿标题" output: revealjs::revealjs_presentation: pandoc_args: ["--lua-filter=incremental-table.lua"] ---
3. 使用:::incremental_table包裹表格
和列表的增量用法完全一致,用:::incremental_table块包裹你的Markdown表格即可:
:::incremental_table |variable |class |description | |:------------------|:---------|:------------------| |character_1_gender |character |The gender of the older character, as identified by the person who submitted the data for this couple| |character_2_gender |character |The gender of the younger character, as identified by the person who submitted the data for this couple| :::
原理说明
reveal.js通过给元素添加fragment类实现增量显示效果。Lua过滤器会在Pandoc转换Markdown为HTML的过程中,自动识别incremental_table块内的表格,给每一行数据(表头除外)加上fragment类,从而实现逐行点击显示的效果。
内容的提问来源于stack exchange,提问作者maxpe
相关产品推荐
相关产品推荐

