# Removing character from a hyperref link

I would like to suppress a character from a hyperref link that is built via \url, but the textual representation should keep that character. For example, if the character is *, I want \url{http://tex.stackexchange*.com} act as \href{http://tex.stackexchange.com}{http://tex.stackexchange*.com}.

An embarrassingly failing attempt to hack the \hyper@linkurl macro is given in the MWE, just to show some effort.

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\makeatletter
\begingroup
\lccode\~=\*\relax
\lowercase{\def~}{}%
\catcode\*\active
\lccode\~=0\relax
}
\edef\x{\toks0={#2}}\x
\Hy@pstringdef\Hy@pstringURI{\the\toks0}%
\hyper@chars
\leavevmode
attr{%
\Hy@setpdfborder
\ifx\@pdfhightlight\@empty
\else
/H\@pdfhighlight
\fi
\ifx\@urlbordercolor\relax
\else
/C[\@urlbordercolor]%
\fi
}%
user{%
\ifHy@pdfa /F 4\fi
/A<<%
/Type/Action%
/S/URI%
/URI(\Hy@pstringURI)%
\ifHy@href@ismap
/IsMap true%
\fi
\Hy@href@nextactionraw
>>%
}%
\relax
\endgroup
}
\makeatother
\url{http://tex.stackexchange*.com}
\end{document}


This is trying to make the star active, and let it be replaced by an empty group in the assignment to toks0, but it does not work. It also does not feel right to replicate so much code from hyperref. What is a better way to achieve this?

-
Would it be that bad to just create your own \Url macro? –  Qrrbrbirlbel Mar 22 at 23:13
@Qrrbrbirlbel What do you have in mind? A macro that does all the catcode juggling that hyperref does to get everything printed? Would that be any easier? –  mafp Mar 22 at 23:18
Could you, please, write a bit, why is not possible to use the \href version? (Also, I actually would be interested in the purpose of your aim.) –  Speravir Mar 22 at 23:30
@Speravir The \url are coming from bbl files, formed from user provided URLs. So they are pretty much given. The * shall remain in the link text to be treated with \UrlSpecial for line breaking. –  mafp Mar 22 at 23:37
Sorry, I still don’t get it: Why do you want to show wrong links in your document? Just for line breaking? What is bad on a solution similar to tex.stackexchange.com/a/10419 ? –  Speravir Mar 23 at 0:15
show 1 more comment

With the etoolbox package, patching of \hyper@linkurl seems easy and is more reliable than the my custom \URL definition (see a previous version of this answer).

This allows to use # and & in URLs.

## Code

\documentclass{article}
\usepackage{hyperref}
\usepackage{etoolbox}
\makeatletter
\def\strip@star#1*#2\@strip@star{%
\expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
\if\relax\detokenize{#2}\relax\else
\def\strip@star@temp{#2}%
\expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
\expandafter\@strip@star
\fi
}
\def\strip@star@result{}%
\expandafter\strip@star#2*\@strip@star
\Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

\begin{document}
\url{http://tex*.stack*exchange*.com#anchorwithfun:\$&}
\end{document}


## Output

-
Very good. But this approach removes only the first occurrence of the character, right? And it can not really be extended for all occurrences, right? –  mafp Mar 22 at 23:44
@mafp No, it removes every occurrence, hence the recursive call to \strip@star after checking that #2 is not empty. I’ve added a patch for \hyper@linkurl which seems to work. Of course, the URL needs to be brace-less and should not have any special characters in it, otherwise all bets are off. –  Qrrbrbirlbel Mar 22 at 23:49
Whoa, the patching works with URLs containing #,~,=,?. That's it! –  mafp Mar 23 at 0:06