Render a long code listing in multiple pages with the package minted

listingsmintedsourcecode

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

Best Answer

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'); });


            });