拡張によって得られた値にカウンターを設定する

拡張によって得られた値にカウンターを設定する

私はglossariesおよびminitocパッケージを使用しています。目次に印刷されるすべての用語集に対して、たとえばtoc用語集パッケージのオプションを使用してミニ目次が生成されます。章の前に用語集を印刷すると、章のミニ目次ではなく、用語集のまだ印刷されていないミニ目次が印刷され、章に関するミニ目次が効果的にオフセットされます。

mtc手動では、 minitocs のカウンターをオフセットすることでこれを解決できます。たとえば\addtocounter{mtc}{2}、用語集が 2 つある場合 (たとえば、頭字語 + 実際の用語集) などです。

私はそのシフトを自動的に実行してパッケージに埋め込もうとしています。まず、用語集の数を取得します。glossariesパッケージは、\@glo@types定義された用語集の名前のカンマ区切りのリストとしてマクロを定義します。したがって、次のようにして用語集の数を出力できます。ここで、
\expandafter\listLength\expandafter{\@glo@types}
この\listLengthマクロは、答えetoolbox を利用すると、次のようになります。

\makeatletter
\newcounter{listlength@cnt}
\newcommand*{\listlength@add}[1]{\stepcounter{listlength@cnt}}
\newcommand*{\listLength}[1]{%
    \setcounter{listlength@cnt}{0}%
    \forcsvlist{\listlength@add}{#1}%
    \thelistlength@cnt%
}
\makeatother

しかし、コマンドでその値を使用しようとするとaddtocounter、機能しなくなります。同じ例の場合:

  • \expandafter\listLength\expandafter{\@glo@types}プリント2
  • \addtocounter{mtc}{\expandafter\listLength\expandafter{\@glo@types}}「番号がありません」というエラーが出力されます。そのコマンドの前と中のさまざまな組み合わせを試しましたが\expandafter、お気づきかもしれませんが、展開プロセスで問題が発生しました。

では、拡張によって導出された値にカウンターをオフセットするにはどうすればよいでしょうか?

MWE は次のとおりです。

\documentclass{report}

\usepackage[nohints]{minitoc}
\usepackage{etoolbox}
\usepackage[acronym,xindy,toc]{glossaries}
    \makeglossaries

\begin{filecontents}{glossary.tex}
    \newacronym{nraa}{NRAA}{Not Really An Acronym}
    \newglossaryentry{stuff}{
      name={stuff},
      description={a test}%
    }
\end{filecontents}
\loadglsentries{glossary}

% macro to count args of csv-list
\makeatletter
\newcounter{listlength@cnt}
\newcommand*{\listlength@add}[1]{\stepcounter{listlength@cnt}}
\newcommand*{\listLength}[1]{%
    \setcounter{listlength@cnt}{0}%
    \forcsvlist{\listlength@add}{#1}%                        % from etoolbox
    \thelistlength@cnt%
}
\makeatother

\begin{document}
    \dominitoc            % Generating mini-toc
    \tableofcontents
    \glsaddall            % adding all glossary entries for the test
    \printglossary[type=\acronymtype]
    \printglossary[type=main]
    \addtocounter{mtc}{2} % manual offset
    \newpage
    \makeatletter
    number of glossaries:
    \expandafter{\expandafter\listLength\expandafter{\@glo@types} % prints 2
    %\addtocounter{mtc}{\expandafter\listLength\expandafter{\@glo@types}}
    % results in error, missing number
    \makeatother

    \chapter{chap1}
      chapter's toc is:
      \minitoc

      Content is:
      \section{s1}
        \subsection{s11}
\end{document}

答え1

コマンド\listLengthは純粋な拡張では機能しません。次のように言う必要があります。

\newcommand*{\listLength}[1]{%
    \setcounter{listlength@cnt}{0}%
    \forcsvlist{\listlength@add}{#1}%
}

の定義として、\listLengthそして

\listLength{\@glo@types}%
\addtocounter{mtc}{\value{listlength@cnt}}%

コード内。

もっと巧妙な方法がありますexpl3:

\documentclass{report}

\usepackage[nohints]{minitoc}
\usepackage{xparse}
\usepackage[acronym,xindy,toc]{glossaries}
    \makeglossaries

\begin{filecontents}{glossary.tex}
    \newacronym{nraa}{NRAA}{Not Really An Acronym}
    \newglossaryentry{stuff}{
      name={stuff},
      description={a test}%
    }
\end{filecontents}
\loadglsentries{glossary}

% macro to count args of csv-list
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\listLength}{m}
 {
  \clist_count:n { #1 }
 }
\ExplSyntaxOff

\begin{document}
    \dominitoc            % Generating mini-toc
    \tableofcontents
    \glsaddall            % adding all glossary entries for the test
    \printglossary[type=\acronymtype]
    \printglossary[type=main]
    \addtocounter{mtc}{2}% manual offset
    \newpage
    number of glossaries:
    \makeatletter
    \listLength{\@glo@types} % prints 2
    \addtocounter{mtc}{\listLength{\@glo@types}}
    \themtc
    \makeatother

    \chapter{chap1}
      chapter's toc is:
      \minitoc

      Content is:
      \section{s1}
        \subsection{s11}
\end{document}

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

mtcご覧のとおり、コードが実行された後の の値は3 です。

コマンド\clist_count:nは完全に拡張可能ですが、あなたのは\listLengthそうではありません。

関連情報