% =====================================================================
%  The following macros provide a systematic yet flexible method for
%  incorporating figures in a LaTeX document. They are especially well
%  suited for book preparation using the phbook class and psfrag
%  class. Each figure has its own .tex file. When used with psfrag, it
%  will also have its own .eps file. In the main text,
%	\figdef{figure}
%  inserts the graphics from the file figure.tex centered at that 
%  point. Options defined below control the formating of that figure.
%  If using psfrag, the figure.tex file will be of the form:
%  	\begin{psfrags} \psfrag{xx}{yy} ... \inserteps{figure.eps}
%	\end{psfrags}
% =====================================================================
% *********************************************************************
% ******                          *************************************
% *****   LaTeX2e INITIALIZATION   ************************************
% ******                          *************************************
% *********************************************************************
\NeedsTeXFormat{LaTeX2e}[1993/11/11] % Oldest acceptable version of LaTeX2e
\ProvidesPackage{lyxfig}[1996/01/26]   % Name of package provided, date

%====================== Initial Code & Declaration of Options
\newif\iffigdr@ft \figdr@ftfalse     % defines draft or regular mode.

\newif\iffigframe \figframefalse     % if figures are framed, w/ labels
\newif\ifshowfigs \showfigstrue      % if false, only figure name shown.
\newif\ifdraftfigs \draftfigsfalse   % if true, blank space left for fig.
\newif\ifsc@leoverride 		     % override scale when doing figs. 

\DeclareOption{draft}{\figdr@fttrue\typeout{LYXFIG: Draft option selected.}}
\DeclareOption{final}{\figdr@ftfalse\typeout{LYXFIG: Final option selected.}}
\DeclareOption{blank}{\draftfigstrue\typeout{LYXFIG: Blank option selected.}}
\DeclareOption{noblank}{\draftfigsfalse\typeout{LYXFIG: Blank option de-selected.}}
\DeclareOption{hide}{\showfigsfalse\typeout{LYXFIG: Hide option selected.}}
\DeclareOption{show}{\showfigstrue\typeout{LYXFIG: Show option selected.}}
\DeclareOption{frame}{\figframetrue\typeout{LYXFIG: Frame option selected.}}
\DeclareOption{noframe}{\figframefalse\typeout{LYXFIG: Frame option de-selected.}}

\ProcessOptions
\RequirePackage{graphics}
\RequirePackage{graphicx,ifthen}

\newcommand{\f@gwarning}[1]{\typeout{Warning: #1\space on page \thepage}}

%======================================================================
% LaTeX input file and is input by the figure commands. The figure can
% be "larger than life" and these routines will scale the figures by
% a constant amount to fit into the book.
%
% \setfigscale{scale} Changes the scaling factor applied to each figure 
%	    (which is initialized to 1). If changed inside the main LaTeX 
%           code, then the new scale will apply to all figures from that 
%           point on. If changed within the figure file, it will apply only 
%           to that figure.
%
% \figdef[dim]{figname} This command inputs a figure
%           TeX file of name "figname.tex". If the file "figname.tex" does 
%           not exist, a warning is output and the figure name is displayed
%           in a framed box. A warning is also output if an eps file of name 
%           "figname.eps" does not exist. The optional dimension [dim] is
%           the vspace skipped when draftfigs is true.
%
% \scalefig[dim]{figname}{scale} This is the same as \figdef, except
%           that it overrides any scaling in the "figname.tex" file. 
%
% \showfigsfalse This command will cause figdef to print the figure's
%           name in a framed box instead of including the figure. Saves
%	    time. Note that the figures are "short" since they are just
%           the height of the text.
% \showfigstrue (default) figdef returns to the regular mode of display-
%	    ing the figures, not just the name.
%
% \figframetrue This command will cause figdef to draw a frame around 
%           the figure so that you can adjust the bounding box. Also prints
%           the figure name in the margin of the page.
% \figframefalse (default) Turns off the figure framing.
%
% \draftfigstrue Leaves an empty space *of correct vertical dimension* 
%           on the page for the figure. (speeds up LaTeX since it does not
%           need to search for the figure on the disk).
% \draftfigsfalse (default) Draws the figure instead.
%
% Use \renewcommand{\figfontsize}{\scriptsize}, for example, to change the
% default font size for the figure labels.
% Using \renewcommand{\figfontstyle}{\sffamily}, for example, to change the
% default figure font style to sans-serif.
%======================================================================
\newcommand{\setfigscale}[1]{\gdef\figsc@le{#1}}
\setfigscale{1}
\newsavebox{\figb@x}
\newdimen\htpl@sdp
\newif\ifdr@fteps
\newdimen\b@xwidth
\def\figfontsize{\footnotesize}
\def\figfontstyle{\rmfamily}

\newcommand{\d@fig}[2]{\begingroup\figfontsize\reset@font\figfontstyle%
\b@xwidth=\hsize \advance\b@xwidth by -9\arrayrulewidth
\xdef\figh@ight{#2}%
\xdef\figscales@ve{\figsc@le}% save the global scale factor.
\IfFileExists{#1.tex}{%
\ifsc@leoverride\xdef\figsc@le{\sc@lefig}\fi%
%
\ifshowfigs%
% show figs true...
\ifdraftfigs%
% show figs true and draft figs true...
\vspace*{-2\arrayrulewidth}\frame{\vbox to #2{\vss \hbox to \b@xwidth{\hfill #1 \hfill}\vss}}%
\else%
% show figs true and draft figs false...
\iffigframe%
% showfigstrue, draftfigsfalse, figframetrue...
\vspace*{-2\arrayrulewidth}\hfill\frame{\let\par\relax\input{#1}}\hfill%
\sbox{\figb@x}{\let\par\relax\input{#1}}% read box to get the height!
\htpl@sdp=\ht\figb@x\advance\htpl@sdp by \dp\figb@x%
\smash{\rlap{\vbox{\hbox{\footnotesize\quad\tt{ht=}\the\htpl@sdp\typeout{#1: \the\htpl@sdp}}%
\hbox{\footnotesize \quad #1}}}}%
\else%
% showfigstrue, draftfigsfalse, figframefalse...
\iffigdr@ft%
% showfigstrue, draftfigsfalse, figframefalse, draft mode true...
\hbox to \hsize{\hss\let\par\relax\input{#1}\hss\smash{\rlap{\footnotesize\quad#1}}}%
\else%
% showfigstrue, draftfigsfalse, figframefalse, draft mode false...
%\hbox to \hsize{\hss\let\par\relax\input{#1}\hss\smash{\rlap{}}}%
\noindent{\hfill\let\par\relax\input{#1}\hfill}%
%\hbox to \hsize{\hss\let\par\relax\input{#1}\hss}%
%\centerline{\let\par\relax\input{#1}}%
\fi% end \iffigdr@ft
\fi% end \iffigframe
\fi% end \ifdraftfigs
\else% 
% show figs is false...
\centerline{\fbox{#1}}%
\fi% end \ifshowfigs
}{%
\f@gwarning{#1.tex not found!}%
\centerline{\frame{\vbox to #2{\vss\hbox{\hskip 1in\relax #1 (missing from disk!)\hskip 1in}\vss}}}%
}% end IfFileExists #1.tex
\xdef\figsc@le{\figscales@ve}
\ifdr@fteps%
\vspace*{0.05in}%
\centerline{\large \bf This is a draft figure!}%
\fi%
\endgroup
}%end \fgdf

%======================================================================
% \positionfig[top,bot,left,right]{fig}
% This macro puts "fig" inside a tabular array to shift it around on 
% page. "top" vspace (e.g. 0.1in ... units are necessary!) is added
% to the top of the figure, "bot" is added to the bottom. "top" and
% "bot" may be negative. "left" hspace is added to the left of the 
% figure, and "right" hspace is added to the right. "left" and "right"
% must be positive, or zero. If they are negative, you will get a
% warning message such as "underful hbox by xxx pt" but LaTeX will not
% crash (the number used will be 0pt)
%
% NOTE: THIS COMMAND IS NOW OBSOLETE! INSTEAD, USE THE
% \inserteps "trim" COMMAND INSTEAD. e.g.,
% \inserteps[trim a b c d]{filename.eps}. THE "trim" COMMAND IS
% DOCUMENTED IN THE LATEX2E GRAPHICS BUNDLE DOCUMENTATION.
%======================================================================
\newdimen\b@tdelta
\def\positionfig[#1,#2,#3,#4]#5{%
\renewcommand{\arraystretch}{0}%
\b@tdelta=-\fboxrule%
\advance\b@tdelta by #2\relax%
%\edef\@fig{#5}%
\begin{tabular}{p{#3}@{}c@{}p{#4}}
\multicolumn{3}{c}{\rule{0in}{0.0in}}\\[#1]&#5&\\[\b@tdelta]%
\end{tabular}%
}


%======================================================================
% \inserteps{epsfilename}
% The specified eps file will be inserted into a figdef environment 
% (such as when using psfrag). If the file does not exist, a box is
% left on the page, of height specified by the \figdef command, with
% a warning message.
%
% \insertdrafteps{epsfilename}
% The specified file is inserted just as above. Below the figure, and
% centered on the page is a framed message stating that the figure is
% a draft figure.
%======================================================================
\def\insert@eps[#1]#2{\IfFileExists{#2}{\ifsc@leoverride%
\includegraphics[scale=\sc@lefig,#1,clip=]{#2}\else%
\includegraphics[scale=\figsc@le,#1,clip=]{#2}\fi}{%
\f@gwarning{file \protect #2 is missing!}\vspace*{-2\arrayrulewidth}%
\frame{\vbox to \figh@ight{\vss \hbox{\mbox{\qquad #2 
(missing from disk)\qquad}}\vss}}}\global\dr@ftepsfalse}

\def\inserteps{\@ifnextchar [{\insert@eps}{\insert@eps[angle=0]}}

\newcommand{\insertdrafteps}[1]{\inserteps{#1}\global\dr@ftepstrue}

\def\figdef{\@ifnextchar [{\figdefa}{\figdefa[0.5in]}}
\def\figdefa[#1]#2{\def\figfilename{#2}\sc@leoverridefalse\d@fig{#2}{#1}}
\def\scalefig{\@ifnextchar [{\@scalefiga}{\@scalefiga[0.5in]}}
\def\@scalefiga[#1]#2#3{\xdef\sc@lefig{#3}\def\figfilename{#2}\sc@leoverridetrue\d@fig{#2}{#1}}

%\newdimen\htplda
%\newdimen\htpldb
%\newdimen\htpldc
%\newdimen\htpldd

%\def\positionfig[#1,#2,#3,#4]{
  %\htplda=-#1\relax \htpldb=-#2\relax \htpldc=-#3\relax \htpldd=-#4\relax 
  %\typeout{POSITIONFIG: \figfilename, \the\htpldc, \the\htpldb, \the\htpldd, 
	   %\the\htplda}
%}
