2

I need the following behavior for web links from my pdf. I've spend a fair amount of time trying to make the changes play nicely together, with no success

  • urls that don't start with www should resolve correctly. Without some kind of intervention, my pdf frequently (but not always) thinks they are file:// rather than http:// . I've written a macro to supply the http:// prefix explicitly for \href, without showing it in the document.

  • long urls must be split over multiple lines. There are few of these that don't split naturally and I am prepared to deal with them one at a time with \UrlBreak, as at Making url package customized break settings apply only to specific urls

In the following M(N)WE the links all work, but the splitting sometimes doesn't, and \detokenize replaces the underscores with dots. I seem not be able to use the font encoding suggested at Strange behaviour with \detokenize and underscore

I tried to replace \detokenize with my own macro to replace _ with \_ in my rewritten \url but couldn't get it to work.

Any complete solution would be welcome. There may well be one starting from scratch without trying to patch mine. I've tried that long enough.

I'm going to watch the blood moon eclipse now. Perhaps when I return (tomorrow?) I'll find an answer here.

\documentclass[10pt]{article}

\usepackage[showframe,text={5in,3in}]{geometry}

\usepackage{microtype} % since it's in my document

%\usepackage[T1]{fontenc} % fails to compile 

\usepackage[pdftex,breaklinks]{hyperref}
\usepackage{xstring}

\let\realurl\url
\renewcommand{\url}[1]{%
\IfBeginWith{#1}{www}%
{\realurl{#1}}%{
{\href{http://#1}{\detokenize{#1}}}%
}

\begin{document}

\begin{itemize}
\item Url without www: \url{donnellycolt.com}. This works as is in
  Firefox, but would fail from the pdf without the explicit http
  prefix I generate. 
\item Url without www: \url{tex.stackexchange.com/questions}. This one
  would work from the pdf without the explicit http.
\item Long url with underscore, no www:
\url{en.wikipedia.org/wiki/List_of_humorous_units_of_measurement}. It
doesn't break, and the underscores are dots.
\item Long url to break, without www:
{\def\UrlBreaks{\do\-\do\d}%
\url{earthsky.org/science-wire/recycling-is-good-for-the-environment-as-well-as-the-brain}}
\item Long url to break, without www:
{\def\UrlBreaks{\do\/\do\=\do\0\do\A}%
\url{answers.yahoo.com/question/index?qid=20080914095156AAR7NXU}}. This
one would break if I didn't prefix the http://. It would work, too.
\item Long url to break, with www:
{\def\UrlBreaks{\do\/\do\=\do\P\do\U\do\S\do\H\do\A}%
\url{www.adfg.alaska.gov/index.cfm?adfg=PersonalUsebyAreaSouthcentralHerringandHooligan.regs}}.

\end{itemize}
\end{document}

enter image description here

1 Answer 1

3

Here's an answer that seems to work. I redefine \url to invoke \href to generate the http:// prefix all the time for the actual link. Then I use the \path macro from the url package to escape the underscore in the visible url. \path seems to consult \UrlBreak so the splitting problem solves itself.

\renewcommand{\url}[1]{%
\href{http://#1}{\path{#1}}%
}

I know this will fail if I accidentally leave an explicit http:// in a url I paste into my document, but that's easy to check for with grep.

If it might potentially break something I hope someone will tell me.

You must log in to answer this question.

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