# Using pythontex \py with fancyhdr

I don't know what I'm doing wrong, but this MWE does not work for me:

\documentclass[a4paper]{article}

\usepackage{pythontex}
\usepackage{lipsum}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyfoot[C]{\py{1+1}}

\begin{document}
\lipsum[1]
\end{document}


Running this (pdflatex test.tex; pythontex3 test) results in this error message:

\$ pythontex3 test
This is PythonTeX 0.14
* PythonTeX warning
Unknown option ""
Traceback (most recent call last):
File "/home/carsten/texmf/scripts/pythontex/pythontex3.py", line 2699, in <module>
main(python=sys.version_info.major)
File "/home/carsten/texmf/scripts/pythontex/pythontex3.py", line 2622, in main
load_code_get_settings(data, temp_data)
File "/home/carsten/texmf/scripts/pythontex/pythontex3.py", line 475, in load_code_get_settings
key, val = line.split('=', 1)
ValueError: need more than 1 value to unpack


Can \py content not be evaluated in the preamble?

## 1 Answer

\py doesn't work in the preamble, but that isn't the problem here, because what is given to \fancyfoot isn't actually used in the preamble. (For future reference, \pyc and pycode do currently work in the preamble for bringing in Python output.)

This is due to a bug in PythonTeX. Every \AtEndDocument in pythontex.sty should actually be \AfterEndDocument. Currently, fancyhdr is inserting the footer after PythonTeX has already finalized its temp file of code, so the footer code is inserted in the temp file in an unexpected location and PythonTeX doesn't know how to parse it.

I've opened an issue on GitHub and will fix this for the next release.

• I figure I'll do some manual search and replace for the time being then. I installed the current version in ~/texmf, so that's not a big problem. The package that comes with Ubuntu 14.04 still contains 0.12 by default.
– Jipí
Feb 27 '15 at 22:50
• I tried a bit around with search-and-replace. If you replace all instances of \AtEndDocument with \AfterEndDocument, the script indeed worked, but there's something going wrong with Pygments then and the respective lines aren't printed. pygments=false makes them reappear, except – of course – without code highlighting.
– Jipí
Feb 27 '15 at 23:42
• @Jipí Switching to \AfterEndDocument interferes with Pygments-related code that already depends on \AfterEndDocument. Getting the ordering fixed may be a little tricky. In the meantime, you might try \clearpage at the very end of the document with default pythontex.sty, since that should finalize the last page before PythonTeX finalizes its temp file. Feb 28 '15 at 4:10