1

I'm using the package minted for formatting code in my paper. The package minted provides a "listing" environment, which treats the code inside it as a float. It works very well under most circumstances. But, how to embed a long listing of code which can span two or more pages using minted? Its listing environment does not work well for this situation.

I'd also like to use \caption and \label below the minted code listing. And I'd like to index the listings in my ToC.

Any help will be highly appreciated!

-Nicole

2
  • 1
    Search this site for the caption package and the captionof macro.
    – TeXnician
    Apr 26, 2022 at 6:10
  • 1
    tcolorbox package has a listings module which can use breakable boxes across pages, using minted as the listing formatter.
    – Cicada
    Apr 27, 2022 at 11:35

2 Answers 2

2

As example for the first part, a breakable tcb listing box using minted (tcb code somewhat adapted from another answer on this site about quoting; javascript code is from this page):

example tcb

There are three or four ways of dealing with the perennial problem of wrapping long lines: here, some (manual) line-wrapping and indentation removal has been done to the first code block, as illustration.

MWE

\documentclass[
    12pt,     
    openright,
    twoside,  
    a4paper,  
    english,  
    draft
]{memoir}
\usepackage{microtype}
\usepackage{minted}
%\usepackage{geometry}
\usepackage{tcolorbox}

\usepackage{tikz}
\usepackage{tkz-euclide}
\tcbuselibrary{minted,skins,breakable,xparse}
\usetikzlibrary{positioning}

\definecolor{corAzulTema}{RGB}{0,66,137}
\definecolor{corFundoCaixas}{RGB}{245,245,245}

\tcbset{
    listing engine=minted,
    minted options={fontsize=\small,linenos,numbersep=3mm,breaklines,escapeinside=@@},
    colback=corFundoCaixas,
    colframe=corAzulTema!40,
    fonttitle=\bfseries,
    listing only,
    left=7mm,
    enhanced,
    breakable,
    drop fuzzy shadow,
    before skip=\baselineskip,
    grow to left by=13mm,grow to right by=15mm,
    overlay={\begin{tcbclipinterior}\fill[corAzulTema!40](frame.south west)rectangle([xshift=7mm]frame.north west);\end{tcbclipinterior}}
}

\newtcbinputlisting{\javaScriptCode}[3][]{%
    listing file={#3},
    title=\texttt{#2},
    colframe=corAzulTema,
    minted language=javascript
}

\DeclareTotalTCBox{\inlineJavaScriptCode}{ s v }{
    reset,
    listing only,
    on line,
    enhanced,
    nobeforeafter,
    tcbox raise base,
    boxrule=0.7pt,
    top=1mm,
    bottom=0mm,
    right=1mm,
    left=1mm,
    boxsep=0.5pt,
    before upper={\vphantom{dlg}},
    colback=corFundoCaixas,
    colframe=corAzulTema,
    drop fuzzy shadow,
        minted options={escapeinside=@@},
%       text=\ttfamily,
    arc=3pt}
{\mintinline{javascript}{#2}}%



\begin{document}

\begin{enumerate}
    \item apostrophe/single quotes: \inlineJavaScriptCode{'string'}
    \item double quotes:  \inlineJavaScriptCode{"string"}
    \item grave: \inlineJavaScriptCode{`string`}
    \item line \inlineJavaScriptCode{let s1 = 'string';    // apostrophe/single quotes}
\end{enumerate}

\javaScriptCode{title}{strings2.js}

\end{document}

js file:

 StackExchange.ready(function () {

      StackExchange.using("postValidation", function () {
        StackExchange.postValidation.initOnBlurAndSubmit( $('#post-form'), 2, 'answer');
                });


      StackExchange.question.init({canOpenBounty:true,votesCast:[], canViewVoteCounts:true, totalCommentCount:2, shownCommentCount:2, enableTables:true, questionId:642048});

       styleCode();

          StackExchange.realtime.subscribeToQuestion('85', '642048');
          StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

2

Thanks to comment from @TeXnician , I have solved this problem with the package caption.

In preamble:

\usepackage[chapter, newfloat]{minted} % Code listings, with syntax highlighting
\usepackage[font=small]{caption}

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

%   Code list in content index
\renewcommand{\listoflistings}{
  \cleardoublepage
  \addcontentsline{toc}{chapter}{List of Code}
  \listof{listing}{List of Code}
}

\setminted{
    frame=lines,
    framesep=2mm,
    bgcolor=Snow1,
    fontsize=\small,
    linenos,
    breaklines
}

In chapter or section:

\begin{code}
    \begin{minted}{yaml}
# long long YAML code
# long long YAML code
...

    \end{minted}
    \captionof{listing}{A very long YAML snippet}
    \label{lst:longYamlSnippet}
\end{code}

You must log in to answer this question.

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