7

When I use the option ocgcolorlinks of the hyperref package together with the package ocg-p (MiKTeX + pdflatex), it looks like the ocgcolorlinks option becomes not working anymore (independently of the order of inclusion of the packages). When ocg-p is excluded from the code, all links become printed again in the default text color.

Is this problem described somewhere? Or, maybe, someone can suggest, what I have to do to combine in one code the option ocgcolorlinks and the package ocg-p?

MWE (provided by cyberSingularity):

\documentclass{article}
%\usepackage{ocg-p}% uncomment this, and print preview to see the problem
\usepackage[ocgcolorlinks]{hyperref}
\begin{document}
    \section{Test}
    See \autoref{next}
    \section{Next}
    \label{next}
    Target.
\end{document}

Print preview when ocg-p loaded (left) and not loaded (right):

ocg-p loaded ocg-p not loaded

10

Both packages, hyperref and ocg-p, try to create optional content groups (OCGs) without knowing from one another. This results in incorrect PDF files (to be more specific, duplicate /OCProperties and /Properties entries), which prevent the OCGs from working as expected.

It is not particularly easy to make the two packages cooperate, as the implementations are completely independent. The following code should work for the current versions hyperref v6.83m and ocg-p v0.4:

\documentclass{article}
\usepackage{ocg-p}
\usepackage[ocgcolorlinks]{hyperref}

% Fix incompability between ocg-p and the hyperref option ocgcolorlinks
% (http://tex.stackexchange.com/a/104227)
\makeatletter
\Hy@colorlinkstrue
\Hy@ocgcolorlinksfalse
\newcommand*{\reenable@ocglinks@pdftex}{%
  \Hy@AtBeginDocument{%
    \def\Hy@colorlink##1{%
      \begingroup
        \def\Hy@ocgcolor{##1}%
        \setbox0=\hbox\bgroup\color@begingroup
    }%
    \def\Hy@endcolorlink{%
      \color@endgroup\egroup
      \mbox{%
        \pdfliteral page{/OC/OCPrint BDC}%
        \rlap{\copy0}%
        \pdfliteral page{EMC/OC/OCView BDC}%
        \begingroup
          \expandafter\HyColor@UseColor\Hy@ocgcolor
          \box0 %
        \endgroup
        \pdfliteral page{EMC}%
      }%
      \endgroup
    }%
  }%
}
\newcommand*{\reenable@ocglinks@dvipdfm}{%
  \Hy@AtBeginDocument{%
    \def\Hy@colorlink##1{%
      \begingroup
        \def\Hy@ocgcolor{##1}%
        \setbox0=\hbox\bgroup\color@begingroup
    }%
    \def\Hy@endcolorlink{%
      \color@endgroup\egroup
      \mbox{%
        \@pdfm@mark{content /OC/OCPrint BDC}%
        \rlap{\copy0}%
        \@pdfm@mark{content EMC/OC/OCView BDC}%
        \begingroup
          \expandafter\HyColor@UseColor\Hy@ocgcolor
          \box0 %
        \endgroup
        \@pdfm@mark{content EMC}%
      }%
      \endgroup
    }%
  }%
}
\def\Hy@temp{hpdftex}
\ifx\Hy@driver\Hy@temp
  \reenable@ocglinks@pdftex
\else
  \def\Hy@temp{hdvipdfm}
  \ifx\Hy@driver\Hy@temp
    \reenable@ocglinks@dvipdfm
  \else
    \def\Hy@temp{hxetex}
    \ifx\Hy@driver\Hy@temp
      \reenable@ocglinks@dvipdfm
    \fi
  \fi
\fi
\@ocgp@newocg{View}{View}{1}{printocg=never,listintoolbar=never}
\@ocgp@newocg{Print}{Print}{0}{printocg=always,listintoolbar=never}
\makeatother

\begin{document}
    \section{Test}
    See \autoref{next}
    \section{Next}
    \label{next}
    Target.
\end{document}

Explanation of the code

We deliberately disable the ocgcolorlinks feature to stop the interference with the OCG management provided by the ocg-p package:

\Hy@colorlinkstrue
\Hy@ocgcolorlinksfalse

Now we re-enable the necessary OCG markup of the hyperlinks by hand. For the three drivers currently supporting ocgcolorlinks (pdftex, dvipdfm and xetex), different code is necessary (cf. hpdftex.def, ll. 292-318, hdvipdfm.def, ll. 280-306, and hxetex.def, ll. 362-388):

\newcommand*{\reenable@ocglinks@pdftex}{%
  % see above
}
\newcommand*{\reenable@ocglinks@dvipdfm}{%
  % see above
}
\def\Hy@temp{hpdftex}
\ifx\Hy@driver\Hy@temp
  \reenable@ocglinks@pdftex
\else
  \def\Hy@temp{hdvipdfm}
  \ifx\Hy@driver\Hy@temp
    \reenable@ocglinks@dvipdfm
  \else
    \def\Hy@temp{hxetex}
    \ifx\Hy@driver\Hy@temp
      \reenable@ocglinks@dvipdfm
    \fi
  \fi
\fi

At last, we create the optional content groups View and Print with the necessary visibility options using ocg-p:

\@ocgp@newocg{View}{View}{1}{printocg=never,listintoolbar=never}
\@ocgp@newocg{Print}{Print}{0}{printocg=always,listintoolbar=never}

Like this, the OCGs are introduced correctly to the PDF viewer, avoiding misguiding duplicate definitions.

1

Package ocgx2, v0.17 [2016/03/22], reimplements hyperref's ocgcolorlinks option, borrowing from diabonas' code. Moreover, it ports Layers and the ocgcolorlinks option to other engines where they haven't been available before.

Thus, to have both, coloured links that print in the colour of the surrounding text and working PDF Layers, just say

\usepackage{hyperref} % do NOT set [ocgcolorlinks] here!
\usepackage[ocgcolorlinks]{ocgx2}

Example:

\documentclass{article}

\usepackage{hyperref} % Do NOT set [ocgcolorlinks] here!
\usepackage[ocgcolorlinks]{ocgx2}

\begin{document}

  \section{Test}
  See \autoref{next}

  \section{Next}
  \label{next}
  Target.

  \begin{ocg}{some layer}{some_layer}{1}
  \noindent Text on a PDF layer
  \end{ocg}

  \noindent\toggleocgs{some_layer}{\fbox{Click}} to toggle visibility.

\end{document}

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.