# Setting a counter to a value obtained by expansion

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}[1]{%
\setcounter{listlength@cnt}{0}%
\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}

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

\begin{document}
\dominitoc            % Generating mini-toc
\tableofcontents
\printglossary[type=\acronymtype]
\printglossary[type=main]
\newpage
\makeatletter
number of glossaries:
\expandafter{\expandafter\listLength\expandafter{\@glo@types} % prints 2
% 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. Apr 23 '15 at 15:39
• Sounds like a good task for expl3's \exp_args:no :) Apr 23 '15 at 16:28

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

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


as the definition of \listLength and then

\listLength{\@glo@types}%


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}

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

\begin{document}
\dominitoc            % Generating mini-toc
\tableofcontents
\printglossary[type=\acronymtype]
\printglossary[type=main]
\newpage
number of glossaries:
\makeatletter
\listLength{\@glo@types} % prints 2
\themtc
\makeatother

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

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


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? Apr 24 '15 at 8:49
• @luneart Not yet; but texdoc interface3 has pretty much all of it. Apr 24 '15 at 8:50