5

我有一个宏可以并排显示,如下所示。它使用子浮点数,所以它是一个数字。

\newcommand{\listbylist}[6][showlines=true]{
   \begin{figure}
      \subfloat[ ]{
        \lstinputlisting[showlines=true,#1]{#2}                                                  
     \label{#4:A}
  }
  \hfill{}
  \subfloat[ ]{                                                                               
     % by setting the frame to leftline, we avoid a box into oblivion                         
     % turn off numbers                                                                       
     \lstinputlisting[showlines=true,frame=leftline,#1,numbers=none]{#3}                      
     \label{#4:B}                                                                             
  }                                                                                           
  \hfill{}
   \caption[#5]{#6}
       \label{#4}                                                                                         
   \end{figure}
}

不幸的是,这使用了 Figure 计数器,而不是 Listings 计数器。它也出现在错误的目录中,并在标题中使用“图”而不是“列表”,对它的引用等。有没有办法纠正这个问题?

我更喜欢一种简单的方法,比如在某处添加“Listing”这个词......

4

3 回答 3

2

不要使用 lstlistings 的内置浮点数,而是将它们包装在自定义浮点数中:

\begin{mylisting}
\begin{lstlisting}
int x = 1;
\end{lstlisting}
\end{mylisting}

然后对 subfloat 使用相同的浮点数(mylisting):

\newcommand{\listbylist}[6][showlines=true]{
  \begin{mylisting}
    \subfloat[ ]{
      ...
    }
    \hfill{}
    \subfloat[ ]{
      ...
    }
    \hfill{}
    \caption[#5]{#6}
    \label{#4}
  \end{mylisting}
}

这需要在序言中全部设置:

\newfloat{mylisting}{tbphH}{lopbl}[chapter]
\floatname{mylisting}{Listing}
\newsubfloat{mylisting}
\newcommand{\listofmylistings}{\listof{mylisting}{List of Listings}}
% if you use the hyperref package
\providecommand*\theHmylisting{\themylisting}
于 2009-10-06T17:28:13.117 回答
0

您可能想查看 subfloat 文档。我确信有一个宏调用可以在“figure”环境中计算 subfloat。您可以尝试将“figure”环境重新定义为“listings”——如果这有任何意义的话。

于 2009-10-06T00:17:41.653 回答
0

好的,这是错误的答案,但我几乎是这样到达那里的。它只是未能在右侧添加字幕\listof

就快到了。它可能会做得更好,但这几乎可以工作。剩下的就是让标题出现在 .lol 文件中,而不是 .loc 文件中。我会问一个关于这个的问题,然后修复这个答案。

基本上,这只是备份“数字”计数器,并复制“列表”计数器。在数字之后,它将它们放回原处。

% Need a counter to save the value to
\newcounter{pbsavefigurecounter}

\newcommand{\listbylist}[6][showlines=true]{
{% scope

   % Change ``Figure'' to ``Listing''
   \renewcommand{\figurename}{Listing}

   % save the figure counter
   \setcounter{pbsavefigurecounter}{\value{figure}}

   % copy the listings counter to the figure counter
   \setcounter{figure}{\value{lstlisting}}


   \begin{figure}
  \subfloat[ ]{
     \lstinputlisting[nolol,showlines=true,#1]{#2}
     \label{#4:A}
  }
  \hfill{}
  \subfloat[ ]{
     % by setting the frame to leftline, we avoid a box into oblivion
     % turn off numbers
     \lstinputlisting[nolol,showlines=true,frame=leftline,#1,numbers=none]{#3}
     \label{#4:B}
  }
  \hfill{}

%  \float@caption{lol}[#5]{#6}
   \label{#4}
   \end{figure}

   % Update the listings counter
   \setcounter{lstlisting}{\value{figure}}

   % Restore the figure counter
   \setcounter{figure}{\value{pbsavefigurecounter}}

   % Change ``Listing'' back to ``Figure''
   \renewcommand{\figurename}{Figure}
}
}
于 2009-10-06T14:12:29.100 回答