%% Active-DVI
%%
%% This is the original source file advi.sty
%%
%% Package `advi' to use with LaTeX 2e
%% Copyright Roberto Di Cosmo, Jun Furuse, Didier Remy, and Pierre Weis
%% All rights reserved.

%% $Id: advi.sty,v 1.104 2004/09/03 14:31:33 remy Exp $

%% Administrative definitions for Active-DVI, that need updating.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{advi}
         [2004/17/07 v1.75 LaTeX Package for the advi Previewer]

% Which name is ours
\def \advi@name {Active-DVI}
%\def \advi@ActiveDVI {{\sc A}ctive-{\sc DVI}}
\def \advi@ActiveDVI {{\bf Active-}{\sf DVI}}
% Which year of development are we (used for various copyrights message) ?
\def \advi@year {2004}
% Which version is it ?
\def \advi@version {1.60}

%% End of administrative definitions that need updating.

%% Preliminary declarations

\RequirePackage{keyval}
%% Dirty trick: color may take options, which would produce clashes if the 
%% user load the same package with some options. 
%% Load only if unloaded by main document
%%
\AtBeginDocument
  {\@ifundefined {DefineNamedColor}{\usepackage{color}}{}%
   %% two abstract colors set by advi. Defaut to black and white
   \DefineNamedColor{named}{background}{rgb}{1,1,1} %white
    \definecolor{bgcolor}{named}{background}%
    \DefineNamedColor{named}{foreground}{rgb}{0,0,0} %black
    \definecolor{fgcolor}{named}{foreground}%
  }

%% Options

\newif \ifadvi@ignore \advi@ignorefalse
\DeclareOption {ignore}{\advi@ignoretrue}

\ProcessOptions
% \@ifundefined {AdviOptions}{}{\ExecuteOptions {\AdviOptions}}

%% More declarations

% Auxilliary macros

\def \advi@empty{}
\def \advi@ifempty #1{\def \@test {#1}\ifx \@test \advi@empty
  \expandafter \@firstoftwo \else \expandafter \@secondoftwo \fi}
\def \advi@error #1{\PackageError {Advi}{#1}{Type <RETURN> to proceed.}}
\def \advi@warning #1{\PackageWarning {Advi}{#1}}
\def \advi@undefinedenv {\advi@error {Environment \@currenvir\space undefined.
Maybe you mean \@currenvir ing}}
\def \advi@special@ {\advi@ifadvi{\special}{\advi@ignore}}
\def \advi@special #1{\advi@special@ {advi: #1}}
\def \advi@export #1#2{\@ifdefinable #1{\let #1#2}}
\def \advi@exportenv #1#2{%
  \@ifundefined {#1}{\expandafter \let \csname #1\expandafter \endcsname
                     \csname end#1\endcsname }\relax
  \expandafter \@ifdefinable \csname #1\endcsname
  {\expandafter \let \csname #1\expandafter \endcsname \csname #2\endcsname
   \expandafter \let \csname end#1\expandafter \endcsname
        \csname end#2\endcsname}}

\def \advi@ignore #1{}
\def \advi@id #1{#1}

\def \advi@ifadvi {\ifadvi@ignore
  \expandafter \@secondoftwo \else \expandafter \@firstoftwo \fi}
\advi@export \adviignore \advi@ignoretrue
\advi@export \ifadvi \advi@ifadvi

\advi@export \adviversion \advi@version
\advi@export \adviyear \advi@year
\advi@export \adviname \advi@name
\advi@export \ActiveDVI \advi@ActiveDVI

%%% Record and play

\newif \ifadvi@recording
\def \advi@ifrecording {\ifadvi@recording
   \expandafter \@firstoftwo \else \expandafter \@secondoftwo \fi}
\def \advi@ifrecordenv {\ifx \@currenvir \advi@recordenv
   \expandafter \@firstoftwo \else \expandafter \@secondoftwo \fi}

\def \advi@start {start}
\def \advi@startplay {start play}
\define@key{advi@record}{play}[]{\let \advi@do \advi@startplay}
\def \advi@recordenv {advirecord}

\newenvironment{advi@recording}[2][]%
  {\begingroup
     \let \advi@do \advi@start \setkeys{advi@record}{#1}%
     \advi@special {proc=#2 record=\advi@do}%
   \endgroup}
  {\advi@special {proc record=end}}
\newcommand {\advi@record}[3][]{\advi@recording[#1]{#2}#3\endadvi@recording}

\newcommand {\advi@play}[2][]%
   {\begingroup
      \advi@ifempty{#1}{}{\color {#1}}{\advi@special {proc=#2 play}}%
    \endgroup}

\advi@exportenv {advirecording}{advi@recording}
\advi@export \advirecord \advi@record
\let \endadvirecord \advi@undefinedenv
\advi@export \adviplay \advi@play


%%% Embedded applications

\def \advi@embed@name {}
\def \advi@embed@setname #1{%
  \ifx \advi@embed@name \advi@empty \def \advi@embed@name {#1}\else
  \else \advi@error
            {adviembed: parameter name=\advi@embed@name already defined}%
  \fi}
\def \advi@embed@mode{ephemeral}
\def \advi@embed@width{0pt}
\def \advi@embed@height{0pt}
\define@key {advi@embed}{name}{\advi@embed@setname {#1}}
\define@key {advi@embed}{width}%
   {\@tempdima#1\relax \edef \advi@embed@width {\the\@tempdima}}
\define@key {advi@embed}{height}%
   {\@tempdima#1\relax \edef \advi@embed@height {\the\@tempdima}}
\def \advi@definemode #1{%
  \define@key {advi@embed}{#1}[]{%
     \def \advi@embed@mode {#1}\advi@embed@setname {##1}%
  }}
\advi@definemode{ephemeral}
\advi@definemode{persistent}
\advi@definemode{sticky}
\advi@definemode{raw}
\advi@definemode{fake}

\def \advi@embed@ #1#2#3#4#5{%
  \mbox{\advi@special
          {embed name="#1" mode=#2 width=#3 height=#4 command="#5"}%
          {\vbox to #4{\hbox to #3{}}}}}
\def \advi@length #1{\@tempdima #1\relax \the\@tempdima}
\newcommand{\advi@embed}[2][]{%
  \mbox {\setkeys {advi@embed}{#1}%
         \advi@ifempty {\advi@embed@name}{\def \advi@embed@name{anonymous}}{}%
         \advi@embed@ {\advi@embed@name}{\advi@embed@mode}
             {\advi@embed@width}{\advi@embed@height}{#2}}}

\newcommand{\advi@killembed}[2][]%
  {\advi@special {killembed name="#2" signal="#1"}}

\newcommand{\advi@killallembed}[2][]%
  {\advi@special {killallembed name="#2" signal="#1"}}

\newcommand{\advi@mapembed}[1][]%
   {\advi@special {mapembed name="#1"}}
\newcommand{\advi@unmapembed}[1][]{\advi@special {unmapembed name="#1"}}

\newcommand{\advi@mapallembed}[1][]{\advi@special {mapallembed name="#1"}}
\newcommand{\advi@unmapallembed}[1][]{\advi@special {unmapallembed name="#1"}}

\advi@export \adviembed \advi@embed
\advi@export \advimapembed \advi@mapembed
\advi@export \adviunmapembed \advi@unmapembed
\advi@export \advikillembed \advi@killembed
\advi@export \advimapallembed \advi@mapallembed
\advi@export \adviunmapallembed \advi@unmapallembed
\advi@export \advikillallembed \advi@killallembed

%%% Background colors and images

\def \do #1{\expandafter \def \csname advi@geom@#1@\endcsname {#1}}
\do {center}
\do {left}
\do {right}
\do {bottom}
\do {top}
\do {topleft}
\do {topright}
\do {bottomleft}
\do {bottomright}
\let \do \relax
\def \advi@ifnine #1#2#3{\@ifundefined {advi@geom@#1@}{#3}{#2}}

\let \advi@global \relax
\def \advi@global@ {global}
\newif \ifadvi@bgactive

%% Background stuff
\def \advi@bg@do
 {\do \advi@bgcolor \do \advi@bgcolorstart \do \advi@bgcolorstop
  \do \advi@bgxstart \do \advi@bgystart
  \do \advi@bgwidth \do \advi@bgheight
  \do \advi@bgxcenter \do \advi@bgycenter
  \do \advi@bggradient
  \do \advi@bgimage \do \advi@bgalpha \do \advi@bgblend}

\def \advi@auto@ { fit=auto}
\def \advi@bgreset
 {\def \do ##1{\expandafter \advi@global
                  \expandafter \let \noexpand ##1\advi@empty}\advi@bg@do
  \advi@global \let \advi@bgfit \advi@auto@
  \advi@global \advi@bgactivefalse}
\advi@bgreset

\def \advi@none@ {none}
\def \advi@ifnone #1{\def \@test{#1}\ifx \@test \advi@none@
  \let \@test \advi@empty \fi \ifx \@test \advi@empty
  \expandafter \@firstoftwo \else \expandafter \@secondoftwo \fi}

\def \advi@setbg #1#2#3{\advi@ifnone {#1}
  {\advi@global \expandafter \let \noexpand #1\advi@empty}
  {\advi@global \expandafter \def \noexpand #1{ #2=#3}%
   \advi@global \advi@bgactivetrue}}

%% Setting the background solid color
\def \advi@setbgcolor #1{\advi@ifnone {#1}
  {\advi@global \expandafter \let \noexpand #1\advi@empty}
  {\advi@setbg@color #1\relax}}
\def \advi@setbg@color{%
  \@ifnextchar[\advi@setbg@undeclared\advi@setbg@declared}
\def \advi@setbg@declared #1\relax{%
  \@ifundefined{\string\color @#1}%
    {\c@lor@error{`#1'}}%
    {\advi@global \edef \advi@bgcolor
         { color="\csname \string \color@#1\endcsname"}%
     \advi@global \advi@bgactivetrue}}
% Apparently, named color may start with white space.
% \def \advi@nospace@tmp #1#2\relax {\def \advi@tmp {#1#2}}
\def \advi@setbg@undeclared[#1]#2\relax{%
  \@ifundefined{color@#1}%
    {\c@lor@error{model `#1'}}%
    {\csname color@#1\endcsname\advi@tmp {#2}%
%     \expandafter \advi@nospace@tmp \advi@tmp\relax
     \advi@global \edef \advi@bgcolor{ color="\advi@tmp"}%
     \advi@global \advi@bgactivetrue}}

%% Setting the gradient start color
\def \advi@setbgcolorstart #1{\advi@ifnone {#1}
  {\advi@global \expandafter \let \noexpand #1\advi@empty}
  {\advi@setbg@colorstart #1\relax}}
\def \advi@setbg@colorstart{%
  \@ifnextchar[\advi@setbgstart@undeclared\advi@setbgstart@declared}
\def \advi@setbgstart@declared #1\relax{%
  \@ifundefined{\string\color @#1}%
    {\c@lor@error{`#1'}}%
    {\advi@global \edef \advi@bgcolorstart
         { colorstart="\csname \string \color@#1\endcsname"}%
     \advi@global \advi@bgactivetrue}}
\def \advi@setbgstart@undeclared[#1]#2\relax{%
  \@ifundefined{color@#1}%
    {\c@lor@error{model `#1'}}%
    {\csname color@#1\endcsname\advi@tmp {#2}%
     \advi@global \edef \advi@bgcolorstart{ colorstart="\advi@tmp"}%
     \advi@global \advi@bgactivetrue}}

%% Setting the gradient stop color
\def \advi@setbgcolorstop #1{\advi@ifnone {#1}
  {\advi@global \expandafter \let \noexpand #1\advi@empty}
  {\advi@setbg@colorstop #1\relax}}
\def \advi@setbg@colorstop{%
  \@ifnextchar[\advi@setbgstop@undeclared\advi@setbgstop@declared}
\def \advi@setbgstop@declared #1\relax{%
  \@ifundefined{\string\color @#1}%
    {\c@lor@error{`#1'}}%
    {\advi@global \edef \advi@bgcolorstop
         { colorstop="\csname \string \color@#1\endcsname"}%
     \advi@global \advi@bgactivetrue}}
\def \advi@setbgstop@undeclared[#1]#2\relax{%
  \@ifundefined{color@#1}%
    {\c@lor@error{model `#1'}}%
    {\csname color@#1\endcsname\advi@tmp {#2}%
     \advi@global \edef \advi@bgcolorstop{ colorstop="\advi@tmp"}%
     \advi@global \advi@bgactivetrue}}

\define@key {advi@bg}{color}[]{\advi@setbgcolor{#1}}
\define@key {advi@bg}{colorstart}[]{\advi@setbgcolorstart{#1}}
\define@key {advi@bg}{colorstop}[]{\advi@setbgcolorstop{#1}}
\define@key {advi@bg}{xstart}[]{\advi@setbg{\advi@bgxstart}{xstart}{#1}}
\define@key {advi@bg}{ystart}[]{\advi@setbg{\advi@bgystart}{ystart}{#1}}
\define@key {advi@bg}{width}[]{\advi@setbg{\advi@bgwidth}{width}{#1}}
\define@key {advi@bg}{height}[]{\advi@setbg{\advi@bgheight}{height}{#1}}
\define@key {advi@bg}{xcenter}[]{\advi@setbg{\advi@bgxcenter}{xcenter}{#1}}
\define@key {advi@bg}{ycenter}[]{\advi@setbg{\advi@bgycenter}{ycenter}{#1}}
\define@key {advi@bg}{gradient}[]{\advi@setbg{\advi@bggradient}{gradient}{#1}}
\define@key {advi@bg}{image}[]{\advi@setbg{\advi@bgimage}{image}{#1}}
\define@key {advi@bg}{alpha}[]{\advi@setbg{\advi@bgalpha}{alpha}{#1}}
\define@key {advi@bg}{blend}[]{\advi@setbg{\advi@bgblend}{blend}{#1}}
\define@key {advi@bg}{fit}[auto]{\def \advi@bgfit {#1}%
     \ifx \advi@bgfit \advi@auto@ \else
     \advi@ifnine {\advi@bgfit}
       {\advi@global \def \advi@bgfit{ fit=#1}}
       {\advi@error {Ill formed background fit=#1}}%
   \fi}
\def \advi@bgset #1{\advi@ifnone {#1}{\advi@bgreset}{\setkeys {advi@bg}{#1}}}

%\define@key {advi@bg}{inherit}[]{\advi@special{setbg inherit}}

\def \advi@bgemit
  {\advi@special
     {setbg \advi@bgcolor
      \advi@bgcolorstart \advi@bgcolorstop
      \advi@bgxstart \advi@bgystart \advi@bgheight \advi@bgwidth
      \advi@bgxcenter \advi@bgycenter
      \advi@bggradient
      \advi@bgimage \advi@bgalpha \advi@bgblend \advi@bgfit 
      }}
\newif \ifadvi@bglocal

\newcommand{\advi@bg}[2][]{%
  \begingroup
  \def \@test {#1}\ifx \@test \advi@global@ \let \advi@global \global
  \advi@bgset {#2}\else
  \ifx \@test \advi@empty \else \advi@warning
     {Optional argument [#1] to \string \advibg ignored}\fi
  \global \advi@bglocaltrue
  \advi@bgset{#2}\advi@bgemit \fi
  \endgroup}
\def \advi@bgpage
   {\ifadvi@bgactive \ifadvi@bglocal\else \advi@bgemit \fi\fi
    \global \advi@bglocalfalse}

\advi@export \advibg \advi@bg

%%% Pausing and waiting

\def\advi@pause {\advi@special{pause}}
\def\advi@wait#1{\advi@special{wait sec=#1}}

\newcommand {\adviwait}[1][]%
   {\advi@ifempty {#1}{\advi@pause}{{\advi@dim %to check well-formedness
      #1\advi@dim}\advi@wait {#1}}}
\advi@export \advipause \advi@pause

%%% Saving pages or areas of pages

\def\advi@savepageimage@ {\advi@special {savepageimage}}
\def\advi@savepageimagefile@ #1{\advi@special {savepageimagefile file=#1}}

\newcommand{\advi@savepageimagefile}[1][]%
   {\advi@ifempty {#1}{\advi@savepageimage@}%
                      {\advi@savepageimagefile@ {#1}}}

\advi@export \advisavepageimage \advi@savespageimage@
\advi@export \advisavepageimagefile \advi@savespageimagefile

\def\advi@savepageareaimage@ #1#2#3#4%
  {\advi@special {savepageareaimagefile x=#1 y=#2 w=#3 h=#4}}
\def\advi@savepageareaimagefile@ #1#2#3#4#5%
  {\advi@special {savepageareaimagefile file=#1 x=#2 y=#3 w=#4 h=#5}}

\newcommand{\advi@savepageareaimagefile}[5][]%
   {\advi@ifempty {#1}{\advi@savepageareaimage@ {#2} {#3} {#4} {#5}}%
                      {\advi@savepageareaimagefile@ {#1} {#2} {#3} {#4} {#5}}}

\advi@export \advisavepageareaimage \advi@savespageareaimagefile@
\advi@export \advisavepageareaimagefile \advi@savespageareaimagefile

%%% Pushing key events

\def\advi@pushkeys #1{\advi@special {pushkeys #1}}
\advi@export \advipushkeys \advi@pushkeys

%%% Transparency and alpha blending for EPS
%%% To be revisited.

\def\advi@epstransparent
   {\advi@special{epstransparent push true}%
    \aftergroup \advi@resetepstransparent}
\def\advi@epswhite
   {\advi@special{epstransparent push false}%
    \aftergroup \advi@resetepstransparent}
\def\advi@setalpha#1%
   {\advi@special{alpha push #1}%
    \aftergroup \advi@resetalpha}
\def\advi@setblend#1%
   {\advi@special{blend push #1}%
    \aftergroup\advi@resetblend}
\def\advi@epsbygs
   {\advi@special{epsbygs push true}%
    \aftergroup \advi@resetepsbygs}
\def\advi@epsbycamlimages
   {\advi@special{epsbygs push false}%
    \aftergroup \advi@resetepsbygs}
\def\advi@epswithantialiasing
   {\advi@special{epswithantialiasing push true}%
    \aftergroup \advi@resetepsantialiasing}
\def\advi@epswithoutantialiasing
   {\advi@special{epswithantialiasing push false}%
    \aftergroup \advi@resetepswithantialiasing}
\def\advi@resetepstransparent {\advi@special{epstransparent pop}}
\def\advi@resetalpha {\advi@special{alpha pop}}
\def\advi@resetblend {\advi@special{blend pop}}
\def\advi@resetepsbygs{\advi@special{epsbygs pop}}
\def\advi@resetepswithantialiasing{\advi@special{epswithantialiasing pop}}

\advi@export \epstransparent \advi@epstransparent
\advi@export \epswhite \advi@epswhite
\advi@export \setalpha \advi@setalpha
\advi@export \setblend \advi@setblend
\advi@export \epsbygs \advi@epsbygs
\advi@export \epsbycamlimages \advi@epsbycamlimages
\advi@export \epswithantialiasing \advi@epswithantialiasing
\advi@export \epswithoutantialiasing \advi@epswithoutantialiasing

%%% Animated transitions

\def \advi@transfrom{}
\def \advi@transsteps{}
\def \advi@transstartx{}
\def \advi@transstarty{}
\def \advi@transstopx{}
\def \advi@transstopy{}
\def \advi@transgenpath{}
\def \advi@settrans {\advi@global \def}
\define@key {advi@trans}{none} []{\advi@settrans \advi@transmode {none}}
\define@key {advi@trans}{slide}[]{\advi@settrans \advi@transmode {slide}}
\define@key {advi@trans}{block}[]{\advi@settrans \advi@transmode {block}}
\define@key {advi@trans}{wipe} []{\advi@settrans \advi@transmode {wipe}}
\define@key {advi@trans}{path} []{\advi@settrans \advi@transmode {path}}
\define@key {advi@trans}{from} {\advi@settrans \advi@transfrom { from=#1}}
\define@key {advi@trans}{startx} {\advi@settrans \advi@transstartx { startx=#1}}
\define@key {advi@trans}{starty} {\advi@settrans \advi@transstarty { starty=#1}}
\define@key {advi@trans}{stopx} {\advi@settrans \advi@transstopx { stopx=#1}}
\define@key {advi@trans}{stopy} {\advi@settrans \advi@transstopy { stopy=#1}}
\define@key {advi@trans}{steps}{\advi@settrans \advi@transsteps { steps=#1}}
\define@key {advi@trans}{genpath}{\advi@settrans%
                                  \advi@transgenpath { genpath=#1}}

\def \advi@transemit
   {\advi@special{trans \advi@transmode \advi@transfrom \advi@transsteps%
    \advi@transstartx \advi@transstarty \advi@transstopx \advi@transstopy%
    \advi@transgenpath}}
\newif \ifadvi@translocal
\newcommand {\advi@transition}[2][]{%
  \begingroup
     \def \@test {#1}\ifx \@test \advi@global@ \let \advi@global \global
     \setkeys {advi@trans}{#2}\else
     \ifx \@test \advi@empty \else \advi@warning
        {Optional argument [#1] to \string \advitransition ignored}\fi
     \global \advi@translocaltrue
     \setkeys {advi@trans}{#2}\advi@transemit \fi
  \endgroup}

\def \advi@transpage
  {\@ifundefined {advi@transmode}{}
        {\ifadvi@translocal\else \advi@transemit \fi}%
   \global \advi@translocalfalse}

%% Hook \advi@setpagesetyle at \@begindvi that run at every page

\def \advi@setpagestyle {\advi@bgpage \advi@transpage}
\def \endpage@hook {}
\def \AtEndPage {\g@addto@macro \endpage@hook}
\AtEndPage {\advi@setpagestyle}

% We must patch \@begindvi to put our hook.
% However, hyperref may patch it as well. So we should do it at begin
% document to have the control (no one after us).
% However, one must be careful, because \@begindvi redefines itself at the
% first call to its prerecorded final value.
% So our first patch will be overridden with the value that it was
% meant to have after the first page.
% Hence, we patch it a second time to put our hook to this final value.

% we can use \g@addto@macro which redefines #1 to so that it procedes as
% before and then execute #2 at the end.

\def \advi@begindvi@patch
  {\g@addto@macro \@begindvi
      {\endpage@hook \g@addto@macro \@begindvi {\endpage@hook}}}

\AtBeginDocument {\advi@begindvi@patch}

%  {\let \advi@begindvi@save \@begindvi   %% value at begindocument
%   \def \@begindvi                       %% our new value
%     {\advi@begindvi@save                %% may redefine \@begindvi
%      \global\let \advi@begindvi@save    %% so we this new value
%         \@begindvi
%      \gdef \@begindvi                   %% now and forever
%         {\advi@begindvi@save \endpage@hook}%
%      \endpage@hook                      %% our hook for the
%     }}

%% Transitions

\def\advi@transbox@save#1#2#3{\advi@special
  {transbox save width=#1 height=#2 depth=#3}}
\def\advi@transbox@go#1{\advi@special{transbox go #1}}

\def \advi@transslide {slide}
\def \advi@transbox #1{%
   \def \advi@afterbox
      {\hbox {\advi@transbox@save{\the\wd0 }{\the\ht0 }{\the\dp0}%
              \unhbox0\setkeys {advi@trans}{#1}%
              \advi@transbox@go
                  {\advi@transmode \advi@transfrom \advi@transsteps%
                   \advi@transstartx \advi@transstarty%
                   \advi@transstopx \advi@transstopy \advi@transgenpath}}}%
   \def \advi@@afterbox {\aftergroup \advi@afterbox}%
   \afterassignment \advi@@afterbox \setbox0 \hbox }

\advi@export \advitransition \advi@transition
\advi@export \advitransbox \advi@transbox

%%% For PS Tricks

\def \advi@putbegin {\advi@special {putbegin}}
\def \advi@putend {\advi@special {putend}}
\def \advi@CP{advi@CP }

%% Original version
% \def\psput@special#1{%
% \hbox{%
% \pst@Verb{{ \pst@coor } \tx@PutCoor \tx@PutBegin}%
% \box#1%
% \pst@Verb{\tx@PutEnd}}}
\def\advi@psput@special#1{%
\hbox{%
\pst@Verb{{ \pst@coor } \tx@PutCoor \tx@PutBegin \advi@CP}%
\hbox {\advi@putbegin \box#1\advi@putend}%
\pst@Verb{\tx@PutEnd}}}

%% Original version
% \def\ncput@iii{%
% \leavevmode
% \hbox{%
% \pst@Verb{%
% \pst@nodedict
% /t \psk@npos def
% \tx@LPut
% end
% \tx@PutBegin}%
% \box\pst@hbox
% \pst@Verb{\tx@PutEnd}}}

\def\advi@ncput@iii{%
\leavevmode
\hbox{%
\pst@Verb{%
\pst@nodedict
/t \psk@npos def
\tx@LPut
end
\tx@PutBegin \advi@CP}%
\hbox {\advi@putbegin \box\pst@hbox\advi@putend}%
\pst@Verb{\tx@PutEnd}}}

\def \advi@pstricks@patch
  {\@ifundefined {psput@special}{}
    {\let \psput@special \advi@psput@special
     \@ifundefined {ncput@iii}{}{\let \ncput@iii \advi@ncput@iii}%
     }}
\ifadvi@ignore \else
\AtBeginDocument 
  {\message {Patching PStricks commands...}\advi@pstricks@patch}
\fi

%%% Anchors

\def \advi@style {box}
\def \advi@mode {advi}
\define@key {advi@anchor}{over}  []{\def \advi@mode {advi}}
\define@key {advi@anchor}{click} []{\def \advi@mode {hdvi}}
\define@key {advi@anchor}{box}   []{\def \advi@style {box}}
\define@key {advi@anchor}{invisible}[]{\def \advi@style {invisible}}
\define@key {advi@anchor}{underline}[]{\def \advi@style {underline}}

\newcommand {\setanchor}{\setkeys{advi@anchor}}
\newenvironment {advi@anchoring}[2][]%
   {\begingroup
     \setkeys {advi@anchor}{#1}%
     \advi@special@ {html:<a \advi@mode="#2" style=\advi@style>}%
    \endgroup}
  {\advi@special@ {html:</a>}}
\newcommand {\advi@anchor}[3][]%
   {\advi@anchoring[#1]{#2}#3\endadvi@anchoring}

\def \advi@endanchor #1{#1\endadvi@anchor \endgroup}
\advi@exportenv {advianchoring}{advi@anchoring}
\advi@export \advianchor \advi@anchor
\let \endadvianchor \advi@undefinedenv

%%% Some anchors

\newcommand {\advi@emphasize}[2][yellow]{%
  \global \advance \advi@c@annot by 1%
  \leavevmode
  \advi@record{\the\advi@c@annot}{\kern-\fboxsep
     \colorbox{#1}%
        {\advi@anchor[invisible]{\the\advi@c@annot}%
           {\advi@record[play]{\the\advi@c@annot}{#2}%
        }}\kern-\fboxsep}}
\advi@export \adviemphasize \advi@emphasize

%%% Partial patch for overlays -- 0 will be shown > 0 will not be shown

\def \advi@max {0}
\def \advi@overlay #1{%
  \advi@ifadvi
    {%\advance \c@overlay by 1
     \ifnum \c@overlay>\advi@max \global \xdef \advi@max {\the \c@overlay}\fi
     \advi@recording {overlay@#1}\aftergroup \endadvi@recording}
    {\latex@overlay {#1}}}

\def \advi@overlay@loop
 {\advi@ifadvi
  {\begingroup
    \c@overlay=0
    \@whilenum\c@overlay<\advi@max
    \do {\advance \c@overlay  by 1%
         \adviwait \adviplay{overlay@\the\c@overlay}}\relax
  \endgroup}
 {\latex@overlay@loop}}

\def \advi@end@slide
  {\advi@ifadvi {\overlay@loop}{}\gdef\advi@max{0}\latex@end@slide}

\def \advi@overlay@patch {%
   \let \latex@overlay \@overlay
   \let \latex@end@slide \end@slide
   \let \latex@overlay@loop \overlay@loop
   \let \@overlay \advi@overlay
   \let \overlay@loop \advi@overlay@loop
   \let \end@slide \advi@end@slide
  }

\@ifundefined {overlay}{}
   {\AtBeginDocument {\advi@overlay@patch}}

%%% Patch for hyperrefs

% \AtBeginDocument {\@ifundefined {ifHy@nesting}{}{\Hy@nestingtrue}}
% Resetting the normal default driver to hypertex
\providecommand*{\Hy@defaultdriver}{hypertex}%

%%% Thumbnails and Tocs

\def\advi@html#1{\special{html:<a name="#1">}\special{html:</a>}}
\def\advitoc {\advi@html {advitoc.first}}
\def\endadvitoc
  {\advi@html {advitoc.last}%
  %% So that next call is uneffective
  \global \let \advitoc \relax \global \let \endadvitoc \relax}
\newcommand {\advithumbnail}[1][\thepage]{\advi@html {/page.#1}}
%% Old version: delete if new is fine.
%
% \@ifundefined {@starttoc}{}
%   {\let \latex@starttoc \@starttoc
%    \def \@starttoc
%      {\advitoc \let \@starttoc \latex@starttoc \@starttoc}}
% \@ifundefined {tableofcontents}{}
%   {\let \latex@tableofcontents \tableofcontents
%    \def \tableofcontents
%       {\@ifundefined{latex@starttoc}{\advitoc}{}%
%        \latex@tableofcontents
%        \endadvitoc}}
% Allows redefinition of \tableofcontents---then may be inneffective.
\@ifundefined {@starttoc}{}
  {\let \advi@starttoc \@starttoc
   \def \@starttoc #1{\advitoc \advi@starttoc {#1}\endadvitoc}}

%%% Edit: special for edition with WhizzyTeX

\newcount \advi@count
\newdimen \advi@dim
\let \advidim \advi@dim
\let \advicount \advi@count
\newdimen \advi@edit@unit
\let \adviunit \advi@edit@unit
\newif \ifadvi@move
\newdimen \advix
\newdimen \adviy
\newdimen \adviw
\newdimen \advih
\newdimen \advid
\def \advi@dimset {%
\ifx \advi@edit@x \relax\else \advix \advi@edit@x \advi@edit@unit\fi
\ifx \advi@edit@y \relax\else \adviy \advi@edit@y \advi@edit@unit\fi
\ifx \advi@edit@w \relax\else \adviw \advi@edit@w \advi@edit@unit\fi
\ifx \advi@edit@h \relax\else \advih \advi@edit@h \advi@edit@unit\fi
\ifx \advi@edit@d \relax\else \advid \advi@edit@d \advi@edit@unit\fi
}
% \def \advix {\advi@edit@x \advi@edit@unit}
% \def \adviy {\advi@edit@y \advi@edit@unit}
% \def \adviw {\advi@edit@w \advi@edit@unit}
% \def \advih {\advi@edit@h \advi@edit@unit}
% \def \advid {\advi@edit@d \advi@edit@unit}
\def \advicx {\advi@edit@x}
\def \advicy {\advi@edit@y}
\def \advicw {\advi@edit@w}
\def \advich {\advi@edit@h}
\def \advicd {\advi@edit@d}

\advi@edit@unit 1em\relax
\def \advi@edit@defaults{%
%  \advi@edit@unit 1em\relax
  \def \advi@edit@x{0}%
  \def \advi@edit@y{0}%
  \let \advi@edit@h \relax%
  \let \advi@edit@w \relax%
  \let \advi@edit@d \relax%
  \def \advi@edit@X{X}%
  \def \advi@edit@Y{Y}%
  \def \advi@edit@W{W}%
  \def \advi@edit@H{H}%
  \def \advi@edit@D{D}%
  \advi@movefalse
}
\define@key {advi@embed}{width}%
   {\@tempdima#1\relax \edef \advi@embed@width {\the\@tempdima}}

\define@key {advi@edit}{unit}{\advi@edit@unit #1}
\define@key {advi@edit}{X}{\def \advi@edit@x{#1}\advi@movetrue}
\define@key {advi@edit}{Y}{\def \advi@edit@y{#1}\advi@movetrue}
\define@key {advi@edit}{W}{\def \advi@edit@w{#1}}
\define@key {advi@edit}{H}{\def \advi@edit@h{#1}}
\define@key {advi@edit}{D}{\def \advi@edit@d{#1}}

\define@key {advi@edit}{x}[1]%
        {\def \advi@edit@x{#1}\advi@movetrue\def\advi@edit@X{x}}
\define@key {advi@edit}{y}[1]%
        {\def \advi@edit@y{#1}\advi@movetrue\def\advi@edit@Y{y}}
\define@key {advi@edit}{w}[1]{\def \advi@edit@w{#1}\def\advi@edit@W{w}}
\define@key {advi@edit}{h}[1]{\def \advi@edit@h{#1}\def\advi@edit@H{h}}
\define@key {advi@edit}{d}[1]{\def \advi@edit@d{#1}\def\advi@edit@D{d}}

\def \advi@edit@comm {\string\adviedit}
\let \advi@edit@makelineno \relax
\def \advi@edit@line {\the \inputlineno}
\def \advi@edit@file {\jobname}
\define@key {advi@edit}{comm}{\def \advi@edit@comm {\string #1}}
\define@key {advi@edit}{line}{\def \advi@edit@line {#1}}
\define@key {advi@edit}{file}{\def \advi@edit@file {#1}}
\define@key {advi@edit}{min}{\def \advi@edit@min {#1}}
\def \advi@edit@min{1ex}

%\advi@edit {command}{name}{keys}{precode}{keys}{code}
\newcommand{\advi@edit}[4]{%
  \hbox \bgroup
    \def \advi@edit@comm {\string#1}%
    \def \advi@edit@name {#2}%
    \advi@edit@defaults
    \setkeys {advi@edit}{#3}%
    \advi@dimset
 %\advi@@edit}
 %\newcommand{\advi@@edit}[3][]{%
 %     #1% precode 
 %     \setkeys {advi@edit}{#2}% postbindings
    \setbox0=\hbox{#4}%
    \ifx \advi@edit@w \relax \advisetw {\wd0}\fi
    \ifx \advi@edit@h \relax \adviseth {\ht0}\fi
    \ifx \advi@edit@d \relax \advisetd {\dp0}\fi
    \if \advi@edit@W W\relax
      \ifdim \adviw<\advi@edit@min
        \ifdim \adviw<0ex\else
          \advi@setdim {\advi@edit@w}{\advi@dim}{\advi@edit@min}%
        \fi\fi
    \fi
    \if \advi@edit@H H\relax
      \ifdim \advih<\advi@edit@min
         \advi@setdim {\advi@edit@h}{\advi@dim}{\advi@edit@min}\fi
    \fi
    \ifadvi@move
       \setbox0=\hbox {\raise \adviy \hbox {\kern \advix\box0}}%
       \dp0=0pt\wd0=0pt\ht0=0pt
    \fi
    \advi@edit@makelineno
    \advi@special
      {edit\space
       comm="\advi@edit@comm"\space
       name="\advi@edit@name"\space
       line=\advi@edit@line\space
       file=\advi@edit@file\space
       unit=\the\advi@edit@unit\space
       \advi@edit@X=\advi@edit@x\space
       \advi@edit@Y=\advi@edit@y\space
       \advi@edit@W=\advi@edit@w\space
       \advi@edit@H=\advi@edit@h\space
       \advi@edit@D=\advi@edit@d\space
       }%
    \box0%
  \egroup
}

\def \advi@round #1#2{%
  \divide #2 by \advi@edit@unit
  \edef #1{\the #2}\relax
  \multiply #2 by \advi@edit@unit
}
  
\def \advi@E{\advi@count\advi@dim \divide \advi@count by \advi@edit@unit
        \advance \advi@dim by -\advi@count\advi@edit@unit
        \edef \advi@tmp {\advi@tmp \the \advi@count}}
\def \advi@D{\advi@dim 10\advi@dim \advi@E}

\def \advi@setdim #1#2#3{%
   \advi@dim=#3\relax#2=\advi@dim
   \ifnum \advi@dim <0\advi@dim-\advi@dim
        \def \advi@tmp{-}\else \def \advi@tmp{}\fi
   \advi@E \edef \advi@tmp {\advi@tmp.}\advi@D \advi@D \advi@D \advi@D
   \edef #1{\advi@tmp}}
\def \advisetx {\advi@setdim {\advi@edit@x}{\advix}}
\def \advisety {\advi@setdim {\advi@edit@y}{\adviy}}
\def \advisetw {\advi@setdim {\advi@edit@w}{\adviw}}
\def \adviseth {\advi@setdim {\advi@edit@h}{\advih}}
\def \advisetd {\advi@setdim {\advi@edit@d}{\advid}}

\def \advirndx {\advi@round {\advi@edit@x}{\advix}}
\def \advirndy {\advi@round {\advi@edit@y}{\adviy}}
\def \advirndw {\advi@round {\advi@edit@w}{\adviw}}
\def \advirndh {\advi@round {\advi@edit@h}{\advih}}
\def \advirndd {\advi@round {\advi@edit@d}{\advid}}

\def \adviratio#1#2{\advi@dim #2\relax #1\advi@dim}

\newcommand{\adviedit}[3][]{\advi@edit{\adviedit}{#1}{#2}{#3}}

\newcommand{\setedit}[1]{\setkeys{advi@edit}{#1}}
% \advi@edit{\advidget}{#1}{#2}{}
%    {\setbox0=\hbox to \adviw {\hsize=\adviw\vtop to \advihd
%         {\vsize=\advihd\vskip -\advih\relax
%          #3\relax\vfil}\hss}%
%     \box0}%
% }


%%% End

\endinput
