8

I know there are already many questions there covering that or at least similar topics. I've read this for example but can't get it figured out...

At first here comes my MWE:

\documentclass{scrartcl}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}% not needed, but not sure if it has any effect

\newcommand{\lstCapLbl}[2]{%
    \vspace{-0.9\baselineskip}
    \captionof{listing}{#1\label{lst:#2}}
    \vspace{0.9\baselineskip}
}

\begin{document}
There is some random text here. And some some some some some some some
some some some some some more to get a line full.

\begin{minted}[gobble=1,linenos,tabsize=4,xleftmargin=0.7cm]{js}
    function myFunc(){
        someCode();
        // make it as long as needed to get the page full
        // in my case 35 more lines were needed
        // but they were omitted here for the sake of brevity
    }
\end{minted}
\lstCapLbl{some caption text}{andTheLabel}

And again some random Text just for fun.

\end{document}

What you get if you insert the right number of rows looks like that:

what an unwanted pagebreak looks like

But what I'd like to get is something like that (I just added some more lines for the showcase):

how it should look

The last lines of the minted block should go on the next page! As you probably guess, I don't want to put the code into a minipage environment or something since it should span over multiple pages.

What I tried so far is putting \nopagebreak at every possible place. It doesn't seem to have any effect.

\newcommand{\lstCapLbl}[2]{%
  \nopagebreak
  \vspace{-0.9\baselineskip}
  \nopagebreak
  \captionof{listing}{#1\label{lst:#2}}
  \vspace{0.9\baselineskip}
}

I also put it between the minted environment and the \lstCapLbl. I also tried to play with widowpenalty and clubpenalty but that didn't help either.

\end{minted}
\widowpenalty=10000
\clubpenalty=10000
\lstCapLbl{some caption text}{andTheLabel}
caligula
  • 563

1 Answers1

2

You can encapsulate the minted environment into a breakable tcolorbox environment. This allows to use some options from the tcolorbox package to influence the breaking.

The minimal effect of the following code is that at least one line will be broken to the next page, if any. Since that may be not pleasing enough, I encluded an option

enlargepage flexible=3\baselineskip

This allows the unbroken page to be enlarged up to 3 lines to fit the listing (that is rather large). But then, a broken listing will always have at least three lines on the following page. You can reduce that to

enlargepage flexible=2\baselineskip

for two lines or delete that option, if one broken line is enough.

I made a new environment myminted which contains all that stuff.

The full code is:

\documentclass{scrartcl}
\usepackage[minted,breakable,skins]{tcolorbox}
\usepackage[skip=0pt]{caption}

\newtcblisting{myminted}[4][]{
  blanker,bottom=0.9\baselineskip,
  breakable,listing only,
  before skip=0.9\baselineskip,
  after skip=0.9\baselineskip,,
  minted options={#1},
  minted language={#2},
  attach boxed title to bottom center,
  minipage boxed title,
  boxed title style={blanker},
  title={{\captionof{listing}{#3\label{lst:#4}}}},
  enlargepage flexible=3\baselineskip% <--- may be reduced to 2\baselineskip or be deleted
}

\begin{document}
There is some random text here. And some some some some some some some
some some some some some more to get a line full.

\begin{myminted}[gobble=1,linenos,tabsize=4,xleftmargin=0.7cm]{js}
  {some caption text}{andTheLabel}
    function myFunc(){
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        someCode();
        // make it as long as needed to get the page full
        // in my case 35 more lines were needed
        // but they were omitted here for the sake of brevity
    }
\end{myminted}


And again some random Text just for fun.

\end{document}
  • Tried it, needed some time to update packages, but it works great, even over several pages!

    Since I was on holiday in the final time of the bounty, I unfortunately couldn't award you the bounty. But I read that you get the bounty afterwards if you give the highest voted answer. I could even start a new bounty and award it, I don't know...

    – caligula Aug 04 '15 at 08:17
  • @caligula That automatism did'nt work here, because there was only one click given to my answer at that time. But I'm glad to hear that you appreciate my answer :-) – Thomas F. Sturm Aug 04 '15 at 12:32