LaTeX 对齐铸造的代码片段

LaTeX 对齐铸造的代码片段

我想将代码的第一行和以下列表中的标题对齐。这是我的 LaTeX 代码。我还上传了输出的图像。

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

输出:(我希望清单 1 和清单 2 的标题在同一行)

输出

答案1

您可以使用以下方式手动完成:

\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}添加第一个环境和其标题之间缺失的两行,minted以便对齐两个标题。

答案2

使用[b]选项minipage并为第二个标题插入第二条虚拟线。

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

在此处输入图片描述

相关内容