我正在尝试制作一个包含 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
\let
listings
\AtBeginDocument
listings.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}
上述建议来自从属重复计数器。
\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}