You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Jupyter转LaTeX的Lua过滤器:隐藏指定代码块方案

解决方案

一、隐藏Jupyter中标记为不显示的代码块

Jupyter导出的Markdown里,被设置为“不显示输入”的代码块会被包裹在带有hide-input类的Div中。你可以用下面的Lua过滤器移除这类代码块:

function Div(el)
  -- 检测是否是Jupyter标记为隐藏输入的Div
  if el.classes:includes("hide-input") then
    -- 遍历内容,移除所有CodeBlock元素
    for i = #el.content, 1, -1 do
      if el.content[i].t == "CodeBlock" then
        el.content:remove(i)
      end
    end
  end
  return el
end

使用时,将过滤器保存为hide-jupyter-hidden.lua,然后调用pandoc转换:

pandoc input.ipynb -o output.tex --lua-filter=hide-jupyter-hidden.lua

二、隐藏包含特定文本的代码块

如果要移除内容中包含特定文本(比如# HIDE_THIS)的代码块,可以用下面的过滤器:

function CodeBlock(el)
  -- 检测代码块内容是否包含指定文本(这里以"# HIDE_THIS"为例)
  if string.find(el.text, "# HIDE_THIS") then
    -- 返回空元素,即移除该代码块
    return {}
  end
  return el
end

同样保存为hide-specific-code.lua,转换命令:

pandoc input.ipynb -o output.tex --lua-filter=hide-specific-code.lua

三、合并两种需求的过滤器

如果需要同时实现两种隐藏逻辑,可以把上述代码合并:

function Div(el)
  if el.classes:includes("hide-input") then
    for i = #el.content, 1, -1 do
      if el.content[i].t == "CodeBlock" then
        el.content:remove(i)
      end
    end
  end
  return el
end

function CodeBlock(el)
  if string.find(el.text, "# HIDE_THIS") then
    return {}
  end
  return el
end

内容的提问来源于stack exchange,提问作者Richard Huntsinger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 06:53:15