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.
\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}
Go to View > Messages Pane:
Then click on the "Settings" tab in the bottom right corner:
Then click on the "Selected" radio button:
Then double click on "User commands" so that "no" turns to "yes":
Then click on the "Output" tab:
Then do the action that you want to copy the shortcut of. In this case, insert your horizontal line. In the output tab at the bottom, you will see:
This tell us that the correct command is:
inset-insert line CommandInset line LatexCommand rule offset "0.5ex" width "100col%" height "1pt" \end_inset
Verify that the command is correct by inserting it directly into the mini buffer (Alt + X or View > Toolbars > Command Buffer) at the bottom of the screen:
Run the command by pressing return. If it has the desired effect, then you know you have the right command and can create the shortcut in the usual way.
Note that you can have one shortcut do multiple commands by using command-sequence
.
Best Answer
No sledgehammer like
tabularx
; the important thing is to avoid\hrulefill
at the start of a line, because glue disappears at line breaks. The first is after the indentation box or at the beginning of the paragraph like in my answer, but it doesn't disappear there. For the second one we need something that will not vanish at a line break. like\mbox{}
.The use of
*
is in order to avoid page breaks.