LaTeX Source Code Listing Horizontally In Figure With Subfloats

I'm trying to embed a couple of listings

\begin{lstlisting}
>>> from sympy import Integral, latex
>>> from sympy.abc import x
>>> latex(x**2)
x^{2}
\end{lstlisting}

\begin{lstlisting}
>>> from sympy.printing.mathml import mathml
>>> from sympy import Integral, latex
>>> from sympy.abc import x
>>> print mathml(x**2)
<apply><power/><ci>x</ci><cn>2</cn></apply>
\end{lstlisting}

\begin{lstlisting}
>>> from sympy import *
>>> x = symbols("x")
>>> fcode(sqrt(1-x**2))
'      sqrt(-x**2 + 1)'
\end{lstlisting}


as a figure with images subfloated as shown here.

Is there an easy way (least amount of LaTeX code) for me to do that?

migrated from stackoverflow.comDec 10 '11 at 2:47

This question came from our site for professional and enthusiast programmers.

• A simple way would be to put them in a minipage, assuming that they can all fit next to each other – Peter Grill Dec 10 '11 at 2:48
• Duplicate? tex.stackexchange.com/questions/34946/… – Torbjørn T. Dec 10 '11 at 2:53
• Are you sure you want all three listings in one single row? Do you want the result to be treated as a floating object? – Gonzalo Medina Dec 10 '11 at 3:00
• Good Point. Just testing the waters with LaTeX and what I could do with it. Thanks everyone for the help! – Jasoneer Dec 14 '11 at 22:27

The listings you gave are quite wide and won't fit horizontally next to each other. But, for a case where they will fit next to each other, you can put each in a minipage. This will put each listing next to each other. To make this float just place it within a figure environment.

\documentclass[border=5pt]{standalone}
\usepackage{listings}

\begin{document}
\begin{minipage}[t]{0.3\linewidth}
\begin{lstlisting}
>>> abc
>>> def
>>> ghi
x^{2}
\end{lstlisting}
\end{minipage}
%
\begin{minipage}[t]{0.3\linewidth}
\begin{lstlisting}
>>> jkl
>>> mno
>>> pqr
>>> stu
vwx
\end{lstlisting}
\end{minipage}
%
\begin{minipage}[t]{0.3\linewidth}
\begin{lstlisting}
>>> 123
>>> 456
>>> 789
\end{lstlisting}
\end{minipage}
\end{document}


You could also post these vertically, since they are very wide. \documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
%\usepackage{subfig}% http://ctan.org/pkg/subfig
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\begin{document}
\begin{figure}
\centering
\begin{varwidth}{\linewidth}%
\begin{lstlisting}[caption={Listing A}]
>>> from sympy import Integral, latex
>>> from sympy.abc import x
>>> latex(x**2)
x^{2}
\end{lstlisting}
\end{varwidth} \par
\begin{varwidth}{\linewidth}%
\begin{lstlisting}[caption={Listing B}]
>>> from sympy.printing.mathml import mathml
>>> from sympy import Integral, latex
>>> from sympy.abc import x
>>> print mathml(x**2)
<apply><power/><ci>x</ci><cn>2</cn></apply>
\end{lstlisting}
\end{varwidth} \par
\begin{varwidth}{\linewidth}%
\begin{lstlisting}[caption={Listing C}]
>>> from sympy import *
>>> x = symbols("x")
>>> fcode(sqrt(1-x**2))
'      sqrt(-x**2 + 1)'
\end{lstlisting}
\end{varwidth}
\caption{A number of listings.}
\end{figure}
\end{document}


The choice of using varwidth is merely to center the listings horizontally with respect to one another in the figure. Replacing varwidth with minipage will yield a flush left-aligned set of listings.