# Put figure and listing side by side [duplicate]

This question already has an answer here:

I'm trying to put a listing and figure side by side. I tried to follow the suggestions in this link How to put algorithm and figure(s) side by side? without using caption package but the listing stays below in the figure.

\begin{figure}[htbp]
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[height=0.7in]{figure}
\caption{Figure one.}
\label{fig:fig1}
\end{minipage}
\end{figure}
\begin{minipage}{.5\textwidth}
\begin{lstlisting}
#include <stdio.h>

main()
{
printf ("Hello World!\n");
}
\end{lstlisting}
\end{minipage}


## marked as duplicate by Martin Schröder, Heiko Oberdiek, OSjerick, Thorsten, Peter JanssonJan 22 '14 at 14:58

• – user13907 Jan 22 '14 at 13:45
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. A suggestion: Do us a favour and change your username to something more telling than "user1234". – Martin Schröder Jan 22 '14 at 14:05
• first: you really don't want a figure env here; remove \begin/\end{figure}; second: put % after the first \end{minipage}; third: use the capt-of package and use captionof{figure}{Figure one.} – wasteofspace Jan 22 '14 at 14:10

Something like this?

\documentclass{article}

\usepackage{listings}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htbp]
\begin{tabular}{p{0.5\textwidth}p{0.5\textwidth}}
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[height=0.7in]{figure}
\caption{Figure one.}
\label{fig:fig1}
\end{minipage}
&
\begin{minipage}{.5\textwidth}
\begin{lstlisting}
#include <stdio.h>

main()
{
printf ("Hello World!\n");
}
\end{lstlisting}
\end{minipage}
\end{tabular}
\end{figure}

\end{document}


A recommended solution by me. We can abuse showexpl package, that is for rendering TeX code and its output side by side, by specify the graphic option to prevent the \LTXexample or \LTXinputExample from rendering the code.

The following code speaks my idea clearly. Some settings are left intact for you to make the rendered code look beautiful.

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{Program.cs}
using System;
namespace Delegate
{
class Program
{
// start
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
Console.WriteLine(x);
}
// stop
}
}
\end{filecontents*}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{showexpl}

\lstdefinestyle{Common}
{
language={[Sharp]C},
numbers=left,
numbersep=1em,
numberstyle=\tiny\noaccsupp,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=2,
columns=flexible,
includerangemarker=false,
rangeprefix=//\ ,
}

\lstdefinestyle{A}
{
style=Common,
backgroundcolor=\color{yellow!10},
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{blue}\bf,
identifierstyle=\color{black},
stringstyle=\color{red},