# How to let AUCTeX launch automatically pythontex.py script

I need to use the pythontex package that I find very useful if someone has to do some calculation before inserting the results in LaTeX. So Here is an example:

\documentclass{article}
\usepackage{pythontex}
\begin{document}
If we add five to eight we get \py{5+8}.
\end{document}


I am used to typeset my LaTeX documents in Emacs with AUCTeX. To compile the document I generally use the the keys C-c C-a which are bound to the AUCTeX command TeX-command-run-all described in the AUCTeX manual as:

Compile the current document until an error occurs or it is finished. If compilation finishes successfully, run the viewer at the end.

But with the example provided above I get this result:

As we can see the result of the python calculation has been replaced by ?? . This is because as described in the pythontex package manual :

Compiling a document with pythontex involves three steps: 1) running a LaTeX-compatible tex engine; 2) running pythontex.py script; 3) and finally running the TeX engine again.

The second step is not automatically performed by the TeX-command-run-all command so I need to leave momentarily Emacs, open a terminal (I am on GNU/Linux) and type pythontex <myfile.tex>, and return to Emacs to run again TeX-command-run-all. As we can easily imagine it is somewhat painful to do this seven times.

The question: How to configure AUCTeX in a way that when I call TeX-command-run-all the pythontex.py script is automatically launched so there is no need any more to use a terminal and execute the script manually?

I suggest you don't hack on AUCTeX's commands but define your own one. You can define a series of shell commands to run via TeX-command-sequence.

(TeX-command-sequence COMMAND &optional RESET FILE-FN)

Run a sequence of TeX commands defined by COMMAND.

The COMMAND argument may be

• nil: no command will be run in this case

• a string with a command from TeX-command-list

• a non-nil list of strings, which are commands from TeX-command-list; the car of the list is used as command to be executed in the first run of TeX-command-sequence, the cdr of the list will be passed to the function in the next run, etc.

• a function name, returning a string which is command from TeX-command-list; it will be funcall’d (without arguments!) and used again in the next run of TeX-command-sequence.

• with any other value the function TeX-command-default is used to determine the command to run, until a stopping condition is met.

[...] A non-nil value for the optional argument RESET means this is the first run of the function and some variables need to be reset.

So the basic strategy is: Add an entry for PythonTeX to TeX-command-list, define a private function which saves your document and runs TeX-command-sequence appropriately, bind this private function to a key in LaTeX mode and then run it.

(with-eval-after-load "tex"
'("PythonTeX" "pythontex %s" TeX-run-command nil (latex-mode)
:help "Run PythonTeX script")
t))

(defun my/TeX-run-TeX-pythontex ()
"Save current master file, run LaTeX, PythonTeX and start the viewer."
(interactive)
(unless (featurep 'tex-buf)
(require 'tex-buf))
(TeX-save-document (TeX-master-file))
(TeX-command-sequence '("LaTeX" "PythonTeX" "LaTeX" "View")
t))


I think that latexmk is designed for this kind of situation. It's included in some TeX distributions, so you may have it already. Once it's configured properly, you should just have to run it once, and it will automatically detect whether Python needs to be run, run it, and then rerun LaTeX if needed. Anyway, once you have latexmk, in Emacs, run M-x customize-group AUCTeX. Then choose TeX Command, and within that menu, customize TeX-command-list: add a line
("latexmk" "latexmk %t" TeX-run-command nil t)

Now you should have an option to run latexmk on your LaTeX files, so you need to configure latexmk to check whether pythontex requires running Python. (I also add (setq TeX-command-default "latexmk") to an appropriate hook so this is evaluated whenever I edit a .tex file.) The author of latexmk has provided a sample configuration file for use with pythontex. You can also do an internet search for "latexmk pythontex". (I don't use pythontex, so I can't vouch for this. I use sagetex, and latexmk works beautifully with that, so I would expect it to work equally well with pythontex.)