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

在Pandoc Lua过滤器中导入TikZ生成图片后添加下方文本的方法

在Lua过滤器中为转换后的TikZ图像添加下方文本

我自己写了个Lua过滤器,能提取TikZ生成的合成图像(不是已导入的现成图),用ImageMagick转成.png文件,再通过pandoc.Para插入到docx里。现在想问问,能不能在转换后的图像下方加一段文本?

我的最小工作示例代码:

function RawBlock(el)
    if string.find(el.text,"\\begin{tikzpicture}") then
        io.output("C:/Logs/output.tex")
        io.write("\\documentclass{standalone}", "\n")
        io.write("\\usepackage{tikz}", "\n")
        io.write("\\usepackage{pgfplots}", "\n")
        io.write("\\pgfplotsset{compat=newest}", "\n")
        io.write("\\usepackage{siunitx}", "\n")
        io.write("\\begin{document}", "\n")
        io.write(el.text, "\n")
        io.write("\\end{document}", "\n")
        io.close()
        os.execute("cd C:/Logs & pdflatex output.tex & convert -density 288 output.pdf -alpha off -resize 35% output.png")
        print(el.content)
        return pandoc.Para({pandoc.Image({}, "C:/Logs/output.png")})
    end
end

解决方案

要实现这个需求,只需要让RawBlock函数返回一个块元素列表,而不是单个的Para对象。把图像段落和文本段落放在同一个数组里返回,pandoc就会按顺序渲染它们,文本自然就出现在图像下方了。

修改后的代码示例:

function RawBlock(el)
    if string.find(el.text,"\\begin{tikzpicture}") then
        io.output("C:/Logs/output.tex")
        io.write("\\documentclass{standalone}", "\n")
        io.write("\\usepackage{tikz}", "\n")
        io.write("\\usepackage{pgfplots}", "\n")
        io.write("\\pgfplotsset{compat=newest}", "\n")
        io.write("\\usepackage{siunitx}", "\n")
        io.write("\\begin{document}", "\n")
        io.write(el.text, "\n")
        io.write("\\end{document}", "\n")
        io.close()
        os.execute("cd C:/Logs & pdflatex output.tex & convert -density 288 output.pdf -alpha off -resize 35% output.png")
        
        -- 返回包含图像和文本的块列表
        return {
            pandoc.Para({pandoc.Image({}, "C:/Logs/output.png")}),
            pandoc.Para({pandoc.Str("这是图像下方的自定义文本")})
        }
    end
end

如果需要文本是动态生成的(比如从TikZ代码里提取说明),可以在代码里解析RawBlock的内容。比如假设你在TikZ块前加了注释标注说明:

% 图像说明:这是一个正弦函数的TikZ绘图
\begin{tikzpicture}
    % TikZ代码...
\end{tikzpicture}

可以用下面的代码自动提取这段说明作为图像下方的文本:

function RawBlock(el)
    if string.find(el.text,"\\begin{tikzpicture}") then
        -- 提取注释里的图像说明,没有的话用默认文本
        local image_caption = el.text:match("%% 图像说明:(.*)\n") or "无图像说明"
        
        io.output("C:/Logs/output.tex")
        io.write("\\documentclass{standalone}", "\n")
        io.write("\\usepackage{tikz}", "\n")
        io.write("\\usepackage{pgfplots}", "\n")
        io.write("\\pgfplotsset{compat=newest}", "\n")
        io.write("\\usepackage{siunitx}", "\n")
        io.write("\\begin{document}", "\n")
        -- 写入TikZ内容时去掉注释行
        local clean_tikz = el.text:gsub("%% 图像说明:.*\n", "")
        io.write(clean_tikz, "\n")
        io.write("\\end{document}", "\n")
        io.close()
        os.execute("cd C:/Logs & pdflatex output.tex & convert -density 288 output.pdf -alpha off -resize 35% output.png")
        
        return {
            pandoc.Para({pandoc.Image({}, "C:/Logs/output.png")}),
            pandoc.Para({pandoc.Str(image_caption)})
        }
    end
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 19:07:03