Donald Arsenau's url package provides the macro \urldef, which is very handy when one needs to typeset a URL string in a footnote or endnote. For instance,
\documentclass{article}
\usepackage{url} % for \url and \urldef commands
\usepackage{enotez} % for \endnote and \printendnotes commands
\setenotez{backref = true}
\usepackage{hyperref}
\hypersetup{colorlinks,urlcolor=blue}
\begin{document}
\urldef{\basicurl}\url{https://tex.stackexchange.com/questions}
blabla\endnote{\basicurl}
\clearpage
\printendnotes
\end{document}
generates
on the endnotes page, where the red numeral 1 is a back-reference to the location in the main document where the \endnote command occurred.
So far, so good. Is there a way to generalize this setup by replacing the hard-coded macro name \basicurl with, say, \csname zz\themycounter\endcsname?
Here, \themycounter is the arabic-numeral expression of the value of a counter variable name named mycounter. (By way of background: The document will eventually hold many endnotes, each with one URL string, and it's not feasible to define a new \basicurl directive before each and every \endnote command. I'm trying to make the argument of \urldef unique, in order to play nice with the enotez package. For more background information, please see this query.)
I naively tried
\documentclass{article}
\newcounter{mycounter}
\usepackage{url} % for \url and \urldef commands
\usepackage{enotez} % for \endnote and \printendnotes commands
\setenotez{backref = true}
\usepackage{hyperref}
\hypersetup{colorlinks,urlcolor=blue}
\begin{document}
\stepcounter{mycounter}
\urldef{\csname zz\themycounter\endcsname}\url{https://tex.stackexchange.com/questions}
blabla\endnote{\csname zz\themycounter\endcsname}
\clearpage
\printendnotes
\end{document}
But this generates the following error message:
! Extra \endcsname.
\declare@robustcommand ...e \string #1 \endcsname
l.11 ...url{https://tex.stackexchange.com/questions}
Am I doing something wrong, or can \urldef truly not handle \csname ... \endcsname-type macro commands?
Addendum, posted upon receiving @JairoA.delRio' suggestion to replace
\urldef{\csname zz\themycounter\endcsname}\url{...}
with
\expandafter\urldef\expandafter{\csname zz\themycounter\endcsname}\url{...}
This modification definitely solves the problem mentioned above. However, it leads to another problem, best illustrated by the following, expanded (pun intended) MWE:
\documentclass{article}
\newcounter{mycounter}
\usepackage{url} % for \url and \urldef commands
\usepackage{enotez} % for \endnote and \printendnotes commands
\setenotez{backref = true}
\usepackage{hyperref}
\hypersetup{colorlinks,urlcolor=blue}
\begin{document}
\stepcounter{mycounter}
\expandafter\urldef\expandafter{\csname zz\themycounter\endcsname}\url{https://tex.stackexchange.com/questions}
blabla\endnote{\csname zz\themycounter\endcsname}
\stepcounter{mycounter}
\expandafter\urldef\expandafter{\csname zz\themycounter\endcsname}\url{https://www.sciencedirect.com/science/article/abs/pii/S0304405X00000763}
more blabla\endnote{\csname zz\themycounter\endcsname}
\clearpage
\printendnotes
\end{document}
This is basically the same as before -- except for the additional set of \stepcounter{mycounter}, \urldef, and \endnote directives. Unhappily, the endnotes page now consists of the following:
Oberve that the same URL string is shown twice even though the two instances of \endnote use the equivalent of \zz1 and \zz2, respectively.
Updated question: Is there something else about my code that's not quite right, or have I maybe come across a bug in the enotez package?




\expandafter\urldef\expandafter{\csname zz\themycounter\endcsname}\url{https://tex.stackexchange.com/questions}? – Apr 25 '21 at 07:53