\documentclass{article}
\usepackage[%latextoc,
noxcolor,
usetemplates,pdftex]{web}
%\usepackage{times}
\screensize{4.875in}{6.5in}
\margins{12pt}{24pt}{24pt}{24pt}

\definecolor{pozadi}{rgb}{1,1,0.9}
\pagecolor{pozadi}

%\scrollmode
\def\MyPersonalMacro{Hello world}

\title{OCGtools and preview} 

\optionalPageMatter{
  \begin{center}
    \colorbox{yellow}{\begin{minipage}{0.5\linewidth} This is a demo
        file to test cooperation between ocgtools and preview
        packages. Go to the Section \ref{page} on page \pageref{page}
        and put you mouse to the question marks following referrences
        to figure and equation. The figure and equation should appear
        in minilayer at the same page.

        The concept is DoItYourself, see the source
        \texttt{.tex} file and script \texttt{ocgtools-preview.sh}.
      \end{minipage}}
  \end{center}
}

\def\title#1{}
\usepackage[minimouseover]{ocgtools}


\ifx\ocgpreview\undefined\def\ocgpreview#1#2{#1}\fi

 \ocgpreview{\AtBeginDocument{
 \gdef\maplabeltopage#1#2{\expandafter\xdef\csname ocgtools@page@#1\endcsname{#2}
\message{^^J OCG: page #2}}
 \gdef\savemaplabels#1#2#3#4{\xdef\ocgtoolstemp{#2}}
 \gdef\maplabelstoall#1#2{\savemaplabels#2 \maplabeltopage{#1}{\ocgtoolstemp}}
 \input preview.labels
 \let\orieqref\eqref
 \let\oriref\ref
 \def\eqref#1{(\oriref{#1})%
 \expandafter\ifx\csname  ocgtools@page@#1\endcsname\relax\else
  \,{\ocgminitext{$^{\text{?}}$}%
  {\includegraphics[page=\csname ocgtools@page@#1\endcsname]{preview-temp}}}%
  \fi}%
 \def\ref#1{\oriref{#1}%
 \expandafter\ifx\csname  ocgtools@page@#1\endcsname\relax\else
    \,{\ocgminitext{$^{\text{?}}$}%
   {\includegraphics[page=\csname ocgtools@page@#1\endcsname]{preview-temp}}}%
  \fi}%
 }}{\AtBeginDocument{
 \newwrite\refsfile
 \pagecolor{lightblue}
 \immediate\openout\refsfile=preview.labels
 \let\orilabel\label
 \def\label#1{\orilabel{#1}\write\refsfile{\string\maplabeltopage{#1}{\thepage}}}
 }}

\input ocgtools-test.tex

\end{document}
