[Tex/LaTex] How to reduce the space between two slashes when using \url

characterskerningspacingurls

This is a follow-up question to Space between the two slashes in “http://” too big. Bianca Lobo's answer (defining a \twobar command with proper kerning) is nice; however, it doesn't work in the argument of the \url command of the package of the same name. How can one reduce the space between two slashes when using \url? (I'm interested in a solution for sans-serif as well as for roman fonts.)

\documentclass{article}

\usepackage{url}

\newcommand{\twobar}{/\kern-0.2em/}

\begin{document}

\sffamily
\urlstyle{sf}

http:\twobar www.tex.stackexchange.com

\url{http://www.tex.stackexchange.com}

\url{http:\twobar www.tex.stackexchange.com}

\bigskip

\rmfamily
\urlstyle{rm}

http:\twobar www.tex.stackexchange.com

\url{http://www.tex.stackexchange.com}

\url{http:\twobar www.tex.stackexchange.com}

\end{document}​

enter image description here

Best Answer

The url package has a way how to do search-and-replace in the url strings. See the self-documented code. We tweak into \Url@acthash because it's called by \url exactly at the right place.

enter image description here

\documentclass{article}

\pagestyle{empty}

\usepackage{url}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\makeatletter
% nice "//"
\newcommand{\twobar}{/\kern-0.2em/}
% store original \Url@acthash
\let\orig@Url@acthash\Url@acthash
% make new \Url@acthash that acts "//" as well
\let\new@Url@acthash\Url@acthash
\g@addto@macro{\new@Url@acthash}{\Url@Edit\Url@String{//}{\twobar}}
% make \urlstyle use the original \Url@acthash
\let\orig@urlstyle\urlstyle
\def\urlstyle{\let\Url@acthash\orig@Url@acthash\orig@urlstyle}
% make selected url styles use the new \Url@acthash
\g@addto@macro{\url@rmstyle}{\let\Url@acthash\new@Url@acthash}
\g@addto@macro{\url@sfstyle}{\let\Url@acthash\new@Url@acthash}
\makeatother

\begin{document}

\sffamily
\urlstyle{sf}
http:\twobar www.tex.stackexchange.com
\par
\url{http://www.tex.stackexchange.com}
\par
\url{http:\twobar www.tex.stackexchange.com}
\bigskip

\ttfamily
\urlstyle{tt}
http:\twobar www.tex.stackexchange.com
\par
\url{http://www.tex.stackexchange.com}
\par
\url{http:\twobar www.tex.stackexchange.com}
\bigskip

\rmfamily
\urlstyle{rm}
http:\twobar www.tex.stackexchange.com
\par
\url{http://www.tex.stackexchange.com}
\par
\url{http:\twobar www.tex.stackexchange.com}
\bigskip

\end{document}
Related Question