12

I've recently learned about PythonTeX (Conference presentation). It's a tool that lets you execute python code in your LaTeX documents. This is useful for, e.g., creating matplotlib figures inside your TeX document.

According to the manual (available at the Github link above), to compile documents, one needs to run latex, run an included python script, then run latex again. Can I set up LyX to execute this all for me?

4
  • I had the same issue in the past and i think at that time it was impossible to integrate these tools. Hopefully this has changed and someone will point it out.
    – juliohm
    May 14, 2013 at 21:59
  • 1
    This might help a bit: LyX has a module called "Rnw (knitr)" that allows to input code for "R" into LyX / LaTeX via the package knitr. Maybe one can re-use that...
    – e-birk
    May 14, 2013 at 23:19
  • 1
    The three-step compile process is very similar to that of SageTeX, so you might use the LyX SageTeX module as a starting point. I imagine it might require only a few modifications.
    – G. Poore
    May 15, 2013 at 0:09
  • Thanks for the tip, G. I figured this out by combing through the SageTeX preferences script.
    – BenB
    May 16, 2013 at 23:58

4 Answers 4

7

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" ""
2
  • 1
    Nice detailed solution, Ben. Note that your new converters are stored in your user preferences file. On Ubuntu, it's ~/.lyx/preferences. This way you don't have to manually add them each time you reinstall LyX or change computers. Also note that e-birk's idea (in the comment to your question) of using knitr is a good one. knitr does support Python as a backend, I believe.
    – scottkosty
    May 17, 2013 at 0:18
  • @BenB - I cant get this to work, it says "An error occured while running: python pythontex.py test.pytexs1ext" which makes sense because there is no file test with extension pytexs1ext only test.tex. Also the Message pane in Lyx shows an 'ImportError: No module name multiprocessing' but when run through the command prompt it runs fine. Any idea what is going on?
    – nluigi
    Sep 15, 2015 at 22:03
1

Just change the converter or add a new one. Go to Tools > Preferences > File Handling > Converters. Select the converter you want to change, for example LaTeX (pdflatex) -> PDF (pdflatex). Then just edit the field converter.

For more information, see Help > Customization > 3.3 Converters.

See the following screenshot: enter image description here

1

I followed BenB's solution but encountered two problems:

  • Lyx would not produce any useful error messages if one of the conversion steps failed.
  • Lyx would not run bibtex anymore.

Therefore, I created a simple wrapper in bash-script which calls pdflatex, pythontex and pdflatex again:

#!/bin/bash
pdflatex $@ && pythontex $1 && pdflatex $@

Then I pointed the PDF converter in lyx to this bash-file instead of pdflatex.

This at least solved the problem of lyx not calling bibtex any more. I ended up creating a more advanced bash-script with better error reporting, running pythontex only if needed, and possible requests from inside the python-code to run pythontex twice, see this github project:

https://github.com/mghansen256/pythontex-wrapper

Since it is written in bash-script, it is not cross-platform yet, but there is no reason it could not be ported to Python instead.

-1

Have the same problem on LyX 2.2 and ubuntu 14.04. Solve by putting this converter:

\converter "pytexs1" "PyTeXs2" "pythontex $$i" ""

instead of

\converter "pytexs1" "PyTeXs2" "pythontex.py $$i" ""
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .