Python - Pylatex header?

459 Views Asked by At

Currently I am making a .txt file with Python, to later transform it into a PDF file using PyLaTeX. This is the code I am using:

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)

I have worked well with it, however I would like to be able to customize the delivered PDF, currently this is the output:

PDF Example

PDF Example

The margin at the top is too much, besides that I would like to be able to add a heading, in short like this (Made in word):

Word Example

Word Example

The top margin is adequate, and it has a heading that appears on all the pages that my exercise requires.

I would also like this heading to be able to be changed when a new exercise or example is introduced.

I've read the PyLaTeX documentation, but I can't think of a way to include it all in one code. I would greatly appreciate your help.

0

There are 0 best solutions below