6

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}

2 Answers2

3

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}
JPi
  • 13,595
1

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},
    commentstyle=\color{green}
}

\usepackage{graphicx}
\begin{document}
\LTXinputExample[style=A,graphic=example-image-a]{Program.cs}
\end{document}

enter image description here