summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lpic/lpic.sty
blob: 923f44e8e97cf90c8e00fc0a518a7c78e7f7667c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
%% lpic.sty
%% Copyright 2010 R. Matveyev
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is R. Matveyev.
%
% This work consists of the files: 
%       lpic.sty  instructions.tex
%       instructions.pdf instructions-differential.eps 
%       instructions-differential.pdf README 

% lpic.sty V0.8, 23.12.2010
% 

\ProvidesPackage{lpic}
    [2010/12/23, version 0.8]

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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}%
   }

%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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} 
	  \setcounter{lp@tmpdx}{( \lpbgboxsep + \lp@tmpa ) / \unitlength}%
	  \setcounter{lp@tmpdy}{( \lpbgboxsep + \lp@tmpa ) / \unitlength}%
	  \def\lp@bgbox##1{%  
            \begingroup%
            \setlength{\fboxsep}{\lpbgboxsep}%
            \colorbox{white}{##1}
            \endgroup%
          }%
        }% 
	{}%
    % ``w'' put white background without margins
    \lp@ifsubstring w in #1;%
	{%
	  \def\lp@bgbox##1{%
            \begingroup%
            \setlength{\fboxsep}{0mm}%
            \colorbox{white}{##1}%
            \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
%}