2

I am using hyperlink text formatted with graphicx' \scalebox command with independent factors for horizontal and vertical scaling. When formatting is applied inside hyperref's hyperlinks, the text in resulting PDF is still present in the PDF (may be copied), but became invisible (and also cause further problems when other packages are added that would use such solution).

The obvious workaround would be to switch to hyperlink used inside formatting command. Unfortunately, in actual document \hyperlinks are generated by other package that forces internal formatting (as described in this answer to the original question that led to identifying the problem). Then, relsize's \textscale command is an alternative, but it does not allow for independent scaling in both dimensions, so it cannot fully relace \scalebox.

When ocgx2 package is not used (or is used without ocgcolorlinks option) the internal formatting works properly.

MWE:

\documentclass{article}

\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage[ocgcolorlinks]{ocgx2}

\usepackage{graphicx}
\newcommand{\myfmt}[1]{\scalebox{.8}[.7]{#1}}

\begin{document}

\hypertarget{t1}{Some text}.

\hyperlink{t1}{Unformatted link}.

Desired but not working: \hyperlink{t1}{\myfmt{Formatted (inside) link}}.

Working but not applicable: \myfmt{\hyperlink{t1}{Formatted (outside) link}}.

\end{document}
  • 1
    The link is already invisible if one inserts a \pdfsave/\pdfrestore pair (\newcommand{\myfmt}[1]{{#1\pdfsave\pdfrestore}}). Imho this interrupts the path and ocgx can no longer use the text for clipping. I would drop the idea to use scalebox here and try to implement whatever you want to achieve with proper font commands. – Ulrike Fischer Aug 1 '18 at 15:11
  • 1
    Further experiments indicate that \hyperlink has a problem with graphics in general, not just \scalebox. Even \fbox{...} has problems. – John Kormylo Aug 1 '18 at 16:22
4

Command \ocglinkprotect{...} defined by ocgx2 was provided for such special cases:

\documentclass{article}

\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage[ocgcolorlinks]{ocgx2}

\usepackage{graphicx}
\newcommand\myfmt[1]{\ocglinkprotect{\scalebox{.8}[.7]{#1}}}

\begin{document}

\hypertarget{t1}{Some text}.

\hyperlink{t1}{Unformatted link}.

%%%%%%%%%%%
This works: \hyperlink{t1}{\myfmt{Formatted (inside) link}}.
%%%%%%%%%%%

\end{document}

Other use-cases:

  • graphics inside link text:

    \hyperlink{t1}{Unformatted link text \ocglinkprotect{\includegraphics[width=1cm]{example-image}} unformatted link text cont'ed}
    
  • boxed portions inside link text:

    \hyperlink{t1}{Unformatted link text \ocglinkprotect{\fbox{f-boxed text}} unformatted link text cont'ed}
    

Note that the text argument of \ocglinkprotect{...} doesn't wrap around line breaks. But this should not be an issue, as such portions of the link text that need protection (\scalebox..., \fbox{...}, \includegraphics{...} etc.) are not breakable anyway.

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.