98

I like having my hyperref hyperlinks a different colour, so it's obvious that they are links. But when I print a copy of the PDF, I don't want the links to be coloured, because you can't click them, so it doesn't matter and it's distracting.

Other than just having a separate "for printing" version of the PDF where I turn off the hyperlinks, is there a way to have the printed version come out without the coloured links?

SamB
  • 2,837
Seamus
  • 73,242

5 Answers5

76

Yes. Use

\usepackage[ocgcolorlinks]{hyperref}

This option must be given when loading hyperref. You cannot give it in \hypersetup.

With this option hypreref will make the colorlinks as Optional Contents Groups (OCG) with /PrintState/OFF and /ViewState/ON. Your pdf reader needs to support OCG.

Also, the default behaviour of hyperref is to draw a coloured box around links. This box does not print.

Martin Heller
  • 11,391
  • 41
    The ocgcolorlinks does indeed colour the links for the screen version and leave them black for the printed version, but it has some downsides: the links cannot break across lines; any spaces within the links are set at their natural width and not stretched or shrunk to match with the surrounding text. Therefore ocgcolorlinks is really only a good option when the links are short, unbreakable, single words. I've been pondering if there is a way to use the power of the soul package to get around these restrictions, but I haven't spent a lot of time on it. – Lev Bishop Oct 22 '10 at 23:42
  • 4
    (Yeah I should have mentioned that I changed the link behaviour from "ugly as hell primary colour boxes" to "sexy subtle dark coloured text") – Seamus Oct 23 '10 at 12:32
  • 5
    Perhaps you could incorporate Lev Bishop's comment into your answer, I think it's worth making clear the option has its downsides... – Seamus Oct 23 '10 at 12:39
  • @LevBishop, could this answer provide a possible approach? (Admittedly, it'd make one very powerful package dependent on another very powerful one...) But basically, it gives a (fairly robust) way of detecting linebreaks, and maybe that could be used to reset the text coloring on each line without needing to box up the text? Failing that, is there any hook to detect line breaks short of monkeying around with the output routine? – Ben Lerner Aug 28 '11 at 02:53
  • 3
    Apropos my older comment, I've actually implemented a hack/patch to hyperref that addresses the two downsides Lev mentioned in his comment. I've emailed the maintainers of hyperref the code; I haven't heard from them yet. I could post the code here if people were interested in testing it themselves, but I make no guarantees to its robustness! :) It should work for any text that appears in a URL, but wouldn't work for, say, rules or graphics content...but those can't go in a URL anyway. – Ben Lerner Feb 11 '12 at 06:09
  • 3
    @LevBishop Wow, this is amazing. This is totally OT but could this OCG feature in principle be used to have a color plot on screen but print a dotted line b/w plot? Would also be useful to print photos in a dedicated high-contrast black and white version. Of course color printers are a problem but I don't think there's a PDF feature to solve that problem. Would still be good for journals that require b/w images but put the same PDF online. – Christian May 17 '12 at 13:53
  • @Ben Lerner - I'd be interested in seeing the patch you wrote. Does it make hyperref always depend on soul, or is there now an extra option that tells hyperref to load soul? – Jason Gross Apr 16 '13 at 13:45
  • 1
    @Jason, I've posted the patch here. It doesn't rely on soul at all. – Ben Lerner Apr 18 '13 at 16:53
  • 1
    @BenLerner Have you had any luck incorporating your patch into hyperref? Isn't Heiko Oberdiek an active member of this site? – petobens Jul 16 '14 at 18:46
  • 1
    @LevBishop OCG colour links which break across lines and pages are now provided by the ocgx2 package. It uses B. Lerners code with some refinements, to allow for page breaks and nested links. See below. – AlexG Apr 11 '17 at 10:41
18

Another option, using ocgx2 package:

\usepackage{hyperref}
\usepackage[ocgcolorlinks]{ocgx2}[2017/03/30]

Similar to hyperref's ocgcolorlinks, but with additional support for

  • dvipdfmx/xelatex
  • dvips
  • breakable links across lines and pages (except dvips), based on Ben Lerners suggestion for line-breakable links

->Example of nested, line and page breaking OCG colour links:

enter image description here enter image description here

AlexG
  • 54,894
  • 7
    Keep in mind that this needs a compatible PDF viewer. Currently it doesn't work on Evince/Okular, but it works e.g. on Adobe Reader and the embedded PDF viewer of Chromium and Chrome. – MakisH Apr 13 '17 at 14:26
14

Try the (experimental) option ocgcolorlinks to the hyperref package. I don't think it's documented in the manual, but see http://www.tug.org/applications/hyperref/ftp/README for a description.

Villemoes
  • 4,131
2

hyperref's option hidelinks will not turn the hyperlinks off, but will just not make them distinguishable from the text.

You still need to make a separate PDF for printing, but at least you can be sure that the links will be printed in text color and break normally.

lblb
  • 3,454
  • 3
    Noteworthy remark, since only AR and Foxit seem to acknowledge the do-print/do-not-print settings that are associated with the two PDF layers created for this purpose. – AlexG Apr 11 '17 at 08:18
1

Try with this:

\newif\ifPrinter\Printerfalse%

\DeclareOption{printer}{\global\Printertrue}

\ifPrinter
\RequirePackage[dvips,colorlinks=false,breaklinks,hidelinks]{hyperref}%
\else%
\RequirePackage[dvips,colorlinks=true,breaklinks,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}%
\fi

While doing "Printer" mode, give the option "printer" in your TeX application file, it will switch off the color links

MadyYuvi
  • 13,693
  • \newif\ifPrinter leaves \Printerfalse initially –  Apr 11 '17 at 11:08
  • 2
    why have you added dvips option (it does nothing useful if latex=dvips is being used and completely breaks hyperref if pdflatex is being used) – David Carlisle Apr 11 '17 at 13:21