5

I am using package listings, and I would like to combine a listing and a figure into a single float. As an example, I am using listings as follows:

\begin{lstlisting}[language=XML,float=h,label=lst:transition_selector_example, 
  basicstyle=\footnotesize\ttfamily,tabsize=2,
  caption={Example SCXML document used to illustrate transition selection optimization strategies}]
<scxml>
    <state id="A">
        <transition target="B" event="e1" id="transition1"/>
        <transition target="B" event="e2" id="transition2"/>
        <transition target="B" event="e2" id="transition3"/>
    </state>
    <state id="B">
        <transition target="A" event="e3" id="transition4"/>
    </state>
</scxml>
\end{lstlisting}

Later, I have a figure with an \includegraphics in it, like the following:

\begin{figure}[p]
\centering
\includegraphics[width=1.00\textwidth]{chapters/3/figures/firefox-transitionSelector}
\caption{Results of Transition Selection optimization strategy in Firefox}
\label{fig:firefox-transitionSelector}
\end{figure}

I would like to make the listing become a part of that figure, such that it share's the figure's reference number, and is laid out in the page as a single combined graphical entity.

I'd greatly appreciate any guidance anyone can offer regarding how to do this.

4

listings and \includegraphics can each be included essentially anywhere. The following puts them one above the other, side by side layout is also possible if you put the listing into a minipage of a width that allows the graphic to the side.

\begin{figure}[p]
\centering
\includegraphics[width=1.00\textwidth]{chapters/3/figures/firefox-transitionSelector}

\begin{lstlisting}[language=XML,float=h,label=lst:transition_selector_example,basicstyle=\footnotesize\ttfamily,tabsize=2,caption={Example SCXML document used to illustrate transition selection optimization strategies}]
<scxml>
    <state id="A">
        <transition target="B" event="e1" id="transition1"/>
        <transition target="B" event="e2" id="transition2"/>
        <transition target="B" event="e2" id="transition3"/>
    </state>
    <state id="B">
        <transition target="A" event="e3" id="transition4"/>
    </state>
</scxml>
\end{lstlisting}

\caption{Results of Transition Selection optimization strategy in Firefox}
\label{fig:firefox-transitionSelector}
\end{figure}
2

To add to David's answer (in case you want "subcaptions"):

enter image description here

\documentclass{article}

\usepackage{subcaption}
\usepackage{listings}
\usepackage[demo]{graphicx}

\begin{document}

\begin{figure}
    \begin{subfigure}[b]{\textwidth}
        \begin{lstlisting}[%
            language=XML,%
            label=lst:transition_selector_example,%
            basicstyle=\footnotesize\ttfamily,%
            tabsize=2
        ]
        <scxml>
            <state id="A">
                <transition target="B" event="e1" id="transition1"/>
                <transition target="B" event="e2" id="transition2"/>
                <transition target="B" event="e2" id="transition3"/>
            </state>
            <state id="B">
                <transition target="A" event="e3" id="transition4"/>
            </state>
        </scxml>
        \end{lstlisting}
        \subcaption{Example SCXML document used to illustrate\ldots}
    \end{subfigure}

    \vspace{2em}
    \begin{subfigure}[b]{\textwidth}
    \includegraphics[width=1.00\textwidth]{chapters/3/figures/firefox-transitionSelector}
    \subcaption{A screenshot (??)}
    \end{subfigure}
    \label{fig:firefox-transitionSelector}
    \caption{Results of Transition Selection optimization strategy in Firefox}
\end{figure}

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