21

I want to add a caption and label on a Minted source code so I can reference it, similarly to what you can do to figures.

In Minted manual it says I can use the listing environment, which allows to use both caption and label, the problem is that listings creates a float and only allows to use one page. My source code is long and spans on two pages.

How can I add caption and label but still be able to use two pages?

3
  • 2
    A solution is described in minted docs. 8 FAQ and Troubleshooting, I need a listing environment that supports page breaks. It has a link to this answer. Commented Jul 7, 2015 at 7:44
  • Thank you @ArashEsbati, it works now but the caption shows like this: Listing 1: MY_LABEL. Is there a way to change from Listing 1 to Source Code 1? Commented Jul 7, 2015 at 15:16
  • See my answer below. Commented Jul 7, 2015 at 18:20

2 Answers 2

34

You can define a non-floating environment which can break over pages and put the minted environment in that one. This is a documented feature:

8 FAQ and Troubleshooting

  • I need a listing environment that supports page breaks. The built-in listing environment is a standard float; it doesn’t support page breaks. You will probably want to define a new environment for long floats. [...]
    With the caption package, it is best to use minted’s newfloat package option. See
    https://tex.stackexchange.com/a/53540/10742

Loading minted.sty with newfloat option enables you to customize the listing environment, caption.sty offers \captionof for captions outside floating environments. Check their docs for further (plenty of) options.

Putting it together:

\documentclass{article}
\usepackage[newfloat]{minted}
\usepackage{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Source Code}

\begin{document}
\begin{code}
\captionof{listing}{My C-Code}
\label{code:c-code}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{code}

Reference to \ref{code:c-code}.  

\end{document}

enter image description here

6
  • 1
    Why do you use captionof instead of caption?
    – skan
    Commented Sep 10, 2019 at 23:52
  • 1
    @skan - For 2 reasons: 1) I use \caption in floating env's, this is a matter of taste. 2) My editor understands the first floating type argument in \captionof and includes the right prefix in the \label argument, e.g. \label{lst:foo} (not shown in the code above, though). Commented Sep 11, 2019 at 19:09
  • How to add location parameters like [t] to the code environment? so that it places that on the top of the page Commented Dec 17, 2021 at 17:55
  • @KennetCeleste - code is a non-floating environment, so you can have a listing with a page break. This is what the question was about. If you want a floating environment, read the minted manual, section 4 Floating listings. Commented Dec 18, 2021 at 11:14
  • @ArashEsbati I basically want to have something like "subfigure" for minted. 3 minted subfigures in one bigger listing. Can you help me with that? I can create a question if you can help with that. Commented Dec 18, 2021 at 16:00
1

Use figure or table block to add the source. I think it is a simple solution.

\begin{figure}[!ht]
\centering
\begin{minted}[mathescape, breaklines,frame=single, fontsize=\footnotesize]{python}
import tensorflow as tf

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(100, input_shape=(12,), activation='relu'))
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
\end{minted}
\caption{Classification model and layers}\label{code:CML}
\end{figure}

You must log in to answer this question.

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