3

I would like to align the first lines of the code and the captions in the following listings. This is my LaTeX code. I've also uploaded an image of the output.

\begin{figure}[H]
 \begin{minipage}[t]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Run()
{
    // no work
    if (!m_HasWork) 
        return;

    /* process the new frame */

    // done
    m_HasWork = false;
}
  \end{minted}
  \captionof{listing}{Main thread (process a frame)}
  \label{fig:thread1}
 \end{minipage}
 \begin{minipage}[t]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Update(
    const video::ImageFrame& Frame)
{
    // skip frame (not done with previous)
    if (m_HasWork)
        return;

    /* make a local copy 
     * of the frame */

   // indicate that there's work
    m_HasWork = true; 
}
  \end{minted}
  \captionof{listing}{Interrupt (get a frame)}
  \label{fig:interrupt}
 \end{minipage}
 \captionof{listing}{not relevant}
  \label{fig:sync}
\end{figure}

Output: (I want the captions of listings 1 and 2 on the same line)

output

Jasper
  • 33
  • After the first \end{minipage} you need a %. At the moment you get an unwanted space which results in the with 0.5\textwidth+0.5\textwidth+\space. – Marco Daniel Apr 27 '13 at 15:08

2 Answers2

4

You can do it manually with:

\documentclass{article}

\usepackage{minted}
\usepackage{caption}

\begin{document}

\begin{figure}[H]
 \begin{minipage}[t]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Run()
{
    // no work
    if (!m_HasWork) 
        return;

    /* process the new frame */

    // done
    m_HasWork = false;
}
  \end{minted}
  \vspace{2\baselineskip}
  \captionof{listing}{Main thread (process a frame)}
  \label{fig:thread1}
 \end{minipage}
 \begin{minipage}[t]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Update(
    const video::ImageFrame& Frame)
{
    // skip frame (not done with previous)
    if (m_HasWork)
        return;

    /* make a local copy 
     * of the frame */

   // indicate that there's work
    m_HasWork = true; 
}
  \end{minted}
  \captionof{listing}{Interrupt (get a frame)}
  \label{fig:interrupt}
 \end{minipage}
 \captionof{listing}{not relevant}
  \label{fig:sync}
\end{figure}

\end{document}

\vspace{2\baselineskip} adds the two lines missing between the first minted environment and its caption in order to align both captions.

Mensch
  • 65,388
Namrod
  • 106
1

use the [b] option for minipage and indert a second dummy line for the second caption.

\documentclass{article}
\usepackage{caption,minted}

\begin{document}

\begin{figure}
 \begin{minipage}[b]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Run()
{
    // no work
    if (!m_HasWork) 
        return;

    /* process the new frame */

    // done
    m_HasWork = false;
}
  \end{minted}
  \captionof{listing}{Main thread (process a frame)}
  \label{fig:thread1}
 \end{minipage}
 \begin{minipage}[b]{0.5\textwidth}
  \centering
  \begin{minted}{c++}
void MyClass::Update(
    const video::ImageFrame& Frame)
{
    // skip frame (not done with previous)
    if (m_HasWork)
        return;

    /* make a local copy 
     * of the frame */

   // indicate that there's work
    m_HasWork = true; 
}
  \end{minted}
  \captionof{listing}{Interrupt (get a frame)\newline~ }
  \label{fig:interrupt}
 \end{minipage}
 \captionof{listing}{not relevant}\label{fig:sync}
\end{figure}

\end{document}

enter image description here