0

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}

enter image description here

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.

minseong
  • 487

1 Answers1

1

Sometimes, reading the manual helps. Section 4.3.8 is devoted to captions and shows

enter image description here

You can therefore set the caption position for every listing having a caption by doing

\lstset{captionpos=b}

in the document preamble.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{newtxtt}

% Default fixed font does not support bold face %\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold %\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12} % for normal

% 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=\ttb\color{deepred}, % Custom highlighting style stringstyle=\color{deepgreen}, frame=tb, % Any extra options here showstringspaces=false, }% }

\lstset{ captionpos=b, 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}[caption={Text of the caption},label=whatever] µ = 5 for f \end{python}

\end{document}

enter image description here

egreg
  • 1,121,712