5

I have several pieces of Matlab script I have put into boxes using the below method. I want to be able to call them listings 1 2 3 4 etc. Preferably a command that will do it automatically for me. New to LaTeX so no idea I have searched and found very little.

\documentclass[a4paper,12pt]{article}
    \usepackage{mdframed}
    \begin{document}
    \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}
jub0bs
  • 58,916

2 Answers2

6

If you want to keep your current settings (mdframed+verbatim), you can use the \captionof command from the caption package to obtain a caption. Another option is to use the features provided by the listings package (instead of using mdframed+verbatim) to write your listings; the lstlisting environment gives you the possibility to have a frame and caption for your listings:

\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\usepackage{mdframed}
\usepackage{caption}
\captionsetup[lstlisting]{labelsep=none}

\lstset{frame={tblr}}

\begin{document}

\begin{lstlisting}[caption={\null}]
    S = 55; % Value of the underlying
   ...
    V =
        2.2147   %This is the value of our put option
\end{lstlisting}

\begin{mdframed}[skipabove=\topsep,skipbelow=\topsep]
\captionof{lstlisting}{}
\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}

enter image description here

If you want this to apply automatically, you can surround verbatim with a mdframed and use the settings key to automatically generate the caption:

\documentclass[a4paper,12pt]{article}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{caption}

\captionsetup[lstlisting]{labelsep=none}

\surroundwithmdframed[
  skipabove=\topsep,
  skipbelow=\topsep,
  settings=\captionof{lstlisting}{}
]{verbatim}

\begin{document}

\begin{verbatim}
    S = 55; % Value of the underlying
   ...
    V =
        2.2147   %This is the value of our put option
\end{verbatim}

\begin{verbatim}
    S = 55; % Value of the underlying
   ...
    V =
        8.2147   %This is the value of our put option
\end{verbatim}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Perfect. I was deliberating over having the font how I wanted but the title not or the other way around. Just what I wanted! – Kane Blackburn Apr 29 '13 at 14:27
0

I would suggest to use the listings package, with which you can both enter code snippets and load complete (or partial) files into your output. Referencing single listings is fairly easy too.

I tried to make you a small example, and hope this helps.

\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\usepackage{color}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\begin{lstlisting}[language=Matlab,
    frame=single,
    caption=Some Matlab code,
    label=matlab]
        S = 55; % Value of the underlying
        ...
        V = 2.2147   %This is the value of our put option
\end{lstlisting}

Here is some more text, which is then also referring to listing~\ref{matlab}, which we will explain later. There is also a second example, which is shown in listing~\ref{python}.

\lipsum[1]

\lstinputlisting[lastline=25,
    language=Python,
    frame=single,
    caption=Python code,
    label=python]
    {/path/to/python/file.py}

\lipsum[1]

\end{document}
Habi
  • 7,694