This simplest code



    \href{"some URL"}{"long text"}

by compiling with pdflatex puts the "long text" in one line, i.e. with overfull of the line. When the option ocgcolorlinks of the hyperref package is omitted, the text is normally carried over to the next lines.

My question: How can I force the text to be carried over to the next lines with ocgcolorlinks present?


Option ocgcolorlinks does not support links that can be broken across lines. From hyperref's README:

  • Main disadvantage: Links cannot be broken across lines. PDF reference 1.7: 4.10.2 "Making Graphical Content Optional": Graphics state operations, such as setting the color, ..., are still applied. Therefore the link text is put in a box and set twice, with and without color.
| improve this answer | |

Package ocgx2, as of version 0.24, re-implements the ocgcolorlinks option of hyperref, based on B. Lerner's code with some refinements:

Besides OCG colour links that wrap around line breaks, also wrapping at page breaks and nested hyperlinks are supported (only pdfLaTeX/LuaLaTeX, as XeTeX does not support nested links at all), and the issue with empty link text has been resolved.

Example code (open on ShareLaTeX):




  Empty links $\Rightarrow$\href{https://tex.stackexchange.com}{\qquad}$\Leftarrow$ don't flood the page with link colour.\\[2ex]

  Visit \href{https://ctan.org/tex-archive/macros/latex/contrib/ocgx2}{ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 ocgx2 \hypersetup{urlcolor=red}\href{https://ctan.org}{on CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN CTAN} ocgx2 ocgx2 ocgx2 ocgx2 ocgx2}!\\[2ex]

    \normalsize Graphical content, if mixed with text inside the link text, must be protected by enclosing in \verb+\ocglinkprotect{...}+:\\[1ex]

  Visit \href{https://ctan.org/tex-archive/macros/latex/contrib/mwe}{this fancy \ocglinkprotect{\includegraphics[width=1cm]{example-image}} on CTAN!}

enter image description here enter image description here

| improve this answer | |
  • Very promising solution, but for me, when I print preview your ShareLaTeX example in Evince and Okular they still look colored. Am I doing something wrong? – MakisH Apr 13 '17 at 10:01
  • Not with the PDF. It is Evince and Okular which do not acknowledge the printing-settings associated with the PDF layers (OCGs) that contain the viewer and printer versions of the links. If you have time, you could file a bug report to Okular/Evince developers. The same also happens with the original ocgcolorlinks option of hyperref as well as with B. Lerners code. – AlexG Apr 13 '17 at 10:10
  • 1
    Until it gets implemented in Evince/Okular, I found that it works in Chromium/Chrome, which have their own pdf reader. – MakisH Apr 13 '17 at 13:53
  • 1
    What is the purpose of the [2017/03/30] option? – MakisH Apr 13 '17 at 17:29
  • 1
    It spits out a warning if you try to load an older version of the package sty file. (in case your TeX installation is not up to date) – AlexG Apr 13 '17 at 17:37

This answer on TeX.SX by Ben Lerner allows automatic line breaking when ocgcolorlinks is in use, with a hack:

The code […] relies on a quirk of the meaning of Optional Content Groups in PDFs: while color-changing operations will occur regardless of whether the OCG is enabled or not, graphics operations like filling a path are conditional. And, PDFs have a mode where text is used to set the clipping path, rather than to render as actual text

The mechanism has a few issues, notably with empty text and page breaks. The former can likely be auto-detected by typesetting the text in a box first and checking whether it is empty or not. The latter might be solvable using tricks from Reacting to a page break in LaTeX.

In principle, it would also be possible to write a macro which breaks down the text into individual words (or characters, as the OP (LittleLatexNoTex) of that answer tried to do), and wraps each one on a distinct \href{url}{word}. Doing so is non-trivial when macros are used in the text, however, as the LittleLatexNoTex pointed out.

| improve this answer | |
  • 2
    This feature has been made available through the ocgx2 package, v0.24 [2017/03/30]. – AlexG Mar 31 '17 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.