What is the proper way to define a wrapper to a command which takes an optional argument (such as \chapter
)? I mean a wrapper, not a clone, such that we can inject code into the wrapper without meddling with the command of which it is a wrapper.
This is how I do it. Is there a better way to do that? (I owe the conditional clause to egreg here.)
\documentclass{book}
\newcommand\mychapter[2][] {\if\relax\detokenize{#1}\relax
\chapter{#2}
\else
\chapter[#1]{#2}
\fi}
\begin{document}
\tableofcontents
\mychapter[Short title]{Long title}
\end{document}
let\mychapter\chapter
will clone it, so that \mychapter
won't be a wrapper to \chapter
.
\newcommand{\mychapter}{\chapter}
do what you want?#1
and#2
in addition to passing them forward to\chapter
. A wrapper would be able to do it, but your clone would not.