Generallatex and pythonscript

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
hueschel
Posts: 2
Joined: Mon Dec 27, 2010 2:49 pm

latex and pythonscript

Post by hueschel »

hello latex community

i wrote a package which provides a new command called \QJinclude. This command is to be used instead of \includegraphics when i actually do not want to include the original eps-graphic but the new eps graphic which is created by the script. This python script takes the original pic picname, edits it and creates a new pic called picname-QJ.eps . Then the command \includegraphics{picname-QJ.eps} follows. But when i run pdflatex an error appears from the package epstopdf that claims that there would not be that file picname-QJ.eps. However when i look in the directory i can see that the file exists. If i run pdflatex again there are no more problems and the eps gets converted to pdf.
My guess is that latex calls the script and heads to the next command instead of waiting for the script to be finished. That way the file would not yet be created and creates an error.
As the data given to the python script is dynamic, pdflatex would probably be to run many times. Besides it is not very nice to get errors all the time. Can anyone help me with this? :)

edit: Oh and i almost forgot: You can see that i call the script by \write18{python QJchem.py}. This of course will only work on Linux. Does anybody know how to put this for a windows user?

Code: Select all

\newcommand{\QJinclude}[3][]{%
   	\newwrite\myfile%
	\immediate\openout\myfile=pythonjob.out% 
	\write\myfile{#3}% Data for the script to edit the pic
	\closeout\myfile%
    \newwrite\myfile%
	\immediate\openout\myfile=QJfile.out%
	\write\myfile{#2}% Tells the script which pic to edit
	\closeout\myfile%
    \write18{python QJchem.py}%
    \includegraphics[#1]{#2-QJ.eps}%
}%
And for those of you who know python:

Code: Select all

#!/usr/local/bin/python
import os,re,sys,time,tempfile
sys.path.append(os.getcwd())
fileext = ".eps"
arr = []
c = open("pythonjob.out", "r")
text = c.read()
text = text.replace(";", "")
text = text.replace("\n", "")
for B in text.split():
   arr.append(B)
c.close()
d = open("QJfile.out", "r")
text = d.read()
file = text.replace("\n", "")
d.close()

f = open(file + fileext, "r")
text = f.read()
f.close()
counter = 0
while counter <= len(arr)-1:
  m = re.search(r"QJ\d{1,}", text)
  text = text.replace(m.group(0), arr[counter])
  counter = counter + 1
ff = open(file + "-QJ" + fileext, "w")
ff.write(text)
ff.close()
exit()

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Re: latex and pythonscript

Post by frabjous »

I don't know exactly what your workflow is, but why not have the python script call the epstopdf commandline tool directly from the script before calling pdflatex, and have the LaTeX source include the PDF file instead? That would do the same thing the epstopdf package does, but without encountering this problem.
hueschel
Posts: 2
Joined: Mon Dec 27, 2010 2:49 pm

latex and pythonscript

Post by hueschel »

I´m not sure what you mean but if you think this would work:

Code: Select all

\newcommand{\QJinclude}[3][]{%
      \newwrite\myfile%
   \immediate\openout\myfile=pythonjob.out%
   \write\myfile{#3}% Data for the script to edit the pic
   \closeout\myfile%
    \newwrite\myfile%
   \immediate\openout\myfile=QJfile.out%
   \write\myfile{#2}% Tells the script which pic to edit
   \closeout\myfile%
    \write18{python QJchem.py}%
    \write18{epstopdf #2-QJ.eps}%
    \includegraphics[#1]{#2-QJ.pdf}%
}%
Unfortunately pdflatex will tell me then that epstopdf can´t find the eps-file (as pdflatex heads again to the next command instead of waiting for pythonscript to finish...)
I can try to get the python-script call epstopdf instead of latex but i think this would result in the problem that latex won´t find the pdf file as the python script is not fast enough again
Post Reply