# Stars of macros in definition of another starred/unstarred macro look like disconnected in some circumstances

This question follows a previous question of mine: Prepend different stuffs to starred and unstarred \chapter commands.

In some circumstances, a starred macro in definition of another starred/unstarred macro doesn't work as expected. The following MCE works like a charm but, as soon as the glossaries packages is loaded, the star of \resetsecnumdepth looks like disconnected: it appears in the text.

\documentclass{book}
\usepackage{xparse}
\usepackage{tocvsec2}
% \usepackage{glossaries}
%
\let\ORIchapter\chapter%
\RenewDocumentCommand\chapter{som}{%
\IfBooleanTF{#1}
{
\setsecnumdepth{none}%
}
{%
\resetsecnumdepth*
}
\IfNoValueTF{#2}
{\ORIchapter{#3}}
{\ORIchapter[#2]{#3}}%
}
%
\setsecnumdepth{chapter}
\begin{document}
\chapter{A chapter}
\end{document}


The problem doesn't come from xparse package, as the following MCE, using only LaTeX macros, has the same trouble.

\documentclass{book}
\usepackage{tocvsec2}
\usepackage{glossaries}
%
\let\ORIchapter\chapter%
\makeatletter%
\renewcommand\chapter{%
\@ifstar{\starred@chapter}{\unstarred@chapter}%
}
\newcommand{\starred@chapter}{%
\setsecnumdepth{none}%
\ORIchapter%
}%
\newcommand{\unstarred@chapter}{%
\resetsecnumdepth*
\ORIchapter%
}%
%
\makeatother%
%
\setsecnumdepth{chapter}
\begin{document}

\chapter{A chapter}
\end{document}


I had a look of the glossaries' code, and made some tests (patching the glossaries macros involving \chapter in order they use \ORIchapter instead) but I didn't succeed to debug the trouble (neither warning nor error in the .log file).

-
glossaries doesn't explicitly redefine \@ifstar so it must load something else that does. – Nicola Talbot Mar 9 '14 at 19:59
It's amsgen that redefines it. – Nicola Talbot Mar 9 '14 at 20:00
@NicolaTalbot I identified wrongly the source of the problem; the bug is indeed in tocvsec2. Sorry. – egreg Mar 9 '14 at 20:36
@egreg Drat those pesky spurious spaces :-) – Nicola Talbot Mar 9 '14 at 20:49
Note that tocvsec2 (2014/03/10 v1.3a) should have solved the issue. – egreg Mar 11 '14 at 0:11

It's another case of a spurious space!

\documentclass{book}
\usepackage{xparse}
\usepackage{tocvsec2}
\usepackage{glossaries}

\makeatletter
\renewcommand\resetsecnumdepth{%
\@ifstar{%
\setcounter{secnumdepth}{\value{max@secnumdepth}}%
\edef\stack@secnumdepth{\the\c@secnumdepth\relax}%
}{%
\ifx\stack@secnumdepth\@empty
\PackageWarning{tocvsec2}{There is no previous value for secnumdepth}%
\else
\afterassignment\gobble@secnumdepth
\expandafter\c@secnumdepth\expandafter\numexpr\stack@secnumdepth\@nil
\fi
}%<--- Was missing
}
\makeatother

\let\ORIchapter\chapter
\RenewDocumentCommand\chapter{som}{%
\IfBooleanTF{#1}
{
\setsecnumdepth{none}%
}
{%
\resetsecnumdepth*
}
\IfNoValueTF{#2}
{\ORIchapter{#3}}
{\ORIchapter[#2]{#3}}%
}

\setsecnumdepth{chapter}
\begin{document}
\chapter{A chapter}
\end{document}


What happens? In amsgen.sty, \@ifstar receives a new definition based on \new@ifnextchar, which does not ignore spaces like it happens for \@ifnextchar.

The missing % in the definition causes \new@ifnextchar not seeing the * as the next character, but a space.

The problem shows because glossaries loads amsgen via amsmath.

-
OK.I'll let know Will Robertson, the current tocvsec2 maintainer. – Denis Bitouzé Mar 9 '14 at 21:08
Who would guess: you found a missing % :-) – David Carlisle Mar 10 '14 at 0:54
@DavidCarlisle: not me, for sure! :) – Denis Bitouzé Mar 11 '14 at 7:17