2

pythontex causing error with \subfigure environment.

I have to rewrite my dissertation with many figures and subfigures, of which some are plotted with python inside LaTeX (I had to give up the python package). When a subfigure is added ptyhontex complains, that

!

LaTeX Error: Environment subfigure undefined.

 (... cut ...)

l.20 \begin{subfigure}
                      {0.4\textwidth}
?

My minimal working example:

% !TEX options=--shell-escape

% Create subfigures in LaTeX
% https://latex-tutorial.com/subfigure-latex/

\RequirePackage{pythontex}  % when these
\setpythontexlistingenv{listingPtex}  % lines are commented, it compiles just fine

\documentclass[a4paper,onecolumn,oneside,12pt]{mwrep}


\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}
        
\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Firts subfigure.}
    \label{fig:first}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Second subfigure.}
    \label{fig:second}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Third subfigure.}
    \label{fig:third}
\end{subfigure}
        
\caption{Creating subfigures in \LaTeX.}
\label{fig:figures}

\end{figure}

\end{document}

I tried to compile it with the report class, instead of mwrep -- didn't help. Changing listingPtex to listing neither.

There are at least a few solution for combining python and LaTeX -- perhaps it would be worth to take another approach? Have you got any suggestions?

Or should I try different approach to subfigures? Such one, that would work with pythontex?

Thanks in advance.

1 Answer 1

1

Don't load pythontex before \documentclass. Just fix the missing compatibility bit in mwrep.cls (it doesn't define \@chapter).

\documentclass[a4paper,onecolumn,oneside,12pt]{mwrep}


\usepackage{subcaption}
\usepackage{graphicx}

\makeatletter\providecommand\@chapter{}\makeatother

\usepackage{pythontex}
\setpythontexlistingenv{listingPtex}

\begin{document}
        
\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Firts subfigure.}
    \label{fig:first}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Second subfigure.}
    \label{fig:second}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Third subfigure.}
    \label{fig:third}
\end{subfigure}
        
\caption{Creating subfigures in \LaTeX.}
\label{fig:figures}

\end{figure}

\end{document}
4
  • I've been trying to use LaTeX for some years now and, I must say, I've been struggling. Quite often I find myself in a situation when a new solution breaks what was written up to the point of its introduction. I was going in circles with figures, subfigures, captions and subcaptions, and pythontex seemed to complicate things even more. So without much enthusiasm I tried to compile your example. I was simply shocked when it worked! Even more when I tried referencing figures and subfigures with \ref and
    – khaz
    Commented Mar 29, 2023 at 11:51
  • @khaz Do you really need mwrep?
    – egreg
    Commented Mar 29, 2023 at 11:53
  • 3) inserted some python snippets. Grazie mille @egreg.
    – khaz
    Commented Mar 29, 2023 at 12:04
  • I really don't know if I need it -- I have to contact the printing house -- you don't print dissertations by yourself, at my university (probably at no university). Is report much better? Less conflicts, I assume?
    – khaz
    Commented Mar 29, 2023 at 12:06

You must log in to answer this question.

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