3

Well, the title says it all. I have code scripts that span over multiple pages. Is it possible to display a dashed line at the bottom and/or top of the page in which the listings environment experience a page break, and keep the solid line at the very top and very bottom of the environment?

A screen dump illustrating my question is given below. I would like to add dashed lines where the listings environment break.

enter image description here

I use mcode in combination with the listings enivorment, providing a set of lstset parameters. The following MWE illustrates where I would prefer to have horizontal, dashed lines, avoiding that the lstlistings environment just breaks to white.

\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
    It would be nice if you could provide a MWE of what you have got. – user10274 May 14 '13 at 12:12
  • Normally I would close this question but the bounty avoids it. Your question is similar to do it for me because you didn't provide a mwe. Do you use any related packages? – Marco Daniel May 17 '13 at 11:35
  • @MarcoDaniel Affirmative! Updated. – Holene May 17 '13 at 11:54
  • You might be interested in the matlab-prettifier package; see this answer. – jub0bs Apr 28 '14 at 15:55
4
+50

To draw such a frame you should use an other package which allows such modifications. The default packages are mdframed or tcolorbox. In the example below I provided a solution using tcolorbox whereby a new listings environment lstmcode is defined. Please note that I am using the version 2.22 for the example below.

enter image description here enter image description here

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

Here an other approach using mdframed. The output is equal:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.