I am using this answer to add syntax-highlighted Python snippets to my document
\documentclass[a4paper,12pt]{article} \usepackage[utf8]{inputenc} \usepackage{newtxtt}% Custom colors \usepackage{color} \definecolor{deepblue}{rgb}{0,0,0.5} \definecolor{deepred}{rgb}{0.6,0,0} \definecolor{deepgreen}{rgb}{0,0.5,0}
\usepackage{listings}
% Python style for highlighting \newcommand\pythonstyle{% \lstset{ language=Python, basicstyle=\ttfamily, morekeywords={self}, % Add keywords here keywordstyle=\bfseries\color{deepblue}, emph={MyClass,init}, % Custom highlighting emphstyle=\bfseries\color{deepred}, % Custom highlighting style stringstyle=\color{deepgreen}, frame=tb, % Any extra options here showstringspaces=false, }% }
\lstset{literate={µ}{\textmu}{1}}
% Python environment \lstnewenvironment{python}[1][] {\pythonstyle\lstset{#1}} {}
% Python for external files \newcommand\pythonexternal[2][]{{% \pythonstyle \lstinputlisting[#1]{#2}% }}
% Python for inline \newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}
\begin{document} \begin{python} µ = 5 for f \end{python} \end{document}
I would like to caption it, similarly to how tables or figures can be captioned.
I've tried
\begin{python}[label=code1]
do_some_code = []
\end{python}
\captionof{lstlisting}{This is how some code was done}
it looks good, but gives the warning "\captionof outside box or environment" so I am wary of it being unreliable or breaking as the document gets more complex.
I have also tried as suggested
\begin{python}[label=code1,caption={This is how some code was done}]
do_some_code = []
\end{python}
but this places the caption above the code which is undesirable.
I would like the caption to be placed underneath the code.


