14

I'm using the minted package. How can I get the caption to show before the code in the document?

\documentclass[a4paper]{scrartcl}
\usepackage{minted}
\begin{document}
    \begin{listing}[H]
        \caption{This is below the code.}
        \inputminted{matlab}{myfile.m}
        \label{lst:the-code}
    \end{listing}
\end{document}
13

The floatrow package will do the trick, but it must be loaded before minted to prevent it from loading the float package.

\documentclass[a4paper]{scrartcl}    
\usepackage{floatrow}
\usepackage{minted}    
\floatsetup[listing]{style=Plaintop}    
\begin{document}    
    \begin{listing}[H]    
        \caption{This is above the code.}    
        \inputminted{matlab}{myfile.m}    
        \label{lst:the-code}    
    \end{listing}    
\end{document}

enter image description here

  • Your code works fine, but the output you attached does not match it. – Anthony Labarre Aug 24 '16 at 12:17
  • Impressive that apparently no one (including myself) noticed this for more than four years. That's what happens when answering questions while I should be asleep. – Jonas Nyrup Aug 24 '16 at 17:55
5

The in January released 2.0 version of minted allows you to use the newfloat-package instead of the (rather old) float-package. If you want to use newfloat, the method for positioning the caption above the source is not the same as Jonas's but rather the following.

\documentclass[a4paper]{scrartcl}

\usepackage{caption}
\usepackage[newfloat]{minted}
\captionsetup[listing]{position=top}

\begin{document}

\begin{listing}[H]
    \caption{This is above the code.}
    \inputminted{matlab}{myfile.m}
    \label{lst:the-code}
\end{listing}

\end{document}

Minted-listing using newfloat


Important: you have to place the \caption-command above your source-code within the listing-float. The \captionsetup only applies correct spacing but you have to place the command at the right position yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.