I want to patch \label, involving #1, and it seems that with hyperref, you have to do so after \begin{document} or using \AtBeginDocument. (The code should work without hyperref, too.) So this is what I tried, and it fails:
\documentclass{article}
\usepackage{hyperref}
\usepackage{xpatch}
% Attempt 1 - does nothing
% \pretocmd{\label}{#1}{}{err}
% Attempt 3 - fails (err)
% \AtBeginDocument{\pretocmd{\label}{#1}{}{err}}
% Attempt 4 - fails (err)
\AtBeginDocument{\pretocmd{\label}{##1}{}{err}}
\begin{document}
% Attempt 2 - works, inelegant
% \pretocmd{\label}{#1}{}{err}
Label name is \label{mylabel}.
\end{document}
#1in\pretocmd. What should#1do there? -- it is not accessible there! – Jan 30 '17 at 10:49\AtBeginDocumentsaves its argument for later use, it tokenizes its argument. In this process#gets doubled to##, which is what\pretocmdlater adds to the\labelcommand. – gernot Jan 30 '17 at 10:50showkeysetc would not work. – bers Jan 30 '17 at 11:16\pretocmdonce and accessing of#1, but it failed, because I did not realize that the command hat moving arguments. I've forgotten about that this was the cause of the failure – Jan 30 '17 at 11:24