1

I wrote a piece of code in the listing form. Then I want to put its running result graph below this code. But in Latex, although I write them next to each other, sometimes they are distributed on different pages. Note that there are separate captions. I read other posts, I read other posts, they are all using the same environment next to each other. For example, if two pictures are next to each other, they can use minipage in the figure environment. But if I use it this way, the caption of the code I write will also start with Figure. What should I do? Thanks!

They are next to each other in the code below. But sometimes they are not. I hope to be able to set them up next to each other.

\documentclass{article}

\usepackage{listings}
%\usepackage{caption,subcaption}
\usepackage{subfig}   
\usepackage{caption}
\usepackage{graphicx}
\usepackage{algorithmic}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{lipsum}


\usepackage{mathtools}
%\usepackage{subfigure}  
\usepackage{subfig}       
\usepackage{enumitem}
\usepackage{pifont}
\newlength{\restofline}
\usepackage{multirow}
\usepackage{tablefootnote}


\usepackage{listings}
\usepackage{framed}
\usepackage{capt-of}
\usepackage{newfloat}
\usepackage{xcolor}

\definecolor{keywordcolor}{RGB}{157,0,129}
\definecolor{commentcolor}{RGB}{157,0,129}
\definecolor{numbercolor}{RGB}{120,120,120}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}
\begin{document}
\lipsum[1-2]

\begin{listing}[t]
    \noindent
    \centering
    \begin{minipage}[t]{0.45\textwidth}
        
        \begin{lstlisting}[gobble=16, language=C,
        keywordstyle={\color{keywordcolor}},basicstyle = \sffamily,
        xleftmargin=2em,xrightmargin=2em,frame=lrtb,numberstyle={\color{numbercolor}\normalfont},   numbersep={-0.2cm},numbers=left,tabsize=7,
        basicstyle=\linespread{1.1}\footnotesize]
        int main(){
            printf("hello")
        }
        \end{lstlisting}
        
    \end{minipage}%
    \caption{Code.}
\end{listing}
    
\begin{figure}[t]
    \centering
    \begin{minipage}{0.5\textwidth}
        \centering
%       \includegraphics[width=0.9\linewidth]{hpca/latency_cdf.pdf}
        a picture
        \caption{The latencies in accessing the replacement set when the target set contains a different number of dirty cache lines.  }
        \label{CDF}
    \end{minipage}\hfill
\end{figure}
\lipsum
\end{document}
1
  • If you want the image to always be positioned close to the code do not use the figure environment and instead use the caption package with the command \captionof{figure}{the caption here}
    – Ivan
    Apr 26, 2021 at 17:37

1 Answer 1

1

Simply use two minipages with a length of 4.5\textwidth to get a listing in the first minipage and an image in the second minipage beneath each other.

Please see the following MWE (important code changings marked with <======):

\documentclass{article}

\usepackage{listings}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{algorithmic}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{lipsum}

\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{pifont}
\newlength{\restofline}
\usepackage{multirow}
\usepackage{tablefootnote}

\usepackage{framed}
\usepackage{newfloat}
\usepackage{xcolor}

\definecolor{keywordcolor}{RGB}{157,0,129}
\definecolor{commentcolor}{RGB}{157,0,129}
\definecolor{numbercolor}{RGB}{120,120,120}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}


\begin{document}
\lipsum[1-2]

\begin{minipage}[c]{0.45\textwidth} % <=================================
  \noindent
  \centering
  \begin{lstlisting}[gobble=16, language=C,
    keywordstyle={\color{keywordcolor}},basicstyle = \sffamily,
    xleftmargin=2em,xrightmargin=2em,frame=lrtb,numberstyle={\color{numbercolor}\normalfont},
    numbersep={-0.2cm},numbers=left,tabsize=7,
    basicstyle=\linespread{1.1}\footnotesize]
      int main(){
          printf("hello")
      }
  \end{lstlisting}
  \captionof{listing}{Code.}
\end{minipage}% <=======================================================
\begin{minipage}[c]{0.45\textwidth} % <=================================
  \centering
  \includegraphics[width=0.9\linewidth]{example-image} % <==============
  \captionof{figure}{The latencies in accessing the replacement set when the target set contains a different number of dirty cache lines.  }
  \label{CDF}
\end{minipage} % <======================================================
 
\lipsum
\end{document}

and its result:

resulting pdf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .