將計數器設定為擴展獲得的值

將計數器設定為擴展獲得的值

我使用glossariesminitoc包。 Minitocs 是為目錄中列印的所有術語表產生的,例如,具有toc術語表包的選項。當在章節之前列印術語表時,會列印術語表中尚未列印的 minitoc,而不是章節的 minitoc,從而有效地抵消了與章節有關的 minitoc。

手動地,這可以透過偏移mtcminitocs 的計數器來解決,例如,\addtocounter{mtc}{2}如果有 2 個術語表(例如首字母縮寫詞 + 實際術語表)。

我正在嘗試自動執行此轉換以將其嵌入到包中。首先是取得術語表的數量。術語表包將巨集定義\@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則不是。

相關內容