目前我正在用 Python 制作一个 .txt 文件,然后使用 PyLaTeX 将其转换为 PDF 文件。这是我正在使用的代码:
from pylatex import Document, PageStyle, LineBreak, Section, Subsection, Math, Alignat, LargeText, Center, MediumText
from pylatex.utils import bold, italic, NoEscape
import re
from sympy import *
import sympy as sp
TituloPDF="Ejercicio 001"
archivotxt = "%s.txt"% TituloPDF
texto = open(archivotxt,"w")
x = sp.Symbol('x', real=True)
y = sp.Symbol('y', real=True)
MatrizCoef=[y-x*y+1-x,-x*y]
g=0
texto.write("\\text{Para la ecuación diferencial:} \r")
texto.write("\\left({%s}\\right)dy = \\left({%s}\\right)dx \r"%(sp.latex(MatrizCoef[0]),sp.latex(MatrizCoef[1])))
texto.close()
class Math_LaTeX ():
def __init__(self, doc):
#---Inicializamos---
self.doc = doc
#---De aqui sacamos las ecuaciones del archivo .txt---
equations = self.Read()
#---Creamos el documento Latex---
self.Create(equations)
#---Creamos el PDF---
self.doc.generate_pdf(filepath = TituloPDF, clean_tex = True, compiler = 'pdflatex')
def Read(self):
data = open(archivotxt, 'rt')
equations = data.readlines()
eqs = []
for eq in equations:
eqs.append(' '.join([line.strip() for line in eq.strip().splitlines()]))
return eqs
def Create(self, equations):
for eq in equations:
with self.doc.create(Alignat(numbering = False, escape = False)) as math_eq:
math_eq.append(eq)
geometry_options = {
"head": "0.5cm",
"left": "0.5cm",
"right": "0.5cm",
"bottom": "2.5cm"
}
doc = Document(geometry_options = geometry_options, inputenc = 'utf8')
Math_LaTeX(doc)
我已经很好地使用了它,但是我希望能够自定义交付的 PDF,目前这是输出:
PDF 示例
顶部的边距太多了,除此之外我希望能够添加一个标题,简而言之就是这样(Made in word):
单词示例
上边距是足够的,并且它的标题出现在我的练习所需的所有页面上。
我还希望在引入新的练习或示例时能够更改此标题。
我已经阅读了 PyLaTeX 文档,但我想不出一种方法将它全部包含在一个代码中。非常感谢您的帮助。