# Automatically convert \href URLs to citation and bibliography

Using the wonderful hyperref package, I've been writing PDF files that I've been very happy with that contain links to web-based URLs so that while reading the PDF file using a PDF viewer, when a reader clicks the link in the PDF file, her web browser opens up and takes her to the page linked in the PDF. I have a large number of such files that I can't take the time to rework in any time-consuming way.

But now for the first time I need to send printed versions of these documents. Obviously, printing the PDF onto paper distills what would be active hypertext embedded in the PDF (if viewed in a PDF viewer) down to just colored ink on a piece of paper, which causes the URL embedded in the link to be absent from the printed version.

Is there any way (with a minimum of changes in the documents I have already produced) when compiling such a PDF to direct that for the production of this particular PDF (that I intend to send to the printer), instead of making embedded links in the PDF, the document should be produced with citation notes and a bibliography containing nothing but the printed URLs?

Here's the MWE of a TeX file that produces hypertext:

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass[11pt]{article}
\usepackage[margin=2.54cm]{geometry}
\usepackage{tikz}
\usepackage{lipsum,xcolor,graphicx}
\usepackage{booktabs}
\usepackage[latin]{babel}
\usepackage{pullquote}
\usepackage{xcolor}
\usepackage{calc}

\definecolor{my-purple}{RGB}{105,60,94}
\definecolor{my-ltblue}{RGB}{0,92,184}

\colorlet{MyColorA}{my-ltblue}
\colorlet{MyColorB}{my-purple}

\usepackage{fancyhdr}

\usepackage{lastpage}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{eso-pic}
\usepackage{xfrac}
\usepackage{mhchem}

\usetikzlibrary{shapes,backgrounds,calc}

\begin{document}

This is a \href{http://en.wikipedia.org}{link to wikipedia}.
This is a \href{http://search.credoreference.com}{link to Credo}.

\end{document}


Though not strictly necessary for the MWE, I've left in several other packages because I am using them in the PDF documents in question, and I'd like to avoid using something that will conflict with one of these other packages.

My goal would be to add an option to a package I'm already using or else perhaps add a package that will somehow magically compile this file into a PDF that looks something like this:

This is a link to wikipedia[1]. This is a link to Credo[2].

References:
[1] http://en.wikipedia.org
[2] http://search.credoreference.com


Except maybe the [1] citation notes would be superscripted marks or something nice-looking like that. That way, the URL information (that would otherwise be lost upon printing) is preserved in a sane, sensical way for the reader in case they want to take the time to type the URL into a browser (unlikely I know, but at least the option is there).

• Where is pullquote from? Is it necessary at all? – user31729 Oct 9 '14 at 19:31
• Thanks for your comment @ChristianHupfer . I learned about pullquote from tex.stackexchange.com/questions/53073/… – TeXnewbie Oct 9 '14 at 19:53
• pullquote is not a TeXLive package? – user31729 Oct 9 '14 at 19:53
• Can't recall for sure, but I think maybe not. – TeXnewbie Oct 9 '14 at 19:54

The simplest solution would be to (re)define \href with something like:

\renewcommand\href[2]{#2\footnote{#1}}


With this your MWE (using pullquote.sty) produces:

A more complete version would be to have the best of both worlds and use:

\let\realHref=\href
\renewcommand\href[3][\relax]{
\ifx\relax#1\relax\realHref{#1}{#2}%
\else\realHref[#1]{#2}{#3}%
\fi%
\footnote{#2}%
}


This makes \href always print the links as footnotes whilst maintaining the standard functionality of \href for adding links to the pdf file. (The \ifx branches are there to cope with the case when optional arguments are passed to \href.)

Edit One caveat: If you have lots of \href-links on one page then this will create a large mess of footnotes, which is probably not what you want. For example, I use bibtex to add links to the arXiv and journal web pages in my bibliography, so if I was using this then I would want to turn this feature off for my references. This can be done using:

\let\href=\realHref


This will return \href to its standard definition for the rest of the document. Let me know if you would want to turn this "feature" on and off several time in the one document and I'll suggest possible modifications.

• Thanks very much, Andrew. That does seem like a great idea (it works for me in my MWE as you showed), but it seems to be conflicting with my pullquote package. When I run it in a PDF that uses pullquote, the compiler incorrectly complains about a missing \$ that was inserted. – TeXnewbie Oct 9 '14 at 19:47
• I wrote that before I saw your revision. I'll try your more sophisticated version now. – TeXnewbie Oct 9 '14 at 19:48
• That more sophisticated version looks promising, but it's not working in my setup. I'm seeing the compiler complain that \href is an undefined control sequence. Or else maybe it's something in your code that my compiler doesn't like when it sees the \href and tries the substitution. – TeXnewbie Oct 9 '14 at 20:21
• @TeXnewbie Sorry, this was my fault: I had a typo in my macro (there was a \realHRef that should have been \realHref). I have corrected my solution and it is now compatible with the version of pullquote.sty given by (implementing-a-pullquotes-algorithm-in-latex)[tex.stackexchange.com/questions/45958/…. – Andrew Oct 10 '14 at 0:12

This solution is a proposition: Redefining the \href to do a citation for a specific source to be found online, for example a website or a .pdf file, however, the urls have to be added to a .bib file.

I used the biblatex with biber backend, but this can be changed.

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass[11pt]{article}
\usepackage[margin=2.54cm]{geometry}
\usepackage{tikz}
\usepackage{lipsum,xcolor,graphicx}
\usepackage{booktabs}
\usepackage[latin]{babel}
%\usepackage{pullquote}
\usepackage{xcolor}
\usepackage{calc}

\definecolor{my-purple}{RGB}{105,60,94}
\definecolor{my-ltblue}{RGB}{0,92,184}

\colorlet{MyColorA}{my-ltblue}
\colorlet{MyColorB}{my-purple}

\usepackage{fancyhdr}

\usepackage{lastpage}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{eso-pic}
\usepackage{xfrac}
\usepackage{mhchem}
\usepackage{etoolbox}%
\usepackage[backend=biber]{biblatex}

\let\standardhref\href%
\renewcommand{\href}[2]{\cite{#2}}%

\usetikzlibrary{shapes,backgrounds,calc}

\begin{document}

This is a reference to \href{http://en.wikipedia.org}{wikipedia}%
This is a reference to \href{http://search.credoreference.com}{credo}.

\printbibliography

\end{document}


# onlineref.bib

This is a very rudimentary .bib source, just for a quick setup.

@online{wikipedia,
url={http://en.wikipedia.org}
}

@online{credo,
url={http://search.credoreference.com}
}