列表环境中的分页符上的水平虚线

列表环境中的分页符上的水平虚线

好吧,标题说明了一切。我有跨多个页面的代码脚本。是否可以在环境listings遇到分页符的页面底部和/或顶部显示虚线,并在环境的最顶部和最底部保留实线?

下面是一个屏幕截图,说明了我的问题。我想在listings环境中断的地方添加虚线。

在此处输入图片描述

mcode与环境结合使用listings,提供一组lstset参数。以下 MWE 说明了我希望使用水平虚线的位置,避免环境lstlistings突然变为白色。

\documentclass{article}
\usepackage{mcode, listings}
\definecolor{cornsilk}{RGB}{255,248,220}
\begin{document}
\section{Section Something}

The following code is just a really long list of comments, really. 

\begin{lstlisting}
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % It would be nice with a horisontal dashed line below me. 
    % Another dashed above me, too.
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
\end{lstlisting}
\end{document}

答案1

要绘制这样的框架,您应该使用允许进行此类修改的其他包。默认包是mdframed或者tcolorbox。在下面的示例中,我提供了一个解决方案,通过tcolorbox该解决方案可以定义新的 listings 环境lstmcode。请注意,在下面的示例中,我使用的是 2.22 版本。

在此处输入图片描述 在此处输入图片描述

\documentclass{article}
\usepackage{mcode, listings}
\lstdefinestyle{mymcode}{%
  basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize,
  numbers=left,
  numberstyle=\color{gray}\fontfamily{pcr}\fontseries{m}\selectfont\tiny,
  stepnumber=1,
  numbersep=5pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  tabsize=2,
  breaklines=true,
  breakatwhitespace=false,
  belowskip=0pt,
 aboveskip=0pt,
}

\definecolor{cornsilk}{RGB}{255,248,220}

\usepackage[breakable,listings,skins,hooks]{tcolorbox}
\newtcblisting{lstmcode}{%
 breakable,
 colback=cornsilk,
 listing options={style=mymcode},
 listing only,
 enhanced,
 overlay first app={%
   \draw[dashed,line width=1pt,xshift=1pt] (frame.south east) -- (frame.south west);%
  },
 overlay middle app={%
   \draw[dashed,line width=1pt,xshift=-1pt] (frame.north east) -- (frame.north west);
   \draw[dashed,line width=1pt,xshift=1pt] (frame.south east) -- (frame.south west);%
  },
 overlay last app={%
   \draw[dashed,line width=1pt,xshift=-1pt] (frame.north east) -- (frame.north west);
  },
}
\begin{document}
\section{Section Something}

The following code is just a really long list of comments, really. 

\begin{lstmcode}
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % It would be nice with a horisontal dashed line below me. 
    % Another dashed above me, too.
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
\end{lstmcode}

\end{document}

这是使用 的另一种方法mdframed。输出相等:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{mcode, listings}
\lstdefinestyle{mymcode}{%
  basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize,
  numbers=left,
  numberstyle=\color{gray}\fontfamily{pcr}\fontseries{m}\selectfont\tiny,
  stepnumber=1,
  numbersep=5pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  tabsize=2,
  breaklines=true,
  breakatwhitespace=false,
  belowskip=0pt,
 aboveskip=0pt,
}

\definecolor{cornsilk}{RGB}{255,248,220}



\mdfdefinestyle{mcode}{%
 roundcorner=5pt,
 skipabove=10pt,skipbelow=10pt,
 middlelinewidth=1pt,
 backgroundcolor=cornsilk,
 firstextra={\draw[dashed,line width=1pt,xshift=1pt] (O) -- (P|-O);},
 secondextra={\draw[dashed,line width=1pt,xshift=-1pt] (O|-P) -- (P);},
 middleextra={\draw[dashed,line width=1pt,xshift=1pt] (O) -- (P|-O);\draw[dashed,line width=1pt,xshift=-1pt] (O|-P) -- (P);},
}
\lstnewenvironment{lstmcode}
 {\lstset{style=mymcode}}{}
\surroundwithmdframed[style=mcode]{lstmcode}
\begin{document}
\section{Section Something}

The following code is just a really long list of comments, really. 

\begin{lstmcode}
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % It would be nice with a horisontal dashed line below me. 
    % Another dashed above me, too.
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
    % Matlab code 
\end{lstmcode}

\end{document}

相关内容