17

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

2 Answers 2

13

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}
2
  • Nice! url is by D. Arsenau, after all. :)
    – egreg
    Feb 16, 2013 at 14:57
  • 1
    That's ... egregious. ;-)
    – lockstep
    Feb 16, 2013 at 14:59
15

Define a special behavior for the slash:

\documentclass{article}

\usepackage{url}
\makeatletter
\def\Url@twoslashes{\mathchar`\/\@ifnextchar/{\kern-.2em}{}}
\g@addto@macro\UrlSpecials{\do\/{\Url@twoslashes}}
\makeatother

\begin{document}

\urlstyle{sf}\url{http://tex.stackexchange.com/4427}

\bigskip

\urlstyle{rm}\url{http://tex.stackexchange.com}

\bigskip

\urlstyle{tt}\url{http://tex.stackexchange.com}


\end{document}

enter image description here

A more complex definition that avoids kerning in tt style is

\def\Url@twoslashes{%
  \mathchar`\/% print a slash
  \expandafter\ifx\UrlFont\ttfamily
    \expandafter\@gobble % do nothing if style is tt
  \else
    \expandafter\@firstofone % check if a / follows and insert a kern
  \fi
  {\@ifnextchar/{\kern-.2em}{}}
}

Not completely foolproof if silly definitions of \UrlFonts are made for some new style.

4
  • \def\Url@twoslashes{\@ifnextchar/{\mathrlap{\mathchar`\/}\phantom{.}}{\mathchar`\/}}
    – yo'
    Feb 17, 2013 at 9:01
  • @yo' The original code in that answer worked fine. With your patch, I had to include \usepackage{mathtools}, but I still get Missing number, treated as zero.. With LNCS class, I could not get your real answer running, but this here works fine. I should provide a MVE for checking though :\.
    – koppor
    Mar 18, 2016 at 7:31
  • Is it possible, that this solution prevents URLs be wrapped at /?
    – koppor
    May 23, 2016 at 14:36
  • @koppor Quite possible.
    – egreg
    May 23, 2016 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.