1

I have a main file which calls several chapters with \include. For each chapter I rename the short name of the chapter with a renewcommand. This works fine but it doesn't anymore with RenewDocumentCommand. Here are 3 MWE (they are really short but it is enough to understand the problem):

The first one works fine:

\documentclass[10pt,a4paper]{book}

\newcommand\chap{}

\begin{document}

\renewcommand{\chap}{Premier chapitre}
\chapter{Chapitre 1}\label{Chapitre:\chap}

Début du chapitre 1

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay

\renewcommand{\chap}{Deuxième chapitre}
\chapter{Chapitre 2}\label{Chapitre:\chap}

Début du chapitre 2

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay
\end{document}

Now, the second one with RenewDocumentCommand:

\documentclass[10pt,a4paper]{book}

\NewDocumentCommand\chap{}{}

\begin{document}

\RenewDocumentCommand{\chap}{}{Premier chapitre}
\chapter{Chapitre 1}\label{Chapitre:\chap}

Début du chapitre 1

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay

\RenewDocumentCommand{\chap}{}{Deuxième chapitre}
\chapter{Chapitre 2}\label{Chapitre:\chap}

Début du chapitre 2

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay
\end{document}

As you can see there are two problems:

  • I have a warning: Label `Chapitre:\chap ' multiply defined.
  • the \chap command works fine but the reference \ref{Chapitre:\chap} doesn't work. I don't understand why it doesn't work whereas \chap works

So I tried to replace \label{Chapitre:\chap} by \label{Chapitre:Premier chapitre} and this works fine:

\documentclass[10pt,a4paper]{book}

\NewDocumentCommand\chap{}{}

\begin{document}

\RenewDocumentCommand{\chap}{}{Premier chapitre}
\chapter{Chapitre 1}\label{Chapitre:Premier chapitre}

Début du chapitre 1

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay

\RenewDocumentCommand{\chap}{}{Deuxième chapitre}
\chapter{Chapitre 2}\label{Chapitre:Deuxième chapitre}

Début du chapitre 2

\chap\quad\ref{Chapitre:\chap}%to see if everything is okay
\end{document}

So, is there a real difference between renewcommand and RenewDocumentCommand? And am I doing something wrong?

PS: In my real files,

  • the \newcommand{\chap}{} is in my main file
  • the \renewcommand{\chap}{...} is in each chapter
5
  • 3
    Macros defined by \(Re)NewDocumentCommand are robust and don't expand in certain situations, as you've found out.
    – campa
    Commented Mar 23, 2023 at 9:14
  • Seems to work if you use \NewExpandableDocumentCommand and \RenewExpandableDocumentCommand. Not sure if that is by chance.
    – daleif
    Commented Mar 23, 2023 at 10:30
  • @daleif no, by design Commented Mar 23, 2023 at 10:48
  • 1
    For simple data-storage inside a macro don't use \(Re)New(Expandable)DocumentCommand, stick to \(re)newcommand for this. There are good reasons to switch to the new interface, but not for usages like this.
    – Skillmon
    Commented Mar 23, 2023 at 11:00
  • Thank you for your answers. I thought \newcommand and \NewDocumentCommand were identical but I was wrong. So I will stick to (re)newcommand for data storage, as @Skillmon recommended me
    – Didier
    Commented Mar 23, 2023 at 12:56

0

Browse other questions tagged .