如何用Python生成含高质量LaTeX数学公式的PDF报告?
推荐方案与示例
方案一:使用PyLaTeX直接生成PDF报告
PyLaTeX允许用Python代码构建LaTeX文档并直接编译为PDF,原生支持所有LaTeX数学表达式,排版质量、对齐效果完全遵循专业LaTeX标准,彻底规避转图片带来的问题。
前置要求
- 安装PyLaTeX:
pip install pylatex - 本地部署TeX发行版(如TeX Live、MikTeX),确保
pdflatex命令可在终端正常执行
示例:图片与矩阵方程对齐的报告
以下代码生成包含两组图片及对应矩阵方程的PDF,方程与图片自然排版对齐,所有数学元素均为矢量格式,缩放无失真:
from pylatex import Document, Section, Subsection, Figure, Math, NoEscape from pylatex.utils import italic # 创建文档,加载amsmath用于复杂数学排版,graphicx用于图片插入 doc = Document(documentclass='article', packages=['amsmath', 'graphicx']) with doc.create(Section('图像变换与矩阵分析')): doc.append(italic('以下为两组图像及其对应的线性变换矩阵')) # 第一组:原始图像与变换方程 with doc.create(Subsection('平移缩放变换')): with doc.create(Figure(position='h!')) as fig1: fig1.add_image('image1.png', width='0.45\\textwidth') fig1.add_caption('原始输入图像') # 用align*环境实现方程对齐 doc.append(Math(data=NoEscape(r'''\begin{align*} \mathbf{y} = \mathbf{A} \mathbf{x} + \mathbf{b} \\ \text{变换矩阵: } \mathbf{A} = \begin{pmatrix} 2 & 0 & 1 \\ 0 & 3 & -1 \\ 0 & 0 & 1 \end{pmatrix}, \quad \mathbf{b} = \begin{pmatrix} 5 \\ 2 \\ 0 \end{pmatrix} \end{align*}'''), inline=False)) # 第二组:旋转图像与旋转矩阵 with doc.create(Subsection('旋转变换')): with doc.create(Figure(position='h!')) as fig2: fig2.add_image('image2.png', width='0.45\\textwidth') fig2.add_caption('45°旋转后图像') doc.append(Math(data=NoEscape(r'''\begin{align*} \mathbf{R}(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix} \\ \text{当}\theta = 45^\circ\text{时: } \mathbf{R}(45^\circ) = \begin{pmatrix} \frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2} & 0 \\ \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0 \\ 0 & 0 & 1 \end{pmatrix} \end{align*}'''), inline=False)) # 生成PDF,保留tex文件便于调试 doc.generate_pdf('math_analysis_report', clean_tex=False)
关键说明
align*环境自动处理方程对齐,确保数学表达式排版规范- 图片通过
graphicx包插入,可通过width参数控制尺寸,与方程自然衔接 - 最终PDF中所有数学元素均为矢量格式,完全满足专业报告的分辨率要求
方案二:Matplotlib渲染矢量图嵌入ReportLab
若必须使用ReportLab,可通过Matplotlib将LaTeX公式渲染为SVG(矢量格式),再嵌入ReportLab,避免位图的分辨率缺陷,同时通过坐标控制实现图片与方程的对齐。
示例代码
import matplotlib.pyplot as plt from reportlab.pdfgen import canvas from reportlab.graphics import renderPDF from svglib.svglib import svg2rlg # 将LaTeX公式渲染为SVG矢量图 def latex_to_svg(latex_str, output_path): plt.figure(figsize=(6, 2)) plt.text(0.5, 0.5, latex_str, fontsize=14, ha='center', va='center', usetex=True) plt.axis('off') plt.savefig(output_path, format='svg', bbox_inches='tight', pad_inches=0.1) plt.close() # 生成两个方程的SVG文件 eq1_latex = r'$\mathbf{y} = \mathbf{A} \mathbf{x} + \mathbf{b}, \quad \mathbf{A} = \begin{pmatrix}2&0&1\\0&3&-1\\0&0&1\end{pmatrix}$' eq2_latex = r'$\mathbf{R}(45^\circ) = \begin{pmatrix}\frac{\sqrt{2}}{2}&-\frac{\sqrt{2}}{2}&0\\\frac{\sqrt{2}}{2}&\frac{\sqrt{2}}{2}&0\\0&0&1\end{pmatrix}$' latex_to_svg(eq1_latex, 'transform_eq.svg') latex_to_svg(eq2_latex, 'rotate_eq.svg') # 用ReportLab组装PDF c = canvas.Canvas('reportlab_math_report.pdf') # 插入第一张图片及对应方程 c.drawImage('image1.png', 50, 600, width=200, height=150) drawing = svg2rlg('transform_eq.svg') renderPDF.draw(drawing, c, 50, 550) # 插入第二张图片及对应方程 c.drawImage('image2.png', 50, 350, width=200, height=150) drawing = svg2rlg('rotate_eq.svg') renderPDF.draw(drawing, c, 50, 300) c.save()
关键说明
- 需确保Matplotlib已配置LaTeX支持(依赖本地TeX环境)
- SVG为矢量格式,嵌入PDF后缩放无失真
- 通过控制ReportLab的坐标参数,可精准实现图片与方程的对齐
内容的提问来源于stack exchange,提问作者Siddhu
相关产品推荐
相关产品推荐

