% LaTeX package pdfsync.sty
% =========================
% 
% Presentation
% ------------
% pdfsync.sty allows one to synchronize between LaTeX source and pdf output.
% When used with a text editor and a PDF viewer that both support pdfsync,
% you can navigate from the source to the output and vice versa.
% This is some kind of srcltx ported from dvi to pdf.
% 
% When you typeset foo.tex with pdfsync.sty and either pdfetex or xetex, a foo.pdfsync auxiliary file is created.
% It only contains geometrical information used by text editors or PDF viewers for synchronization.
% You can delete this file when you are done.
% 
% Actually, iTeXMac, iTeXMac2, TeXShop, TextMate, AucTeX are text editors implementing pdfsync support at various level.
% iTeXMac, iTeXMac2, TeXShop, TeXniscope, PDFView are PDF viewers implementing pdfsync support at various level.
% 
% Version
% -------
% - 01/25/2008: This is the second version officially available.
%  Some internals were modified in order to provide a stronger
% \pdfsyncstart, \pdfsyncstop pair.
% Anticipating over pdftex improvements, this package will load with option "off" if the \synchronize macro is defined
%
% Install
% -------
% If this package is not already included in you TeX distribution, just copy the pdfsync.sty file to the proper location.
% On Mac OS X, it can be
% YOUR_HOME_DIRECTORY/Library/texmf/tex/latex/graphics/graphics.sty
% where you should replace YOUR_HOME_DIRECTORY by its actual value.
% 
% Usage
% -----
% Put \usepackage{pdfsync} in your LaTeX preamble.
% 
% In case of sever conflicts with another package, try \usepackage[novbox]{pdfsync} instead.
% 
% If pdfsync only breaks some part of your LaTeX code, you can try to enclose it in a \pdfsyncstop \pdfsyncstart pair.
% If you want to add more control point add \pdfsync at sensible locations in your code.
% In that case, \usepackage[off]{pdfsync} will disable pdf synchronization and \relax the above commands.
% 
% Use option nohbox (resp nocr, nopar, nomath, nodisplay) in order not to add control points every hbox (resp cr, par, math,display)
% 
% Bugs
% ----
% pdfsync uses extremely severe code. You should not use pdfsync on final documents because it can change the layout rather significantly
% (different page/line breaks are the most obvious changes), despite this is rather rare,
% 17.8th Murphy's law states that it will happen to you when it absolutely must not...
% 
% You should report bugs and package conflicts to jlaurens AT users DOT sourceforge DOT net.
% 
% The accuracy of pdf synchronization depends on the editor/viewer used for that purpose.
% iTeXMac2 is actually the most accurate implementation because it combines pdfsync with pdf searching.
% So, the lack of accuracy is not a bug in pdfsync a priori.
% 
% Credits:
% -------
% The original idea of pdfsync was proposed by Piero D'Ancona in the summer of 2003.
% He and Jerome Laurens (jlaurens AT usersDOT sourceforge DOT net) both created the first working package.
% Hans Hagen and David Kastrup made very significant enhancements to the original code.
% 
% License
% -------
% This program is free software; you can redistribute it and/or modify
% it under the terms of the The LaTeX Project Public License version 1.3c at least
% http://www.latex-project.org/lppl.txt
% 
% Home page
% ---------
% The official site is
% 
% http://itexmac.sourceforge.net/pdfsync.html
% 
% copyright 2006-2008, jlaurens AT users DOT sourceforge DOT net
% 
\ifx\pdfsync\undefined\else\message{****  pdfsync.sty: WARNING, the package is already loaded.}\expandafter\endinput\fi
\def\c@nfigPDFSYNC@ff{%
  \let\pdfsync\relax%
  \let\pdfsyncstart\relax%
  \let\pdfsyncstop\relax%
}%
\ifx\synchronize\undefined\else
\message{****  pdfsync.sty: WARNING, the package is obsolete with this version of pdftex.}%
\c@nfigPDFSYNC@ff
\let\c@nfigPDFSYNC@ff=\undefined
\expandafter\endinput
\fi
\ifx\pdfsavepos\undefined
\message{****  pdfsync.sty: WARNING, synchronization is only available with pdftex like engines.}%
\c@nfigPDFSYNC@ff
\let\c@nfigPDFSYNC@ff=\undefined
\expandafter\endinput
\fi
% Defining the preamble of the style file:
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{pdfsync}[2008/01/26 v1.1]%
% start of the code
\let\c@nfigPDFSYNCp@r\relax%
\let\c@nfigPDFSYNCm@th\relax%
\let\c@nfigPDFSYNCdispl@y\relax%
\let\c@nfigPDFSYNChb@x\relax%
\let\c@nfigPDFSYNCvb@x\relax%
\let\c@nfigPDFSYNC@cr\relax%
% Subverting |\everypar| to prepend pdfsync |\@PDFSYNC| specific code that will
% be executed for each new paragraph.
%This was primarly suggested by Curtis Clifton on 12/17/2004 (google "subvert token" for details).
\def\c@nfigPDFSYNCp@r{\wlog{****  pdfsync.sty: Synchronizing at \string\everypar}%
\newtoks\PDFSYNCeveryp@r
\PDFSYNCeveryp@r\everypar
\everypar{\bgroup\setbox\z@\lastbox\@PDFSYNC\box\z@\egroup
  \the\PDFSYNCeveryp@r}%
\let\everypar\PDFSYNCeveryp@r
}%
%
\def\c@nfigPDFSYNCdispl@y{\wlog{****  pdfsync.sty: Synchronizing at \string\everydisplay}%
\newtoks\PDFSYNCeverydispl@y
\PDFSYNCeverydispl@y\frozen@everydisplay
\frozen@everydisplay{\@PDFSYNC\the\PDFSYNCeverydispl@y}%
\let\frozen@everydisplay\PDFSYNCeverydispl@y
}%
%
\def\c@nfigPDFSYNCm@th{\wlog{****  pdfsync.sty: Synchronizing at \string\everymath}%
\newtoks\PDFSYNCeverym@th
\PDFSYNCeverym@th\frozen@everymath
\frozen@everymath{\@PDFSYNC\the\PDFSYNCeverym@th}%
\let\frozen@everymath\PDFSYNCeverym@th
}%
%
\def\c@nfigPDFSYNChb@x{\wlog{****  pdfsync.sty: Synchronizing at \string\everyhbox}%
\newtoks\PDFSYNCeveryhb@x
\PDFSYNCeveryhb@x\everyhbox
\everyhbox{\PDFSYNCeveryhb@xcontent}%
\def\PDFSYNCeveryhb@xon{\ifPDFSYNC@unblocked
    \begingroup
      \PDFSYNC@unblockedfalse
      \setbox\z@\hbox\bgroup
       \@PDFSYNC
       \setbox\z@\hbox\bgroup\bgroup
         \aftergroup\PDFSYNC@endhbox
			\the\PDFSYNCeveryhb@x
		\PDFSYNC@unblockedtrue\fi}
\def\PDFSYNCeveryhb@xoff{\the\PDFSYNCeveryhb@x}
\let\everyhbox\PDFSYNCeveryhb@x
\def\PDFSYNC@endhbox{\ifnum\lastnodetype>\m@ne
    \egroup \unhbox\z@ \egroup \unhbox\z@
  \else \egroup \egroup \fi \endgroup \egroup}%
}%
%
\def\c@nfigPDFSYNCvb@x{\wlog{****  pdfsync.sty: Synchronizing at \string\everyvbox}%
\newtoks\PDFSYNCeveryvb@x
\PDFSYNCeveryvb@x\everyvbox
\everyvbox{\PDFSYNCeveryvb@xcontent}%
\def\PDFSYNCeveryvb@xon{\ifPDFSYNC@unblocked
    \begingroup
      \PDFSYNC@unblockedfalse
      \setbox\z@\vbox\bgroup
       \@PDFSYNC
       \setbox\z@\vtop\bgroup\bgroup
         \aftergroup\PDFSYNC@endvbox
			\the\PDFSYNCeveryvb@x\PDFSYNC@unblockedtrue\fi}
\def\PDFSYNCeveryvb@xoff{\the\PDFSYNCeveryvb@x}
\let\everyvbox\PDFSYNCeveryvb@x
\def\PDFSYNC@endvbox{\ifnum\lastnodetype>\m@ne
    \egroup
    \edef\next{\egroup\hrule\@height\the\ht\z@\@depth-\the\ht\z@\relax}%
    \unvbox\z@ \next \unvbox\z@
  \else \egroup \egroup \fi \endgroup \egroup}%
}%
%
\def\c@nfigPDFSYNC@cr{\wlog{****  pdfsync.sty: Synchronizing at \string\everycr}%
\newtoks\PDFSYNCevery@cr
\PDFSYNCevery@cr\everycr
\everycr{\the\PDFSYNCevery@cr\@PDFSYNC}%
\let\everycr\PDFSYNCevery@cr
}%
% Configure pdsync
\def\c@nfigPDFSYNC{\wlog{Configuring pdfsync}%
\newwrite\PDFSYNC@ut%
\immediate\openout\PDFSYNC@ut\jobname.pdfsync%
\immediate\write\PDFSYNC@ut{\jobname}% control info
\immediate\write\PDFSYNC@ut{version 1}% control info
%   patching \shipout
\let\PDFSYNCship@ut@ld\shipout%
\gdef\PDFSYNCship@utn@w{%
\afterassignment\PDFSYNC@utput%
\setbox\@cclv= %
}%
\def\PDFSYNC@utput{%
\relax%
\ifvoid\@cclv\relax%
\aftergroup\PDFSYNC@@utput%
\else%
\PDFSYNC@@utput%
\fi%
}%
\gdef\PDFSYNC@@utput{%
\global\advance\PDFSYNCf@li@ by 1%
\immediate\write\PDFSYNC@ut{s\space\the\PDFSYNCf@li@}%
\PDFSYNCship@ut@ld\box\@cclv%
}%
%
% patching LaTeX \InputIfFileExists for \include and \input
\let\PDFSYNCInputIfFileExists@ff\InputIfFileExists%
\long\def\PDFSYNCInputIfFileExists@n##1##2##3{%
\immediate\write\PDFSYNC@ut{(##1}%
\PDFSYNCInputIfFileExists@ff{##1}{##2}{##3}%
\immediate\write\PDFSYNC@ut{)}}%
%
\def\PDFSYNCP@siti@n{\the\pdflastxpos\space\the\pdflastypos}%
%
\newif\ifPDFSYNCst@r%
\newif\ifPDFSYNCvb@x%
\newcount\PDFSYNCf@li@\PDFSYNCf@li@=0%
\newcount\PDFSYNC@rder\PDFSYNC@rder=0%
\newif\ifPDFSYNC@unblocked
\PDFSYNC@unblockedtrue
% defining \@@PDFSYNC: the macro that writes to the .pdfsync file
\def\@@PDFSYNC{%
\relax%
\immediate\write\PDFSYNC@ut{l\space\the\PDFSYNC@rder\space\the\inputlineno}%
\pdfsavepos%
\edef\next{\write\PDFSYNC@ut{p\ifPDFSYNCst@r*\else\ifPDFSYNCvb@x+\fi\fi\space\the\PDFSYNC@rder\space\noexpand\PDFSYNCP@siti@n}}%
\next%
\global\advance\PDFSYNC@rder by 1%
\relax%
}%
%
\def\@pdfsync{\bgroup\PDFSYNCst@rtrue\@PDFSYNC\egroup}%
%
\def\pdfsyncstart{%
\let\shipout\PDFSYNCship@utn@w%
\let\pdfsync\@pdfsync%
\let\@PDFSYNC\@@PDFSYNC%
\let\InputIfFileExists\PDFSYNCInputIfFileExists@n%
\let\PDFSYNCeveryhb@xcontent\PDFSYNCeveryhb@xon
\let\PDFSYNCeveryvb@xcontent\PDFSYNCeveryvb@xon
}%
\def\pdfsyncstop{%
%\let\shipout\PDFSYNCship@ut@ld%
\let\pdfsync\relax%
\let\@PDFSYNC\relax%
\let\InputIfFileExists\PDFSYNCInputIfFileExists@ff%
\let\PDFSYNCeveryhb@xcontent\PDFSYNCeveryhb@xoff
\let\PDFSYNCeveryvb@xcontent\PDFSYNCeveryvb@xoff
}%
%
\c@nfigPDFSYNCp@r%
\let\c@nfigPDFSYNCp@r\undefined%
\c@nfigPDFSYNCm@th%
\let\c@nfigPDFSYNCm@th\undefined%
%\c@nfigPDFSYNCdispl@y% This does not work with amsmath
\let\c@nfigPDFSYNCdispl@y\undefined%
\c@nfigPDFSYNChb@x%
\let\c@nfigPDFSYNChb@x\undefined%
\c@nfigPDFSYNCvb@x%
\let\c@nfigPDFSYNCvb@x\undefined%
%\c@nfigPDFSYNC@cr% This does not work "Missing \cr inserted" in maketitle
\let\c@nfigPDFSYNC@cr\undefined%
% the problem of the soul package
    \ifx\SOUL@\undefined\relax\else%
	\message{! pdfsync is patching the soul package}
        \let\PDFSYNC@SOUL@\SOUL@
        \def\SOUL@##1{%
            \bgroup\pdfsyncstop\PDFSYNC@SOUL@{##1}\egroup
        }%
    \fi%
\pdfsyncstart%
\wlog{****  pdfsync.sty: started}%
}%
%
\AtBeginDocument{%
\c@nfigPDFSYNC%
\let\c@nfigPDFSYNC\undefined%
}%
% end of the core code
% Declaring the option 'nopar' to disable this feature
\DeclareOption{nopar}{\let\c@nfigPDFSYNCp@r\relax}%
% Declaring the option 'nomath' to disable this feature
\DeclareOption{nomath}{\let\c@nfigPDFSYNCm@th\relax}%
% Declaring the option 'nodisplay' to disable this feature
\DeclareOption{nodisplay}{\let\c@nfigPDFSYNCdispl@y\relax}%
% Declaring the option 'nohbox' to disable this feature
\DeclareOption{nohbox}{\let\c@nfigPDFSYNChb@x\relax}%
% Declaring the option 'novbox' to disable this feature
\DeclareOption{novbox}{\let\c@nfigPDFSYNCvb@x\relax}%
% Declaring the option 'nocr' to disable this feature
\DeclareOption{nocr}{\let\c@nfigPDFSYNC@cr\relax}%
% Declaring the option 'off', all the macros are set to \relax
\DeclareOption{off}{\let\c@nfigPDFSYNC\c@nfigPDFSYNC@ff}%
% Beamer problem
\ifx\beamer@version\undefined\relax\else\let\c@nfigPDFSYNCvb@x\relax\fi
\ProcessOptions%
\endinput%
