2

I am trying to put a box around matlab code but the bottom of the box sits extremely close to the next line. I try a line break but it does not work. Any ideas?

\documentclass[a4paper,12pt]{article}
\usepackage{mdframed}
\begin{document}
...get,\\

\begin{mdframed}
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
    2.2147   %This is the value of our put option
\end{verbatim}
\end{mdframed}
\end{document}
bla bla bla
5
  • 1
    Please make your code compilable, starting with \documentclass{...} and ending with \end{document}.
    – jub0bs
    Apr 29, 2013 at 11:30
  • \usepackage doesn't work inside the document. Apr 29, 2013 at 11:33
  • 1
    Try passing the options skipabove=\topsep,skipbelow=\topsep (for instance) to your mdframed environment.
    – jub0bs
    Apr 29, 2013 at 11:34
  • Sorry Jubobs I do not understand what you mean passing the options. Apr 29, 2013 at 11:35
  • Consider using the matlab-prettifier package; see this answer of mine.
    – jub0bs
    Feb 10, 2014 at 13:20

2 Answers 2

5

You can add vertical space above and below an mdframed environment by assigning nonzero (length) values to the skipabove and skipbelow optional keys, respectively. For instance:

\documentclass{book}

\usepackage{mdframed}

\begin{document}

favorite
I am trying to put a box around matlab code but the bottom of the box sits extremely close
to the next line. I try a line break but it does not work. Any ideas?

...get,\\

\begin{mdframed}[skipabove=\topsep,skipbelow=\topsep]
\begin{verbatim}
S = 55; % Value of the underlying
...
V =
    2.2147   %This is the value of our put option
\end{verbatim}
\end{mdframed}
\end{document}

gives

enter image description here

3
  • If you use the current version at github it works out of the box. Apr 29, 2013 at 12:05
  • @MarcoDaniel "Out of the box"; is that a pun? I'm not sure I see what you mean...
    – jub0bs
    Apr 29, 2013 at 18:19
  • I added \topsep as default option for skipabove Apr 29, 2013 at 18:28
3

This is to show an alternative for matlab code with highlighting.

It is better to use mcode to typeset the matlab codes. mcode internally uses listings package and offers features like different colored text etc.

An example:

\documentclass{article}

% load package with ``framed'' and ``numbered'' option.
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\lstset{
    rulecolor=\color{black}
}

% something NOT relevant to the usage of the package.
\setlength{\parindent}{0pt}
\setlength{\parskip}{18pt}

\begin{document}
 The following demo block shows some matlab code:
\begin{lstlisting}
for i = 1:3
    if i >= 5                    % literate programming replacement some thing to fill in the space
        disp('cool');           % comment with some §\mcommentfont\LaTeX\ in it: $\mcommentfont\pi x^2$§
    end
    really really long really really long really really long really really long really really long line % blaaaaaaaa
end
\end{lstlisting}
\end{document}

enter image description here

enter image description here

You can add captions by using

\begin{lstlisting}[caption=My code here]
1
  • 1
    Check out the matlab-prettifier package; it's aimed at highlighting Matlab code in a more "pedantic" way than mcode does :)
    – jub0bs
    Feb 8, 2014 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .