7

There seems to be a problem using a hyphen in a URL inside a fancyvrb Verbatim environment when using the hyperref package.

The Problem

The following document:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{hyperref}

\begin{document}
\begin{Verbatim}[commandchars=\\\{\}]
\url{http://a-z.example}
\end{Verbatim}
\end{document}

produces the following text in the output pdf:

http://a\unhbox\voidb@x\kern\z@\char`\discretionary{-}{}{}z.example

Less than ideal workaround

The best workaround I can come up with is to replace the url command with \href{http://a%2dz.example}{http://a-z.example}. Note the hyphen needs to be percent-encoded. This is not ideal.

Is there a way to solve this problem? Or at least a better workaround?

0

2 Answers 2

3

In verbatim environments the - is made active in order to break ligatures, but \url, among its checks doesn't take care of this.

You can fix the behavior in this way:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{hyperref}

\makeatletter
\let\ORIGhyper@normalise\hyper@normalise
\def\grigg@hyper@normalise{%
  \begingroup\begingroup\lccode`\~=`\-
   \lowercase{\endgroup\edef~}{\string-}\expandafter\@gobble\ORIGhyper@normalise}
\def\fixurl{\let\hyper@normalise\grigg@hyper@normalise}
\makeatother

\begin{document}
\begin{Verbatim}[commandchars=\\\{\},formatcom=\fixurl]
\url{http://a--z.example}
\end{Verbatim}
\end{document}

enter image description here

With a simplistic \detokenize you lose the possibility of using % in URLs: check that

\url{http://a--z.example.com/%7Exyz}

works as expected, which wouldn't with

\url{\detokenize{http://a--z.example.com/%7Exyz}}

as suggested by Werner.

3

Here's one work-around:

enter image description here

\documentclass{article}
\usepackage{fancyvrb,hyperref}% http://ctan.org/pkg/{fancyvrb,hyperref}
\newcommand{\URL}[1]{\url{\detokenize{#1}}}

\begin{document}
\begin{Verbatim}[commandchars=\\\{\}]
\URL{http://a-z.example}
\end{Verbatim}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .