Context: This question is a follow-up of Handling _ (underscores) in a macro's comma-separated list of arguments. In this question I create the macro \codecitep that takes a comma-separated list as argument, and prints these keys generating hyperref links.
Problem: My MWE works great, however, I did not succeed in implementing it in my real document. I indeed get the following error:
! LaTeX Error: \do undefined.
I have tracked down the cause, which is... the \chapter command:
\codecitep commands positioned before the first \chapter compile well, but those after don't.
Solution: (Yep, the solution comes before the question!) As suggesting in Cannot find LaTeX Error, reseting \do definition after the error-causing command makes compilation work.
However, adding \def\do{} after each \chapter command is way to dirty, and I don't think redefining/patching KOMA's \chapter command with \def\do{} as suffix is a sustainable solution.
Question: What makes \chapter change \do's definition, and how to avoid it impacts my own \codecitep macro?
\documentclass{scrbook}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{lipsum}
\newcommand{\codecitep}[1]{% cf. https://tex.stackexchange.com/a/87423/64454
[%
\def\nextitem{\def\nextitem{, }}% Separator
\renewcommand*{\do}[1]{\nextitem{\hyperref[code:##1]{##1}}}% How to process each item
\docsvlist{#1}% Process list
]%
}
\begin{document}
\section{Body before chapter}
A sentence with one code-citation only \codecitep{key1}.
Another sentence with two code-citations and followed by dummy text \codecitep{key1, key2}.
\chapter{Chapter title}
%\def\do{}% <----- uncomment to make the error disappear
\section{Body after chapter}
A sentence with one code-citation only \codecitep{key1}.
Another sentence with two code-citations and followed by dummy text \codecitep{key1, key2}.
\lipsum[1-2]
\section{Appendix}
\lipsum[3]
\subsection{key1}
\label{code:key1}
\label{code:a_123}
\lipsum[4]
\subsection{key2}
\label{code:key2}
\label{code:bb_456}
\lipsum[5]
\end{document}
\def\doin your command. – Ulrike Fischer Apr 30 '17 at 06:40