1

I have a python script that reads a file containing data + some "latex template". The script produces a long latex source which is compiled to obtain a PDF.

Incidentally, the LaTeX source heavily relies on PythonTex. So I wonder if it might be possible to keep script, data, and template in the same latex file (not at the cost of legibility of the source) and just run latex/pythontex.

It would have the following advantages:

  1. everything would be kept in a single file;

  2. the script and pythontex could run in the same python session and be aware of each other.

Below a simple (almost trivial) example of what the script does. Suppose the template/data file is

\documentclass{article}
\begin{document}
*** is *** years old\par
\end{document}
\begin{data}
Bob 88
Mik 76
\end{data}

The script produces

\documentclass{article}
\begin{document}
Bob is 88 years old\par
Mik is 76 years old\par
\end{document}

EDIT For a less trivial example, imagine that the template is an arbitrary piece of latex/pythontex and that the data contains pieces of latex/pythontex code.

3

Edit: answer using pythontex

I am sure that the answer to your to your question is yes but a "proper" answer is not really possible without more details about what you are trying because you are currently asking for a black box to produce a black box:).

This said, the following code shows one way of using pythontex to correctly typeset your MWE:

\documentclass{article}
\usepackage{pythontex}

\begin{pycode}
data = { 'Bob' : 88,
         'Mik' : 76
        }
\end{pycode}

\begin{document}

\begin{pycode}
for person in data:
  print('%s is %s\n' % (person, data[person]))
\end{pycode}

\end{document}

This produces:

enter image description here

A few comments:

  • As with python, indentation is really important: the initial indentation inside thepycode environments should be flush left and the following lines have to obey the usual python rules
  • There are several other ways of injecting python code into the document. Depending on what your real application looks like, the pycode environment may not be the best approach. Consult the pythontex manual for more details
  • To compile, first run (pdf)latex, then pythontex and then (pdf)latex again
  • I needed to install the pygments library to get this to work (for example, use pip install pygments).

First suggestion using csvsimple

Perhaps this does not suit your real application, but given your MWE I would suggest using csvsimple rather than pythontex.

For example if your main tex file looks like

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\csvreader[head to column names, tabular=l]{data.csv}{}{\person\ is \age}

\end{document}

and data.csv contains

person,age
Bob,88
Mik,76

The output is the same as above.

As I said this may not fit your actual use-case, but personally I find csvsimple really useful for things like this. It is surprisingly powerful, well documented and easy to use. [This is not meant to be a sale pitch I have no stake in the package and do not know its author!]

  • My MWE is deceptively minimal. I have edited the question to explain the level of complexity of the real application. – Primo Petri Feb 5 '17 at 13:56
  • @PrimoPetri OK, I've added a pythontex answer – Andrew Feb 5 '17 at 22:53

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.