Pythontex workflow with LyX

Thanks for the pointer, Scott. The process does involve (extensive) use of the converters.

EDIT: Quick and dirty solution follows after step 11.

This assumes you already have PythonTeX installed.

The converter utilities are used to convert one file format to another. Since LyX already has a way to convert from LyX --> Tex --> PDF, we need to convince it that it's doing something else. So we create some extra file types.

Step 1) In LyX, select Tools, then Preferences. Select File Handling. Select File Formats.

Step 2) Select "New". Fill in the fields as follows:

Format: PythonTeXStep1
Shortname: pytexs1
extension: pytexs1extension

Leave everything else alone. Click "save".

Now we create a couple more file types (using the same "File Formats" menu):

Step 3) Select "New". Fill in the fields as follows:

Format: PythonTeXStep2
Shortname: pytexs2
extension: tex

Leave everything else alone. Click "save".

Step 4) Select "New". Fill in the fields as follows:

Format: PythonTeXPDF
Shortname: PyTeXPDF
extension: pdf

Leave everything else alone. Click "save".

Now we need to tell LyX how we want conversions to and from these file types to work:

Step 5) (Under Tools--> Preferences --> File Handling) select "Converters".

Step 6) Change the fields as follows:

From format:  LaTeX(LuaTeX)
To format: PythonTeXStep1
Converter: pdflatex $$i

step 7) Click "Add"

Step 8) Change the fields as follows:

From format: PythonTeXStep1
To format: PythonTeXStep2
Converter: pythontex.py $$i

Step 9) Click "Add"

Step 10) Change the fields as follows:

From format: PythonTeXStep2
To format: PythonTeXPDF
Converter: pdflatex $$i

Step 11) Click "Add".

Now you're ready. Create your file, invoke the pythonTeX commands all you want. When you're ready to export from LyX, go File --> Export --> More Formats & Options, choose "PythonTeXPDF" then "OK".

To test if this is working, a barebones PythonTeX script can be made by, in a new document, hitting Control + L to open up an "ERT" box. In the ERT box, paste:

\begin{pycode}
print('llamas!')
\end{pycode}

Good point again, Scott. Steps 1-11 can be replaced by these steps:

Edit your lyx preference file (By default should be ~/.lyx/preferences) as follows:

In the format section, paste:

\format "PyTeXPDF" "pdf" "PythonTeXPDF" "" "" "" ""
\format "pytexs1" "pytexs1extension" "PythonTeXStep1" "" "" "" ""
\format "PyTeXs2" "tex" "pythonTeXs2" "" "" "" ""

and in the converter section, paste:

\converter "luatex" "pytexs1" "pdflatex $$i" ""
\converter "pytexs1" "PyTeXs2" "pythontex.py $$i" ""
\converter "PyTeXs2" "PyTeXPDF" "pdflatex $$i" ""

Tags:

Python

Lyx