# How to print URL typed using \href command

When I print document that contain \href{url}{title} I get title. Is it possible to print the url also but when viewing the document the URL is not shown. If yes how to do it?

Try to print this :

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\href{http://tex.stackexchange.com/questions/178512}{My question on StackExchange}
\end{document}


You will get :

My question on StackExchange


When I view my document I want to have clickable link but when printing my document it should print the URL.

• I'm not sure, what you want to achieve? Can you give an example? Are you just looking for \url{url}? – musicman May 16 '14 at 12:55
• @musicman I have updated my question. Yes I want something like url{url} just when printing the document. – Hunsu May 16 '14 at 13:06
• If you change the text in that way you will change all line and page breaking for the whole document so you need to run tex twice, once to typeset the online version, then change the macro and typeset the print version – David Carlisle May 16 '14 at 13:09
• I think, he wants ONE version: showing the href-title in the pdf reader, but showing the href-url, when he prints it. – musicman May 16 '14 at 13:12
• How is the document supposed to know if it is being viewed or printed? You would have to create 2 documents, possibly using the same code except for something like [draft] or \seturl. – John Kormylo May 16 '14 at 13:23

I have taken a stab at this with the use of PDF's Optional Content Groups, which allow you to control the visibility of text on screen and for print. N.B.: OCG is a feature that is not universally supported; this method will work best (and perhaps only) with Adobe Acrobat.

The idea here is to have the regular text be enhanced with a footnote/marginpar containing the URL itself. I have written this with the assumption that the screen version is the "canonical" version and the print version can deal with some degree of irregularity. In line with this, the on-screen text appears "normally," as if no footnote existed, and the footnotemark is laid on top for print. (Now, placement of additional footnotes/marginpars would be affected in the screen version, as would footnote numbering.) The drawback is that, as many have mentioned in the comments, without reflowing the whole text, you can't even add something as small as a footnote symbol without degrading the goodness of the typesetting. This solution will lay the footnote symbol over top of the text without pushing the text aside, but as a result, it may interfere with the surrounding text and/or appear cramped.

I have added the [hidelinks] option to hyperref to avoid having the link boxes in Acrobat when generating the examples seen below.

## Result

The on-screen version:

When printing:

Rendered as:

The cramped layout of the footnote mark can be easily seen.

## MWE

\documentclass{article}
\usepackage[symbol,side,perpage]{footmisc}
\usepackage{etoolbox} % for patch/preto-cmd
\usepackage{ocg-p} % http://www.ctan.org/pkg/ocg-p
\usepackage{blindtext} % for additional demo text

\makeatletter
\footnotemargin-0.4em\relax % adjust footnote in marginpar
\def\setfn#1{\raisebox{0.1em}{\smash{\hbox to 0pt{\hss\hskip .3em #1\hss}}}} % helper to set footnotemark without interruping flow of text
\patchcmd{\@makefnmark}{\hbox}{\setfn}{}{} % use \setfn
\pretocmd{\@makefnmark}{\begin{ocg}[printocg=always]{PrintVersionOnly}{printonly}{0}}{}{} % start ocg layer
\apptocmd{\@makefnmark}{\end{ocg}}{}{} % end ocg layer
\makeatother

% \printurl{<url>}{<text>}
\newcommand{\printurl}[2]{%
\href{#1}{#2}\footnote{\begin{ocg}[printocg=always]{PrintVersionOnly}{printonly}{0}\noindent\url{#1}\end{ocg}}}

\begin{document}
\blindtext
\printurl{http://tex.stackexchange.com/questions/178512}{My question on StackExchange} is really interesting!
\blindtext

\end{document}


# Edit: list based

To do this with replacing the URL title with the URL itself:

\documentclass{article}
\usepackage{ocg-p} % http://www.ctan.org/pkg/ocg-p
\usepackage{tikz}

% \urlitem{<url>}{<text>}
\newcommand{\urlitem}[2]{
\item\begin{tikzpicture}[baseline=0]
\tikzstyle{nome}=[anchor=base,outer sep=0,inner sep=0,minimum height=.45cm,minimum width=4.4cm]
\begin{ocg}[printocg=never]{ScreenVersionOnly}{screenonly}{1}\node[nome] (p2) {\parbox[t][][t]{\linewidth}{\href{#1}{#2}}};\end{ocg}
\begin{ocg}[printocg=always]{PrintVersionOnly}{printonly}{0}\node[nome] (p1) {\parbox[t][][t]{\linewidth}{#1}};
\end{ocg}\end{tikzpicture}}

\begin{document}

This is a list:
\begin{enumerate}
\item one
\item two
\urlitem{http://tex.stackexchange.com/questions/178512}{My question on StackExchange}
\item four
\item five
\end{enumerate}

\end{document}

• Is it possible to print the url where the title were printed. I want to replace the title by the url. In your example the title appear also. But Thanks your solution is interesting! – Hunsu May 16 '14 at 17:47
• @user230137 as others have mentioned, it is near impossible without re-typesetting the whole document. Otherwise you will have extra space in one form or the other if the title and URL aren't the exact same size. – cslstr May 16 '14 at 17:49
• That's not a problem in my case as the url are not printed inside text but an item list. – Hunsu May 16 '14 at 17:55