# Incompatibility of the ocg-p package with the ocgcolorlinks option of the hyperref package

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
\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):

-
I have edited your question to include what I consider to be a minimal working example (MWE) that illustrates your problem. In future, please try to do this yourself. Also, we don't usually put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  cyberSingularity Mar 25 '13 at 10:17

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}

% Fix incompability between ocg-p and the hyperref option ocgcolorlinks
% (http://tex.stackexchange.com/a/104227)
\makeatletter
\Hy@AtBeginDocument{%
\begingroup
\def\Hy@ocgcolor{##1}%
\setbox0=\hbox\bgroup\color@begingroup
}%
\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
}%
}%
}
\Hy@AtBeginDocument{%
\begingroup
\def\Hy@ocgcolor{##1}%
\setbox0=\hbox\bgroup\color@begingroup
}%
\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
\else
\def\Hy@temp{hdvipdfm}
\ifx\Hy@driver\Hy@temp
\else
\def\Hy@temp{hxetex}
\ifx\Hy@driver\Hy@temp
\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


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
}
% see above
}
\def\Hy@temp{hpdftex}
\ifx\Hy@driver\Hy@temp
\else
\def\Hy@temp{hdvipdfm}
\ifx\Hy@driver\Hy@temp
\else
\def\Hy@temp{hxetex}
\ifx\Hy@driver\Hy@temp
\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.

-