# mylatexformat and pythontex

I use mylatexformat to easily build format files. Consider the following file:

\documentclass{article}
% This is file t.tex
\usepackage{pythontex}
\begin{document}
Test
\end{document}


Then, I run pdflatex -ini -jobname="myfmt" "&pdflatex" mylatexformat.ltx """t.tex""" to get myfmt.fmt.

Now, I have another file:

%&myfmt
% This is s.tex
\begin{document}
\begin{pygments}{python}
def foo():
return 'bar'
\end{pygments}
\end{document}


When I compile it, the format myfmt.fmt is loaded and it comes with pythontex preloaded. A pdf file is produced: compilation is fine. Then, I run pythontex s.tex: no warnings. Then, I compile once again s.tex. Unfortunately, the python code does not show up in the pdf file. All I get is ?? PythonTeX ??.

The log tells me that

Package PythonTeX Warning: Non-existent Pygments content on input line 8.


and also

No file pythontex-files-myfmt/myfmt.pytxmcr


So I guess there me be some "hard coded" filenames and directory names defined when the format is created.

Is there a workaround so that I could use mylatexformat with pythontex the way I showed here?

• pythontex uses \jobname to create a directory name for saving output, among other things. You are probably running into the issues with precompiling discussed in this answer. When I use pythontex with a precompiled preamble, I generally load it separately, after the precompiled material. I don't know if that's possible with mylatexformat. – G. Poore Nov 25 '15 at 1:13
• @G.Poore Thanks for your comments. Would it be possible to define those filenames based on \jobname with some etoolbox hooks (\AtEndPreamble or \AfterEndPreamble). I could probably hack something if I knew where to look. The reason why I'd like to avoid usepackaging pythontex after precompiled material is that I'd like to avoid my users the pain to do it. – cjorssen Nov 25 '15 at 12:07
• It's possible to use \setpythontexoutputdir{<dirname>} to set the directory manually. The bigger problem is that the temp files using \jobname are opened immediately when the package is loaded, and that breaks with precompiling. I suppose it might be possible to reopen them at the end of the preamble, to try to correct for the file issues caused by precompiling. The *.pytxcode file would be the one to worry about (unless you're using depythontex). – G. Poore Nov 25 '15 at 12:56
• @G.Poore Would it be possible to defer the opening after the preamble (with the hooks of etoolbox)? – cjorssen Nov 25 '15 at 20:54

I found a workaround with etoolbox \AtEndPreamble (for some reason, a simple \AtBeginDocument does not work, producing a file named @codefile=s.pytxcode).

\documentclass{article}
% This is file t.tex
\usepackage{etoolbox}
\AtEndPreamble{\usepackage{pythontex}}
\begin{document}
Test
\end{document}


It is only a workaround as pythontex is not precompiled.

• I have some ideas about how to support proper precompiling, but it will likely be more complicated than a simple etoolbox patch. I've opened an issue at GitHub. I will be spending much of December and January working on the next version of PythonTeX, and should be able to have a fix in the next release. – G. Poore Nov 26 '15 at 14:38