2

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?

Torbjørn T.
  • 206,688
Jasoneer
  • 121

2 Answers2

3

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.

enter image description here

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}
Peter Grill
  • 223,288
3

You could also post these vertically, since they are very wide.

enter image description here

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

Werner
  • 603,163