防止 minted 和 captionof 之间出现分页符

防止 minted 和 captionof 之间出现分页符

我知道那里已经有很多问题涉及这个或至少类似的主题。我读过例如但无法弄清楚......

首先是我的 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}

如果你插入正确数量的行,你将得到如下所示的结果:

不需要的分页符是什么样子的

但我想要得到的是类似的东西(我只是为展示添加了一些行):

看起来应该怎么样

块的最后几行minted应该放在下一页!正如您可能猜到的那样,我不想将代码放入环境minipage或其他地方,因为它应该跨越多个页面。

我到目前为止尝试过将它放在\nopagebreak所有可能的地方。但似乎没有任何效果。

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

我也把它放在minted环境和之间\lstCapLbl。我也试着玩和widowpenaltyclubpenalty但这也没什么帮助。

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

答案1

您可以将minted环境封装到可破坏tcolorbox环境中。这允许使用tcolorbox包中的某些选项来影响破坏。

以下代码的最小效果至少是如果有的话,行将换行到下一页。由于这可能不够令人满意,我添加了一个选项

enlargepage flexible=3\baselineskip

这样可以将完整页面放大至最多 3 行以适合列表(这相当大)。但是,损坏的列表总是至少有下一页的行。您可以将其缩减为

enlargepage flexible=2\baselineskip

为了行或删除该选项,如果虚线就够了。

myminted我创建了一个包含所有这些东西的新环境。

完整代码如下:

\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}

相关内容