創建兩個半獨立的材料列表,它們共享其計數器

創建兩個半獨立的材料列表,它們共享其計數器

我成功地使用\newlistof{listofA}{A}{List of As}\newlistentry{mycounter}{A}{0}創建了一個 A 列表,其內容使用 來添加\addcontentsline{A}{mycounter}{\protect\numberline{\themycounter}...}。現在我想要一個共享 counter 的 B 清單mycounter。我有\newlistof{listofB}{B}{List of Bs}\newlistentry{mycounter}{B}{0}失敗了一些Command ... already defined。沒有它,使用\addcontentsline{B}{mycounter}{...}, 兩個都列表變成空的。我究竟做錯了什麼?

答案1

透過此xassoccnt封裝,可以耦合多個計數器,即,如果其中一個計數器是階梯式的,則其他計數器也可以是階梯式的。

使用一些“任意”名稱聲明一個計數器組,並將相關計數器分配給它們。

使用相同的計數器是不可能的,因為\newlistof需要一個不存在的計數器,例如,foo並根據計數器名稱產生一堆命令。

\documentclass{article}

\usepackage{tocloft}
\usepackage{xassoccnt}

\usepackage{blindtext}


\newcommand{\listoffooname}{List of foo environments}
\newcommand{\listoffoobarname}{List of foobar environments}

\newlistof[section]{foo}{foo}{\listoffooname}
\newlistof[section]{foobar}{foobar}{\listoffoobarname}

\DeclareCoupledCountersGroup{foofoobar}
\DeclareCoupledCounters[name=foofoobar]{foo,foobar}


% Dummy usage of the counter and generating a 'ToC' entry
\newenvironment{foo}[1]{%
  \refstepcounter{foo}
  \par\noindent\textbf{Foo \thefoo. #1}
  \addcontentsline{foo}{foo}{\protect\numberline{\thefoo} #1}\par%
}{\hrule}

\newenvironment{foobar}[1]{%
  \refstepcounter{foobar}
  \par\noindent\textbf{Foobar \thefoobar. #1}
  \addcontentsline{foobar}{foobar}{\protect\numberline{\thefoobar} #1}\par%
}{\hrule}



\begin{document}
\listoffoo
\listoffoobar


\section{Where foo starts}

\begin{foo}{A nice foo}

\blindtext[2]
\end{foo}

\begin{foobar}{A nice foobar}

\blindtext[2]
\end{foobar}


\begin{foo}{A nice foo again}

\blindtext[2]
\end{foo}

\begin{foo}{A nice foo again}

\blindtext[2]
\end{foo}

\begin{foobar}{A nice foobar again}

\blindtext[2]
\end{foobar}




\section{Where foo ends}

\begin{foo}{Another nice foo too}

\blindtext[1]
\end{foo}

\begin{foobar}{Another nice foobar}

\blindtext[2]
\end{foobar}




\end{document}

在此輸入影像描述

相關內容