如何共享子浮点数的计数器

如何共享子浮点数的计数器

我正在尝试制作一个包含 3 个子浮点数的列表浮点数。我已设法通过组合以下答案来制作实际的浮点数问题1问题2

现在我只需要让 mylisting 使用与 listings 相同的计数器,因此这三个程序分别名为 Program 1、Program 2 和 Program 3。

\documentclass[a4paper,article,oneside]{memoir}
\usepackage{subfig}
\let\newfloat\undefined %for working with memoir

\usepackage{floatrow}
\usepackage{filecontents}
\usepackage{listings}
\lstset{
columns=flexible,
basicstyle=\footnotesize\ttfamily,
showstringspaces=false,
tabsize=3,
numberbychapter=false,
frame=single,
extendedchars=true,
}

%% Declare mylisting
\DeclareNewFloatType{mylisting}{placement=htbp,fileext=lol,name=Program}
\floatsetup[mylisting]{style=Plaintop}

%% Rename "List of listings" to "Programmer"
\floatname{lstlisting}{Program}
\renewcommand{\lstlistoflistings}{ 
  \begingroup 
    \listof{lstlisting}{Programmer} 
  \endgroup 
}

\begin{document}
\lstlistoflistings
\chapter{}

\begin{filecontents*}{dangling.hun}
if <expression> then
<statement>
if <expression> then
<statement>
<else>
<statement>
\end{filecontents*}

\lstinputlisting[caption={A program}]{dangling.hun}

\begin{mylisting}[htbp]
  \caption{These are my subfloats}
  \subfloat[First]{\begin{minipage}{0.28\linewidth}
\lstinputlisting[label=dangling,nolol]{dangling.hun}\end{minipage}}
  \hfill
  \subfloat[Second]{\begin{minipage}{0.32\linewidth}
\lstinputlisting[label=dangling1,nolol]{dangling.hun}\end{minipage}}
  \hfill
  \subfloat[Third]{\begin{minipage}{0.32\linewidth}
\lstinputlisting[label=dangling2,nolol]{dangling.hun}\end{minipage}}
\end{mylisting}

\lstinputlisting[caption={Another program}]{dangling.hun}



\end{document}

更新 我通过以下更改修复了这个问题calc。但是,我对更简洁的方法感兴趣。

\begin{mylisting}[htbp]
...
\end{mylisting}

\begin{mylisting}[htbp]
\setcounter{mylisting}{\value{lstlisting}}
\setcounter{lstlisting}{\value{mylisting}+1}
...
\end{mylisting}

答案1

您希望listings为您的mylisting环境创建一个与 相同的从属重复计数器。为此,您需要发出

\makeatletter
\AtBeginDocument{\let\c@mylisting\c@lstlisting}
\makeatother

listings这会将计数器的内容复制到计数器\c@lstlisting的内容中。因此,将引用与相同的计数器寄存器,从而复制其行为。发布此的原因是因为在发布时按原样复制其内容。但是,将其计数器的创建延迟到;作为参考,以下内容取自mylisting\c@mylisting\c@mylisting\c@lstlisting\AtBeginDocument\letlistings\AtBeginDocumentlistings.sty

\AtBeginDocument{
  \@ifundefined{thechapter}{\let\lst@ifnumberbychapter\iffalse}{}
  \lst@ifnumberbychapter
      \newcounter{lstlisting}[chapter]
      \gdef\thelstlisting%
           {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@lstlisting}
  \else
      \newcounter{lstlisting}
      \gdef\thelstlisting{\@arabic\c@lstlisting}
  \fi}

上述建议来自从属重复计数器

enter image description here

\documentclass[a4paper,article,oneside]{memoir}
\usepackage{subfig}
\let\newfloat\undefined %for working with memoir

\usepackage{floatrow}
\usepackage{filecontents}
\usepackage{listings}
\lstset{
columns=flexible,
basicstyle=\footnotesize\ttfamily,
showstringspaces=false,
tabsize=3,
numberbychapter=false,
frame=single,
extendedchars=true,
}

%% Declare mylisting
\DeclareNewFloatType{mylisting}{placement=htbp,fileext=lol,name=Program}
\makeatletter
\AtBeginDocument{\let\c@mylisting\c@lstlisting}
\makeatother
\floatsetup[mylisting]{style=Plaintop}

%% Rename "List of listings" to "Programmer"
\floatname{lstlisting}{Program}
\renewcommand{\lstlistoflistings}{ 
  \begingroup 
    \listof{lstlisting}{Programmer} 
  \endgroup 
}

\begin{document}
\lstlistoflistings
\chapter{}

\begin{filecontents*}{dangling.hun}
if <expression> then
<statement>
if <expression> then
<statement>
<else>
<statement>
\end{filecontents*}

\lstinputlisting[caption={A program}]{dangling.hun}

\begin{mylisting}[htbp]
  \caption{These are my subfloats}
  \subfloat[First]{\begin{minipage}{0.28\linewidth}
\lstinputlisting[label=dangling,nolol]{dangling.hun}\end{minipage}}
  \hfill
  \subfloat[Second]{\begin{minipage}{0.32\linewidth}
\lstinputlisting[label=dangling1,nolol]{dangling.hun}\end{minipage}}
  \hfill
  \subfloat[Third]{\begin{minipage}{0.32\linewidth}
\lstinputlisting[label=dangling2,nolol]{dangling.hun}\end{minipage}}
\end{mylisting}

\lstinputlisting[caption={Another program}]{dangling.hun}

\end{document}

相关内容