2

The question: possibility of using of working python scripts embedded in LaTeX.

When I was using \usepackage{python}, it did wonders to my dissertation (although I am very new to LaTeX, but some thousands of lines of code have been written in python). Python was drawing plots and putting them on hard drive, then LaTeX was putting them inside the document. After serious problem with the system (linux Mint), I was forced to reinstall, and python + LaTeX stopped working. I was looking for a solution and pythontex was promising. It turned out, eventually, there is some serious conflict between mwrep and pythontex.

Is it possible to use pythontex with mwrep? (mwrep is a must [EDIT: On second thought I don't know if it really is required -- I thought that the printing house wants the document of this class -- to tell the truth I do not know the difference. I'll ask about it the editor.])

Or is there a better possibility of emmbeding python scripts inside LaTeX (i.e. working scripts)?

P.S.

I was looking for an answer, but I found no definitive solution.

UPDATE

Minimal working example.

.TEX file:

% !TEX options=--shell-escape

\documentclass[11pt]{mwrep}%

\usepackage{pythontex}

\usepackage{graphicx}

\begin{document} \section{pythontex try}

% \begin{pyconsole} % x = 987.27 % x = x**2 % print(f"{x = }") % print("Hello Seba :)") % \end{pyconsole}

\end{document}

Sublime Text message:

This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2022/dev/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./mwrepPythontryMini.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-01-21>
(/usr/share/texlive/texmf-dist/tex/latex/mwcls/mwrep.cls
Document Class: mwrep 2017/05/13 v0.75 A LaTeX document class (MW)
*** Beta version. Formatting may change
*** in future versions of this class.
(/usr/share/texlive/texmf-dist/tex/latex/mwcls/mw11.clo))

(... cut ...)

(./mwrepPythontryMini.aux) No file pythontex-files-mwrepPythontryMini/mwrepPythontryMini.pytxmcr. Run PythonTeX to create it. ! Undefined control sequence. \newfloat@setwithin ...apter}@chapterlistsgap@on {#1}\newfloat@@setwithin {... l.9 \begin{document}

?

*.sublime-build:

{
    "shell_cmd": "pdflatex $file_name && pythontex $file_name --interpreter python:python3 && pdflatex $file_name && xreader $file_base_name.pdf "
}
khaz
  • 155

2 Answers2

4

Looks like some weird interfacing between newfloat and mwrep. This is my understanding:

  • Unlike article documentclass, mwrep documentclass does not define \@chapter.

  • This breaks the \DeclareFloatingEnvironment command for some reason.

  • pythontex package defines a listing environment \AtBeginDocument if it's not defined before:

    \AtBeginDocument{
        \ifcsname listing\endcsname
            \ifbool{pytx@listingenv}{}%
                {\PackageWarning{\pytx@packagename}%
                    {A "listing" environment already exists \MessageBreak
                     \pytx@packagename\space will not create one \MessageBreak
                     Use \string\setpythontexlistingenv\space to create a custom listing environment}}%
        \else
            \ifbool{pytx@listingenv}{}{\DeclareFloatingEnvironment[fileext=lopytx]{listing}}
        \fi
    }
    

You can also reproduce the issue without newfloat:

\documentclass{mwrep}
\usepackage{newfloat}
\DeclareFloatingEnvironment{diagram}
\begin{document}
Text
\end{document}

So, apparently a workaround (at least in the current version) is to force pythontex to declare that floating environment very early on:

\RequirePackage{pythontex}
\setpythontexlistingenv{listing}

\documentclass[11pt]{mwrep}%

\begin{document}

\section{pythontex try}

\begin{pyconsole} x = 987.27 x = x**2 print(f"{x = }") print("Hello Seba :)") \end{pyconsole}

\end{document}

Another workaround is to add

\csname @chapter\endcsname

to the very start of the document.

Looks like it was caused by some other unrelated change: https://gitlab.com/axelsommerfeldt/newfloat/-/issues/13

user202729
  • 7,143
  • By the way someone should probably report the issue to the developer https://gitlab.com/axelsommerfeldt/newfloat/-/issues ... – user202729 Mar 27 '23 at 12:02
  • Thank you very much for help, @user202729, I checked also the response by egreg -- both worked for me. I opted for the second reply, the one without the workaround. Now I am struggling with the list of listings -- perhaps you know how to do that with pythontex? – khaz Mar 27 '23 at 20:20
  • 1
    @khaz That's a different question, you should probably ask a new question. – user202729 Mar 28 '23 at 04:32
3

Just add a dummy definition of \@chapter, because newfloat uses this macro to distinguish between classes that support chapters and those that don't.

\documentclass[11pt]{mwrep}

\makeatletter \providecommand@chapter{} \makeatother

\usepackage{pythontex}

\usepackage{graphicx}

\begin{document} \section{pythontex try}

\begin{pyconsole} x = 987.27 x = x**2 print(f"{x = }") print("Hello Seba :)") \end{pyconsole}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks for your replay, @egreg -- I checked it, and it worked. Perhaps you know how to make a list of listings with pythontex? – khaz Mar 27 '23 at 20:24