Creating a box around a figure that includes the tabbing environment

Eyvind picture Eyvind · Mar 4, 2011 · Viewed 10.5k times · Source

I would like to create a frame or box around my LaTeX figure, which consists of code formatted with the tabbing environment. My code looks something like this:

\begin{figure}
\begin{tabbing}
\texttt{void method(I, T, E)} \{ \\
\ \ \texttt{some code}<\emph{some pseudo code}>();\\
...
\end{tabbing}
\caption{The caption for the figure}
\end{figure}

I have tried using various box and frame commands, both within and without the figure, but LaTeX typically responds with "Something's wrong--perhaps a missing \item" or "Not in outer par mode".

How can I create a box around the contents of my figure?

Thanks!

Answer

Peter Grill picture Peter Grill · Oct 14, 2011

One way is to use the mdframed package. It is quite customizable, I defined a style to illustrate its use:

enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage{mdframed}

\mdfdefinestyle{mystyle}{
    backgroundcolor=yellow!20
}

\begin{document}
\begin{figure}
\begin{mdframed}[style=mystyle]
\begin{tabbing}
\texttt{void method(I, T, E)} \{ \\
\ \ \texttt{some code}<\emph{some pseudo code}>();\\
...
\end{tabbing}
\caption{The caption for the figure}
\end{mdframed}
\end{figure}
\end{document}