9

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).

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

1 Answer 1

10

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.

3
  • OK.I'll let know Will Robertson, the current tocvsec2 maintainer. Commented Mar 9, 2014 at 21:08
  • Who would guess: you found a missing % :-) Commented Mar 10, 2014 at 0:54
  • @DavidCarlisle: not me, for sure! :) Commented Mar 11, 2014 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .