I want to automatically trim a PDF (cut out e-stamps from a larger sheet and insert them into a letter). That should work like that: I give a number n, to cut out the n-th stamp on the sheet, and for that I wanted to use pyhontex to calculate the coordinates to do so. Here is the working python code for that:

n = 9

spalten = 4
zeilen  = 8

xOff = 27.0
yOff = 30.5

xStamp = 32.0
yStamp = 11.5

xSpacing = 38.2
ySpacing = 31.4

left   = xOff + ((n-1) / spalten) * xSpacing
right  = left + xStamp
top    = yOff + ((n-1) % spalten) * ySpacing
bottom = top + yStamp

print("trim={0}mm {1}mm {2}mm {3}mm,".format(left, right, top, bottom))

trim=103.4mm 135.4mm 30.5mm 42.0mm, would be a sample string which should be used by \includegraphics to crop the PDF. So far so good.

The problem is that the file compiles fine but the pycode environment does not give anything back. Minimal working sample:



print('python test')


I use MiKTeX-pdfTeX 2.9.5653 (1.40.16) (MiKTeX 2.9 64-bit) via: C:\Program Files\MiKTeX 2.9\miktex\bin\x64\pdflatex" ?me" -parse-first-line -shell-escape -enable-write18 -aux-directory="C:\Users\Lenny\Documents\LaTeX\tmp" -synctex=1 -interaction=nonstopmode.

Any suggestions?

  • Did you try compiling in the three steps pdflatex, pythontex, pdflatex? – Andrew Swann Jul 3 '15 at 11:52
  • Oh god! Totally missed that part in the manual. Thank you for the hint. Got it to work. – milkpirate Jul 3 '15 at 22:14

To compile a file using pythontex requires three steps. For example if using pdflatex on the main file then you need to at least run


Using a compilation framework such as latexmk that is aware of pythontex can simplify this process.

  • Another thing: Do you have an idea how I can make pythontex use DIR in the --aux-directory=DIR parameter of pdflatex? – milkpirate Jul 6 '15 at 15:37
  • I am not on a windows system so my pdflatex does not have that option. Does \setpythontexworkingdir{<outputdir>} do what you want? Otherwise you should ask a separate question. – Andrew Swann Jul 7 '15 at 6:45

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.