4

I use the glossaries and minitoc packages. Minitocs are generated for all glossaries printed in the table of content, eg with the toc option for the glossaries package. When one prints the glossaries before the chapters, the not-yet printed minitoc of the glossaries are printed instead of the chapter ones, effectively offsetting the minitocs with regards to the chapters.

Manually, this can be solved by offsetting the mtc counter of the minitocs, eg \addtocounter{mtc}{2} if there are 2 glossaries (acronyms + actual glossary for example).

I'm trying to automatically do that shift to embed it in a package. First is getting the number of glossaries. The glossaries package define the macro \@glo@types as a comma-separated list of the names of the glossaries defined. Thus, I can print the number of glossaries with:
\expandafter\listLength\expandafter{\@glo@types}
where the \listLength macro, inspired by this answer and relying on etoolbox, is:

\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

However, when I try to use the value in the addtocounter command, it doesn't work anymore. For the same example:

  • \expandafter\listLength\expandafter{\@glo@types} prints 2
  • \addtocounter{mtc}{\expandafter\listLength\expandafter{\@glo@types}} outputs a "Missing number" error. I tried with different combinations of \expandafter before/in that command, but as you might have picked up, I have troubles with the expansion process.

So, how can I offset a counter to a value derived by expansion?

Here is a 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}
  • Please make your code into a minimum (non)working example (starting with \documentclass, including packages, etc.), to help those wanting to help you. – Steven B. Segletes Apr 23 '15 at 15:39
  • Sounds like a good task for expl3's \exp_args:no :) – Sean Allred Apr 23 '15 at 16:28
3

Your \listLength command doesn't work by pure expansion; you should say

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

as the definition of \listLength and then

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

in the code.

There's a much slicker way with 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}

enter image description here

As you see, the value of mtc is 3, after the code has been executed.

The \clist_count:n command is fully expandable, your \listLength isn't.

  • Worked like a charm. I'll hold on going into latex3 for now but I'll soon have some time available. Is there some kind of comprehensive guide for it, like the not so short introduction to latex2e? – luneart Apr 24 '15 at 8:49
  • 1
    @luneart Not yet; but texdoc interface3 has pretty much all of it. – egreg Apr 24 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.