views:

702

answers:

2

I'm trying to use scons to build a latex document. In particular, I want to get scons to invoke a python program that generates a file containing a table that is \input{} into the main document. I've looked over the scons documentation but it is not immediately clear to me what I need to do.

What I wish to achieve is essentially what you would get with this makefile:

document.pdf:  table.tex
    pdflatex document.tex

table.tex:
    python table_generator.py

How can I express this in scons?

A: 

In this simple case, the easiest way is to just use the subprocess module

from subprocess import call
call("python table_generator.py")
call("pdflatex document.tex")

Regardless of where in your SConstruct file these lines are placed, they will happen before any of the compiling and linking performed by SCons.

The downside is that these commands will be executed every time you run SCons, rather than only when the files have changed, which is what would happen in your example Makefile. So if those commands take a long time to run, this wouldn't be a good solution.

If you really need to only run these commands when the files have changed, look at the SCons manual section Writing Your Own Builders.

Eli Courtwright
How does this integrate with the latex scanner? Can I still have PDF(target='document.pdf', source='document.tex') in the SConstruct?
saffsd
+3  A: 

Something along these lines should do -

env.Command ('document.tex', '', 'python table_generator.py')
env.PDF ('document.pdf', 'document.tex')

It declares that 'document.tex' is generated by calling the Python script, and requests a PDF document to be created from this generatd 'document.tex' file.

Note that this is in spirit only. It may require some tweaking. In particular, I'm not certain what kind of semantics you would want for the generation of 'document.tex' - should it be generated every time? Only when it doesn't exist? When some other file changes? (you would want to add this dependency as the second argument to Command() that case).

In addition, the output of Command() can be used as input to PDF() if desired. For clarity, I didn't do that.

Hexagon