% lpic.sty V0.6 14.10.2009
% by Rostislav Matveyev
% Permition is granted to redistribute and/or modify this code with no restrictions whatsoever
% except that if it is modified, the name of the author should be removed.

\ProvidesPackage{lpic}
    [2009/10/14, version 0.6]

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{epsfig}
\RequirePackage{rotating}
\RequirePackage{calc}
\RequirePackage{ifthen}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% debugging stuff
%

\newcommand{\lp@skipthis}[1]{\relax}
\newcommand{\lp@msg}[1]{\message{^^J! lpic message: #1^^J}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Counters, lengths, boxes
%

% Adjustable parameters
\newlength{\lpunitlength}\setlength{\lpunitlength}{1mm}

\newlength{\lpmarginright}\setlength{\lpmarginright}{0mm}
\newlength{\lpmarginleft}\setlength{\lpmarginleft}{0mm}
\newlength{\lpmargintop}\setlength{\lpmargintop}{0mm}
\newlength{\lpmarginbottom}\setlength{\lpmarginbottom}{0mm}

\newlength{\lpbgboxsep}\setlength{\lpbgboxsep}{\fboxsep}

\newlength{\lpgridthickness}\setlength{\lpgridthickness}{0.01mm}
\newlength{\lpframethickness}\setlength{\lpframethickness}{0.7mm}
\newlength{\lplblframethickness}\setlength{\lplblframethickness}{0.01mm}
\newlength{\lpfigframethickness}\setlength{\lpfigframethickness}{0.2mm}

\newcounter{lpgridstep}\setcounter{lpgridstep}{5}
\newcounter{lpcoordstep}\setcounter{lpcoordstep}{10}

% Internal parameters
\newlength{\lp@marginright}
\newlength{\lp@marginleft}
\newlength{\lp@margintop}
\newlength{\lp@marginbottom}
\newlength{\lp@xsizeorig}
\newlength{\lp@ysizeorig}
\newlength{\lp@xtotalsizeorig}
\newlength{\lp@ytotalsizeorig}
\newlength{\lp@gridstep}
\newlength{\lp@coordstep}
\newlength{\lp@coordx}
\newlength{\lp@coordy}

\newcounter{lp@xtotalsize}
\newcounter{lp@ytotalsize}
\newcounter{lp@xorigin}
\newcounter{lp@yorigin}
\newcounter{lp@coordx}
\newcounter{lp@coordy}

\newsavebox{\lp@pictbox}		%box to save (ps or pdf)-figure

% Temp stuff
\newlength{\lp@tmpa}
\newlength{\lp@tmpb}
\newlength{\lp@tmpc}
\newlength{\lp@tmpd}
\newlength{\lp@tmpx}
\newlength{\lp@tmpy}
\newlength{\lp@tmpdx}
\newlength{\lp@tmpdy}

\newcounter{lp@tmpa}
\newcounter{lp@tmpb}
\newcounter{lp@tmpc}
\newcounter{lp@tmpd}
\newcounter{lp@tmpe}
\newcounter{lp@tmpf}
\newcounter{lp@tmpx}
\newcounter{lp@tmpy}
\newcounter{lp@tmpdx}
\newcounter{lp@tmpdy}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Procedure to check whether argument is a number or length
% we need a way to check whether given argument is a number or 
% length \lp@ifnumber{arg}{true cmds}{false cmds}

\def\lp@substring#1 in #2;%
   {%
     \def\lp@check##1#1##2ENDOFTHEARGS%
		  {\edef\lp@foundsubstring{\lp@foundsubstring##2}}%
     \expandafter\lp@check#2#1ENDOFTHEARGS%
   }

\def\lp@ifsubstring#1 in #2;#3#4%
   {%
     \edef\lp@foundsubstring{}%
     \lp@substring #1 in #2;%
     \ifthenelse{\equal{x}{x\lp@foundsubstring}}{#4}{#3}%
   }

\newcommand{\lp@ifnumber}[3]%
   {%
     \edef\lp@foundsubstring{}%
     \lp@substring pt in #1;%
     \lp@substring pc in #1;%
     \lp@substring in in #1;%
     \lp@substring bp in #1;%
     \lp@substring cm in #1;%
     \lp@substring mm in #1;%
     \lp@substring dd in #1;%
     \lp@substring cc in #1;%
     \lp@substring sp in #1;%
     \lp@substring em in #1;%
     \lp@substring ex in #1;%
     \ifthenelse{\equal{\lp@foundsubstring}{}}{#2}{#3}%
   }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \spbox from \psboxit
\long\def\spbox#1{\leavevmode\setbox1\hbox{#1}%
    \dimen0\fboxrule \advance\dimen0 \fboxsep%
    \advance\dimen0 \dp1%
    \hbox{\lower \dimen0\hbox%
    {\vbox{\hrule height \fboxrule width 0pt%
          \hbox{\vrule width \fboxrule height 0pt \hskip\fboxsep%
          \vbox{\vskip\fboxsep \box1\vskip\fboxsep}\hskip%
                 \fboxsep\vrule width \fboxrule height 0pt}%
                 \hrule height \fboxrule width 0pt}}}}%
% \psboxit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Save scaled picture in \lp@pictbox, calculate its sizes, calculate
% sizes and the origin of the whole picture box and define procedure
% for calculating coordinates
% \lp@dimens defines \lp@xtotalsize!, \lp@ytotalsize!, \lp@xorig!,
% \lp@yorig!, \lp@xcoord{original_length_in_\lp@units},
% \lp@ycoord{original_length_in_\lp@units} 
%

\newcommand{\lp@dimens}
{%
  % Save unscaled pict and calculate sizes
  \sbox{\lp@pictbox}{\epsfig{figure=\lp@epsfile}}%
  \settowidth{\lp@xsizeorig}{\usebox{\lp@pictbox}}% Original x-size
  \settoheight{\lp@ysizeorig}{\usebox{\lp@pictbox}}% Original y-size

  % Are we given absolute dimensions or scale coefficients?
  \ifthenelse{\equal{\lp@xscale}{}}%
      % x scale is not given
      {%
	%\lp@msg{x=\lp@xscale, y=\lp@yscale}
	\ifthenelse{\equal{\lp@yscale}{}}
	    % x - no, y - no
	    {%
	      \edef\lp@xnum{1cm}\edef\lp@xdenom{1cm}%
	      \edef\lp@ynum{1cm}\edef\lp@ydenom{1cm}%
	    }%
	    % x - no, y - yes
	    {%
	      \lp@ifnumber{\lp@yscale}
		 {%
		   \edef\lp@ynum{\lp@yscale cm}\edef\lp@ydenom{1cm}% 
		   \edef\lp@xnum{\lp@yscale cm}\edef\lp@xdenom{1cm}%
		 }%
		 {%
		   \edef\lp@ynum{\lp@yscale}\def\lp@ydenom{\lp@ysizeorig}%
		   \edef\lp@xnum{\lp@yscale}\def\lp@xdenom{\lp@ysizeorig}% 
		 }%
	    }%
      }%
      % x is given
      {%
	\ifthenelse{\equal{\lp@yscale}{}}
	    % x - yes, y - no
	    {%
	      \lp@ifnumber{\lp@xscale}
		 {%
		   \edef\lp@ynum{\lp@xscale cm}\edef\lp@ydenom{1cm}%
		   \edef\lp@xnum{\lp@xscale cm}\edef\lp@xdenom{1cm}%
		 }%
		 {%
		   \edef\lp@ynum{\lp@xscale}\def\lp@ydenom{\lp@xsizeorig}%
		   \edef\lp@xnum{\lp@xscale}\def\lp@xdenom{\lp@xsizeorig}%
		 }%
	    }%
	    % x - yes, y - yes
	    {%
	      \lp@ifnumber{\lp@xscale}
	         {%
		   \edef\lp@xnum{\lp@xscale cm}\edef\lp@xdenom{1cm}%
		 }
		 {%
		   \edef\lp@xnum{\lp@xscale}\def\lp@xdenom{\lp@xsizeorig}%
		 }
	      \lp@ifnumber{\lp@yscale}
	         {%
		   \edef\lp@ynum{\lp@yscale cm}\edef\lp@ydenom{1cm}%
		 }
		 {%
		   \edef\lp@ynum{\lp@yscale}\def\lp@ydenom{\lp@ysizeorig}%
		 }%
		 
	    }%
      }%
  %\lp@msg{xscale=\lp@xnum/\lp@xdenom, yscale=\lp@ynum/\lp@ydenom}
  \setlength{\lp@tmpx}{\lp@xsizeorig * \ratio{\lp@xnum}{\lp@xdenom}}% x-size in the doc
  \setlength{\lp@tmpy}{\lp@ysizeorig * \ratio{\lp@ynum}{\lp@ydenom}}% y-size in the doc
  % Save scaled picture  
  \sbox{\lp@pictbox}% 
       {\epsfig{figure=\lp@epsfile, width=\lp@tmpx, height=\lp@tmpy}}%
  % Calculate sizes and coordinates of the origin of the whole pict 
  \setlength{\lp@tmpa}{\lp@tmpx + \lpmarginleft + \lpmarginright}%
  \setcounter{lp@xtotalsize}{\lp@tmpa / \unitlength}%
  \def\lp@xtotalsize{\value{lp@xtotalsize}}%
  \setlength{\lp@tmpa}{\lp@tmpy + \lpmargintop + \lpmarginbottom}%
  \setcounter{lp@ytotalsize}{\lp@tmpa / \unitlength}%
  \def\lp@ytotalsize{\value{lp@ytotalsize}}%
  \setcounter{lp@xorigin}{-\lpmarginleft / \unitlength}%
  \def\lp@xorigin{\value{lp@xorigin}}%
  \setcounter{lp@yorigin}{-\lpmarginbottom / \unitlength}%
  \def\lp@yorigin{\value{lp@yorigin}}%
  % Redefine margins in units of the original
  \setlength{\lp@marginleft}{\lpmarginleft / \ratio{\lp@xnum}{\lp@xdenom}}%
  \setlength{\lp@marginright}{\lpmarginright / \ratio{\lp@xnum}{\lp@xdenom}}%
  \setlength{\lp@margintop}{\lpmargintop / \ratio{\lp@ynum}{\lp@ydenom}}%
  \setlength{\lp@marginbottom}{\lpmarginbottom / \ratio{\lp@ynum}{\lp@ydenom}}%
  \setlength{\lp@xtotalsizeorig}%
	    {\lp@xsizeorig + \lp@marginleft + \lp@marginright}%
  \setlength{\lp@ytotalsizeorig}%
	    {\lp@ysizeorig + \lp@margintop + \lp@marginbottom}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Define \lp@put(x,y)[position]{object}
% \lp@multiput(x,y)(deltax,deltay)[position]{count}{object} 
% where (x,y)(deltax,deltay) are lengths in the original unscaled eps
% picture.
% 
\def\lp@put(#1,#2)[#3]#4%
{{%
  \setlength{\lp@tmpx}{( #1 ) * \ratio{\lp@xnum}{\lp@xdenom}}%
  \setlength{\lp@tmpy}{( #2 ) * \ratio{\lp@ynum}{\lp@ydenom}}%
  \setcounter{lp@tmpx}{\lp@tmpx / \unitlength}%
  \setcounter{lp@tmpy}{\lp@tmpy / \unitlength}%
  \put(\value{lp@tmpx},\value{lp@tmpy})%
      {\makebox(0,0)[#3]{#4}}%
}}
 
\def\lp@multiput(#1,#2)(#3,#4)(#5)[#6]#7%
{{%
  \setlength{\lp@tmpx}{( #1 ) * \ratio{\lp@xnum}{\lp@xdenom}}%
  \setlength{\lp@tmpy}{( #2 ) * \ratio{\lp@ynum}{\lp@ydenom}}%
  \setcounter{lp@tmpx}{\lp@tmpx / \unitlength}%
  \setcounter{lp@tmpy}{\lp@tmpy / \unitlength}%
  \setlength{\lp@tmpdx}{( #3 ) * \ratio{\lp@xnum}{\lp@xdenom}}%
  \setlength{\lp@tmpdy}{( #4 ) * \ratio{\lp@ynum}{\lp@ydenom}}%
  \setcounter{lp@tmpdx}{\lp@tmpdx / \unitlength}%
  \setcounter{lp@tmpdy}{\lp@tmpdy / \unitlength}%
  \multiput(\value{lp@tmpx},\value{lp@tmpy})%
           (\value{lp@tmpdx},\value{lp@tmpdy})%
           {#5}%
	   {\makebox(0,0)[#6]{#7}}%
}}

\def\lp@hline#1%
{{%
  \setlength{\lp@tmpa}{#1}%
  \setlength{\lp@tmpa}{\lp@tmpa * \ratio{\lp@xnum}{\lp@xdenom}}%
  \setcounter{lp@tmpa}{\lp@tmpa / \unitlength}%
  \line(1,0){\value{lp@tmpa}}%
}}

\def\lp@vline#1%
{{%
  \setlength{\lp@tmpa}{#1}%
  \setlength{\lp@tmpa}{\lp@tmpa * \ratio{\lp@ynum}{\lp@ydenom}}%
  \setcounter{lp@tmpa}{\lp@tmpa / \unitlength}%
  \line(0,1){\value{lp@tmpa}}%
}}

% \lp@put.....
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Drawing commands
%

% \lp@pictframecmd draws a frame around all of the picture including
% margins. \lpframethickness -- thickness of the lines
\newcommand{\lp@pictframecmd}
{\begingroup%
  \linethickness{\lpframethickness}%
  \lp@put(-\lp@marginleft,-\lp@marginbottom)[l]
	 {\lp@hline{\lp@xsizeorig+\lp@marginleft+\lp@marginright}}%
  \lp@put(-\lp@marginleft,-\lp@marginbottom)[b]
	 {\lp@vline{\lp@ysizeorig+\lp@margintop+\lp@marginbottom}}%
  \lp@put(\lp@xsizeorig+\lp@marginright,\lp@ysizeorig+\lp@margintop)[r]
	 {\lp@hline{\lp@xsizeorig+\lp@marginleft+\lp@marginright}}%
  \lp@put(\lp@xsizeorig+\lp@marginright,\lp@ysizeorig+\lp@margintop)[t]
	 {\lp@vline{\lp@ysizeorig+\lp@margintop+\lp@marginbottom}}%
\endgroup}

% \lp@gridcmd draws a grid with the step \value{lpgridstep} x
% \lpunitlength wrt original and lines \lpgridthickness thick
\newcommand{\lp@gridcmd}
{%
\begingroup%
  \linethickness{\lpgridthickness} % 
  \setlength{\lp@gridstep}{\lpunitlength * \value{lpgridstep}}%
  \setcounter{lp@tmpa}{\lp@marginleft / \lp@gridstep}%
  \setlength{\lp@tmpa}{-\lp@gridstep * \value{lp@tmpa}}%
  \setcounter{lp@tmpb}
	     {(\lp@xsizeorig-\lp@tmpa+\lp@marginright ) / %
	       \lp@gridstep + 1}%
  \lp@multiput(\lp@tmpa,-\lp@marginbottom)%
              (\lp@gridstep,0mm)%
              (\value{lp@tmpb})%
              [b]%
              {\lp@vline{\lp@ysizeorig+\lp@margintop+\lp@marginbottom}}%
  \setcounter{lp@tmpa}{\lp@marginbottom / \lp@gridstep}%
  \setlength{\lp@tmpa}{-\lp@gridstep * \value{lp@tmpa}}%
  \setcounter{lp@tmpb}
	     {(\lp@ysizeorig-\lp@tmpa+\lp@margintop ) / %
	       \lp@gridstep + 1}%
  \lp@multiput(-\lp@marginleft,\lp@tmpa)%
              (0mm,\lp@gridstep)%
              (\value{lp@tmpb})%
              [l]%
              {\lp@hline{\lp@xsizeorig+\lp@marginleft+\lp@marginright}}%
\endgroup%
}

% \lp@coordscmd draws coordinates on all four sides of the picture
% with the step \lpunitlength * \value{lpcoordstep}
\newcommand{\lp@coordscmd}
{\begingroup%
  \setlength{\lp@coordstep}{\lpunitlength * \value{lpcoordstep}}%
  \setcounter{lp@tmpa}{\lp@marginleft / \lp@coordstep}
  \setlength{\lp@tmpa}{-\lp@coordstep * \value{lp@tmpa}}
  \setcounter{lp@tmpb}
	     {(\lp@xsizeorig-\lp@tmpa+\lp@marginright) / \lp@coordstep+1}
  \setcounter{lp@tmpc}{\lp@tmpa / \lpunitlength}
  \lp@multiput(\lp@tmpa,-\lp@marginbottom-\lpframethickness-0.1mm)%
              (\lp@coordstep,0mm)%
              (\value{lp@tmpb})%
              [t]%
              {%
		{\tiny\bf\arabic{lp@tmpc}}%
		\addtocounter{lp@tmpc}{\value{lpcoordstep}}%
	      }%
  \setcounter{lp@tmpc}{\lp@tmpa / \lpunitlength}
  \lp@multiput(\lp@tmpa,\lp@ysizeorig+\lp@margintop+\lpframethickness)%
              (\lp@coordstep,0mm)%
              (\value{lp@tmpb})%
              [b]%
              {%
		{\tiny\bf\arabic{lp@tmpc}}%
		\addtocounter{lp@tmpc}{\value{lpcoordstep}}%
	      }%
  \setcounter{lp@tmpa}{\lp@marginbottom / \lp@coordstep}
  \setlength{\lp@tmpa}{-\lp@coordstep * \value{lp@tmpa}}
  \setcounter{lp@tmpb}
	     {(\lp@ysizeorig-\lp@tmpa+\lp@margintop)/\lp@coordstep+1}
  \setcounter{lp@tmpc}{\lp@tmpa / \lpunitlength}
  \lp@multiput(-\lp@marginleft-\lpframethickness,\lp@tmpa)%
              (0mm,\lp@coordstep)%
              (\value{lp@tmpb})%
              [r]%
              {%
		{\tiny\bf\arabic{lp@tmpc}}%
		\addtocounter{lp@tmpc}{\value{lpcoordstep}}%
	      }%
  \setcounter{lp@tmpc}{\lp@tmpa / \lpunitlength}
  \lp@multiput(\lp@xsizeorig+\lp@marginright+\lpframethickness,\lp@tmpa)%
              (0mm,\lp@coordstep)%
              (\value{lp@tmpb})%
              [l]%
              {%
		{\tiny\bf\arabic{lp@tmpc}}%
		\addtocounter{lp@tmpc}{\value{lpcoordstep}}%
	      }%
\endgroup}


% \lp@lblframecmd draws a frame around each label \lplblframethickness
% thick 
\newcommand{\lp@lblframecmd}[1]
{\begingroup%
  \setlength{\linethickness}{\lplblframethickness}%
  \frame{#1}%
\endgroup}

% \lp@figframecmd draws a frame around eps figure \lpfigframethickness 
% thick 
\newcommand{\lp@figframecmd}[1]
{\begingroup%
  \setlength{\linethickness}{\lpfigframethickness}%
  \frame{#1}%
\endgroup}

%
\newcommand{\lp@putepsfigurecmd}
{%
  \put(0,0){\makebox(0,0)[bl]{\lp@figframe{\usebox{\lp@pictbox}}}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% options

% Print nothing but the picture and labels
\def\lpopt@clean
{%
  \let\lp@lblframe=\relax%
  \let\lp@figframe=\relax%
  \let\lp@pictframe=\relax%
  \let\lp@coords=\relax%
  \let\lp@grid=\relax%
  \let\lbl=\lbl@cmd%
  \let\lp@putepsfigure=\lp@putepsfigurecmd%
}

% Set margins
\def\lpopt@l#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpmarginleft}{#1}}
	     {}%
}
\def\lpopt@r#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpmarginright}{#1}}
	     {}%
}
\def\lpopt@t#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpmargintop}{#1}}
	     {}%
}
\def\lpopt@b#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpmarginbottom}{#1}}
	     {}%
}

% Print frame around whole picture margins included
\def\lpopt@frame#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpframethickness}{#1}}{}%
\let\lp@pictframe=\lp@pictframecmd%
}

% Print frame around eps figure
\def\lpopt@figframe#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lpfigframethickness}{#1}}{}%
  \let\lp@figframe=\lp@figframecmd%
}

% Draw frames around labels
\def\lpopt@frames#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setlength{\lplblframethickness}{#1}}{}%
  \let\lp@lblframe=\lp@lblframecmd%
}

% Write coordinates on all four sides of the picture
% argument=step
\def\lpopt@coords#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setcounter{lpcoordstep}{#1}}{}%
  \let\lp@coords=\lp@coordscmd%
}

% Draw a grid, argument=step
\def\lpopt@grid#1%
{%
  \ifthenelse{\not\equal{#1}{}}
             {\setcounter{lpgridstep}{#1}}{}%
  \let\lp@grid=\lp@gridcmd%
}

% Ignore all the labels
\def\lpopt@nolbl{\renewcommand{\lbl}[2][]{\relax}}

% Don't insert epsfigure
\def\lpopt@nofigure{\let\lp@putepsfigure=\relax}

% Draw what is usually convenient for adjusting the labels
\def\lpopt@draft
{%
\lpopt@figframe{}\relax%
\lpopt@frame{}\relax%
\lpopt@coords{}\relax%
\lpopt@grid{}\relax%
\lpopt@frames{}\relax%
}

% Print help
\def\cmd#1{{\tt $\backslash$#1}}

\def\lpopt@help%
{%
\newpage\noindent
The package {\tt\bf\tt lpic} allows to put any \LaTeX{} material on top of
included graphics. It can also create white background rectangle under \LaTeX annotations.

It defines an environment {\tt lpic} and two
commands \cmd{lbl} and \cmd{lpichelp}.  Command \cmd{lpichelp} dumps
usage instructions into the document.  

\vspace{5mm}
\centerline{\bf USAGE:}\noindent
The environment \vspace{2mm}\par\noindent
\cmd{begin}\{{\tt lpic}\}[{\sc OPTIONS}]
   \{$<${\sc filename}$>${\sc DIMENSIONS}\}
   \begin{quote}
     \cmd{lbl}[{\sc POSITION}]
     \{{\sc x-coord,y-coord,angle; \LaTeX\ material}\}\\
     \vdots
   \end{quote}
   \cmd{end}\{{\tt lpic}\} \vspace{2mm}\par\noindent
produces a \TeX\ box with included graphics and the \LaTeX{} material on
top of it.

\begin{itemize}
  \vspace{2mm}
\item[$<${\sc filename}$>$] is a name of external
  graphics in {\tt PS, EPS} or {\tt PDF} format.  There is no need
  to give the extension of the file, appropriate format will be
  chosen, depending whether latex or pdflatex is run on the
  document. Convention is the same as for {\tt epsfig} package.
  \vspace{2mm}
\item[{\sc DIMENSIONS}] is one of the following:
  \begin{enumerate}
  \item empty string 
  \item {\tt () }	
  \item {\tt (,) }
  \item {\tt (X) }
  \item {\tt (X,) }
  \item {\tt (,Y) }
  \item {\tt (X,Y) }
  \end{enumerate}
  where each {\tt X} and {\tt Y} is either a positive decimal (with
  decimal point, not coma!) or length (any length expression
  understandable to \TeX).  Decimal number is interpreted as a scale
  coefficient and length is interpreted as the desired horisontal or
  vertical dimension of the figure.
  
  If neither {\tt X} nor {\tt Y} are supplied (cases 1, 2, 3), then
  the original unscaled picture is included.  If only one of {\tt X} and
  {\tt Y} is given (cases 4, 5, 6), then picture is scaled
  homotetically.  If both are present (case 7), then graphics is
  scaled vertically and horisontally, according to given parameters.\\
  For example: \\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation}\} -- means include
  graphics in the file {\tt tessalation} at the original size.\\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation(0.5)}\} -- means
  include graphics at the half the original size.\\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation(0.5,3)}\} -- means
  include graphics shrinking twice in the horisontal direction and
  stretching 3 times in the vertical direction.\\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation(,10cm)}\} -- include
  graphics homotetically scaled, so that height is equal to 10cm.\\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation(150mm)}\} -- include
  graphics homotetically scaled, so that width is equal to 150mm.\\
  \cmd{begin}\{{\tt lpic}\}\{{\tt tessalation(150mm,0.7)}\} --
  include graphics scaled, so that width is equal to 150mm and
  height is 0.7 of the original.

  \vspace{2mm}
  \item[\sc OPTIONS] is a coma separated list of options (no spaces,
    this is a bug, I will fix it sometimes). Option are processed
    sequentially, thus options to the right override preceding ones.
    Parameter in {\tt ()} is optional, if omitted then some default
    value is taken.
    
    Options are:
    	\item[{\tt l(length)}] -- the size of the left margin beyond
			bounging box of graphics (default 0mm).
	\item[{\tt r(length)}] - right margin 
	\item[{\tt t(lenght)}] - top margin
	\item[{\tt b(length)}] -- bottom maqrgin
	\item[{\tt grid(step)}] -- Draw a grid over the picture of
			step {\tt step*}\cmd{lpunitlength} and
			thickness \cmd{lpgridthickness}.  Parameter
			{\tt step} must be an integer and defaults to 5.
        \item[{\tt coords(step)}] -- write coordinates with the step
			{\tt step*}\cmd{lpunitlength}.  Parameter step
			must be an integer and defaults to 10.
        \item[{\tt frames(length)}] -- draw boxes of thickness {\tt
			length} over \LaTeX{} material. Default value is
			0.01mm
	\item[{\tt frame(length)}] -- draw a box around the whole
			thing (margins included). {\tt length} is the
			thickness of the lines.  Default value is
			0.7mm.
        \item[{\tt figframe(length)}] -- draw a box around the
			included graphics.  {\tt length} is the
			thickness of the lines.  Default value is
			0.2mm.
        \item[{\tt draft}] -- draw whatever is convenient for
			adjusting the positions of \LaTeX{}
			material. That includes grid, coordinates, all
			types of frames.
	\item[{\tt clean}] -- remove all auxiliary stuff.
	\item[{\tt nolbl}] -- don't typeset any \LaTeX{} material.
	\item[{\tt nofigure}] -- don't include the graphics, just an
			empty box of appropriate size
  
\end{itemize}
  \vspace{2mm}\par\noindent
  Inside of {\tt lpic} environment one can issue \cmd{lbl} command
    with the following syntax:\vspace{2mm}\par 
    {\tt \cmd{lbl}[{\sc OPTIONS}]%
      \{{\sc COORDINATES; any \LaTeX{} material}\}}
     \vspace{2mm}\par\noindent  
    \LaTeX{} material is put in a box, rotated if necessary and printed
    in appropriate place. White rectangular background, which overshadows part of graphics could also be created.

\begin{itemize}
\item[\sc COORDINATES] are either pair or a triple of decimals.  The
  first two numbers are coordinates of the point, where \LaTeX{} material
  should be placed and the third is an angle of rotation applied to
  the box containing the material.  Coordinates are measured in the
  coordinate system in the original, unscaled picture, with the unit
  equal to \cmd{lpunitlength} (default 1mm). So, if the picture is
  scaled, all the boxes containing \LaTeX{} will hopefully stay at their
  apropriate places.
\item[\sc OPTIONS] is optional and consists of at most three letters,
  one from the set {\tt\{t,b\}}, one from {\tt\{r,l\}} and one from {\tt\{w,W\}}
  in any
  order.  {\tt [t$|$b,l$|$r]} options give a reference point in a box containing \LaTeX{}
  text. Coordinates refer to this reference point.  Rotation is also
  centered at this point. Thus, there are total 9 possibilities
  refering to 9 evenly spaced points in the rectangle.
  If one of {\tt [w$|$W]} is given then \LaTeX is put on top of white box.
  The size of the box is equal to the size of the box containing the material in case of  {\tt w}
  and margins \cmd{lpbgsep}-wide are added in case of {\tt W}.
  Note that {\tt xdvi} apparently renders postscript after dvi, so any postscript covers dvi material.
  Thus, if you use background, you will not be able to see your latex in {\tt xdvi}.
  but once postscript file is created, \LaTeX{} will show.
\end{itemize}

\vspace{5mm}\par
\centerline{\bf ADJUSTABLE PARAMETERS:} 

  \begin{itemize}
  \item[\cmd{lpunitlength}] (1mm) Units in the unscaled coordinate
    system
  \item[\cmd{lpmarginright}] (0mm)	
  \item[\cmd{lpmarginleft}] (0mm)
  \item[\cmd{lpmargintop}] (0mm)
  \item[\cmd{lpmarginbottom}] (0mm) Default sizes of margins.
  \item[\cmd{lpbgsep}] (\cmd{fboxsep}) Margins for the white background.
  \item[\cmd{lpgridthickness}] (0.01mm) Thickness of the grid lines.
  \item[\cmd{lpframethickness}] (0.7mm) Thickness of the frame around
    the whole box.
  \item[\cmd{lplblframethickness}] (0.01mm) Thickness of the frame
    around labels.
  \item[\cmd{lpfigframethickness}] (0.2mm) Thickness of the frame
    around included graphics.
  \item[\tt lpgridstep] (5) Counter containing the step to draw grid lines
  \item[\tt lpcoordstep] (10) Counter containing the step to draw coordinates
  \end{itemize}

\vspace{5mm}\par
\centerline{\bf TODO:} 
  \begin{enumerate}
    \item
      Optimise the whole thing.
    \item
      Write legible usage.
    \item 
      Spaces in the list of options make troubles!
    \item
      Angles should be measured in unscaled coordinate system!
    \item
      Different steps for the grid and coordinates in vertical and
      horisontal direction. 
    \item With nofigure frame is not drawn.
    \end{enumerate}



\newpage


}




\let\lpichelp=\lpopt@help
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Option processing commands
%
%

% Should be called \exec@option{name(arg)} or 
% \exec@option{name}. It executes \name and passes
% arg if present as an argument.
% That is \name should have at most one argument.

\def\exec@optionmisc#1(#2)#3ENDOFTHEARGS%
{%
  \csname#1\endcsname{#2}\relax%
}
\newcommand{\exec@option}[1]
{%
  \exec@optionmisc#1()ENDOFTHEARGS%
}


% Called with a coma-separated list of options, and then executes
% each ``name'' prefixed with the value of \option@prefix. It passes
% this command the argument if present. Each option is either
% ``name'' or ``name(arg)''. ``arg'' should not contain comas or
% should be enclosed in {}.
\def\process@optionsmisc#1,#2ENDOFTHEARGS%
{%
  %\lp@msg{1=#1, 2=#2}
  \ifthenelse{\not\equal{#1}{}}%
     {%
       \exec@option{\option@prefix#1}%
       \process@optionsmisc#2ENDOFTHEARGS%
     }%
     {}%
}
\newcommand\process@options[1]
{%
  \process@optionsmisc#1,,ENDOFTHEARGS%
}


% Next command process ``filename'' argument. 
% Defines \lp@epsfile, \lp@xscale, \lp@yscale
% Syntaxis is one of the following
% \lp@getscaleandfile{FILE}
% \lp@getscaleandfile{FILE()}
% \lp@getscaleandfile{FILE(,)}
% \lp@getscaleandfile{FILE(x)}
% \lp@getscaleandfile{FILE(x,)}
% \lp@getscaleandfile{FILE(,y)}
% \lp@getscaleandfile{FILE(x,y)}

\def\lp@getname#1(#2ENDOFTHEARGS{\edef\lp@epsfile{#1}\edef\lp@string{#2}}
\def\lp@getscales#1)#2ENDOFTHEARGS{\edef\lp@string{#1}}
\def\lp@getxscale#1,#2ENDOFTHEARGS{\edef\lp@xscale{#1}\edef\lp@string{#2}}
\def\lp@getyscale#1,#2ENDOFTHEARGS{\edef\lp@yscale{#1}\edef\lp@string{#2}}

\def\lp@getscaleandfile#1%
{%
  \edef\lp@epsfile{}%
  \edef\lp@xscale{}%
  \edef\lp@yscale{}%
  \edef\lp@string{#1}%
  \expandafter\lp@getname\lp@string(ENDOFTHEARGS%
  \expandafter\lp@getscales\lp@string)ENDOFTHEARGS%
  \expandafter\lp@getxscale\lp@string,ENDOFTHEARGS%
  \expandafter\lp@getyscale\lp@string,ENDOFTHEARGS%
  %\lp@msg{file=\lp@epsfile, x=\lp@xscale, y=\lp@yscale}%
}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{lpic}[coma separated options]{filename[(xscale[,yscale])]},
% \lbl[position]{xcoord,ycoord;object}
% ....
% \end{lpic}
% Where options are clean, draft, grid, frame, frames, coords, nolbl
% Position is at most two of the following t,b,l,r
\newenvironment{lpic}[2][clean]
{\begingroup
  %Process options
  \edef\option@prefix{lpopt@}
  \setlength{\unitlength}{1sp}%
  \lpopt@clean%
  \process@options{#1}%
  % read scale and filename from the second argument
  \expandafter\lp@getscaleandfile{#2}%
  % Calculate parameters
  \lp@dimens%
  % Start picture environment
  \begin{picture}(\lp@xtotalsize,\lp@ytotalsize)
                 (\lp@xorigin,\lp@yorigin)
    %insert picture. We do it before any LaTeX, so that white bg rectangles are on top
    \lp@putepsfigure
}
{%
    %draw a frame around the whole picture
    \lp@pictframe
    % draw grid
    \lp@grid
    % draw coordinates
    \lp@coords
  \end{picture}%
\endgroup}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \lbl[position]{<xcoord>,<ycoord>[,angle];object}
%

% read coordinates and the angle from the argument of \lbl
\def\lp@getxya#1,#2,#3,#4ENDOFTHEARGS%
{%
  \setlength{\lp@tmpx}{\lpunitlength * \real{#1} * \ratio{\lp@xnum}{\lp@xdenom}}%
  \setcounter{lp@tmpx}{\lp@tmpx / \unitlength}%
  \setlength{\lp@tmpy}{\lpunitlength * \real{#2} * \ratio{\lp@ynum}{\lp@ydenom}}%
  \setcounter{lp@tmpy}{\lp@tmpy / \unitlength}%
  \def\lp@angle{#3}%
}

% disassemble the arg of \lbl
\def\lp@getdata#1;#2#3ENDOFTHEARGS%
{%
  \lp@getxya#1,,,ENDOFTHEARGS% 
  \def\lp@lbldata{#2#3}%
}

\newcommand{\lp@getcoords}[1]
{%
  \lp@getdata#1ENDOFTHEARGS%
}

%\setlength{\lpbgboxsep}{10mm}

\newcommand{\lbl@cmd}[2][]
{\begingroup%
    % Set the defaults
    \def\lp@lblbox{\makebox(0,0)[#1]} % 
    \def\lp@bgbox{\relax}         % No background
    \setcounter{lp@tmpdx}{0}      % No shifting
    \setcounter{lp@tmpdy}{0}
    % Dissasemble the second argument
    \lp@getcoords{#2}%
    % Let's see whether angle is supplied
    \ifthenelse{\equal{\lp@angle}{}}
               {\let\lp@rotatelbl=\relax}
	       {\def\lp@rotatelbl{\turnbox{\lp@angle}}}%
    % What options are given?
    % ``W'' put white background with margins
    \lp@ifsubstring W in #1;%
	{%
	  \setlength{\lp@tmpa}{0.152mm} % this is to reduce some strange gap
				      % that appears when using \spbox
	  \setcounter{lp@tmpdx}{( \lpbgboxsep + \lp@tmpa ) / \unitlength}%
	  \setcounter{lp@tmpdy}{( \lpbgboxsep + \lp@tmpa ) / \unitlength}%
	  \def\lp@bgbox##1{%  
            \begingroup%
            \setlength{\fboxsep}{\lpbgboxsep}%
            \setbox0=\hbox{\spbox{##1}}%
            \hbox{%
              \special{
                ps: gsave currentpoint translate
                0
                \number\dp0 \space 7900 div    
                \number\wd0 \space 7900 div    
                \number\ht0 \space -7900 div   
                whitebgbox grestore
              }%
              \copy0%
            }%
            \endgroup%
          }%
        }%
	{}%
    % ``w'' put white background without margins
    \lp@ifsubstring w in #1;%
	{%
	  \def\lp@bgbox##1{%
            \begingroup%
            \setlength{\fboxsep}{0mm}%
            \setbox0=\hbox{\spbox{##1}}%
            \hbox{%
              \special{
                ps: gsave currentpoint translate
                0
                \number\dp0 \space 7900 div    
                \number\wd0 \space 7900 div    
                \number\ht0 \space -7900 div   
                whitebgbox grestore
              }%
              \copy0%
            }%
            \endgroup%
          }%
	}%
	{}%
    % Positioning
    \lp@ifsubstring t in #1;{\addtocounter{lp@tmpy}{\value{lp@tmpdy}}}{}%
    \lp@ifsubstring b in #1;{\addtocounter{lp@tmpy}{-\value{lp@tmpdy}}}{}%
    \lp@ifsubstring l in #1;{\addtocounter{lp@tmpx}{-\value{lp@tmpdx}}}{}%
    \lp@ifsubstring r in #1;{\addtocounter{lp@tmpx}{\value{lp@tmpdx}}}{}%
    % Put the material	
    \put(\value{lp@tmpx},\value{lp@tmpy})%
        {\lp@rotatelbl{\lp@lblbox{\lp@bgbox{\lp@lblframe{\lp@lbldata}}}}}%
\endgroup}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PS definitions for the white bg
\special{! TeXDict 
  begin
  /whitebgbox{% Needs : x0 y0 x1 y1.
    newpath 2 copy moveto 3 copy pop exch lineto 4 copy pop pop
    lineto 4 copy exch pop exch pop lineto closepath 1 setgray fill} bind def
  end
}
