Pandoc Jupyter转LaTeX:如何调整图片尺寸?
Jupyter转LaTeX时的图片尺寸调整方案
Lua过滤器实现尺寸捕获与转换
完全可以用Lua过滤器搞定这个需求,把带尺寸的图片引用转成LaTeX的\includegraphics格式。
比如你在Jupyter Markdown里用HTML标签指定尺寸:
<img src="images/image.jpg" width="300" height="200" alt="示例图片">
写个简单的Lua过滤器(保存为image-size.lua):
function Image(img) local width = img.attributes.width local height = img.attributes.height local options = {} if width then table.insert(options, "width=" .. width) end if height then table.insert(options, "height=" .. height) end if #options > 0 then local opt_str = table.concat(options, ",") return pandoc.RawInline("latex", "\\includegraphics[" .. opt_str .. "]{" .. img.src .. "}") else return img end end
转换时调用这个过滤器就行:
pandoc input.ipynb -o output.tex --lua-filter=image-size.lua
这个过滤器会自动抓取图片的宽高属性,转成LaTeX识别的格式,解决HTML代码被忽略的问题。
其他更优调整方法
直接写LaTeX语法
如果你的Jupyter环境支持渲染LaTeX(大部分都支持),直接在Markdown单元格里写LaTeX图片命令:
\includegraphics[width=3cm,height=2cm]{images/image.jpg}
转LaTeX时不需要额外处理,直接保留原命令,缺点是Jupyter里预览不如Markdown图片直观,但转换效率最高。
用Markdown图片属性扩展
部分Markdown扩展支持给图片加尺寸参数,写法如下:
{width=300 height=200}
转换时需要让pandoc启用+link_attributes扩展,配合上面的Lua过滤器:
pandoc input.ipynb -o output.tex -f markdown+link_attributes --lua-filter=image-size.lua
这种方式语法简洁,Jupyter里也能正常预览图片。
内容的提问来源于stack exchange,提问作者Adverse Camber
相关产品推荐
相关产品推荐

