TransWikia.com

How to convert a latex folder(including multiple tex files, sty, cls files) to PDF using python?

TeX - LaTeX Asked by Manmeet Sethi on February 25, 2021

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?

One Answer

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:
    line=f.readline()
    if line.startswith("%") :
      #we want to skip these lines
      continue
    elif line.startswith("documentclass") :
      # tex-commands here
    else :
      #we are done with this file, its used as an input or include somewhere
      break

Answered by Michael Bölting on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP