## LaTeX forum ⇒ General ⇒ Problem creating a macro that accepts arguments containing a hash/sharp symbol ("#")

LaTeX specific issues not fitting into one of the other forums of this category.
cypherpunks
Posts: 4
Joined: Sat Dec 10, 2016 12:26 pm

### Problem creating a macro that accepts arguments containing a hash/sharp symbol ("#")

This is a mostly working example of how to put URLs in a footnote and hyperlink them in the body of a document:

Notice that the “#” in the URL had to be escaped with a backslash. This should not be necessary. Note that the \href and \url commands themselves do not require this, so it should be possible to write a macro that also does not require escaping the “#”. Notice all the commented out code. These are all failing attempts to remedy the problem.

Ijon Tichy
Posts: 274
Joined: Mon Dec 24, 2018 10:12 am
This cannot be done using \newcommand{\foo}[1]. You have to first change \catcode (not only of the hash but of several other characters too) and then read the argument and process it. Last but nut least you have to restore the original catcodes. This is fare away from being trivial. A large portion of the (well documented) code of \Url, \Url@y and \Url@x in package url is for reading the argument verbatim and convert it so something that can be handled by itself and by hyperref. Same for hyperref's \href. Here hyperref uses \hyper@normalise to open a group and change catcodes before reading the verbatim argument.

If it would be easy to do something like this, it would be documented in the user manuals instead of recommending to use \urldef. If you really want to implement such a command you have to enter a much lower level.

In the following example I use a temporary redefinition of an internal hyperref command to get the verbatim URL and use it as an argument of another command. However, I do not recommend to use such hacks! They can fail under several circumstances, e.g., if a driver or hyperref itself changes the use of the internal command or the internal command itself!

cypherpunks
Posts: 4
Joined: Sat Dec 10, 2016 12:26 pm
Thanks for the detailed reply. That code is well over my head. I'll use it in one document so I have a copy of it to refer back to, but I guess I'll use this simpler version on a regular basis:
\newcommand{\footnoteurl}[2]{\href{#1}{#2}\footnote{\tiny\nolinkurl{#1}}}