2

I would like to introduce some python using pythontex in some of my chapters. I'm using the package subfiles to have a master documents and some childs and it's very convenient for me, I would like to keep that.

Here is my minimal (not) working example, with 2 files. MWE-1.tex is:

\documentclass[a4paper,10pt]{article}
\usepackage{subfiles}
\usepackage{fontspec}% Pour lua, gestion de l'encodage et des fontes
\usepackage{pythontex}

\begin{document}
This is a code:
\begin{pyverbatim}
print('spam and eggs')
\end{pyverbatim}

 just printing \pyc{print('spam and eggs')}
\end{document}

This files compiles and gives the expected result after lualatex/pythontex/lualatex execution: the verbatim code appears colorfull, and \pyc{print('spam and eggs')} is executed.

The second files is the child files, and should gives the same results:

\documentclass[./MWE-1.tex]{subfiles}


\begin{document}
This is a code:
\begin{pyverbatim}
print('spam and eggs')
\end{pyverbatim}

 just printing \pyc{print('spam and eggs')}

\end{document}

This files returns a warning : Package PythonTeX Warning: Non-existent Pygments content on input line 8.

\pyc{} is well executed, as well as more complex code, but the verbatim code is replaced by a huge and bold "?? PythonTeX ??"

If I manually replace the preambule by the one of MWE-1.tex, the verbatim part is well displayed after just one lualatex compilation: the Pygments content is there but not found when using the \documentclass{subfiles} preambule.

As far as I know, the subfiles documentclass just takes all the preambule of the master file (here MWE-1.tex), so that both files should render the same...

EDIT and reply: It works better with the standalone package. A bit more complicated in my case to setup, but it's working.

My main file, adding an import of the child file:

\documentclass[10pt,a4paper]{article}
\usepackage[subpreambles=true]{standalone}
\usepackage{fontspec}% Pour lua, gestion de l'encodage et des fontes
\usepackage{pythontex}
\begin{document}
This is a code:
\begin{pyverbatim}
print('spam and eggs')
\end{pyverbatim}

 just printing \pyc{print('spam and eggs')}

 And with import:

 \input{MWE-2.tex}
\end{document}

and the child file, containing pythontex code either:

\documentclass[class=article, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{fontspec}% Pour lua, gestion de l'encodage et des fontes
\usepackage{pythontex}
\begin{document}
This is a code:
\begin{pyverbatim}
print('spam and eggs')
\end{pyverbatim}

 just printing \pyc{print('spam and eggs')}

\end{document}

1 Answer 1

3

You have to load the subfiles package last in the preamble (as the documentation says), i.e., in file MWE-1.tex replace

\usepackage{subfiles}
\usepackage{fontspec}% Pour lua, gestion de l'encodage et des fontes
\usepackage{pythontex}

by

\usepackage{fontspec}% Pour lua, gestion de l'encodage et des fontes
\usepackage{pythontex}
\usepackage{subfiles}

The reason is that pythontex apparently fiddles with the \begin{document} command, and the subfiles package needs to see the final version of \begin{document}, including the changes by pythontex.

2
  • I wish you a pleasant welcome back to TeX.SE.
    – Sebastiano
    Oct 2, 2019 at 21:17
  • @Sebastiano Thanks!
    – gernot
    Oct 3, 2019 at 9:50

You must log in to answer this question.

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