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
\(Re)NewDocumentCommandare robust and don't expand in certain situations, as you've found out. – campa Mar 23 '23 at 09:14\NewExpandableDocumentCommandand\RenewExpandableDocumentCommand. Not sure if that is by chance. – daleif Mar 23 '23 at 10:30\(Re)New(Expandable)DocumentCommand, stick to\(re)newcommandfor this. There are good reasons to switch to the new interface, but not for usages like this. – Skillmon Mar 23 '23 at 11:00