summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/mpostinl/mpostinl.dtx
blob: 1039b113bcc2c1995518949831163cde62236e44 (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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
% \iffalse
%
% mpostinl.dtx Copyright (C) 2010-2018 Niklas Beisert
%
% 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 Niklas Beisert.
%
% This work consists of the files mpostinl.dtx and mpostinl.ins
% and the derived files mpostinl.sty and mpinlsmp.tex
%
%<package|sample>\NeedsTeXFormat{LaTeX2e}[1996/12/01]
%<package>\ProvidesPackage{mpostinl}[2018/12/30 v1.5 metapost inline figures]
%<sample>\ProvidesFile{mpinlsmp.tex}[2018/12/30 v1.5 sample for mpostinl]
%<*driver>
%\ProvidesFile{mpostinl.drv}[2018/12/30 v1.5 mpostinl reference manual file]
\PassOptionsToClass{10pt,a4paper}{article}
\documentclass{ltxdoc}

\usepackage[margin=35mm]{geometry}
\usepackage{hyperref}
\usepackage{hyperxmp}
\usepackage[usenames]{color}
\usepackage{amsmath,amsfonts}
\usepackage{mflogo}
\usepackage[utf8]{inputenc}

\hypersetup{colorlinks=true}
\hypersetup{pdfstartview=FitH}
\hypersetup{pdfpagemode=UseNone}
\hypersetup{pdfsource={}}
\hypersetup{pdflang={en-UK}}
\hypersetup{pdfcopyright={Copyright 2010-2018 Niklas Beisert.
  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.}}
\hypersetup{pdflicenseurl={http://www.latex-project.org/lppl.txt}}
\hypersetup{pdfcontactaddress={ETH Zurich, ITP, HIT K,
  Wolfgang-Pauli-Strasse 27}}
\hypersetup{pdfcontactpostcode={8093}}
\hypersetup{pdfcontactcity={Zurich}}
\hypersetup{pdfcontactcountry={Switzerland}}
\hypersetup{pdfcontactemail={nbeisert@itp.phys.ethz.ch}}
\hypersetup{pdfcontacturl={http://people.phys.ethz.ch/\xmptilde nbeisert/}}

\newcommand{\secref}[1]{\hyperref[#1]{section \ref*{#1}}}

\parskip1ex
\parindent0pt
\let\olditemize\itemize
\def\itemize{\olditemize\parskip0pt}

\begin{document}

\title{The \textsf{mpostinl} Package}
\hypersetup{pdftitle={The mpostinl Package}}
\author{Niklas Beisert\\[2ex]
  Institut f\"ur Theoretische Physik\\
  Eidgen\"ossische Technische Hochschule Z\"urich\\
  Wolfgang-Pauli-Strasse 27, 8093 Z\"urich, Switzerland\\[1ex]
  \href{mailto:nbeisert@itp.phys.ethz.ch}
  {\texttt{nbeisert@itp.phys.ethz.ch}}}
\hypersetup{pdfauthor={Niklas Beisert}}
\hypersetup{pdfsubject={Manual for the LaTeX2e Package mpostinl}}
\date{30 December 2018, \textsf{v1.5}}
\maketitle

\begin{abstract}\noindent
\textsf{mpostinl} is a \LaTeXe{} package
which enables the embedding of \MP{} figures
within a \LaTeX{} document.
The package automatically collects the embedded
definitions and figures in a |.mp| file,
adds an appropriate \LaTeX{} document structure,
and compiles it to |.mps| files.
It also allows for various configuration options
to manage the generation of files and compilation.
\end{abstract}

\begingroup
\parskip0ex
\tableofcontents
\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}

\MP{} is a versatile tool
to generate vector graphics files
from a plain text source for inclusion
in \LaTeX{} documents which allows to typeset labels
in native \LaTeX{} code and fonts.
The \MP{} compiler |mpost| typically compiles a
metapost source file (extension |.mp|) to
a set of metapost figure files (extension |.mps|)
which are encapsulated postscript files (|.eps|)
with a somewhat reduced scope.
The figure files can be included in a \LaTeX{}
document by |\includegraphics|,
and modern \TeX{} distributions are typically able to handle
the required conversions on the fly.

While many steps in the compilation process are automated,
some management is left to the user:
\begin{itemize}
\item
Link the figures in the metapost source
with the \LaTeX{} source by choosing an
appropriate figure filename (or numeric identifier).
\item
When a figure is changed,
the metapost source file must be compiled
to update the figure files,
and afterwards the \LaTeX{} source must be compiled
to introduce the changes into the output document.
\item
Keep any changes aligned between \LaTeX{}
and metapost source files.
\item
Set up a proper \LaTeX{} document structure
to compile labels via \LaTeX{}.
\item
Distribute source and figures as separate files.
\end{itemize}
%
The current \LaTeX{} package \textsf{mpostinl}
helps in the management of metapost figures
by embedding them into the \LaTeX{} source:
%
\begin{itemize}
\item
Figures are displayed at the location of their definition
within the \LaTeX{} source
to facilitate alignment between text and figures.
\item
A metapost source file with \LaTeX{} structure
for the labels is generated.
\item
Figure files are compiled automatically
from within the \LaTeX{} compiler.
\end{itemize}
%
For example, a simple figure consisting
of a circle might be represented as:
%
\begin{verbatim}
  \begin{mpostfig}
  draw fullcircle scaled 1cm;
  \end{mpostfig}
\end{verbatim}
%
The package also offers several options and customisations
to streamline its use in several situations:
%
\begin{itemize}
\item
Figures can be assigned labels or filenames for later usage.
\item
There are several options and mechanisms to
minimise the need for multiple compiler passes
to generate the desired output.
\item
The package can handle several metapost files in a row
with common definitions or include files.
\item
The font generation, \LaTeX{} structure
and generated filenames can be customised.
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Related CTAN Packages}

There are at least four other \LaTeX{} packages
which offer a similar functionality:
%
\begin{itemize}
\item
The package \href{http://www.ctan.org/pkg/emp}{\textsf{emp}}
provides similar basic functionality to compose a metapost file,
but it does not automatically compile it.
Analogously to the |picture| environment
the size for every figure must be specified explicitly.
\item
The package \href{http://ci.uofl.edu/tom/software/LaTeX/}{\textsf{mpfig}}
by Tomasz Cholewo (not available on CTAN)
provides very basic functionality to compose a metapost file.
\item
The package \href{http://www.ctan.org/pkg/mpgraphics}{\textsf{mpgraphics}}
provides similar functionality to compose and compile a metapost file.
It processes all figures immediately and does not offer labels
for recycling figures.
\item
The package \href{http://www.ctan.org/pkg/gmp}{\textsf{gmp}}
provides similar functionality to compose and compile a metapost file.
It processes all figures immediately and allows to inject \LaTeX{}
definitions into the metapost code at the price of
modifying the metapost syntax slightly.
\end{itemize}
%
The philosophy of the present package is to generate a
single metapost file containing all figures
as in a traditional metapost setup
which can be compiled in one pass.
The aim is to provide a metapost setup which works
with as little configuration as possible,
but which offers several configuration options to
customise the management in the desired way.
The package offers most of the functionality of the above packages,
but (presently) misses out on some more advanced features,
see \secref{sec:suggestions}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Usage}

This manual assumes familiarity
with the \MP{} figure description language.
The \MP{} manual is a recommended introduction
and an excellent reference.

To use the package \textsf{mpostinl} add the command
\begin{center}
|\usepackage{mpostinl}|
\end{center}
to the preamble of your \LaTeX{} document.
If not yet present,
the package \textsf{graphicx} will be loaded automatically.
Metapost figures and definitions are to be specified using
the environments |mpostfig| and |mpostdef|, respectively,
as described in \secref{sec:figuresdefinitions}.

The package collects the figure files
contained in the \LaTeX{} source,
writes them to a metapost file,
and compiles them at the end of the \LaTeX{} document.
This means that the figures (or their updates)
will normally \emph{not} be available
in the first \LaTeX{} run and a secondary run is required
for the correct output,
see \secref{sec:immediate} for strategies
to avoid a second pass.

You should make sure that \LaTeX{} allows
calling of external programs.
If this feature is not enabled by default,
it is achieved by calling |latex|
with the command line option |-shell-escape|
(or |-shell-restricted| if |mpost|
is in the list of permissible commands):
\begin{center}
|latex -shell-escape |\textit{source}
\end{center}
In the MiK\TeX{} distribution the appropriate
command line option is |-enable-write18|.
In a \LaTeX{} front end this option may
be configurable in the preferences.
If the shell escape is not available,
the generated metapost file(s) \textit{filename}
(typically the same as the \LaTeX{} source \textit{source})
must be compiled manually:
\begin{center}
|mpost -tex=latex |\textit{filename}
\end{center}

Some extended configuration options and situations are described in
the following sections:
package options are listed in \secref{sec:packageoptions};
some options for writing and compiling are discussed
in \secref{sec:compiling};
the generation of multiple metapost files
is described in \secref{sec:multiplefiles};
finally, some issues regarding other \LaTeX{} packages
are discussed in \secref{sec:interactions}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Figures and Definitions}
\label{sec:figuresdefinitions}

\DescribeEnv{mpostfig}
The main functionality provided
by the package is the |mpostfig| environment:
\begin{center}
\begin{tabular}{l}
|\||begin{mpostfig}[|\textit{opts}|]|\\
|  |\textit{metapost code}\\
|\||end{mpostfig}|
\end{tabular}
\end{center}
%
The above block is translated
to the following code in the metapost file:
%
\begin{center}
\begin{tabular}{l}
|filenametemplate:="|\textit{filename}|";|\\
|beginfig(|\textit{number}|)|\\
|  |\textit{metapost code}\\
|endfig;|
\end{tabular}
\end{center}
%
The optional argument \textit{opts} of the |mpostfig| environment
is a comma-separated list of options:
%
\begin{itemize}
\item |show|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Show the figure in place.
If neither |file| nor |label| are specified,
this option is forced to |true|.

\item |file=|\textit{filename} --
Filename for the figure.

\item |label=|\textit{label} --
Label for later use by the command |\mpostuse{|\textit{label}|}|.

\item |opt=|\textit{opt} --
Options to be passed on to |\includegraphics[|\textit{opt}|]|.

\item |now|[|=true|$\vert$|false|]
(no value implies |true|, overrides global setting |nowall|) --
Compile figure immediately. Requires global option |now| to work.

\item |twice|[|=true|$\vert$|false|]
(no value implies |true|, overrides global setting |twice|) --
Compile this figure twice.

\end{itemize}
%
Please note the following restrictions
due to the implementation via the package \textsf{verbatim}:
%
\begin{itemize}
\item
The closing statement |\||end{mpostfig}| must be on a line on its own.
Any amount of leading whitespace is allowed,
and trailing characters are ignored.

\item
The opening statement |\||begin{mpostfig}| has to find out
whether an optional argument is provided
by scanning into the block of metapost content.
If none is given, all the code already scanned needs to be
(re)interpreted as metapost content.
In special circumstances this may cause problems.
It should then help to use an empty optional argument:
|\||begin{mpostfig}[]|.

\item
Lines starting with a single `|%|'
are ignored and do not appear in the metapost file.
Double `|%%|'
or leading spaces preserve comments in the metapost file.

\item
The environment |mpostfig|
cannot be used within macro arguments or particular other environments.
If you want to display a figure in these situations,
you should declare the figure with a label and
display it via the command |\mpostuse| (see below).

\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeMacro{\mpostuse}
Figures which have been previously declared
with a label \textit{label}
can be recycled any number of times with the command:
\begin{center}
|\mpostuse[|\textit{opts}|]{|\textit{label}|}|
\end{center}
The options are passed on to
|\includegraphics[|\textit{opt}|]{|\textit{filename}|}|.
Note that |\mpostuse| accepts the overlay specification
from the \textsf{beamer} package
\begin{center}
|\mpostuse<|\textit{overlay}|>[|\textit{opts}|]{|\textit{label}|}|
\end{center}
and passes it on to |\includegraphics|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeMacro{\mpostgetname}
Furthermore, the filename of a figure can be obtained by
calling |\mpostgetname{|\textit{label}|}|.
The filename is returned in the macro |\mpostfigurename|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeEnv{mpostdef}
Plain metapost code which is not part of a figure
(definitions, assignments)
can be specified by the |mpostdef| environment:
%
\begin{center}
\begin{tabular}{l}
|\||begin{mpostdef}[|\textit{opts}|]|\\
|  |\textit{code}\\
|\||end{mpostdef}|
\end{tabular}
\end{center}
%
Note that the same restrictions as for |mpostfig| (see above)
apply to |mpostdef|.
The optional argument \textit{opts}
is a comma-separated list of options:
%
\begin{itemize}
\item |global|[|=true|$\vert$|false|]
(no value implies |true|, overrides global setting |globaldef|) --
In a setup with multiple metapost files, the block \textit{code}
is applied to all files, not just the current file.

\item |tex|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
The block \textit{code} represents \TeX{} or \LaTeX{} definitions
rather than \MP{} code,
which will be enclosed in the metapost file by |verbatimtex| and |etex|.
This feature is superseded by the environment |mposttex| described below.
The |tex| option is deprecated, and provided for backward compatibility only.

\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeEnv{mposttex}
\TeX{} or \LaTeX{} definitions for use in metapost labels
can be specified by the |mposttex| environment:
%
\begin{center}
\begin{tabular}{l}
|\||begin{mposttex}[|\textit{opts}|]|\\
|  |\textit{code}\\
|\||end{mposttex}|
\end{tabular}
\end{center}
%
The code will be enclosed in the metapost file by |verbatimtex| and |etex|.
Note that the same restrictions as for |mpostfig| (see above)
apply to |mposttex|.
The optional argument \textit{opts}
is a comma-separated list of options:
%
\begin{itemize}
\item |global|[|=true|$\vert$|false|]
(no value implies |true|, overrides global setting |globaldef|) --
In a setup with multiple metapost files, the block \textit{code}
is applied to all files, not just the current file.

\item |dual|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
The definitions within the block \textit{code}
will be available to both metapost figures and subsequent \LaTeX{} code.

\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Package Options}
\label{sec:packageoptions}

\DescribeMacro{\mpostsetup}
Options can be passed to the package by:
\begin{center}
\begin{tabular}{rl}
&|\usepackage[|\textit{opts}|]{mpostinl}|
\\
or&|\PassOptionsToPackage{|\textit{opts}|}{mpostinl}|
\\
or&|\mpostsetup{|\textit{opts}|}|
\end{tabular}
\end{center}
%
|\PassOptionsToPackage| must be used before |\usepackage|;
|\mpostsetup| must be used afterwards (for selected options).
\textit{opts} is a comma-separated list of options.
Below we provide a complete list of available options
while some of the more relevant options are discussed
in detail in the following sections:
%
\begin{itemize}
\item |draft|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable |draft| mode by declaring |draft:=1|
at the top of the metapost file.

\item |final| --
Same as |draft=false|.

\item |write|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Enable/disable writing to metapost file.

\item |compile|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Enable/disable automatic compilation of metapost file.
Requires shell escapes to work properly.

\item |twice|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable secondary metapost compilation.
Some metapost files may require this to produce the intended output.

\item |clean|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Enable/disable cleaning of figure files prior to compilation.
A dummy figure will be shown in case metapost cannot generate a figure.

\item |checksum|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable update detection.
Compiles figures only if the metapost code has changed.

\item |fonts|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable embedding of fonts in metapost figures
by setting |prologues:=3| thus making them proper
encapsulated postscript files.
This option may be required if figure files are
used outside a \TeX{} environment,
e.g.\ if the files are to be viewed or processed by |.eps| tools.
Has no effect if |prologues| option is specified.

\item |prologues=|\textit{value} --
Declares |prologues:=|\textit{value} at the top of the metapost file.
Set \textit{value} to empty to disable the |prologues| statement.

\item |lineno|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable line number indicators in the metapost file.
All blocks in the metapost file will start with the line number
where this block can be found in the \LaTeX{} source file.
To view the source file name
you should load the package \textsf{currfile}.

\item |labelnames|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Use the figure label instead of a consecutive number
to construct the name for figure files.
The benefit of this mode is that the label will usually stay fixed
while the number may change when adding or removing figures.
The drawback is that the \LaTeX{} internal labels will appear as
part of the filenames cluttering the directory slightly more.
Moreover, one has to make sure that the figure labels
are valid filenames in your operating system,
i.e.\ use special characters with care,
better only use alphanumeric characters,
and bear in mind that some operating systems do
not distinguish upper- and lowercase letters.

\item |latex|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Switch between \TeX{} (|false|) and \LaTeX{} (|true|)
processing of labels.
In \LaTeX{} mode a basic \LaTeX{} document structure is
provided by the metapost file.

\item |compiler=|\textit{compiler} --
Set the compiler program for labels to \textit{compiler}
(command line option |-tex=|\textit{compiler} for \MP).
The default is |tex| (in \TeX{} mode)
or |latex| (in \LaTeX{} mode).
Set \textit{compiler} to empty to specify no compiler.

\item |format=|\textit{tag} --
Write format tag |%&|%
\textit{tag} to specify the compiler program for labels.
The default is |tex| (in \TeX{} mode)
or |latex| (in \LaTeX{} mode).
Set \textit{tag} to empty to write no format tag.

\item |class=|\textit{class}
(initially set to |article|) --
Set the document class to be used in \LaTeX{} mode.

\item |classopt=|\textit{options} --
Set the options for the |\documentclass| statement in \LaTeX{} mode,
e.g.\ |12pt| or |11pt|.
No options are specified initially resulting in the |10pt| font set.

\item |mem=|\textit{mem} --
Set the metapost format file to \textit{mem}.

\item |command=|\textit{command} --
Use \textit{command} to process the generated file(s).
By default the \MP{} program |mpost| is invoked
to compile the generated metapost files
(with appropriate command line options).

\item |now|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Activate/deactivate immediate mode.
In immediate mode, figures can be processed immediately
so that a secondary \LaTeX{} pass is not required to
display the figure correctly.
Note that this option merely enables recording
of all the required definitions
so that immediate processing will be possible,
but it does not activate immediate processing per se.

\item |nowall|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable immediate processing for all figures by default.
This option will call the \MP{} compiler for every figure.
While convenient, it requires heavier processing.

\item |nowkeep|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable immediate generation
of figures by individual metapost files.
If this mode is enabled,
the filename for immediate processing
of the figure \textit{filename}|.mps|
will be \textit{filename}|.mp|.
Otherwise the metapost code is stored in a temporary file and
is overwritten by any subsequent immediate processing.

\item |globaldef|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|) --
Enable/disable the |global| option
for the |mpostdef| and |mposttex| environments by default.
This option can be used to specify a global block of definitions
by enclosing it with appropriate |\mpostsetup| statements.

\item |template=|\textit{template}
(initially set to |\mpostfilename-#1.mps|) --
Set the template for figure filenames
for which it is not given explicitly.
The parameter |#1| carries the number (or label) to be used.
Moreover, the macro |\mpostfilename|
carries the current metapost file.

\item |extension=|\textit{ext}
(initially set to |mps|) --
Set the default extension for figure filenames.

\item |numberwithin=|\textit{counter} --
Declares the figure counter
to be a child of \textit{counter}.
In other words, the figure counter
is reset when \textit{counter} is increased,
and the figure number will be composed
as \textit{counter}|-|\textit{figure}.
Using this option with a top-level counter
such as |section| or |chapter|
stabilises the figure numbering
by making changes to the sequence have
effects only within the present section or chapter.

\item |beamer|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Enable/disable processing of overlay parameters |<|\ldots|>|
by |\mpostuse| defined by the \textsf{beamer} package.

\item |warnunused|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |true|) --
Enable/disable warnings for unused figure labels.

\end{itemize}
%
Admittedly, some of these options are hardly necessary
as they will have little impact on output
or performance in ordinary situations.
They are provided for completeness,
to make the package work in more exotic situations,
and/or to satisfy some personal taste regarding
how things should be managed.

\DescribeMacro{\mpostplaceholder}
\DescribeMacro{\mpostdisplaylabel}
Finally, the package allows to customise the placeholder
which is displayed when a figure file is not (yet) present
after the first \LaTeX{} pass (or in case of some compile error)
or a figure label does not exist.
In these situations the following macro is called:
\begin{center}
|\mpostplaceholder[|\textit{type}|]{|\textit{name}|}|
\end{center}
\textit{type} is either `|file|' or `|label|'
and \textit{name} is the missing filename or label.
By default this command displays a 1''$\times$0.6'' box
containing the missing filename or label.
This behaviour can be customised by overwriting the macro.
It makes sense to display the filename or label through
|\mpostdisplaylabel| which can handle some special characters
such as an underscore.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Writing and Compiling Options}
\label{sec:compiling}

The following discusses some package options regarding the writing
and compiling of metapost files in more detail.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Label Typesetting Options.}

Metapost figure files may or may not include the \MF{} fonts
which are used by the figure labels.
When the generated metapost figures
are only used within a \LaTeX{} document
in a standard \TeX{} distribution, it is not necessary to
include the required fonts as they are automatically
supplied by the \LaTeX{} compiler.
This omission reduces the size of the figure files
(but apparently it has no impact
on the size of the compiled \LaTeX{} document
in modern \TeX{} distributions).
However, the postscript structure
of such figure files is incomplete,
and therefore the labels typically appear distorted
in external viewing or processing tools.
If the figure files are to be viewed,
processed or passed on to
a publisher, it makes sense to include the required fonts.
The latter is achieved by enabling the package option |fonts|.
In this context, one can also set the default figure file
extension to |eps| by means of the package option |extension|.

By default, the package provides a \LaTeX{} document structure
for processing labels by \LaTeX{}.
The default document class is |article| without options.
An alternative class and options can be specified
by the package options |class| and |classopt|.
Further packages or macros should be declared as usual
by specifying them in a |mposttex| enviroment.
If no \LaTeX{} structure is desired,
set the package option |latex| to |false|.
If the typesetting requires an advanced compiler
beyond |latex| or |tex|, it should be
specified by the package option |compiler|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Compiling Options.}

When the figure files are in a final form
it may make sense to disable the compiling
or even the writing of the metapost file(s)
by setting the package options |compile|
or |write| to |false|.
In particular, this may be desirable
if the \LaTeX{} source is uploaded to a repository
or passed on to a publisher.

The package can check whether the generated files
change with respect to the previous \LaTeX{} pass,
and only modified files are compiled thereupon.
This check can be enabled by setting the option
|checksum| to |true|.
Note that under some conditions
(in particular, if the previous \LaTeX{} pass
produced compile errors),
figures may not get compiled even though the source changed.
In that case you can trigger compilation
by deleting the generated metapost source or figures
or by introducing further changes to the metapost code.
By default all files are compiled.

When the metapost file compiles with errors,
one can inspect the generated metapost file.
To this end it may be helpful to know which
part of the \LaTeX{} source file is responsible
for which part of the metapost file.
The package option |lineno| activates
prepending every block in the metapost file
by the corresponding location in the \LaTeX{} source.
If the \LaTeX{} source is distributed over several files,
the source filename can also be provided
if the package \textsf{currfile} is loaded.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Filename Options.}

By default the names of figure files take the form
%
\begin{center}
\textit{filename}|-|\textit{specifier}|.|\textit{ext}
\end{center}
where \textit{filename} is the name of the metapost file,
\textit{specifier} is an integer number which enumerates the figures
and \textit{ext} is `|mps|'.
There are several options to customise the scheme:
%
\begin{itemize}
\item
The figure option |file| allows to explicitly specify
the desired filename.

\item
The package option |numberwithin=|\textit{section} can be used to
associate the counting with a top-level section counter
(such as `|section|' or `|chapter|').
Then \textit{specifier} takes the form
\textit{section}|-|\textit{number},
where \textit{number} is an integer that enumerates the figures
within the present \textit{section}.

\item
The package option |labelnames| lets \textit{specifier}
be the figure label. This option should be used with care,
as the operating system does not necessarily allow
or distinguish all characters which are available for \TeX{} macros.

\item
The extension \textit{ext} can be customised
by the package option |extension|.

\item
The package option |template|
allows to customise the above template.
In composing the template,
the argument |#1| carries the \textit{specifier}
and |\mpostfilename| carries the name
of the present metapost file.
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Multiple Files}
\label{sec:multiplefiles}

By default the package \textsf{mpostinl} writes out a single file
\textit{source}|.mp| if the \LaTeX{} source is called \textit{source}|.tex|.
However, the package can also be configured
to write out several metapost files.
This feature can be used to declare one metapost file
for each top-level section of a large \LaTeX{} document
(e.g.\ section or chapter).
Alternatively, one could define
different metapost files for figures of different kinds
(e.g.\ technical drawings, diagrams, graphs, charts).
Note that these could well use different
sets of metapost macros and variables.

This feature can make sense
if the metapost source or generated figures
are to be passed on to someone else (e.g.\ publisher)
in order to help clarify the placement of figures.
It may also be useful if selected sections
of the document are generated
individually by means of |\includeonly|,
in which case only the relevant metapost file
is generated and compiled.

The contents of each metapost file should be enclosed
by |\mpostfile| and |\mpostdone|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeMacro{\mpostfile}
A new metapost file is started by the command:
\begin{center}
|\mpostfile[|\textit{opts}|]{|\textit{filename}|}|
\end{center}
%
\textit{filename} is the filename without |.mp| extension.
\textit{opt} is a comma-separated list of options:
the only available option is |include|[|=true|$\vert$|false|]
(no value implies |true|, initially set to |false|)
which declares whether the file is an include file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DescribeMacro{\mpostdone}
The present metapost file is completed by the command
|\mpostdone| which also compiles the contained figures.
Note that this command is called automatically
at the end of the \LaTeX{} document.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Global Definitions.}

Definitions in |mpostdef| and |mposttex| environments apply to the
present metapost file only. However, definitions can also be
specified for all metapost files
by means of the option |global|:
\begin{center}
|\||begin{mpostdef}[global]|
\end{center}
These definitions are stored internally and will be written
to all subsequent metapost files.

To declare several consecutive (or all)
blocks of definitions as global,
one can use the package option |globaldef|.
To that end, enclose the blocks by
|\mpostsetup{globaldef=true}| and |\mpostsetup{globaldef=false}|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Include Files.}

Alternatively, global definitions can be saved to an include file.
This may make sense if there is a large amount of definitions
which should not be written to each and every metapost file.
An include file is declared by
|\mpostfile[include]{|\textit{filename}|}|
and will be used by all subsequent main metapost files.
Include files are equivalent to global definitions,
but they cannot contain \TeX{} definitions.
Moreover they must not contain figures.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Immediate Processing}
\label{sec:immediate}

By default, the metapost file is compiled
at the end of the \LaTeX{} document.
Therefore all new figures and any changes to existing figures
are not reflected in the compiled \LaTeX{} document
after the first pass.
A second \LaTeX{} pass is needed to generate the desired output.
Moreover, any change to the sequence of figures
(figures inserted, deleted or moved)
can only be seen after the second pass;
the first pass shows the old sequence.

In the following we present some strategies to minimise or avoid
this issue.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Collecting Figures at the Top.}

One option is to declare all figures
with labels near the top of the document,
and close the metapost file by the command |\mpostdone|.
The compiled figures are immediately available afterwards.
A side effect is that metapost
and \LaTeX{} source is necessarily separated
(which may be considered a benefit
or a drawback depending on philosophy).
Note that in a setup with multiple metapost files,
the figures can be defined at the top of each section
thereby allowing some association between figures and manuscript.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Stable Filenames.}

A change of figure sequence can have irritating effects
because some figures are displayed
in the wrong place in the first pass.
Even though a second pass resolves this issue,
the first pass may have upset the page composition
so that some labels containing page numbers can be messed up
in the second pass thus requiring a third pass.
There are some methods to reduce this effect:
\begin{itemize}
\item
Provide a filename for each figure by the option |file|.

\item
Automatically align the figure filenames with the labels by
the package option |labelnames|.

\item
Use the package option |numberwithin| with the counter
of the top-level section.
Changes of the figure order will then
be contained within each section.

\item
Use one metapost file for each top-level section.
Changes of the figure order will then
be contained within each metapost file.
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Immediate Processing.}

The above strategies are workarounds, but the package also provides
a mechanism to compile individual figures or all figures at once.
The drawback is that one metapost file with all relevant definitions
needs to be generated and compiled for each figure.
This may have some negative impact on performance
for very large documents with excessively many figures,
if many such documents are to be generated in a row,
if the computer is slow or busy otherwise,
or if the laptop battery is low on a long journey
(or eventually if the user is old-fashioned or paranoid).

The mechanism is enabled by the package option |now|.
While composing or editing individual figures,
one can specify the figure option |now|,
and remove it when the figure is final.
Alternatively one can activate immediate processing for all figures
with the package option |nowall| (together with |now|).
Ordinarily, a temporary file is used for the metapost source
for immediate processing.
If the metapost sources for individual figures are needed,
the package option |nowkeep| stores the metapost source
for every generated figure \textit{filename}|.mps|
in the file \textit{filename}|.mp|.
When the figures are in a final state,
all immediate processing can be disabled by turning off the package
option |now|.

Note that changing some variables within the figure blocks
can have undesired effects for immediate processing:
In immediate processing only the present figure is included
in the metapost file,
otherwise the metapost file contains the sequence of all figures.
Therefore, a change or (re)definition of a variable within
a figure block will not be visible to all subsequent figures
in immediate mode!
To avoid such situations:
%
\begin{itemize}
\item
Do not (re)define variables within a figure block.
Use a definition block before the figure instead.
\item
Provide an initial value for all used variables
which could change within other figures.
\item
For internal variables (such as the size |ahlength| of an arrow head)
you can |save| the initial value before
the first redefinition within a figure block.
Alternatively you can prepend the first change to the variable
by |interim|.
Note that you can declare variables as internal
by means of |newinternal|.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Interaction with Other Packages}
\label{sec:interactions}

The following lists some potential issues in
the interaction with other \LaTeX{} packages:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Package \textsf{inputenc}.}
This package should not interfere with the input encoding
selected via the package \textsf{inputenc}, e.g.\ |utf8| encoding.
Extended characters are passed on
unchanged by the |mpostfig| environment.
If you declare an input encoding for your \LaTeX{} source by:
\begin{center}
|\usepackage[|\textit{enc}|]{inputenc}|
\end{center}
you should select the \emph{same} input encoding for the labels
in the metapost figures by means of:
\begin{center}
\begin{tabular}{l}
|\||begin{mposttex}|\\
|\usepackage[|\textit{enc}|]{inputenc}|\\
|\||end{mposttex}|
\end{tabular}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Package \textsf{beamer}.}

The package \textsf{beamer} is a popular package for
preparing slideshow presentations.
In particular, slides can be presented in several steps
by means on an overlay mechanism.
To that end the |frame| environment
saves the enclosed block and processes it in several passes.
Therefore, the environments |mpostfig|, |mpostdef| and |mposttex|
must not be used within the |frame| environment.
Instead, figures should be declared outside the |frame| environment
and can be displayed by |\mpostuse| within the |frame| environment.
Note that |\mpostuse| accepts the overlay parameter |<|\ldots|>|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Package \textsf{graphbox}.}

Figures are eventually displayed by the |\includegraphics| command
which aligns the graphics with the bottom of the current line.
To achieve different alignments or placements takes some efforts.
The package \textsf{graphbox} extends the optional
arguments of |\includegraphics| to customise the alignment conveniently.
Since graphics arguments are passed on directly to |\includegraphics|
the \textsf{graphbox} package can be used without restrictions.
For instance, to align a figure vertically with the centre of the line,
you may use |\||begin{mpostfig}[opt={align}]|
or |\mpostuse[align]{|\textit{label}|}|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Package \textsf{latexmp}.}

The \MP{} package \textsf{latexmp} writes its own \LaTeX{} structure
to the metapost file.
Therefore \textsf{mpostinl} must not write the \LaTeX{} structure,
but still use the |latex| compiler.
Furthermore \textsf{latexmp} needs two \MP{} passes.
The required options are:
\begin{center}
|\mpostsetup{latex=false,format,compiler=latex,twice}|
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Script \textsf{mplatex}.}

The \MP{} processing script \textsf{mplatex} expects the default metapost
file naming convention (\textit{filename}|.|\textit{nn})
and no |prologues| statement.
Moreover, it provides the \LaTeX{} structure.
The required options are:
%
\begin{center}
\begin{tabular}{l}
|\mpostsetup{latex=false,format,prologues}|\\
|\mpostsetup{template={\mpostfilename.#1}}|\\
|\mpostsetup{command={mplatex |\textit{opt}|}}|
\end{tabular}
\end{center}
%
In \textit{opt} you can specify command line options for |mplatex|,
e.g.\ to include \LaTeX{} packages.
Make sure to enable full access to external programs,
and that |mplatex| is in the path (or specify its location explicitly).
Note that the file naming convention (\textit{filename}|.|\textit{nn})
does not seem to work with pdf\LaTeX{}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Information}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Copyright}

Copyright \copyright{} 2010--2018 Niklas Beisert

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
  \url{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 Niklas Beisert.

This work consists of the files |README.txt|, |mpostinl.ins| and |mpostinl.dtx|
as well as the derived files |mpostinl.sty|, |mpinlsmp.tex| and |mpostinl.pdf|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Files and Installation}

The package consists of the files:
%
\begin{center}
\begin{tabular}{ll}
    |README.txt|   & readme file \\
    |mpostinl.ins| & installation file \\
    |mpostinl.dtx| & source file \\
    |mpostinl.sty| & package file \\
    |mpinlsmp.tex| & sample file \\
    |mpostinl.pdf| & manual
\end{tabular}
\end{center}
%
The distribution consists of the files
|README.txt|, |mpostinl.ins| and |mpostinl.dtx|.
%
\begin{itemize}
\item
Run (pdf)\LaTeX{} on |mpostinl.dtx|
to compile the manual |mpostinl.pdf| (this file).
\item
Run \LaTeX{} on |mpostinl.ins| to create the package |mpostinl.sty|
and the sample |mpinlsmp.tex|.
Copy the file |mpostinl.sty| to an appropriate directory of your \LaTeX{}
distribution, e.g.\ \textit{texmf-root}|/tex/latex/mpostinl|.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Required Packages and Software}

The package relies on other packages and software:
\begin{itemize}
\item
This package relies on some functionality of the package \textsf{verbatim}
to read verbatim code from the \LaTeX{} source without expansion of macros.
Compatibility with the \textsf{verbatim} package
has been tested with v1.5q (2014/10/28).
\item
This package uses the package \textsf{graphicx}
from the \textsf{graphics} bundle to include graphics files.
Compatibility with the \textsf{graphicx} package
has been tested with v1.0g (2014/10/28).
\item
This package uses the package \textsf{keyval}
from the \textsf{graphics} bundle
to process the options for the package, environments and macros.
Compatibility with the \textsf{keyval} package
has been tested with v1.15 (2014/10/28).
\item
This package uses the command |\currfilename|
provided by the package \textsf{currfile} (if available and loaded)
to indicate the \LaTeX{} source file in the generated metapost file.
Compatibility with the \textsf{currfile} package
has been tested with v0.7c (2015/04/23).
\item
The package assumes a \TeX{} installation with \MP{}
configured appropriately.
Recent \textsf{texlive} and MiK\TeX{} distributions should work well.
Compatibility with the \textsf{texlive} distribution
has been tested with the 2016 issue containing
pdf\TeX{} version 3.14159265-2.6-1.40.17
and \MP{} version 1.9991.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Feature Suggestions}
\label{sec:suggestions}

The following is a list of features which may be useful for future
versions of this package:
%
\begin{itemize}
\item
A method to expand \LaTeX{} macros to the metapost code
to match the corresponding feature of package \textsf{gmp}:
One difficulty is that direct insertion of \LaTeX{} macros
requires a substantially different implementation of the
block scanning method
because the method supplied by \textsf{verbatim} does not work.
Furthermore, some escape mechanism is required
either to expand selected \LaTeX{} macros
or to prevent their expansion (within |btex| \ldots\ |etex| blocks).
%  encapsulate via | |
%  extra env
\item
A simpler approach to the above feature would be to implement a
command or option to prepend some metapost statements
to a given metapost block where \LaTeX{} macros are expanded.
This may work well for passing very few macros to metapost variables.
However, one would have to decide how aggressively
macros are to be expanded.
\item
Write generated files to a subdirectory
or remove files not needed after processing.
However, both appear to be somewhat in contrast
to the usual \TeX{} philosophy.
%\item
%improve detection of changed figure files?
%allow to declare extra include files for update detection
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Revision History}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.5:} 2018/12/30

\begin{itemize}
\item
new environment |mposttex| for \TeX{} or \LaTeX{} definitions
to replace option |tex| of |mpostdef|;
can be used in |dual| mode
\item
allow underscore, caret and ampersand in figure labels and filenames
\item
improve handling of optional arguments and special situations
when scanning metapost blocks
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.4:} 2018/03/01

\begin{itemize}
\item
|\mpostuse| now accepts overlay options |<|\ldots|>|
of the \textsf{beamer} package;
new option |beamer| to control the feature
\item
issue warnings for unused labels;
new option |warnunused| to control the feature
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.3:} 2018/02/09

\begin{itemize}
\item
new |clean| option to initialise figure files
with dummy figures to show \MP{} compiler errors
\item
|checksum| behaviour improved
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.21:} 2018/01/17

\begin{itemize}
\item
remark on leading empty lines added
\item
manual rearranged
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.2:} 2018/01/05

\begin{itemize}
\item
new option |checksum| to compile only modified files
(requires the pdf\TeX{} engine, otherwise disabled)
\item
warn if some figures may not be up to date
\item
bugfix for example if \textsf{graphbox} is not available
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.12:} 2017/06/24

\begin{itemize}
\item
bugfix for blank |mpostfig| display
due to changes in the basic
\LaTeX{} or \textsf{graphicx} system
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.11:} 2017/04/01

\begin{itemize}
\item
implementation of |fonts| option repaired
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.1:} 2017/02/27

\begin{itemize}
\item
improved compatibility with package \textsf{latexmp}
and script \textsf{mplatex}
(thanks to Walter Entenmann for encouragement and testing)
\item
options improved to fine-tune structures written to metapost files
and to specify compiler programs
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v1.0:} 2017/01/04

\begin{itemize}
\item
manual and install package
\item
first version published on CTAN
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v0.7--0.9:} 2016/11/15 -- 2017/01/04

\begin{itemize}
\item
package options
\item
metapost environments simplified and renamed
\item
immediate and multiple file processing
\item
customisation options
\item
internal buffer processing
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v0.6--0.63:} 2015/07/11 -- 2016/10/03

\begin{itemize}
\item
minor improvements
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{v0.5:} 2010/11/01

\begin{itemize}
\item
basic functionality
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\appendix

\settowidth\MacroIndent{\rmfamily\scriptsize 000\ }

 \DocInput{mpostinl.dtx}

\end{document}
%</driver>
% \fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Sample File}
%\iffalse
%<*sample>
%\fi
%
% In this section we provide a \LaTeX{} example how to use
% some of the \textsf{mpostinl} features.
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Preamble}
%
% Standard document class:
%    \begin{macrocode}
\documentclass[12pt,a4paper]{article}
%    \end{macrocode}

% Adjust the paragraph shape:
%    \begin{macrocode}
\parindent0pt
\parskip6pt
%    \end{macrocode}

% Include the \textsf{mpostinl} package,
% include \MF{} fonts in the metapost figures:
%    \begin{macrocode}
\usepackage[fonts=true]{mpostinl}
%    \end{macrocode}

% We will test labels in UTF-8, so include package \textsf{inputenc}:
%    \begin{macrocode}
\usepackage[utf8]{inputenc}
%    \end{macrocode}

% Include packages \textsf{currfile} and \textsf{graphbox} if available,
% declare dummy option |align| for |\includegraphics|
% if \textsf{graphbox} is not available:
%    \begin{macrocode}
%% optional: add filename to position labels in metapost code
\IfFileExists{currfile.sty}{\usepackage{currfile}}{}
%% optional: tools to align graphics
\makeatletter\define@key{Gin}{align}[]{}\makeatother
\IfFileExists{graphbox.sty}{\usepackage{graphbox}}{}
%    \end{macrocode}

% Enable immediate mode and line number indicators,
% prepare some \textsf{mpostinl} options for testing:
%    \begin{macrocode}
%% some sample package options:
%% \mpostsetup{write=false}
%% \mpostsetup{compile=false}
\mpostsetup{checksum}
\mpostsetup{now}
%% \mpostsetup{nowall}
\mpostsetup{lineno}
%% \mpostsetup{latex=false}
%% \mpostsetup{classopt={12pt}}
%    \end{macrocode}

% Include the package \textsf{inputenc}
% for preparing \LaTeX{} labels within the metapost figures;
% as we will be generating several metapost files later on,
% make sure this statement in included in all of them:
%    \begin{macrocode}
%% declare packages to be used for processing labels:
\begin{mposttex}[global]
\usepackage[utf8]{inputenc}
\end{mposttex}
%    \end{macrocode}

% Define an internal variable |unit| and initialise to 1cm;
% as we will be generating several metapost files later on,
% make sure this statement in included in all of them:
%    \begin{macrocode}
%% specify global definitions:
\begin{mpostdef}[global]
newinternal unit;
unit:=1cm;
\end{mpostdef}
%    \end{macrocode}

% Begin document body:
%    \begin{macrocode}
\begin{document}
%    \end{macrocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Basic Functionality}
%
% We start by demonstrating the basic functionality of the package:
%
%    \begin{macrocode}
\section{Basic Functionality}
%    \end{macrocode}

% First, draw a circle of diameter 1cm and write a `1' in the centre:
%    \begin{macrocode}
a plain circle:\\
\begin{mpostfig}
draw fullcircle scaled unit;
label(btex 1 etex, (0,0));
\end{mpostfig}
%    \end{macrocode}

% Use the options for |\includegraphics|.
% Draw another circle containing a `2' and scale it by factor $1.5$.
% Also vertically align to the centre (if \textsf{graphbox} is available):
%    \begin{macrocode}
scaled (and aligned to centre if available):\\
X
\begin{mpostfig}[opt={scale=1.5,align}]
draw fullcircle scaled unit;
label(btex 2 etex, (0,0));
\end{mpostfig}
X
%    \end{macrocode}

% Declare a figure with label |fig|, do not show:
%    \begin{macrocode}
declare figure with label (no display).
\begin{mpostfig}[label={fig}]
draw fullcircle scaled unit;
label(btex 3 etex, (0,0));
\end{mpostfig}
%    \end{macrocode}

% Display the figure:
%    \begin{macrocode}
display:\\
\mpostuse{fig}
%    \end{macrocode}

% Display the figure with options for |\includegraphics|:
%    \begin{macrocode}
display with options:\\
\mpostuse[scale=1.5,align]{fig}
%    \end{macrocode}

% Display the figure within a box:
%    \begin{macrocode}
display in a box:\\
\fbox{\mpostuse{fig}}
%    \end{macrocode}

% Display the figure at the centre of a line:
%    \begin{macrocode}
centred display:
\begin{center}
\mpostuse[scale=1.5]{fig}
\end{center}
%    \end{macrocode}

% Display the figure within an equation:
%    \begin{macrocode}
display in equation (align if possible):
\begin{equation}
\mpostuse[scale=1.5,align]{fig}
\end{equation}
%    \end{macrocode}

% Show the filename of the figure:
%    \begin{macrocode}
filename: \mpostgetname{fig}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Declare a figure with filename, label and show it,
% then display filename:
%    \begin{macrocode}
figure with filename:\\
\begin{mpostfig}[file={\jobname-name.mps},label={name},show]
draw fullcircle scaled unit;
label(btex 4 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{name}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Display the file via |\includegraphics|.
% As the figure may not exist (in the first pass)
% check for existence first to avoid a compile error:
%    \begin{macrocode}
display by \verb+\includegraphics+ (if file exists):\\
\IfFileExists{\jobname-name.mps}{\includegraphics{\jobname-name.mps}}{}
%    \end{macrocode}

% Show a figure which does not exist,
% triggers a warning and displays a box:
%    \begin{macrocode}
label does not exist:\\
\mpostuse{not_exist}
%    \end{macrocode}

% Display a figure with a label containing special characters in UTF-8.
% Note that the internal variable |unit| is changed locally (|interim|)
% so that subsequent figures will see the old value:
%    \begin{macrocode}
utf-8 test:\\
\begin{mpostfig}
interim unit:=1.5cm;
draw fullcircle scaled unit;
label(btex àáâãäåæ etex, (0,0));
\end{mpostfig}
%    \end{macrocode}

% Write a totally empty figure
%    \begin{macrocode}
empty figure:\\
\begin{mpostfig}
\end{mpostfig}
%    \end{macrocode}

% Write a figure with an empty line
%    \begin{macrocode}
empty figure:\\
\begin{mpostfig}

\end{mpostfig}
%    \end{macrocode}

% Make a dual definition
%    \begin{macrocode}
\begin{mposttex}[dual]
\def\mymacro{hello!}
\end{mposttex}
%    \end{macrocode}
% and test it
%    \begin{macrocode}
dual definition `\mymacro':\\
\begin{mpostfig}
draw unitsquare shifted (-0.5,-0.5) xscaled 3 scaled unit;
label(btex \mymacro etex, (0,0));
\end{mpostfig}
%    \end{macrocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Immediate Processing}
%
% Next, we demonstrate immediate processing
% (make sure to enable the package option |now|):
%
%    \begin{macrocode}
\section{Immediate Processing}
%    \end{macrocode}

% Declare a figure for immediate processing.
% You may change the size |unit| and the figure will be
% adjusted after the first \LaTeX{} pass:
%    \begin{macrocode}
immediate processing per figure:\\
\begin{mpostfig}[now]
interim unit:=1.5cm;
draw fullcircle scaled unit;
label(btex 5 etex, (0,0));
\end{mpostfig}
%    \end{macrocode}

% One can also enable immediate processing for all subsequent figures.
% Furthermore, store the metapost source in individual files:
%    \begin{macrocode}
turn on immediate processing for all figures and keep sources.
\mpostsetup{nowall,nowkeep}
%    \end{macrocode}

% Now declare a figure with individual metapost source,
% display via |\includegraphics|,
% and confirm that source exists:
%    \begin{macrocode}
generate file \texttt{\jobname-now.mps}:
\begin{mpostfig}[file={\jobname-now.mps}]
draw fullcircle scaled unit;
label(btex 6 etex, (0,0));
\end{mpostfig}
\\
display by \verb+\includegraphics+:\\
\includegraphics{\jobname-now.mps}
\\
source \texttt{\jobname-now.mp}
\IfFileExists{\jobname-now.mp}{exists}{does not exist}.
%    \end{macrocode}

% Reset immediate processing options for further examples
% (normally not necessary):
%    \begin{macrocode}
turn off immediate processing and discard sources.
\mpostsetup{nowall=false,nowkeep=false}
%    \end{macrocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Filename Composition}
%
% Now, we demonstrate how to adjust the composition of figure filenames:
%
%    \begin{macrocode}
\section{Filename Composition}
%    \end{macrocode}

% Use the figure label instead of a figure counter:
%    \begin{macrocode}
by label:\\
\mpostsetup{labelnames=true}
\begin{mpostfig}[label={circle},show]
draw fullcircle scaled unit;
label(btex 7 etex, (0,0));
\end{mpostfig}
\mpostsetup{labelnames=false}
\\
filename: \mpostgetname{circle}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Change extension to |.eps|:
%    \begin{macrocode}
change extension:
\mpostsetup{extension=eps}
\begin{mpostfig}[label={ext}]
draw fullcircle scaled unit;
label(btex 8 etex, (0,0));
\end{mpostfig}
\mpostsetup{extension=mps}
\\
filename: \mpostgetname{ext}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Change template altogether:
%    \begin{macrocode}
change template:\\
\mpostsetup{template=\mpostfilename-figure-#1.mps}
\begin{mpostfig}[label={template},show]
draw fullcircle scaled unit;
label(btex 9 etex, (0,0));
\end{mpostfig}
\mpostsetup{template=\mpostfilename-#1.mps}
\\
filename: \mpostgetname{template}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Demonstrate numbering within a section (here: |subsection|)
%    \begin{macrocode}
\mpostsetup{numberwithin=subsection}
%    \end{macrocode}

% One figure in the first section:
%    \begin{macrocode}
\subsection{Section 1}

\begin{mpostfig}[show,label={sec1}]
draw fullcircle scaled unit;
label(btex 10 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec1}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Two figures in the second section:
%    \begin{macrocode}
\subsection{Section 2}

\begin{mpostfig}[show,label={sec2}]
draw fullcircle scaled unit;
label(btex 11 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec2}
\texttt{\mpostfigurename}

\begin{mpostfig}[show,label={sec3}]
draw fullcircle scaled unit;
label(btex 12 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec3}
\texttt{\mpostfigurename}
%    \end{macrocode}

% One figure in the third section:
%    \begin{macrocode}
\subsection{Section 3}

\begin{mpostfig}[show,label={sec4}]
draw fullcircle scaled unit;
label(btex 13 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec4}
\texttt{\mpostfigurename}
%    \end{macrocode}

% Reset numbering for further examples
% (normally not necessary, setting cannot be undone completely):
%    \begin{macrocode}
\makeatletter
\def\thempi@count{\arabic{mpi@count}}
\makeatother
%    \end{macrocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Multiple Files}
%
% Finally, demonstrate the generation and usage of several metapost files:
%
%    \begin{macrocode}
\section{Multiple Files}
%    \end{macrocode}

% First, close the old metapost file to get to a clean state:
%    \begin{macrocode}
\mpostdone
%    \end{macrocode}

% Make a definition which will apply to all metapost files.
% |global| option can be activated
% for a block of definitions (here only one):
%    \begin{macrocode}
\mpostsetup{globaldef=true}

\begin{mpostdef}
unit:=0.8cm;
\end{mpostdef}

\mpostsetup{globaldef=false}
%    \end{macrocode}

% Alternatively, we can define an include file
% to be included by all main metapost files.
% This has the same effect as a global definition,
% but is intended for long definitions:
%    \begin{macrocode}
\mpostfile[include]{\jobname-inc}

\begin{mpostdef}
def drawcircle =
draw fullcircle scaled unit
enddef;
\end{mpostdef}

\mpostdone
%    \end{macrocode}

% The first file contains one figure:
%    \begin{macrocode}
\subsection*{Section 1}
\mpostfile{\jobname-sec1}

\begin{mpostfig}[show,label={sec5}]
drawcircle;
label(btex 14 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec5}
\texttt{\mpostfigurename}

\mpostdone
%    \end{macrocode}

% The second file contains a local redefinition of |unit|
% and two figures:
%    \begin{macrocode}
\subsection*{Section 2}
\mpostfile{\jobname-sec2}

\begin{mpostfig}[show,label={sec6}]
drawcircle;
label(btex 15 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec6}
\texttt{\mpostfigurename}

\begin{mpostdef}
unit:=1.5cm;
\end{mpostdef}

\begin{mpostfig}[show,label={sec7}]
drawcircle;
label(btex 16 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec7}
\texttt{\mpostfigurename}

\mpostdone
%    \end{macrocode}

% The third file contains one figure. Note that the redefinition
% of the second file does not apply here:
%    \begin{macrocode}
\subsection*{Section 3}
\mpostfile{\jobname-sec3}

\begin{mpostfig}[show,label={sec8}]
drawcircle;
label(btex 17 etex, (0,0));
\end{mpostfig}
\\
filename: \mpostgetname{sec8}
\texttt{\mpostfigurename}

\mpostdone
%    \end{macrocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of document body:
%    \begin{macrocode}
\end{document}
%    \end{macrocode}
%\iffalse
%</sample>
%\fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Implementation}
%\iffalse
%<*package>
%\fi
%
% In this section we describe the package |mpostinl.sty|.

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \paragraph{Required Packages.}
%
% The package loads the packages
% \textsf{verbatim}, \textsf{graphicx} and \textsf{keyval}
% if not yet present.
% \textsf{verbatim} is used for reading verbatim metapost code.
% \textsf{graphicx} is used for including graphics files.
% \textsf{keyval} is used for extended options processing.
%    \begin{macrocode}
\RequirePackage{verbatim}
\RequirePackage{graphicx}
\RequirePackage{keyval}
%    \end{macrocode}

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \paragraph{Package Options.}
%
% \macro{\mpostfilename}
% \macro{\mpi@nowname}
% \macro{\mpi@extension}
% \macro{\mpi@template}
% |\mpostfilename| stores the metapost filename,
% |\mpi@nowname| stores the filename for immediate processing,
% and |\mpi@template| is the template to generate the figure filenames:
%    \begin{macrocode}
\def\mpostfilename{\jobname}
\def\mpi@nowname{\jobname-tmp}
\def\mpi@extension{mps}
\def\mpi@template#1{\mpostfilename-#1%
  \ifx\mpi@extension\mpi@empty\else.\fi\mpi@extension}
%    \end{macrocode}

% \macro{mpi@count}
% Declare a counter for figure filenames:
%    \begin{macrocode}
\newcounter{mpi@count}
\def\thempi@count{\arabic{mpi@count}}
%    \end{macrocode}

% The package has some boolean \textsf{keyval} options
% which can be set to |true| or |false|.
%    \begin{macrocode}
\newif\ifmpi@draft\mpi@draftfalse
\newif\ifmpi@latex\mpi@latextrue
\newif\ifmpi@fonts\mpi@fontsfalse
\newif\ifmpi@write\mpi@writetrue
\newif\ifmpi@compile\mpi@compiletrue
\newif\ifmpi@twice\mpi@twicefalse
\newif\ifmpi@clean\mpi@cleantrue
\newif\ifmpi@checksum\mpi@checksumfalse
\newif\ifmpi@lineno\mpi@linenofalse
\newif\ifmpi@labelnames\mpi@labelnamesfalse
\newif\ifmpi@nowactive\mpi@nowactivefalse
\newif\ifmpi@now\mpi@nowfalse
\newif\ifmpi@nowkeep\mpi@nowkeepfalse
\newif\ifmpi@include\mpi@includefalse
\newif\ifmpi@defglobal\mpi@defglobalfalse
\newif\ifmpi@texdual\mpi@texdualfalse
\newif\ifmpi@warnunused\mpi@warnunusedtrue
\newif\ifmpi@beamer\mpi@beamertrue
%    \end{macrocode}

% \macro{\mpi@mpostmem}
% \macro{\mpi@mpostcompiler}
% \macro{\mpi@latexclass}
% \macro{\mpi@latexoptions}
% \macro{\mpi@documentclass}
% These definitions store the options for processing labels via
% \TeX{} or \LaTeX{}:
%    \begin{macrocode}
\def\mpi@mpostmem{}
\def\mpi@mpostcompiler{}
\def\mpi@latexclass{article}
\def\mpi@latexoptions{}
\def\mpi@documentclass{\@backslashchar documentclass%
  \mpi@latexoptions{\mpi@latexclass}}
%    \end{macrocode}

% \macro{\mpi@warncompile}
% Warn and disable compiling if |\write18| is unavailable:
%    \begin{macrocode}
\def\mpi@warncompile{\ifmpi@compile\ifeof18%
  \PackageWarning{mpostinl}{write18 disabled, %
    manual metapost compiling required}{}%
  \global\mpi@compilefalse\fi\fi}
%    \end{macrocode}

% Process package options:
%    \begin{macrocode}
\def\mpi@group{mpi@}
\DeclareOption{final}{\mpi@draftfalse}
\define@key{\mpi@group}{draft}[true]{\csname mpi@draft#1\endcsname}
\define@key{\mpi@group}{write}[true]{\csname mpi@write#1\endcsname}
\define@key{\mpi@group}{latex}[true]{\csname mpi@latex#1\endcsname}
\define@key{\mpi@group}{compile}[true]{\csname mpi@compile#1\endcsname}
\define@key{\mpi@group}{twice}[true]{\csname mpi@twice#1\endcsname}
\define@key{\mpi@group}{clean}[true]{\csname mpi@clean#1\endcsname}
\define@key{\mpi@group}{checksum}[true]{\csname mpi@checksum#1\endcsname}
\define@key{\mpi@group}{fonts}[true]{\csname mpi@fonts#1\endcsname}
\define@key{\mpi@group}{prologues}[]{\def\mpi@prologues{#1}}
\define@key{\mpi@group}{lineno}[true]{\csname mpi@lineno#1\endcsname}
\define@key{\mpi@group}{labelnames}[true]{\csname mpi@labelnames#1\endcsname}
\define@key{\mpi@group}{compiler}[]{\def\mpi@texcompiler{#1}}
\define@key{\mpi@group}{format}[]{\def\mpi@texformat{#1}}
\define@key{\mpi@group}{mem}[]{\def\mpi@mpostmem{#1}}
\define@key{\mpi@group}{command}[]{\def\mpi@mpostcompiler{#1}}
\define@key{\mpi@group}{class}{\def\mpi@latexclass{#1}}
\define@key{\mpi@group}{classopt}[]{\def\mpi@latexoptions{[#1]}}
\define@key{\mpi@group}{beamer}[true]{\csname mpi@beamer#1\endcsname}
\define@key{\mpi@group}{warnunused}[true]{\csname mpi@warnunused#1\endcsname}
\define@key{\mpi@group}{now}[true]{\csname mpi@nowactive#1\endcsname}
\define@key{\mpi@group}{nowall}[true]{\csname mpi@now#1\endcsname}
\define@key{\mpi@group}{nowkeep}[true]{\csname mpi@nowkeep#1\endcsname}
\define@key{\mpi@group}{globaldef}[true]{\csname mpi@defglobal#1\endcsname}
\define@key{\mpi@group}{extension}[]{\def\mpi@extension{#1}}
\define@key{\mpi@group}{template}{\def\mpi@template##1{#1}}
\define@key{\mpi@group}{numberwithin}{%
  \@addtoreset{mpi@count}{#1}%
  \def\thempi@count{\arabic{#1}-\arabic{mpi@count}}%
}
%    \end{macrocode}

% Pass undeclared options on to \textsf{keyval} processing:
%    \begin{macrocode}
\DeclareOption*{\expandafter\setkeys\expandafter\mpi@group%
  \expandafter{\CurrentOption}}
%    \end{macrocode}

% Process package options,
% warn if |\write18| mechanism is not available,
% and disable checksum if |\pdfmdfivesum| is not available:
%    \begin{macrocode}
\ProcessOptions
\mpi@warncompile
\ifdefined\pdfmdfivesum\else\mpi@checksumfalse\fi
%    \end{macrocode}

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \paragraph{Internal Commands and Definitions.}
%
% \macro{\mpi@empty}
% Define an empty macro for comparison via |\ifx|:
%    \begin{macrocode}
\def\mpi@empty{}
%    \end{macrocode}

% \macro{\mpi@unusedlist}
% Initialise a list of unused figure labels:
%    \begin{macrocode}
\def\mpi@unusedlist{}
%    \end{macrocode}


% \macro{\mpi@dblquotchar}
% Define a bare double quotation character for writing to the file:
%    \begin{macrocode}
\begingroup\catcode`\"=12\relax\gdef\mpi@dblquotchar{"}\endgroup
%    \end{macrocode}

% \macro{\ifmpi@infile}
% \macro{\ifmpi@inbody}
% |\ifmpi@infile| indicates whether a file is open,
% |\ifmpi@inbody| indicates whether the content section has started:
%    \begin{macrocode}
\newif\ifmpi@infile\mpi@infilefalse
\newif\ifmpi@inbody\mpi@inbodyfalse
%    \end{macrocode}

% \macro{\ifmpi@inclmod}
% \macro{\ifmpi@filemod}
% |\ifmpi@inclmod| indicates whether an include file has been modified,
% |\ifmpi@filemod| indicates whether the current file is modified:
%    \begin{macrocode}
\newif\ifmpi@inclmod\mpi@inclmodfalse
\newif\ifmpi@filemod
%    \end{macrocode}

% \macro{\ifmpi@warnmod}
% \macro{\ifmpi@showinfile}
% |\ifmpi@warnmod| indicates whether a rerun warning is to be issued,
% |\ifmpi@showinfile| indicates whether a figure has been displayed
%   while a file is being composed:
%    \begin{macrocode}
\newif\ifmpi@warnmod\mpi@warnmodfalse
\newif\ifmpi@showinfile
%    \end{macrocode}

% \macro{\mpi@out}
% \macro{\mpi@outnow}
% File handles for the metapost file (|\mpi@out|)
% and for immediate output (|\mpi@outnow|):
%    \begin{macrocode}
\newwrite\mpi@out
\newwrite\mpi@outnow
%    \end{macrocode}

% \macro{\mpi@writebuf}
% Write to the file:
%    \begin{macrocode}
\def\mpi@writebuf{\ifmpi@write\immediate\write\mpi@out{\the\mpi@buf}\fi}
%    \end{macrocode}

% \macro{\mpi@writenow}
% Write to the immediate buffer:
%    \begin{macrocode}
\def\mpi@writenow{\ifmpi@nowactive\mpi@addtoexp\mpi@nowbuf{\the\mpi@buf^^J}\fi}
%    \end{macrocode}

% \macro{\mpi@buf}
% \macro{\mpi@defbuf}
% \macro{\mpi@nowbuf}
% Declare three token buffers to store the current block (|\mpi@buf|),
% global definitions (|\mpi@defbuf|)
% and the definitions for immediate processing (|\mpi@nowbuf|):
%    \begin{macrocode}
\newtoks\mpi@buf
\newtoks\mpi@defbuf
\newtoks\mpi@nowbuf
\newtoks\mpi@verbbuf
\mpi@defbuf={}
%    \end{macrocode}

% \macro{\mpi@addto}
% \macro{\mpi@addtoexp}
% |\mpi@addto| adds the second argument to a global token buffer
% without expansion.
% |\mpi@addtoexp| first expands the second argument (once)
% and adds it to the token buffer:
%    \begin{macrocode}
\def\mpi@addto#1#2{\global#1=\expandafter{\the#1#2}}
\def\mpi@addtoexp#1#2{\expandafter\mpi@addto\expandafter#1\expandafter{#2}}
%    \end{macrocode}

% \macro{\mpi@clearbuf}
% \macro{\mpi@addbufexp}
% \macro{\mpi@addbufbuf}
% \macro{\mpi@addbuf}
% |\mpi@clearbuf| clears the current block buffer.
% |\mpi@addbufexp| expands the first token
% and adds to the current block buffer.
% |\mpi@addbufbuf| adds a buffer to the current block buffer.
% |\mpi@addbuf| adds to the current block buffer via |\protected@edef|:
%    \begin{macrocode}
\def\mpi@clearbuf{\global\mpi@buf={}}
\def\mpi@addbufexp#1{\mpi@addtoexp\mpi@buf{#1^^J}}
\def\mpi@addbufbuf#1{\mpi@addtoexp\mpi@buf{\the#1}}
\def\mpi@addbuf#1{{\protected@edef\mpi@tmp{#1}\mpi@addbufexp\mpi@tmp}}
%    \end{macrocode}

% \macro{\mpi@stripext}
% Strip |.mps| or |.eps| ending of a figure filename,
% return result in |\mpi@stripped|:
%    \begin{macrocode}
\def\mpi@stripext#1{\edef\mpi@tmp{#1}\expandafter%
  \mpi@stripstart\expandafter{\mpi@tmp}}
\def\mpi@ifeq#1#2#3#4{\def\mpi@tmpa{#1}\def\mpi@tmpb{#2}%
  \ifx\mpi@tmpa\mpi@tmpb#3\else#4\fi}
\def\mpi@stripstart#1{\mpi@stripfor{\@gobble}#1.\@@.}
\def\mpi@stripfor#1#2.#3.{%
  \begingroup%
  \mpi@ifeq{#3}{\@@}{%
    \def\mpi@tmp{\def\mpi@stripped{#1.#2}}%
    \mpi@ifeq{#1}{\@gobble}{}{%
      \mpi@ifeq{#2}{eps}{\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
      \mpi@ifeq{#2}{mps}{\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
      \ifx\mpi@extension\mpi@empty\else%
        \expandafter\mpi@ifeq\expandafter{\mpi@extension}{#2}%
          {\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
      \fi%
    }%
  }{\def\mpi@tmp{\mpi@stripfor{#1.#2}#3.}}%
  \expandafter\endgroup\mpi@tmp%
}
%    \end{macrocode}

% \macro{\mpi@warnmod}
% Warn if some figure has been displayed while a file has been written
% (potentially the displayed figure is old):
%    \begin{macrocode}
\newcommand{\mpi@warnmod}{%
  \ifmpi@checksum\ifmpi@warnmod%
    \PackageWarning{mpostinl}{figure(s) may have changed. %
      Rerun to update figures}{}%
  \fi\fi%
}
%    \end{macrocode}

% \macro{\mpi@warnunused}
% This macro processes the list of all figure labels
% and issues a warning for every unused ones:
%    \begin{macrocode}
\def\mpi@warnunused#1{%
  \begingroup\def\mpi@tmp{#1}\ifx\mpi@tmp\mpi@empty\else%
    \ifcsname mpi@lu@#1\endcsname\else
      \PackageWarning{mpostinl}{figure label `#1' not used}{}%
    \fi%
    \def\mpi@tmp{\mpi@warnunused}%
  \fi\expandafter\endgroup\mpi@tmp%
}
%    \end{macrocode}
% Run |\mpi@warnunused| at the end of the document:
%    \begin{macrocode}
\AtEndDocument{\expandafter\mpi@warnunused\mpi@unusedlist{}}
%    \end{macrocode}

% \macro{\mpostdisplaylabel}
% Rescan a string potentially containing the special characters
% underscore (`|_|'), caret (`|^|') and ampersand (`|&|'):
%    \begin{macrocode}
\newcommand{\mpostdisplaylabel}[1]{{%
  \catcode`\_=11\catcode`\^=11\catcode`\&=11%
  \edef\mpi@tmp{#1}\expandafter\scantokens\expandafter{\mpi@tmp}}}
%    \end{macrocode}

% \macro{\mpostplaceholder}
% Display a placeholder for non-existing files or labels;
% this function may be overwritten by the user for customisation purposes
% (optional argument contains either `|file|' or `|label|'):
%    \begin{macrocode}
\newcommand{\mpostplaceholder}[2][]{\parbox[c]{1in}{%
  \hrule\vrule\hfill%
  \parbox[c]{0pt}{\rule{0cm}{0.6in}}%
    \makebox[0pt][c]{\scriptsize\tt\mpostdisplaylabel{#2}}%
  \hfill\vrule\hrule}}
%    \end{macrocode}

% \macro{\mpi@graphics}
% Display a figure;
% if the file does not exist (yet) issue a warning and display a placeholder,
% otherwise expand filename properly and pass on to |\includegraphics|:
%    \begin{macrocode}
\newcommand{\mpi@graphics}[2][]{%
  \IfFileExists{#2}%
    {\edef\mpi@tmp{#2}\includegraphics[#1]{\mpi@tmp}}%
    {\typeout{graphics file `#2' missing}\mpostplaceholder[file]{#2}}%
}
%    \end{macrocode}

% \macro{\mpi@verbatim}
% Start reading the block from the source file
% using the \textsf{verbatim} package;
% add each line to the buffer:
%    \begin{macrocode}
\newcommand{\mpi@verbatim}{%
  \begingroup%
  \@bsphack%
  \let\do\@makeother\dospecials%
  \catcode`\^^M\active%
  \global\mpi@verbbuf={}%
  \def\verbatim@processline{\mpi@addtoexp\mpi@verbbuf{\the\verbatim@line^^J}}%
  \verbatim@start%
}
%    \end{macrocode}

% \macro{\mpi@endverbatim}
% End reading the block from the source file:
%    \begin{macrocode}
\newcommand{\mpi@endverbatim}{%
  \@esphack%
  \endgroup%
}
%    \end{macrocode}

% \macro{\mpi@scanblock}
% Read one optional argument and pass on
% to an environment which reads a block from the source file.
% Handle special cases of an initial empty line
% or a completely empty block.
% Pass prefetched tokens as second argument:
%    \begin{macrocode}
\def\mpi@scanblock#1{%
  \@ifnextchar\par{\mpi@scanblock@par{#1}}{\mpi@scanblock@sel{#1}}}
\long\def\mpi@scanblock@par#1\par{\mpi@scanblock@sel{#1}[]}
\def\mpi@scanblock@sel#1{\@ifnextchar[{\mpi@scanblock@opt{#1}}%
  {\@ifnextchar\end{\mpi@scanblock@end{#1}}{\mpi@scanblock@noopt{#1}}}}
\def\mpi@scanblock@end#1\end#2{%
  \def\mpi@tmp{#2}\ifx\mpi@tmp\@currenvir%
    \def\mpi@verbatim{\global\mpi@verbbuf={}}\def\mpi@endverbatim{}%
  \fi%
  #1{}{\scantokens{\end{#2}}}}
\def\mpi@scanblock@noopt#1#2{#1{}{\scantokens#2}}
\def\mpi@scanblock@opt#1[#2]{#1{#2}{}}
%    \end{macrocode}

% \macro{\mpi@putlineno}
% Write current position in source file to buffer;
% write line number and source file name
% (if available via package \textsf{currfile}):
%    \begin{macrocode}
\newcommand{\mpi@putlineno}{%
  \ifmpi@lineno%
    \mpi@addbuf{\@percentchar---------------------------------------}%
    \mpi@addbuf{\@percentchar%
      \ifx\currfilename\@undefined\else\currfilename\space\fi%
      l.\the\inputlineno}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@beginfig}
% Write beginning of figure block to buffer;
% write filename and |beginfig| statement:
%    \begin{macrocode}
\newcommand{\mpi@beginfig}[1]{%
  \mpi@addbuf{filenametemplate \mpi@dblquotchar#1\mpi@dblquotchar;}%
  \mpi@addbuf{beginfig(\arabic{mpi@count})}%
}
%    \end{macrocode}

% \macro{\mpi@endfig}
% Write end of figure block to buffer;
% write |endfig| statement:
%    \begin{macrocode}
\newcommand{\mpi@endfig}{%
  \mpi@addbuf{endfig;}%
}
%    \end{macrocode}

% \macro{\mpi@declaredoc}
% Write |\documentclass| statement in \LaTeX{} mode to buffer:
%    \begin{macrocode}
\newcommand{\mpi@declaredoc}{%
  \ifmpi@latex%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\mpi@documentclass}%
    \mpi@addbuf{etex}%
    \mpi@addbuf{}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@begindoc}
% Write beginning of content section to buffer;
% write |\||begin{document}| statement in \LaTeX{} mode:
%    \begin{macrocode}
\newcommand{\mpi@begindoc}{%
  \ifmpi@latex%
    \mpi@putlineno%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@backslashchar begin{document}}%
    \mpi@addbuf{etex}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@enddoc}
% Write end of content section to buffer;
% write |\||end{document}| statement in \LaTeX{} mode:
%    \begin{macrocode}
\newcommand{\mpi@enddoc}{%
  \ifmpi@latex%
    \mpi@putlineno%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@backslashchar end{document}}%
    \mpi@addbuf{etex}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@declareformat}
% Write \TeX{} format specifier to buffer:
%    \begin{macrocode}
\newcommand{\mpi@declareformat}{%
  \let\mpi@tmp\mpi@texformat%
  \ifx\mpi@tmp\@undefined\def\mpi@tmp{\ifmpi@latex latex\else tex\fi}\fi%
  \ifx\mpi@tmp\mpi@empty\else%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@percentchar &\mpi@tmp}%
    \mpi@addbuf{etex}%
    \mpi@addbuf{}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@composehead}
% Write file header to buffer;
% declare font inclusion and draft mode,
% write \TeX{} format specifier and \LaTeX{} header:
%    \begin{macrocode}
\newcommand{\mpi@composehead}{%
  \mpi@putlineno%
  \let\mpi@tmp\mpi@prologues%
  \ifx\mpi@tmp\@undefined\def\mpi@tmp{\ifmpi@fonts 3\else 2\fi}\fi%
  \ifx\mpi@tmp\mpi@empty\else%
    \mpi@addbuf{prologues:=\mpi@tmp;}%
  \fi%
  \ifmpi@draft\mpi@addbuf{draft:=1;}\fi%
  \mpi@addbuf{}%
  \mpi@declareformat%
  \mpi@declaredoc%
}
%    \end{macrocode}

% \macro{\mpi@beginfile}
% Write beginning of file to buffer;
% write generated file comment and header:
%    \begin{macrocode}
\newcommand{\mpi@beginfile}{%
  \ifx\mpi@mpostmem\mpi@empty\else%
    \mpi@addbuf{\@percentchar &\mpi@mpostmem}%
  \fi%
  \mpi@addbuf{\@percentchar generated from file `\jobname' by mpostinl.sty}%
  \ifmpi@include\else%
    \mpi@composehead%
    \mpi@addbufbuf{\mpi@defbuf}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@endfile}
% Write end of file to buffer;
% write |end| statement:
%    \begin{macrocode}
\newcommand{\mpi@endfile}{%
  \mpi@putlineno%
  \ifmpi@include\else%
    \mpi@addbuf{end}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@getchecksum}
% Compute the MD5 checksum for a metapost file and store in macro:
%    \begin{macrocode}
\newcommand{\mpi@getchecksum}[3]{%
  \IfFileExists{#3}{#1\edef#2{\pdfmdfivesum file{#3}}}{#1\let#2=\@undefined}%
}
%    \end{macrocode}

% \macro{\mpi@startfile}
% Start a new file if not already open:
%    \begin{macrocode}
\newcommand{\mpi@startfile}{%
  \ifmpi@infile\else%
%    \end{macrocode}
% Prevent reopening and overwriting the previous file:
%    \begin{macrocode}
    \ifx\mpostfilename\mpi@empty%
      \PackageError{mpostinl}{no filename provided to write to}{}%
    \fi%
%    \end{macrocode}
% Compute checksum of old file:
%    \begin{macrocode}
    \ifmpi@checksum\ifmpi@inclmod\else%
      \mpi@getchecksum{\global}{\mpi@checksum@before}{\mpostfilename.mp}%
    \fi\fi%
    \ifmpi@checksum\global\mpi@filemodfalse\else\global\mpi@filemodtrue\fi%
    \ifmpi@inclmod\global\mpi@filemodtrue\fi%
%    \end{macrocode}
% Open file for writing, prepare and write header to file:
%    \begin{macrocode}
    \global\mpi@infiletrue%
    \global\mpi@showinfilefalse%
    \gdef\mpi@filelist{}%
    \ifmpi@write\immediate\openout\mpi@out\mpostfilename.mp\fi%
    \mpi@clearbuf%
    \mpi@beginfile%
    \mpi@writebuf%
%    \end{macrocode}
% For include files, write |input| statement to definition buffer
% so that the file will be included by all main files:
%    \begin{macrocode}
    \ifmpi@include%
      \mpi@clearbuf%
      \mpi@putlineno%
      \mpi@addbuf{input \mpostfilename}%
      \mpi@addtoexp\mpi@defbuf{\the\mpi@buf^^J}%
%    \end{macrocode}
% If immediate mode is available fill immediate buffer with header:
%    \begin{macrocode}
    \else%
      \global\mpi@nowbuf={}%
      \mpi@writenow%
    \fi%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@startcontent}
% Start content section of file;
% make sure the file is open,
% prepare start of content section and write to file,
% if immediate mode is available also add to immediate buffer:
%    \begin{macrocode}
\newcommand{\mpi@startcontent}{%
  \mpi@startfile%
  \ifmpi@inbody\else%
    \global\mpi@inbodytrue%
    \mpi@clearbuf%
    \mpi@begindoc%
    \mpi@writebuf%
    \mpi@writenow%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@compile}
% Compile the metapost file (if writing and compiling is enabled):
%    \begin{macrocode}
\newcommand{\mpi@compile}[1]{%
  \ifmpi@write\ifmpi@compile%
    \ifx\mpi@mpostcompiler\mpi@empty%
%    \end{macrocode}
% Compose command line for |mpost|;
% pass on interactionmode setting to \MP{},
% specify mem filem, specify tex compiler:
%    \begin{macrocode}
      \def\mpi@imode{}%
      \ifcase\the\interactionmode%
        \def\mpi@imode{-interaction=batchmode}\or%
        \def\mpi@imode{-interaction=nonstopmode}\or%
        \def\mpi@imode{-interaction=scrollmode}\or%
        \def\mpi@imode{-interaction=errorstopmode}\fi%
      \let\mpi@texswitch\mpi@texcompiler%
      \ifx\mpi@texswitch\@undefined%
        \def\mpi@texswitch{\ifmpi@latex latex\else tex\fi}%
      \fi%
      \def\mpi@execute{mpost\space%
        \mpi@imode\space%
        \ifx\mpi@mpostmem\mpi@empty\else -mem=\mpi@mpostmem\space\fi%
        \ifx\mpi@texswitch\mpi@empty\else -tex=\mpi@texswitch\space\fi%
        #1}%
    \else%
%    \end{macrocode}
% Compose custom command:
%    \begin{macrocode}
      \def\mpi@execute{\mpi@mpostcompiler\space#1}%
    \fi%
%    \end{macrocode}
% Execute \MP{} by |\write18| command; do it again if needed:
%    \begin{macrocode}
    \immediate\write18{\mpi@execute}%
    \ifmpi@twice%
      \immediate\write18{\mpi@execute}%
    \fi%
  \fi\fi%
}
%    \end{macrocode}

% \macro{\mpi@cleanfile}
% Write a mps file containing a plain cross:
%    \begin{macrocode}
\newcommand{\mpi@cleanfile}[1]{%
  \immediate\openout\mpi@outnow#1%
  \immediate\write\mpi@outnow{%
    \@percentchar!PS-Adobe-3.0 EPSF-3.0^^J%
    \@percentchar\@percentchar Title: missing figure file^^J%
    \@percentchar\@percentchar Creator: mpostinl.sty^^J%
    \@percentchar\@percentchar BoundingBox: -36 -22 36 22^^J%
    \@percentchar\@percentchar EndComments^^J%
    \@percentchar\@percentchar Page: 1 1^^J%
    0 setgray 0 1.5 dtransform truncate idtransform setlinewidth pop^^J%
    [] 0 setdash 1 setlinecap 1 setlinejoin 10 setmiterlimit^^J%
    newpath -35 21 moveto 35 -21 lineto stroke^^J%
    newpath -35 -21 moveto 35 21 lineto stroke^^J%
    \@percentchar\@percentchar EOF}%
  \immediate\closeout\mpi@outnow%
}
%    \end{macrocode}

% \macro{\mpi@cleanfiles}
% Clean all figure files prior to compilation:
%    \begin{macrocode}
\newcommand{\mpi@cleanfiles}[1]{%
  \begingroup\def\mpi@tmp{#1}%
  \ifx\mpi@tmp\mpi@empty\else\def\mpi@tmp{\mpi@cleanfiles}%
    \mpi@cleanfile{#1}\fi%
  \expandafter\endgroup\mpi@tmp%
}
%    \end{macrocode}

% \macro{\mpi@closefile}
% Close the file, compile and reset:
%    \begin{macrocode}
\newcommand{\mpi@closefile}{%
  \ifmpi@infile%
%    \end{macrocode}
% Write end of content section (if started):
%    \begin{macrocode}
    \mpi@clearbuf%
    \ifmpi@inbody%
      \mpi@enddoc%
      \mpi@addbuf{}%
    \fi%
%    \end{macrocode}
% Write end of file and close:
%    \begin{macrocode}
    \mpi@endfile%
    \mpi@writebuf%
    \ifmpi@write\immediate\closeout\mpi@out\fi%
%    \end{macrocode}
% Determine whether file has changed:
%    \begin{macrocode}
    \ifmpi@filemod\else%
      \mpi@getchecksum{}{\mpi@checksum@after}{\mpostfilename.mp}%
      \ifx\mpi@checksum@before\mpi@checksum@after\else\mpi@filemodtrue\fi%
    \fi%
    \ifmpi@include\ifmpi@filemod\global\mpi@inclmodtrue\fi\fi%
%    \end{macrocode}
% Compile if file has changed and if it contains figures.
% Activate warning if file has changed and figures were displayed
% during composition:
%    \begin{macrocode}
    \ifmpi@filemod\ifmpi@inbody%
      \ifmpi@clean\ifmpi@write\ifmpi@compile%
        \expandafter\mpi@cleanfiles\mpi@filelist{}\fi\fi\fi%
      \mpi@compile{\mpostfilename.mp}%
    \fi\fi%
    \ifmpi@filemod\ifmpi@showinfile\global\mpi@warnmodtrue\fi\fi%
%    \end{macrocode}
% Reset variables:
%    \begin{macrocode}
    \global\mpi@infilefalse%
    \global\let\mpostfilename\mpi@empty%
    \global\mpi@inbodyfalse%
    \setcounter{mpi@count}{0}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpi@processnow}
% Write present figure to an individual file and process immediately:
%    \begin{macrocode}
\newcommand{\mpi@processnow}{%
  \ifmpi@nowactive\ifmpi@write\ifmpi@compile%
%    \end{macrocode}
% If immediate file is to be kept,
% use \textit{filename}|.mp| as source
% for \textit{filename}|.mps| output file.
%    \begin{macrocode}
    \ifmpi@nowkeep%
      \mpi@stripext{\mpi@figfile}%
      \edef\mpi@nowname{\mpi@stripped}%
    \fi%
%    \end{macrocode}
% Open immediate file, write the immediate buffer,
% the present figure and the end of file, close the file and compile:
%    \begin{macrocode}
    \immediate\openout\mpi@outnow\mpi@nowname.mp%
    \immediate\write\mpi@outnow{\the\mpi@nowbuf}%
    \immediate\write\mpi@outnow{\the\mpi@buf}%
    \mpi@clearbuf%
    \mpi@enddoc%
    \mpi@addbuf{}%
    \mpi@endfile%
    \immediate\write\mpi@outnow{\the\mpi@buf}%
    \immediate\closeout\mpi@outnow%
    \ifmpi@clean\mpi@cleanfile{\mpi@figfile}\fi%
    \mpi@compile{\mpi@nowname.mp}%
  \fi\fi\fi%
}
%    \end{macrocode}

% Make sure to close and process the file at the end.
% Warn if figures may need another compiler pass:
%    \begin{macrocode}
\AtEndDocument{\mpi@closefile}
\AtEndDocument{\mpi@warnmod}
%    \end{macrocode}

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \paragraph{External Commands.}
%
% The following commands are the interface of the package.
%
% \macro{\mpostsetup}
% |\mpostsetup| processes package options
% when the package has already been loaded:
%    \begin{macrocode}
\newcommand{\mpostsetup}[1]{%
  \setkeys\mpi@group{#1}%
  \mpi@warncompile%
  \ifdefined\pdfmdfivesum\else\mpi@checksumfalse\fi%
}
%    \end{macrocode}

% \macro{mpostdef}
% Declare options for the |mpostdef| environment:
%    \begin{macrocode}
\newif\ifmpi@deftex
\define@key{mpi@def}{tex}[true]{\csname mpi@deftex#1\endcsname}
\define@key{mpi@def}{global}[true]{\csname mpi@defglobal#1\endcsname}
%    \end{macrocode}

% The environment |mpostdef| adds a block
% of definitions to the metapost file.
% Pass on to |\mpi@scanblock| mechanism:
%    \begin{macrocode}
\def\mpostdef{\mpi@scanblock{\mpi@mpostdef}}
\def\mpi@mpostdef#1#2{%
%    \end{macrocode}
% Process optional arguments:
%    \begin{macrocode}
  \mpi@deftexfalse%
  \setkeys{mpi@def}{#1}%
%    \end{macrocode}
% \TeX{} definitions cannot be in an include file:
%    \begin{macrocode}
  \ifmpi@defglobal\else\ifmpi@deftex\ifmpi@include%
    \PackageWarning{mpostinl}{tex definitions within an include file %
      will be ignored by mpost; switching to global definition}{}%
    \mpi@defglobaltrue%
  \fi\fi\fi%
%    \end{macrocode}
% Prepare for recording; start the file if not open and not global,
% clear buffer, write current position,
% and add `|verbatimtex|' if in \TeX{} mode:
%    \begin{macrocode}
  \ifmpi@defglobal\else%
    \mpi@startfile%
  \fi%
  \mpi@clearbuf%
  \mpi@putlineno%
  \ifmpi@deftex%
    \mpi@addbuf{verbatimtex}%
  \fi%
  \mpi@verbatim#2%
}
%    \end{macrocode}
% Postprocessing; add `|etex|' if in \TeX{} mode,
% add to appropriate buffer(s).
%    \begin{macrocode}
\def\endmpostdef
{%
  \mpi@endverbatim%
  \mpi@addbufbuf{\mpi@verbbuf}%
  \ifmpi@deftex%
    \mpi@addbuf{etex}%
  \fi%
  \ifmpi@defglobal%
    \mpi@addtoexp\mpi@defbuf{\the\mpi@buf^^J}%
    \ifmpi@include\else\ifmpi@infile%
      \mpi@writebuf%
      \mpi@writenow%
    \fi\fi%
  \else%
    \mpi@writebuf%
    \ifmpi@include\else\mpi@writenow\fi%
  \fi%
}
%    \end{macrocode}

% \macro{mposttex}
% Declare options for the |mposttex| environment:
%    \begin{macrocode}
\newif\ifmpi@deftex
\define@key{mpi@tex}{dual}[true]{\csname mpi@texdual#1\endcsname}
\define@key{mpi@tex}{global}[true]{\csname mpi@defglobal#1\endcsname}
%    \end{macrocode}

% The environment |mposttex| adds a block
% of tex definitions to the metapost file.
% Pass on to |\mpi@scanblock| mechanism:
%    \begin{macrocode}
\def\mposttex{\mpi@scanblock{\mpi@mposttex}}
\def\mpi@mposttex#1#2{%
%    \end{macrocode}
% Process optional arguments:
%    \begin{macrocode}
  \mpi@texdualfalse%
  \setkeys{mpi@tex}{#1}%
%    \end{macrocode}
% \TeX{} definitions cannot be in an include file:
%    \begin{macrocode}
  \ifmpi@defglobal\else\ifmpi@include%
    \PackageWarning{mpostinl}{tex definitions within an include file %
      will be ignored by mpost; switching to global definition}{}%
    \mpi@defglobaltrue%
  \fi\fi%
%    \end{macrocode}
% Prepare for recording; start the file if not open and not global,
% clear buffer, write current position, and add `|verbatimtex|':
%    \begin{macrocode}
  \ifmpi@defglobal\else%
    \mpi@startfile%
  \fi%
  \mpi@clearbuf%
  \mpi@putlineno%
  \mpi@addbuf{verbatimtex}%
  \mpi@verbatim#2%
}
%    \end{macrocode}
% Postprocessing; add `|etex|',
% add to appropriate buffer(s).
%    \begin{macrocode}
\def\endmposttex
{%
  \mpi@endverbatim%
  \mpi@addbufbuf{\mpi@verbbuf}%
  \mpi@addbuf{etex}%
  \ifmpi@defglobal%
    \mpi@addtoexp\mpi@defbuf{\the\mpi@buf^^J}%
    \ifmpi@include\else\ifmpi@infile%
      \mpi@writebuf%
      \mpi@writenow%
    \fi\fi%
  \else%
    \mpi@writebuf%
    \ifmpi@include\else\mpi@writenow\fi%
  \fi%
%    \end{macrocode}
% Hack |\ignorespacesafterend| mechanism to
% ship out block contents after environment group:
%    \begin{macrocode}
  \ifmpi@texdual%
    \ignorespacesafterend%
    \global\let\mpi@ignorespaces\ignorespaces%
    \gdef\ignorespaces{\expandafter\scantokens\expandafter{\the\mpi@verbbuf}%
      \global\let\ignorespaces\mpi@ignorespaces}%
  \fi%
}
%    \end{macrocode}

% \macro{mpostfig}
% Declare options for the |mpostfig| environment:
%    \begin{macrocode}
\newif\ifmpi@figshow
\define@key{mpi@fig}{show}[true]{\csname mpi@figshow#1\endcsname}
\define@key{mpi@fig}{twice}[true]{\csname mpi@twice#1\endcsname}
\define@key{mpi@fig}{file}{\def\mpi@figfile{#1}}
\define@key{mpi@fig}{label}{\def\mpi@figlabel{#1}}
\define@key{mpi@fig}{opt}{\def\mpi@figopt{[#1]}}
\define@key{mpi@fig}{now}[true]{\csname mpi@now#1\endcsname}
%    \end{macrocode}

% The environment |mpostfig| adds a figure to the metapost file.
% Pass on to |\mpi@scanblock| mechanism:
%    \begin{macrocode}
\def\mpostfig{\mpi@scanblock{\mpi@mpostfig}}
\def\mpi@mpostfig#1#2{%
%    \end{macrocode}
% Make sure that include files do not contain figures:
%    \begin{macrocode}
  \ifmpi@include%
    \PackageError{mpostinl}{cannot write figure to include file}{}%
  \fi%
%    \end{macrocode}
% Process optional arguments:
%    \begin{macrocode}
  \def\mpi@figfile{}%
  \def\mpi@figlabel{}%
  \def\mpi@figopt{}%
  \mpi@figshowfalse%
  \setkeys{mpi@fig}{#1}%
%    \end{macrocode}
% Display figure if no filename or label is provided:
%    \begin{macrocode}
  \ifx\mpi@figlabel\mpi@empty\ifx\mpi@figfile\mpi@empty\mpi@figshowtrue\fi\fi%
%    \end{macrocode}
% Compose filename from label (if desired and specified):
%    \begin{macrocode}
  \ifmpi@labelnames\ifx\mpi@figfile\mpi@empty\ifx\mpi@figlabel\mpi@empty\else%
    \edef\mpi@figfile{\mpi@template{\mpi@figlabel}}%
  \fi\fi\fi%
%    \end{macrocode}
% Compose filename from counter if no filename is provided:
%    \begin{macrocode}
  \ifx\mpi@figfile\mpi@empty%
    \addtocounter{mpi@count}{1}%
    \edef\mpi@figfile{\mpi@template{\thempi@count}}%
  \fi%
%    \end{macrocode}
% Save filename to label, warn if label has already been defined,
% remember all labels for checking whether unused:
%    \begin{macrocode}
  \ifx\mpi@figlabel\mpi@empty\else%
    \expandafter\ifx\csname mpi@l@\mpi@figlabel\endcsname\relax\else%
      \PackageWarning{mpostinl}{label `\mpi@figlabel' already defined; %
        overwriting}{}%
    \fi%
    \expandafter\xdef\csname mpi@l@\mpi@figlabel\endcsname{\mpi@figfile}%
    \ifmpi@warnunused\ifmpi@figshow\else%
      \xdef\mpi@unusedlist{\mpi@unusedlist{\mpi@figlabel}}%
    \fi\fi%
  \fi%
%    \end{macrocode}
% Prepare for recording; start file and content section (if needed),
% clear buffer, write current position, begin figure block:
%    \begin{macrocode}
  \mpi@startcontent%
  \mpi@clearbuf%
  \mpi@putlineno%
  \mpi@beginfig{\mpi@figfile}%
  \mpi@verbatim#2%
}
%    \end{macrocode}
% Postprocessing; end figure block, add to buffer, add file to list,
% process immediately if desired:
%    \begin{macrocode}
\def\endmpostfig
{%
  \mpi@endverbatim%
  \mpi@addbufbuf{\mpi@verbbuf}%
  \mpi@endfig%
  \mpi@writebuf%
  \xdef\mpi@filelist{\mpi@filelist{\mpi@figfile}}%
  \ifmpi@now%
    \mpi@processnow%
  \fi%
%    \end{macrocode}
% Force compile if output file does not exist, is empty or equals dummy figure:
%    \begin{macrocode}
  \ifmpi@filemod\else%
    \mpi@getchecksum{}{\mpi@tmpa}{\mpi@figfile}%
    \ifdefined\mpi@tmpa%
      \edef\mpi@tmpa{\scantokens\expandafter{\mpi@tmpa\noexpand}}%
      \def\mpi@tmpb{965DFFE32ABB43CC14C58E254B4D660E}%
      \ifx\mpi@tmpa\mpi@tmpb\global\mpi@filemodtrue\fi%
      \def\mpi@tmpb{D78A5F2EE4B244260ECD6A0E89ADC9CE}%
      \ifx\mpi@tmpa\mpi@tmpb\global\mpi@filemodtrue\fi%
      \def\mpi@tmpb{D41D8CD98F00B204E9800998ECF8427E}%
      \ifx\mpi@tmpa\mpi@tmpb\global\mpi@filemodtrue\fi%
    \else%
      \global\mpi@filemodtrue%
    \fi
  \fi%
%    \end{macrocode}
% Display figure.
% Remember immediate display of figure unless processed immediately:
%    \begin{macrocode}
  \ifmpi@figshow%
    \expandafter\mpi@graphics\mpi@figopt{\mpi@figfile}%
    \ifmpi@now\else\global\mpi@showinfiletrue\fi%
  \fi%
}
%    \end{macrocode}

% \macro{\mpostuse}
% |\mpostuse| includes a metapost figure which was declared earlier
% via its label. The optional argument is passed as the optional
% argument for |\includegraphics|.
% Remember that the label has been used,
% remember if a file is currently open for writing:
%    \begin{macrocode}
\newcommand{\mpostuse}[2][]{%
  \expandafter\ifx\csname mpi@l@#2\endcsname\relax%
    \PackageWarning{mpostinl}{unknown label `#2'}{}%
    \mpostplaceholder[label]{#2}%
  \else%
    \ifmpi@warnunused\expandafter\gdef\csname mpi@lu@#2\endcsname{}\fi%
    \mpi@graphics[#1]{\csname mpi@l@#2\endcsname}%
    \ifmpi@infile\global\mpi@showinfiletrue\fi%
  \fi%
}
%    \end{macrocode}

% \macro{\mpostgetname}
% |\mpostgetname| gets the filename of a figure declared earlier
% via its label and returns it in the macro |\mpostfigurename|.
% Remember that the label has been used:
%    \begin{macrocode}
\newcommand{\mpostgetname}[1]{%
  \expandafter\ifx\csname mpi@l@#1\endcsname\relax%
    \PackageWarning{mpostinl}{unknown label `#1'}{}%
    \let\mpostfigurename\relax%
  \else%
    \ifmpi@warnunused\expandafter\gdef\csname mpi@lu@#1\endcsname{}\fi%
    \edef\mpostfigurename{\csname mpi@l@#1\endcsname}%
  \fi%
}
%    \end{macrocode}

% \macro{\mpostfile}
% Declare options for |\mpostfile|:
%    \begin{macrocode}
\define@key{mpi@file}{include}[true]{\csname mpi@include#1\endcsname}
%    \end{macrocode}

% |\mpostfile| sets up a new metapost file.
% If the previous file is still open,
% it will be closed and processed first:
%    \begin{macrocode}
\newcommand{\mpostfile}[2][]{%
  \mpi@closefile%
  \mpi@includefalse%
  \setkeys{mpi@file}{#1}%
  \xdef\mpostfilename{#2}%
}
%    \end{macrocode}

% \macro{\mpostdone}
% |\mpostdone| closes the present metapost file
% and processes it if applicable:
%    \begin{macrocode}
\newcommand{\mpostdone}{\mpi@closefile}
%    \end{macrocode}

% \macro{\mpostuse<>}
% This overwrites the definition of |\mpostfile|
% to accept the overlay option |<|\ldots|>|
% of the \textsf{beamer} package:
%    \begin{macrocode}
\ifmpi@beamer\@ifclassloaded{beamer}{
  \let\mpi@plain@mpostuse\mpostuse%
  \newcommand<>{\mpostuse}[2][]{\begingroup%
    \expandafter\def\expandafter\includegraphics\expandafter%
      {\includegraphics#3}%
    \mpi@plain@mpostuse[#1]{#2}\endgroup}%
}{}\fi
%    \end{macrocode}

%\iffalse
%</package>
%\fi
%
\endinput