カウンターを共有する2つの半独立な材料リストを作成する

カウンターを共有する2つの半独立な材料リストを作成する

\newlistof{listofA}{A}{List of As}とを使用して\newlistentry{mycounter}{A}{0}A のリストを作成し、その内容は を使用して追加されました\addcontentsline{A}{mycounter}{\protect\numberline{\themycounter}...}。次に、カウンターを共有する B のリストを作成しますmycounter。 があります\newlistof{listofB}{B}{List of Bs}\newlistentry{mycounter}{B}{0}はいくつかの で失敗しますCommand ... already defined。 これがないと、 を使用します\addcontentsline{B}{mycounter}{...}両方リストが空になります。何が間違っているのでしょうか?

答え1

このxassoccntパッケージを使用すると、複数のカウンターを結合することが可能です。つまり、カウンターの 1 つがステップされると、他のカウンターもステップされます。

任意の名前でカウンター グループを宣言し、関連するカウンターを割り当てます。

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

ここに画像の説明を入力してください

関連情報