9

I want the print version of a document have links or URLs written out but the online version have the abbreviated tag/name. So the switch, set at the beginning, will choose between two alternatives, e.g.

    \url{http://tex.stackexchange.com/questions/ask}
    \href{http://tex.stackexchange.com/questions/ask}{Questions}
lockstep
  • 250,273
Maesumi
  • 9,059

2 Answers2

7

Besides the links, there might be other things you need to set for screen viewing or printing, so you will need some sort of a switch. This would normally go in the preamble of the document as shown below.

I will use the ifthen package for the boolean as people still find its syntax useful:

\usepackage{ifthen}
\newboolean{ForPrinting}

You then set the various parameters as:

\ifthenelse{\boolean{ForPrinting}}{}{}

What you normally need to take care of, is margins, link colors and the typing of urls. For the links you can define a command as:

\newcommand\link[2][1]{%
  \ifthenelse{\boolean{ForPrinting}}{\url{#1}}
    {\href{#1}{#2}}
}

The following MWE illustrates the technique and some typical values for settings:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{ifthen}
\newboolean{ForPrinting}

%\setboolean{ForPrinting}{true}

%% Initialize values to ForPrinting=false
\newcommand{\Margins}{hmarginratio=1:1}     % Symmetric margins
\newcommand{\HLinkColor}{blue}              % Hyperlink color
\newcommand{\PDFPageLayout}{SinglePage}
%% Re-set if ForPrinting=true
\ifthenelse{\boolean{ForPrinting}}{%
  \renewcommand{\Margins}{hmarginratio=2:3} % Asymmetric margins
  \renewcommand{\HLinkColor}{black}         % Hyperlink color
  \renewcommand{\PDFPageLayout}{TwoPageRight}
  \usepackage[body={5.25in,8.4in},\Margins]{geometry}

\usepackage[pdftex,
  hyperfootnotes=false,
  pdftitle={Project Name},
  pdfauthor={Your name},
  pdfkeywords={maths,equations},
  pdfstartview=Fit,    % default value
  pdfstartpage=1,      % default value
  pdfpagemode=UseNone, % default value
  bookmarks=true,      % default value
  linktocpage=false,   % default value
  pdfpagelayout=\PDFPageLayout,
  pdfdisplaydoctitle,
  pdfpagelabels=true,
  bookmarksopen=true,
  bookmarksopenlevel=1,
  colorlinks=true,
  linkcolor=\HLinkColor]{hyperref}}
  \newcommand\link[2][1]{%
    \ifthenelse{\boolean{ForPrinting}}{\url{#1}}
    {\href{#1}{#2}}
   }
\begin{document}
\section{One}
  \link[http://tex.stackexchange.com/questions/ask]{Questions}
\section{Two}
\end{document}
yannisl
  • 117,160
2

It should be as simple as: (the macro is taken almost verbatim from bidi package)

\documentclass{article}
\newif\ifprint
\usepackage{hyperref}
\makeatletter
\begingroup
  \catcode`\$=6 %
  \catcode`\#=12 %
  \gdef\href@$1{\expandafter\href@split$1##\\}%
  \gdef\href@split$1#$2#$3\\$4{%
    \hyper@@link{$1}{$2}{\ifprint$1\else$4\fi}%
    \endgroup
  }%
\endgroup
\makeatother
\newcommand*\PrintOn{\printtrue}
\begin{document}
\PrintOn
This is \href{http://google.com}{Google}
\end{document}
Simurgh12
  • 853