4

I am preparing a data processing code and the results from these are to be fed to a TeX/LaTeX file and the file is to be compiled by Python (i.e. Python should send the command to compile the TeX/LaTeX file)

At present I plan to generate a text file (with .tex extension) with the necessary syntax for TeX/LaTeX and then call external system command using os.system. Is there any easier way or modules that can do this?

  • There's http://starship.python.net/crew/bhoel/PyLaTeX-0.2/html/PyLaTeX.LaTeX.html , but I don't know how well it's currently supported (what versions of Python, etc). – Alex Martelli Feb 14 '15 at 16:39
  • 1
    Depending on how complicated the LaTeX document is, you might need to run `pdflatex` (and possibly other tools) more than once. So a better solution would be to call [`latexmk`](http://users.phys.psu.edu/~collins/software/latexmk-jcc/) if your TeX distribution contains it. This will take care of all this. – Roland Smith May 24 '15 at 23:02

3 Answers3

4

No such Python modules exist but you could try generating text files with the required syntax of the text file with .tex extension and compile it by running system commands with Python:

import os
os.system("pdflatex filename")
Tom Kurushingal
  • 6,086
  • 20
  • 54
  • 86
  • 3
    It is recommended in the documentation to use `subprocess` module instead. Additionally it is a good idea to check the return value and/or the output to see if pdflatex needs to be run again. – Roland Smith May 24 '15 at 22:53
1

You could use PyLaTeX for this. This is exactly one of the things that it is meant to be used for. https://github.com/JelteF/PyLaTeX

JelteF
  • 3,021
  • 2
  • 27
  • 35
  • pyLatex is also asking for a compiler, is there a way to gemerate without having to install any additional packages on the system? – El_1988 Nov 12 '19 at 16:06
0

Make sure, you have installed Perl and MikTeX (for latexmk and pdflatex compilers support) in your system.

If not, you can download Perl from https://www.perl.org/get.html

and MikTeX from https://miktex.org/download.

Also do not forget to check http://mg.readthedocs.io/latexmk.html#installation as it guides nicely about Latex compilers.

I have document.tex with following content.

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
%
%
%
\begin{document}%
\normalsize%
\section{A section}%
\label{sec:A section}%
Some regular text and some %
\textit{italic text. }%
\subsection{A subsection}%
\label{subsec:A subsection}%
Also some crazy characters: \$\&\#\{\}

%
\end{document}

Finally create any python file and paste any one of the below code and run.

1st way

# 1st way
import os

tex_file_name = "document.tex";
os.system("latexmk " + tex_file_name + " -pdf");

2nd way

# 2nd way
import os

tex_file_name = "document.tex";
os.system("pdflatex " + tex_file_name);

For compiling complex latex files, you need to look for extra options passed with latexmk or pdflatex commands.

Thanks.

hygull
  • 8,464
  • 2
  • 43
  • 52