Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
3  
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
add comment

1 Answer

up vote 10 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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