Is there a way to append a list or text to an existing tex file using pylatex?

491 Views Asked by At

I tried to run latexTop() first then latexMiddle() but it does not seem to work however it does not show error message it just does not append latexmiddle into latextop ,I also tried open('name.tex','r') as myfile: text = myfile.read()

def latexTop():
    doc = Document('name')
    doc.append(Command('title', 'title'))
    doc.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))
    doc.generate_tex()
    #doc.generate_pdf('name',clean_tex=False,compiler='pdflatex')

def latexMiddle(doc):
    doc = Document('name')
    section = Section('Matrix tests')
    subsection = Subsection('Array')
    vec = Matrix(a)
    vec_name = VectorName('a')
    math = Math(data=[vec_name, '=', vec])
    subsection.append(math)
    section.append(subsection)
    subsection = Subsection('Matrix')
    vec = Matrix(a, mtype='b')
    vec_name = VectorName('a')
    math = Math(data=[vec_name, '=', vec])
        
    subsection.append(math)
    section.append(subsection)

1

There are 1 best solutions below

0
On

I can't test it but I think there are big mistakes in code.

  1. you have to run latexMiddle() inside latextTop()

    def latexTop():
        doc = Document('name')
        doc.append(Command('title', 'title'))
        doc.append(Command('date', NoEscape(r'\today')))
        doc.append(NoEscape(r'\maketitle'))
    
        latexMiddle(doc)
    
        doc.generate_tex()
        #doc.generate_pdf('name', clean_tex=False, compiler='pdflatex')
    
  2. latexMiddle gets doc but later it creates totally new doc but finally it make nothing with doc - it doesn't append section - so all code is useless.

    def latexMiddle(doc):
        # doc = Document('name') # DON'T create new document
    
        section = Section('Matrix tests')
    
        subsection = Subsection('Array')
        vec = Matrix(a)
        vec_name = VectorName('a')
        math = Math(data=[vec_name, '=', vec])
        subsection.append(math)
        section.append(subsection)
    
        subsection = Subsection('Matrix')
        vec = Matrix(a, mtype='b')
        vec_name = VectorName('a')
        math = Math(data=[vec_name, '=', vec])
        subsection.append(math)
        section.append(subsection)
    
        doc.append(section)  # <--- append to doc 
    

Eventually you should first create doc and later send it to both functions, and finally generate file.

def latexTop(doc):   # <--- get doc
    
    doc.append(Command('title', 'title'))
    doc.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

def latexMiddle(doc):
    # doc = Document('name') # DON'T create new document
    
    section = Section('Matrix tests')
    
    subsection = Subsection('Array')
    vec = Matrix(a)
    vec_name = VectorName('a')
    math = Math(data=[vec_name, '=', vec])
    subsection.append(math)
    section.append(subsection)
    
    subsection = Subsection('Matrix')
    vec = Matrix(a, mtype='b')
    vec_name = VectorName('a')
    math = Math(data=[vec_name, '=', vec])
    subsection.append(math)
    section.append(subsection)

    doc.append(section)  # <--- append to doc 

#def latexBottom(doc):
#    doc.append( ...something...)

# ---

doc = Document('name')

latexTop(doc) 
latexMiddle(doc)
#latexBottom(doc)
    
doc.generate_tex()
#doc.generate_pdf('name', clean_tex=False, compiler='pdflatex')