• 用python批量插入图片到latex中


    背景

            最近在用latex做笔记,有时笔记中需要插入较多图片,以手工的方式逐个插入图片费时费力,所以想到用python管理脚本,实现图片的自动插入。

    方案

            我使用的解决方案是,为每一个图片生成一个独立的tex文件,放置该图片的引用信息,然后以“/include{img_tex_path.tex}”的方式,把图片插入到latex文件中的对应位置。

            用python为每一个图片生成独立tex文件的脚本如下:

    1. import os
    2. import glob
    3. def write_fig_2_tex(fig_path,fig_width=12,fig_caption=""):
    4. # 图片信息整理
    5. fig_path = "/".join(os.path.split(fig_path)[:]) # 图片路径标准化
    6. fig_name = os.path.split(fig_path)[-1].split(".")[0] # 图片name
    7. fig_label = "fig:"+fig_name # 图片label
    8. # 创建并打开tex文件
    9. tex_path = fig_path.split(".")[0]+".tex"
    10. tex_file = open(tex_path,"w",encoding="utf-8")
    11. # # 写入图片引用信息--方式1
    12. # tex_file.write(
    13. # "\\begin{{figure}}[htbp]\n"
    14. # "\t\centering\n"
    15. # "\t\includegraphics[width={}cm]".format(fig_width)+"{"+fig_path+"}\n"
    16. # "\t\caption{"+fig_caption+"}\n"
    17. # "\t\label{"+fig_label+"}\n"
    18. # "\end{figure}\n"
    19. # "\clearpage\n"
    20. # )
    21. # 写入图片引用信息--方式2
    22. tex_file.write(
    23. f"\\begin{{figure}}[htbp]\n"
    24. f"\t\centering\n"
    25. f"\t\includegraphics[width={fig_width}cm]{{{fig_path}}}\n"
    26. f"\t\caption{{{fig_caption}}}\n"
    27. f"\t\label{{{fig_label}}}\n"
    28. f"\end{{figure}}\n"
    29. f"\clearpage\n"
    30. )
    31. # 关闭tex文件
    32. tex_file.close()
    33. if __name__ =="__main__":
    34. fig_dir = "data/chap05/figs/"
    35. fig_paths = glob.glob(fig_dir+"*.png")
    36. for fig_path in fig_paths:
    37. write_fig_2_tex(fig_path,fig_width=12,fig_caption=" ")
    '
    运行

            如果想把一个文件夹下的所有图片的latex插入信息放进同一个tex文件中,可参考以下代码:

    1. import os
    2. import glob
    3. img_dir = "data/chap01/sections/figs/"
    4. tex_path = img_dir+"/figs.tex"
    5. tex_file = open(tex_path,"w",encoding="utf-8")
    6. #tex_file = open(tex_path,"w+",encoding="utf-8")
    7. for i in range(1,60):
    8. img_path = img_dir+"/{}.png".format(i)
    9. if os.path.exists(img_path):
    10. # 图片信息整理
    11. fig_path = "/".join(os.path.split(img_path)[:]) # 图片路径标准化
    12. fig_name = os.path.split(fig_path)[-1].split(".")[0] # 图片name
    13. fig_label = "fig:"+fig_name # 图片label
    14. fig_width = 12
    15. fig_caption = f"sub-fig-{i}"
    16. tex_file.write(
    17. f"\\begin{{figure}}[htbp]\n"
    18. f"\t\centering\n"
    19. f"\t\includegraphics[width={fig_width}cm]{{{fig_path}}}\n"
    20. f"\t\caption{{{fig_caption}}}\n"
    21. f"\t\label{{{fig_label}}}\n"
    22. f"\end{{figure}}\n"
    23. f"\clearpage\n"
    24. )
    25. tex_file.close()

    扩展

            类似地,可以用python批量/自动处理其他latex文件或命令。

  • 相关阅读:
    软件测试面试遇到之redis要怎么测试?
    Ton 区块链的官方 类ERC20-Token 智能合约代码-Transfer部分解析
    次元裂缝已打开,AI绘画突飞猛进,其潜力究竟有多大
    Linux工具——gcc
    蓝桥杯 题库 简单 每日十题 day6
    SRM : A Style-based Recalibration Module for Convolutional Neural Networks论文笔记
    ADB安装方法及常用命令汇总
    数据结构的基本内容
    V90伺服 EPOS模式下回原(详细配置+SCL源代码)
    【youcans 的图像处理学习课】总目录
  • 原文地址:https://blog.csdn.net/Strive_For_Future/article/details/127071318