I am creating a web-app using Python(Flask), the app will have multiple latex templates, based on the user's input, changes will be made to a template and the final file needs to be exported in PDF. I used pdflatex package in python and it does the job on a single tex file, but most templates have other files as dependencies, how can these be converted to PDFs? I found a repo: https://github.com/aslushnikov/latex-online that converts files with dependencies, but it does so on the terminal, I need to do it programmatically, anyway I can achieve that?

1 Answer 1


From the way the question is asked, I guess you have multiple independent .tex files. I wouldn't use LaTeX code here - you are using a programming language.

Loop over all .tex files and look into them. Loop over the lines. Look at each line. Remove spaces at the start of the line. If it now starts with a %, go to next line. If it starts with \documentclass, exit the line-loop and run it through pdflatex. If it starts with anything else, go to next file.

You could e.g. do

import glob
for file in glob.glob("/home/me/*.tex): #You need to edit the path here.
  f=open("file", "r") # open the file in read mode
  while True:
    if line.startswith("%") :
      #we want to skip these lines
    elif line.startswith("\documentclass") :
      # tex-commands here
    else :
      #we are done with this file, its used as an \input or \include somewhere
  • Hey, no the files are not independent, there are multiple templates but every single template has the main tex file and several dependencies(sty, cls, and other tex files as well), think of it as the zip file you'll get if you download the source code of any template from overleaf, how do you convert that source having multiple files into one PDF, like overleaf does, through python(or some API) Jul 8, 2020 at 7:42
  • Like template1.tex, template2.tex, ... and the content is \documentclass{something} \input{main} ? If yes, the answer works. Jul 13, 2020 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.