summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/probsoln/probsoln.dtx
blob: 9b4593b23b0db5809e00bfcce7dfaa6a55bdadb0 (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
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
%\iffalse
% probsoln.dtx generated using makedtx version 1.1 (c) Nicola Talbot
% Command line args:
%   -doc "probsoln-manual.tex"
%   -src "(.+)\.(sty)\Z=>\1.\2"
%   -src "(sample.*)\.(tex)\Z=>\1.\2"
%   -src "(prob-.*)\.(tex)\Z=>\1.\2"
%   -comment ".+\.tex"
%   -macrocode ".+\.tex"
%   -author "Nicola Talbot"
%   probsoln
% Created on 2017/7/10 11:58
%\fi
%\iffalse
%<*package>
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%</package>
%\fi
% \iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2017 Nicola Talbot, all rights reserved.
% \fi
% \iffalse
%<*driver>
\documentclass[a4paper]{nlctdoc}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{color}

\usepackage{probsoln}

\usepackage[colorlinks,
            bookmarks,
            hyperindex=false,
            pdfauthor={Nicola L.C. Talbot},
            pdftitle={probsoln: creating problem sheets optionally with solutions}]{hyperref}
\doxitem{Option}{option}{package options}

\RecordChanges
\PageIndex
\CheckSum{1830}

\newcommand*{\dq}[1]{``#1''}

\begin{document}
\DocInput{probsoln.dtx}
\end{document}
%</driver>
%\fi
%\MakeShortVerb{"}
%\DeleteShortVerb{\|}
%
% \title{probsoln v3.05: 
%creating problem sheets optionally with solutions}
% \author{Nicola L.C. Talbot\\
%\url{http://www.dickimaw-books.com/}}
%
% \date{2017-07-10}
% \maketitle
%\tableofcontents
%
% \section{Introduction}
%The \styfmt{probsoln} package is designed for teachers or lecturers
%who want to create problem sheets for their students. This package
%was designed with mathematics problems in mind, but can be used for
%other subjects as well. The idea is to create a file containing a
%large number of problems with their solutions which can be read in
%by \LaTeX, and then select a number of problems to typeset. This
%means that once the database has been set up, each year you can
%easily create a new problem sheet that is sufficiently different
%from the previous year, thus preventing the temptation of current
%students seeking out the previous year's students, and checking out
%their answers. There is also an option that can be passed to the
%package to determine whether or not the solutions should be printed.
%In this way, one file can either produce the student's version or
%the teacher's version.
%
%You may want to consider using \sty{datatool} with \app{datatooltk}
%instead of \styfmt{probsoln}. See
%\href{http://www.dickimaw-books.com/latex/admin/html/datatoolexam.shtml}{Using
%the \styfmt{datatool} Package for Exams or Assignment Sheets}.
%
%\section{Package Options}\label{sec:pkgopt}
%The following options may be passed to this package:
%\begin{description}
%\item[\pkgopt{answers}]  Show the answers
%\item[\pkgopt{noanswers}] Don't show the answers (default)
%\item[\pkgopt{draft}] Display the label and dataset name when a problem is used
%\item[\pkgopt{final}] Don't display label and dataset name when a problem is used
%\item[\pkgopt{usedefaultargs}] Make \ics{thisproblem} use the
%default arguments supplied in the problem definition.
%\item[\pkgopt{nousedefaultargs}] Make \ics{thisproblem} prompt for
%problem arguments (default).
%\end{description}
%
%\section{Verbatim}\label{sec:verbatim}
%
%As from version 3.02, problems and solutions may contain verbatim
%text, but you must use the \iterm{fragile}\texttt{fragile} (or
%\texttt{fragile=true}) option for the associated environments.
%
%Alternatively, if most of your problems contain verbatim, you can
%globally set this option using:
%\begin{verbatim}
%\setkeys{probsoln}{fragile}
%\end{verbatim}
%You can switch off this option using \texttt{fragile=false}.
%
%The \texttt{fragile} option writes information to a temporary file.
%This defaults to "\jobname.vrb" but the name may be changed. The
%extension (".vrb") is given by:
%\begin{definition}[\DescribeMacro{\ProbSolnFragileExt}]
%\cs{ProbSolnFragileExt}
%\end{definition}
%The base name (\cs{jobname}) is given by:
%\begin{definition}[\DescribeMacro{\ProbSolnFragileFile}]
%\cs{ProbSolnFragileFile}
%\end{definition}
%
%\section{Showing and Hiding Solutions}\label{sec:showanswers}
%
%In addition to the \pkgopt{answers} and \pkgopt{noanswers} package
%options, it is also possible to show or suppress the solutions
%using
%\begin{definition}[\DescribeMacro{\showanswers}]
%\cs{showanswers}
%\end{definition}
%and
%\begin{definition}[\DescribeMacro{\hideanswers}]
%\cs{hideanswers}
%\end{definition}
%respectively.
%
%The boolean variable \bool{showanswers} determines whether the
%answers should be displayed. You can use this value with the
%\sty{ifthen} package to specify different text depending on 
%whether the solutions should be displayed. For example:
%\begin{verbatim}
%Assignment 1\ifthenelse{\boolean{showanswers}}{ (Solution Sheet)}{}
%\end{verbatim}
%Alternatively you can use \ics{ifshowanswers}\ldots\cs{else}\ldots
%\cs{fi}:
%\begin{verbatim}
%Assignment 1\ifshowanswers\space (Solution Sheet)\fi
%\end{verbatim}
%
%For longer passages, you can use the environments
%\begin{definition}[\DescribeEnv{onlyproblem}]
%\cs{begin}\marg{onlyproblem}\oarg{option}
%\end{definition}
%and 
%\begin{definition}[\DescribeEnv{onlysolution}]
%\cs{begin}\marg{onlysolution}\oarg{option}
%\end{definition}
%For example:
%\begin{verbatim}
%\begin{onlyproblem}%
%What is the derivative of $f(x) = x^2$?
%\end{onlyproblem}%
%\begin{onlysolution}%
%$f'(x) = 2x$
%\end{onlysolution}
%\end{verbatim}
%The above will only display the question if \bool{showanswers}
%is false and will only display the solution if \bool{showanswers}
%is true. If you want the question to appear in the answer
%sheet as well as the solution, then don't put the question in
%the \env{onlyproblem} environment:
%\begin{verbatim}
%What is the derivative of $f(x) = x^2$?
%\begin{onlysolution}%
%Solution: $f'(x) = 2x$
%\end{onlysolution}
%\end{verbatim}
%
%\begin{important}
%If you want to include verbatim text in the body of
%\env{onlyproblem} or \env{onlysolution}, you need to specify
%\texttt{fragile} in the optional argument of the environment.
%(See \sectionref{sec:verbatim} for further details.)
%\end{important}
%
%If you use \envfmt{onlysolution} within the \env{defproblem}
%environment, the problem will be tagged as having a solution
%and will be added to the list used by \ics{foreachsolution}.
%The optional argument of \envfmt{onlysolution} (and \env{onlyproblem})
%is inherited from the parent \env{defproblem} setting.
%
%\section{General Formatting Commands}\label{sec:formatting}
%
%The commands and environments described in this section are
%provided to assist formatting problems and their solutions.
%\begin{definition}[\DescribeEnv{solution}]
%\verb|\begin{solution}|\meta{text}\verb|\end{solution}|
%\end{definition}
%By default, this is equivalent to 
%\begin{display}
%\verb|\par\noindent\textbf{\solutionname}: |\meta{text}
%\end{display}
%where \DescribeMacro{\solutionname}\cs{solutionname} defaults
%to \dq{\solutionname}. Note that you must place the \env{solution}
%environment inside the \envfmt{onlysolution} environment or
%between \ics{ifshowanswers}\ldots\cs{fi} to ensure that it
%is suppressed when the solutions are not wanted. (See
%\sectionref{sec:showanswers}.) 
%
%
%Note that the \styfmt{probsoln} package will only define the 
%\env{solution} environment if it is not already defined.
%
%\begin{definition}[\DescribeEnv{textenum}]
%\verb|\begin{textenum}|\ldots\verb|\end{textenum}|
%\end{definition}
%The \envfmt{textenum} environment is like the \env{enumerate}
%environment but is in-line. It uses the same counter that the
%\envfmt{enumerate} environment would use at that level so the
%question can be compact but the answer can use \envfmt{enumerate}
%instead. For example:
%\begin{verbatim}
%\begin{onlyproblem}%
%  Differentiate the following:
%  \begin{textenum}
%    \item $f(x)=2^x$; \item $f(x)=\cot(x)$
%  \end{textenum}
%\end{onlyproblem}
%\begin{onlysolution}
%  \begin{enumerate}
%  \item
%    \begin{align*}
%    f(x) &= 2^x = \exp(\ln(x^2)) =\exp(2\ln(x))\\
%    f'(x) &= \exp(2\ln(x))\times \frac{2}{x}\\
%      &= f(x)\frac{2}{x}
%    \end{align*}
%  \item
%    \begin{align*}
%    f(x) &= \cot(x) = (\tan(x))^{-2}\\
%    f'(x) &= -(\tan(x))^{-2}\times\sec^2(x)\\
%    &=-\csc^2x
%    \end{align*}
%  \end{enumerate}
%\end{onlysolution}
%\end{verbatim}
%In this example, the items in the question are brief, so an
%\env{enumerate} environment would result in a lot of unnecessary
%white space, but the answers require more space, so an
%\envfmt{enumerate} environment is more appropriate. Since the
%\envfmt{textenum} environment uses the same counters as the
%\envfmt{enumerate} environment, the question and answer sheets use
%consistent labelling. Note that there are other packages available
%on CTAN that you can use to create in-line lists. Check the
%\urlfootref{http://www.tex.ac.uk/tex-archive/help/Catalogue/bytopic.html\#enumeration}{TeX
%Catalogue} for further details.
%
%\DescribeMacro{\correctitem}\DescribeMacro{\incorrectitem}
%\begin{definition}
%\cs{correctitem}\\
%\cs{incorrectitem}
%\end{definition}
%You can use the commands \cs{correctitem} and \cs{incorrectitem} 
%in place of \ics{item}. If the solutions are suppressed, these
%commands behave in the same way as \cs{item}, otherwise they
%format the item label using one of the commands:
%\DescribeMacro{\correctitemformat}\DescribeMacro{\incorrectitemformat}
%\begin{definition}
%\cs{correctitemformat}\marg{label}\\
%\cs{incorrectitemformat}\marg{label}
%\end{definition}
%For example:
%\begin{verbatim}
%Under which of the following functions does $S=\{a_1,a_2\}$
%become a probability space?
%\begin{enumerate}
%\incorrectitem $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
%\correctitem $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
%\correctitem $P(a_1)=1$, $P(a_2)=0$
%\incorrectitem $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
%\end{enumerate}
%\end{verbatim}
%The default definition of \cs{correctitemformat} puts a frame around
%the label.
%
%\section{Defining a Problem}\label{sec:defproblem}
%
%It is possible to construct a problem sheet with solutions using the
%commands described in the previous sections, however it is also
%possible to define a set of problems for later use. In this way you
%can create an external file containing many problems some or all of
%which can be loaded and used in a document. The \styfmt{probsoln}
%package has a default data set labelled \dq{default} in which you
%can store problems. Alternatively, you can create multiple data
%sets. You can then iterate through each problem in a problem set.
%You can use a previously defined problem more than once, which means
%that by judicious use of \env{onlyproblem}, \env{onlysolution} or
%the \bool{showanswers} boolean variable in conjunction with
%\ics{showanswers} and \ics{hideanswers}, you can print the solutions
%in a different location to the questions (for example in an
%appendix).
%
%\begin{definition}[\DescribeEnv{defproblem}]
%\verb|\begin{defproblem}|\oarg{n}\oarg{default args}\marg{label}\oarg{option}\newline
%\meta{definition}\newline
%\verb|\end{defproblem}|
%\end{definition}
%This defines the problem whose label is given by \meta{label}. The
%label must be unique for a given data set and should not contain
%active characters or a comma. (Active characters include the special characters
%such as \$ and \&, but some packages may make other symbols active,
%such as the colon (:) character. For example, the \sty{ngerman} and
%\sty{babel} packages make certain punctuation active. Check the
%relevant package documentation for details.)
%
%\begin{important}
%The final optional argument \meta{option} may be \texttt{fragile} to
%indicate that the problem contains verbatim text. Any occurrences of
%\env{onlyproblem} or \env{onlysolution} contained within
%\envfmt{defproblem} are inherited from \envfmt{defproblem}. (See
%\sectionref{sec:verbatim} for further details.)
%\end{important}
%
%If \env{defproblem} occurs in the document or is included via
%\ics{input} or \ics{include}, then the problem will be added to
%the default data set. If \envfmt{defproblem} occurs in an external
%file that is loaded using one of the commands defined in
%\sectionref{sec:load} then the problem will be added to
%the specified data set.
%
%The contents of the \env{defproblem} environment should be the text
%that defines the problem. This may include any of the commands
%defined in \sectionref{sec:showanswers} and
%\sectionref{sec:formatting}.
%
%The problem may optionally take \meta{n} arguments (where 
%\meta{n} is from 0 to 9). The arguments can be referenced
%in the definition via \texttt{\#1},\ldots,\texttt{\#9}.
%If \meta{n} is omitted then the problem doesn't take any
%arguments.
%The following example defines a problem with one argument:
%\begin{verbatim}
%\begin{defproblem}[1]{diffsin}
%Differentiate $f(x)=\sin(#1x)$.
%\begin{onlysolution}%
%  \begin{solution}
%    $f'(x) = #1\cos(#1x)$
%  \end{solution}
%\end{onlysolution}
%\end{defproblem}
%\end{verbatim}
%
%The second optional argument \meta{default args} supplies 
%default problem arguments that will automatically be used within
%\ics{thisproblem} when used in \ics{foreachproblem} in conjunction
%with the package option \pkgopt{usedefaultargs}. (See \sectionref{sec:foreach}.)
%For example:
%\begin{verbatim}
%\begin{defproblem}[1][{2}]{diffsin}
%Differentiate $f(x)=\sin(#1x)$.
%\begin{onlysolution}%
%  \begin{solution}
%    $f'(x) = #1\cos(#1x)$
%  \end{solution}
%\end{onlysolution}
%\end{defproblem}
%\end{verbatim}
%\begin{important}
%If you don't use \cs{thisproblem} or you don't
%use the package option \pkgopt{usedefaultargs}, then you must supply
%the arguments.
%\end{important}
%
%\begin{definition}[\DescribeMacro{\newproblem}]
%\cs{newproblem}\oarg{n}\oarg{default args}\marg{label}\marg{problem}\marg{solution}
%\end{definition}
%This is a shortcut command for:
%\begin{ttfamily}\obeylines
%\cs{begin}\{defproblem\}\oarg{n}\oarg{default args}\marg{label}\%
%\meta{problem}\%
%\cs{begin}\{onlysolution\}\%
%\cs{begin}\{solution\}\%
%\meta{solution}\%
%\cs{end}\{solution\}\%
%\cs{end}\{onlysolution\}\%
%\cs{end}\{defproblem\}
%\end{ttfamily}
%For example:
%\begin{verbatim}
%\newproblem[1]{diffcos}{%
%  \(f(x) = \cos(#1x)\)
%}%
%{%
%  \(f'(x) = -#1\sin(#1x)\)
%}
%\end{verbatim}
%is equivalent to
%\begin{verbatim}
%\begin{defproblem}[1]{diffcos}%
%  \(f(x) = \cos(#1x)\)
%\begin{onlysolution}%
%  \begin{solution}%
%    \(f'(x) = -#1\sin(#1x)\)
%  \end{solution}%
%\end{onlysolution}%
%\end{defproblem}
%\end{verbatim}
%(In this example, the argument will need to be a positive number
%to avoid a double minus in the answer. If you want to perform
%floating point arithmetic on the arguments, then try the
%\sty{fp} or \sty{pgfmath} packages.)
%
%Alternatively, if you want to supply default arguments to use when
%iterating through problems with \ics{foreachproblem}:
%\begin{verbatim}
%\newproblem[1][{3}]{diffsin}{%
%  \(f(x) = \sin(#1x)\)
%}%
%{%
%  \(f'(x) = #1\cos(#1x)\)
%}
%\end{verbatim}
%
%
%\begin{definition}[\DescribeMacro{\newproblem*}]
%\cs{newproblem*}\oarg{n}\oarg{default args}\marg{label}\marg{definition}
%\end{definition}
%This is a shortcut for:
%\begin{ttfamily}\obeylines
%\cs{begin}\{defproblem\}\oarg{n}\oarg{default args}\marg{label}\%
%\meta{definition}\%
%\cs{end}\{defproblem\}
%\end{ttfamily}
%
%\begin{important}
%Note that you can't use verbatim text with \cs{newproblem} or
%\cs{newproblem*}. Use the \env{defproblem} environment instead with
%the \texttt{fragile option}.
%\end{important}
%
%\section{Using a Problem}\label{sec:useproblem}
%
%Once you have defined a problem using \env{defproblem} or
%\ics{newproblem} (see \sectionref{sec:defproblem}), you can 
%later display the problem using:
%\begin{definition}[\DescribeMacro{\useproblem}]
%\cs{useproblem}\oarg{data set}\marg{label}\marg{arg$_1$}\ldots
%\marg{arg$_N$}
%\end{definition}
%where \meta{data set} is the name of the data set that contains
%the problem (the default data set is used if omitted), 
%\meta{label} is the label identifying the required problem and
%\meta{arg$_1$}, \ldots, \meta{arg$_N$} 
%are the arguments to pass to the problem, if the problem was 
%defined to have arguments (where $N$ is the number 
%of arguments specified when the problem was defined).
%
%For example, in the previous section the problem \texttt{diffcos} 
%was defined to have one argument, so it can be used as follows:
%\begin{verbatim}
%\useproblem{diffcos}{3}
%\end{verbatim}
%This will be equivalent to:
%\begin{verbatim}
%\(f(x) = \cos(3x)\)
%\begin{onlysolution}%
%\begin{solution}%
%\(f'(x) = -3\sin(3x)\)
%\end{solution}%
%\end{onlysolution}%
%\end{verbatim}
%
%\section{Loading Problems From External Files}\label{sec:load}
%
%You can store all your problem definitions (see
%\sectionref{sec:defproblem}) in an external file. 
%These problems can all be appended to the default data set by
%including the file via \ics{input} or they can be appended
%to other data sets using one of the commands described below.
%Once you have loaded all the required problems, you can
%iterate through the data sets using the commands described
%in \sectionref{sec:foreach}. Note that the commands below
%will create a new data set, if the named data set doesn't
%exist.
%
%\begin{definition}[\DescribeMacro{\loadallproblems}]
%\cs{loadallproblems}\oarg{data set}\marg{filename}
%\end{definition}
%This will load all problems defined in \meta{filename} and
%append them to the specified data set, in the order in which
%they are defined in the file. If \meta{data set} is
%omitted, the default data set will be used. If \meta{data set}
%doesn't exist, it will be created.
%
%\begin{definition}[\DescribeMacro{\loadselectedproblems}]
%\cs{loadselectedproblems}\oarg{data set}\marg{labels}\marg{filename}
%\end{definition}
%This is like \cs{loadallproblems}, but only those problems whose
%label is listed in the comma-separated list \meta{labels} are
%loaded. For example, if I have some problems defined in the
%file \texttt{derivatives.tex}, then
%\begin{verbatim}
%\loadselectedproblems{diffsin,diffcos}{derivatives}
%\end{verbatim}
%will only load the problems whose labels are \texttt{diffsin}
%and \texttt{diffcos}, respectively. All the other problems in 
%the file will remain undefined.
%
%\begin{definition}[\DescribeMacro{\loadexceptproblems}]
%\cs{loadexceptproblems}\oarg{data set}\marg{exception list}\marg{filename}
%\end{definition}
%This is the reverse of \cs{loadselectedproblems}. This loads all
%problems except those whose labels are listed in \meta{exception
%list}.
%
%\begin{definition}[\DescribeMacro{\loadrandomproblems}]
%\cs{loadrandomproblems}\oarg{data set}\marg{n}\marg{filenames}
%\end{definition}
%This randomly loads \meta{n} problems from the comma-separated
%list\footnote{The list form was added to v3.05. Earlier versions
%only allow a single filename.}
%of \meta{filenames} and adds them to the given data set. If
%\meta{data set} is omitted, the default data set is assumed. Note
%that the problems will be added to the data set in a random order,
%not in the order in which they were defined. There must be at least
%\meta{n} problems defined across the given list of files.
%
%Note that there's a difference between
%\begin{verbatim}
%\loadrandomproblems{5}{problemset1}
%\loadrandomproblems{5}{problemset2}
%\end{verbatim}
%and
%\begin{verbatim}
%\loadrandomproblems{10}{problemset1,problemset2}
%\end{verbatim}
%In the first case, the data set will contain 5 problems randomly
%selected from \texttt{problemset1} and 5 problems randomly selected
%from \texttt{problemset2}. Whereas in the second case, the data set
%will contain 10 problems randomly selected across both files.
%
%\begin{definition}[\DescribeMacro{\loadrandomexcept}]
%\cs{loadrandomexcept}\oarg{data set}\marg{n}\marg{filenames}\marg{exception list}
%\end{definition}
%This is similar to \cs{loadrandomproblems} except that it won't load
%those problems whose labels are listed in \meta{exception list}.
%\textbf{If you want to automatically exclude problems included in
%previous documents, see \sectionref{sec:exprev}.}
%
%Note that the random number generator has been modified in version
%3.01 in order to fix a bug. If you want to ensure that your random
%numbers are compatible with earlier versions, you can switch to the
%old generator using
%\begin{definition}[\DescribeMacro{\PSNuseoldrandom}]
%\cs{PSNuseoldrandom}
%\end{definition}
%
%\begin{important}
%It is generally not a good idea to place anything in 
%\meta{filename} that is not inside the body of \env{defproblem} 
%or in the arguments to \ics{newproblem} or \ics{newproblem*}.
%All the commands in this section input the external file within
%a local scope, so command definitions would need to be made
%global to have any effect. In addition, \cs{loadrandomproblems}
%has to load each file twice, which means that anything outside
%a problem definition will be parsed twice.
%\end{important}
%
%\subsection{Randomly Selecting Problems Not Selected in Previous
%Documents}
%\label{sec:exprev}
%
%Suppose you have a large set of questions that you want to randomly
%select for assignments and exams. The chances are, you don't want to
%include questions that have been previously set for, say, the last
%three years. That is, you don't want to select questions the
%students may already have seen. As from version 3.03, you can now do
%this.
%
%The \sty{probsoln} package defaults to the UK academic year, which
%starts in September. If this isn't appropriate, you can change it
%using:
%\begin{definition}[\DescribeMacro{\SetStartMonth}]
%\cs{SetStartMonth}\marg{n}
%\end{definition}
%where \meta{n} is the number of the month. (1 = January, 2 =
%February, etc.)
%
%The \emph{start year} is the calender year in effect when the
%academic year started. For example, if this is the academic year
%2011/12, then the start year is 2011. This is automatically set to
%the start of the current academic year. It is also updated when
%\cs{SetStartMonth} is used.\footnote{So don't use \cs{SetStartMonth}
%after \cs{SetStartYear}.} If you want to set it to a specific year,
%you can use:
%\begin{definition}[\DescribeMacro{\SetStartYear}]
%\cs{SetStartYear}\marg{year}
%\end{definition}
%For example: \verb|\SetStartYear{2008}| indicates the academic year
%2008/9.
%
%There are two files concerned with previously used labels. They are:
%\begin{description}
%
%  \item[The previously used labels file] This keeps track of all
%    problems used in previous years, as well as problems used by
%    other documents that have this as their previously used labels
%    file, and it contains the problem labels from the last run of
%    the current document.
%
%  \item[The current used labels file] This defaults to
%\cs{jobname}\texttt{.prb}, but the name can be changed using:
%  \begin{definition}[\DescribeMacro{\SetUsedFileName}]
%  \cs{SetUsedFileName}\marg{name}
%  \end{definition}
%  This file keeps track of all the labels used in the current
%  document from the previous \LaTeX\ run. Note that if you want to
%  delete this file, first clear it using
%  \begin{definition}[\DescribeMacro{\ClearUsedFile}]
%  \cs{ClearUsedFile}\marg{file}
%  \end{definition}
%  in place of \cs{ExcludePreviousFile}\marg{file}, described below.
%  The argument \meta{file} is the previously used labels file
%  described above. \cs{ClearUsedFile} will remove all labels in
%  the current used labels file from the previously used labels file
%  and clear the current used labels file. Once this file is empty,
%  it may then be deleted.
%
%\end{description}
%
%Before loading randomly selected problems, first specify the
%previously used labels file with the command:
%\begin{definition}[\DescribeMacro{\ExcludePreviousFile}]
%\cs{ExcludePreviousFile}\oarg{number of years}\marg{file name}
%\end{definition}
%where \meta{file name} is the name of the previously used file. The
%optional argument \meta{number of years} specifies the year cut-off.
%This defaults to 3, which means that only those labels used this
%year or the previous 2 years will be kept. Any problems used before
%then may be reused.
%
%Suppose I'm lecturing a first year undergraduate mathematics course
%(designated, say, mth101). I want to set assignments on each topic
%and an exam at the end of the year (as well as a resit or second
%sitting paper). I've got databases with problems for each topic, but
%the first and second sitting exams mustn't include any of the
%problems used in the assignments or any problems used in assignments
%or exams for the previous two academic years. I'm going to arrange
%my directory structure as follows:
%\begin{itemize}
%\item \texttt{mth101/}
%  \begin{itemize}
%   \item \texttt{assignment1/} (differentiation)
%     \begin{itemize}
%       \item \texttt{assignment1.tex}
%     \end{itemize}
%   \item \texttt{assignment2/} (probability spaces)
%     \begin{itemize}
%       \item \texttt{assignment2.tex}
%     \end{itemize}
%   \item \texttt{assignment3/} (linear algebra)
%     \begin{itemize}
%       \item \texttt{assignment3.tex}
%     \end{itemize}
%   \item \texttt{exams/}
%     \begin{itemize}
%       \item \texttt{exam.tex} (first sitting)
%       \item \texttt{resit.tex} (second sitting)
%     \end{itemize}
%   \item \texttt{databases/}
%     \begin{itemize}
%       \item \texttt{differentiation.tex}
%       \item \texttt{probabilityspaces.tex}
%       \item \texttt{linearalgebra.tex}
%     \end{itemize}
%   \item \texttt{previouslabels.tex} (created by \sty{probsoln})
%  \end{itemize}
%\end{itemize}
%
%\section{Iterating Through Datasets}\label{sec:foreach}
%
%Once you have defined all your problems for a given data set, you
%can use an individual problem with \ics{useproblem} (see
%\sectionref{sec:useproblem}) but it is more likely that you will
%want to iterate through all the problems so that you don't need to
%remember the labels of all the problems you have defined.
%
%\begin{definition}[\DescribeMacro{\foreachproblem}]
%\cs{foreachproblem}\oarg{data set}\marg{body}
%\end{definition}
%This does \meta{body} for each problem in the given data set.
%If \meta{data set} is omitted, the default data set is used.
%Within \meta{body} you can use 
%\begin{definition}[\DescribeMacro{\thisproblem}]
%\cs{thisproblem}
%\end{definition}
%to use the current problem and
%\begin{definition}[\DescribeMacro{\thisproblemlabel}]
%\cs{thisproblemlabel} 
%\end{definition}
%to access the current label. If the problem requires arguments,
%and no default arguments were supplied in the problem definition or
%the package option \pkgopt{usedefaultargs} was not used, then
%you will be prompted for arguments, so if you want to use this
%approach you will need to use \LaTeX\ in interactive mode. If
%you do provide arguments, they will be stored in the event that
%you need to iterate through the data set again. The
%arguments will be included in \cs{thisproblem}, so you only
%need to use \cs{thisproblem} without having to specify
%\ics{useproblem}.
%
%For example, to iterate through all problems
%in the default data set:
%\begin{verbatim}
%\begin{enumerate}
%\foreachproblem{\item\thisproblem}
%\end{enumerate}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\foreachsolution}]
%\cs{foreachsolution}\oarg{data set}\marg{body}
%\end{definition}
%This is equivalent to \cs{foreachproblem}, but only iterates
%through problems that contain the \env{onlysolution} environment.
%Note that you still need to use \ics{showanswers} or the
%\pkgopt{answers} package option for the contents of the
%\env{onlysolution} environment to appear.
%
%\begin{definition}[\DescribeMacro{\foreachdataset}]
%\cs{foreachdataset}\marg{cmd}\marg{body}
%\end{definition}
%This does \meta{body} for each of the defined data sets. Within
%\meta{body}, \meta{cmd} will be set to the name of the current
%data set. For example, to display all problems in all data sets:
%\begin{verbatim}
%\begin{enumerate}
%\foreachdataset{\thisdataset}{%
%\foreachproblem[\thisdataset]{\item\thisproblem}}
%\end{enumerate}
%\end{verbatim}
%
%Suppose I have two external files called
%\texttt{derivatives.tex} and \texttt{probspaces.tex} which
%define problems using both \env{onlyproblem} and 
%\env{onlysolution} for example:
%\begin{verbatim}
%\begin{defproblem}{cosxsqsinx}%
%\begin{onlyproblem}%
%$y = \cos(x^2)\sin x$.%
%\end{onlyproblem}%
%\begin{onlysolution}%
%\[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]
%\end{onlysolution}%
%\end{defproblem}
%\end{verbatim}
%I can write a document that creates two data sets, one for
%the derivative problems and one for the problems about
%probability spaces. I can then use \ics{hideanswers} and
%iterate through the require data set to produce the problems.
%Later, I can use \ics{showanswers} and iterate over all problems defined in both data
%sets to produce the chapter containing all the answers. When
%displaying the questions, I have taken advantage of the fact that
%I can cross-reference items within an \env{enumerate} environment,
%and redefined \ics{theenumi} to label the questions according to
%the chapter. The cross-reference label is constructed from
%the problem label and is referenced in the answer section to
%ensure that the answers have the same label as the questions.
%\begin{verbatim}
%\documentclass{report}
%\usepackage{probsoln}
%\begin{document}
%\hideanswers
%\chapter{Differentiation}
%% randomly select 25 problems from derivatives.tex and add to
%% the data set called 'deriv'
%\loadrandomproblems[deriv]{25}{derivatives}
%
%% Display the problems
%\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
%\begin{enumerate}
%\foreachproblem[deriv]{\item\label{prob:\thisproblemlabel}\thisproblem}
%\end{enumerate}
%% You may need to change \theenumi back here
%
%\chapter{Probability Spaces}
%% randomly select 25 problems from probspaces.tex and add to
%% the data set called 'spaces'
%\loadrandomproblems[spaces]{25}{probspaces}
%
%% Display the problems
%\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
%\begin{enumerate}
%\foreachproblem[spaces]{\item\label{prob:\thisproblemlabel}\thisproblem}
%\end{enumerate}
%% You may need to change \theenumi back here
%
%\appendix
%
%\chapter{Solutions}
%\showanswers
%\begin{itemize}
%\foreachdataset{\thisdataset}{%
%\foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}
%}
%\end{itemize}
%
%\end{document}
%\end{verbatim}
%
%\section{Random Number Generator}\label{sec:random}
%
%This package provides a pseudo-random number generator that is used
%by \ics{loadrandomproblems}. As noted earlier the random number
%generator has been modified in version 3.01 in order to fix a bug.
%If you want to ensure that your random numbers are compatible with
%earlier versions, you can switch to the old generator using
%\begin{definition}[\DescribeMacro{\PSNuseoldrandom}]
%\cs{PSNuseoldrandom}
%\end{definition}
%
%\begin{definition}[\DescribeMacro{\PSNrandseed}]
%\cs{PSNrandseed}\marg{n}
%\end{definition}
%This sets the seed to \meta{n} which must be a non-zero integer.
%For example, to generate a different set of random numbers
%every time you \LaTeX\ your document,\footnote{assuming you
%leave at least a minute between runs.} put the following in your
%preamble:
%\begin{verbatim}
%\PSNrandseed{\time}
%\end{verbatim}
%or to generate a different set of random numbers every year you
%\LaTeX\ your document:
%\begin{verbatim}
%\PSNrandseed{\year}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\PSNgetrandseed}]
%\cs{PSNgetrandseed}\marg{register}
%\end{definition}
%This stores the current seed in the count register specified by 
%\meta{register}.
%For example:
%\begin{verbatim}
%\newcount\myseed
%\PSNgetrandseed{\myseed}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\PSNrandom}]
%\cs{PSNrandom}\marg{register}\marg{n}
%\end{definition}
%Generates a random integer from 1 to \meta{n} and stores in 
%the count register specified by \meta{register}. For example,
%the following generates an integer from 1 to 10 and stores it
%in the register \cs{myreg}:
%\begin{verbatim}
%\newcount\myreg
%\PSNrandom{\myreg}{10}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\random}]
%\cs{random}\marg{counter}\marg{min}\marg{max}
%\end{definition}
%Generates a random integer from \meta{min} to \meta{max} and
%stores in the given counter. For example, the following generates
%a random number between 3 and 8 (inclusive) and stores it in
%the counter \texttt{myrand}.
%\begin{verbatim}
%\newcounter{myrand}
%\random{myrand}{3}{8}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\doforrandN}]
%\cs{doforrandN}\marg{n}\marg{cmd}\marg{list}\marg{text}
%\end{definition}
%Randomly selects \meta{n} values from the comma-separated list
%given by \meta{list} and iterates through this subset. On
%each iteration it sets \meta{cmd} to the current value and
%does \meta{text}. For example, the following will load a
%randomly selected problem from two of the listed files (where
%\texttt{file1.tex}, \texttt{file2.tex} and \texttt{file3.tex}
%are files containing at least one problem):
%\begin{verbatim}
%\doforrandN{2}{\thisfile}{file1,file2,file3}{%
%\loadrandomproblems{1}{\thisfile}}
%\end{verbatim}
%
%\section{Compatibility With Versions Prior to 3.0}
%
%Version 3.0 of the \sty{probsoln} package completely changed the
%structure of the package, but the commands described in this
%section have been provided to maintain compatibility with 
%earlier versions. The only problems that are likely to occur are
%those where commands are contained within groups. This will 
%effect any commands that are contained in external files that are
%outside of the arguments to \ics{newproblem} and \ics{newproblem*}.
%However, since the external files had to be parsed twice in
%order to load the problems, this shouldn't be an issue as adding
%anything other than problem definitions in those files would
%be problematic anyway.
%
%The other likely difference is where the random generator is
%used in a group. This includes commands such as 
%\ics{selectrandomly}. For example, if your document contained
%something like:
%\begin{verbatim}
%\begin{enumerate}
%\selectrandomly{file1}{8}
%
%\item Solve the following:
%\begin{enumerate}
%\selectrandomly{file2}{4}
%\end{enumerate}
%
%\selectrandomly{file3}{2}
%\end{enumerate}
%\end{verbatim}
%Then using versions prior to v3.0 will produce a different
%set of random numbers since the second \cs{selectrandomly}
%is in a different level of grouping. If you want to ensure
%that the document produces exactly the same random set with
%the new version as with the old version, you will need to
%get and set the random number seed. For example, the above
%would need to be modified so that it becomes:
%\begin{verbatim}
%\begin{enumerate}
%\selectrandomly{file1}{8}
%
%\item Solve the following:
%\newcount\oldseed
%\PSNgetrandseed{\oldseed}
%\begin{enumerate}
%\selectrandomly{file2}{4}
%\end{enumerate}
%\PSNrandseed{\oldseed}
%
%\selectrandomly{file3}{2}
%\end{enumerate}
%\end{verbatim}
%
%\begin{definition}[\DescribeMacro{\selectrandomly}]
%\cs{selectrandomly}\marg{filename}\marg{n}
%\end{definition}
%This is now equivalent to:
%\begin{ttfamily}\obeylines
%\{\cs{loadrandomproblems}\oarg{filename}\marg{n}\marg{filename}\}\%
%\cs{foreachproblem}\oarg{filename}\{\cs{PSNitem}\cs{thisproblem}\cs{endPSNitem}\}
%\end{ttfamily}
%
%\begin{definition}[\DescribeMacro{\selectallproblems}]
%\cs{selectallproblems}\marg{filename}
%\end{definition}
%This is now equivalent to:
%\begin{ttfamily}\obeylines
%\{\cs{loadallproblems}\oarg{filename}\marg{filename}\}\%
%\cs{foreachproblem}\oarg{filename}\{\cs{PSNitem}\cs{thisproblem}\cs{endPSNitem}\}
%\end{ttfamily}
%
%Note that in both the above cases, a new data set is created
%with the same name as the file name.
%
% \StopEventually{\clearpage\phantomsection\addcontentsline{toc}{section}{Index}\PrintIndex}
%
%
%
%\section{The Code}
%\iffalse
%    \begin{macrocode}
%<*probsoln.sty>
%    \end{macrocode}
%\fi
%\subsection{Package Definition}
% This package requires \LaTeXe.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
%    \end{macrocode}
% Identify this package and version:
%    \begin{macrocode}
\ProvidesPackage{probsoln}[2017/07/10 v3.05 (NLCT)]
%    \end{macrocode}
% Required packages:
%\changes{3.01}{2011/08/22}{substr package no longer required}
%    \begin{macrocode}
\RequirePackage{ifthen}
\RequirePackage{amsmath}
%    \end{macrocode}
%\changes{3.03}{2011/01/16}{added etoolbox to list of required
%packages}
%    \begin{macrocode}
\RequirePackage{etoolbox}
%    \end{macrocode}
% \subsection{Package Options}
%\begin{macro}{\ifshowanswers}
% Define boolean to determine whether or not to show the
% solutions. This governs whether the contents of \env{onlysolution}
% and \env{onlyproblem} are displayed.
%    \begin{macrocode}
\newif\ifshowanswers
\showanswersfalse
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\showanswers}
% Define synonym for \cs{showanswerstrue}
%    \begin{macrocode}
\let\showanswers\showanswerstrue
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\hideanswers}
% Define synonym for \cs{showanswersfalse}
%    \begin{macrocode}
\let\hideanswers\showanswersfalse
%    \end{macrocode}
%\end{macro}
% The package option \pkgopt{answers} displays the solutions.
%    \begin{macrocode}
\DeclareOption{answers}{\showanswerstrue}
%    \end{macrocode}
% The package option \pkgopt{noanswers} hides the solutions.
%    \begin{macrocode}
\DeclareOption{noanswers}{\showanswersfalse}
%    \end{macrocode}
% Determine whether or not to use default arguments for problems.
%\begin{macro}{\ifusedefaultprobargs}
%    \begin{macrocode}
\newif\ifusedefaultprobargs
%    \end{macrocode}
%\end{macro}
%\begin{option}{usedefaultargs}
%    \begin{macrocode}
\DeclareOption{usedefaultargs}{\usedefaultprobargstrue}
%    \end{macrocode}
%\end{option}
%\begin{option}{usedefaultargs}
%    \begin{macrocode}
\DeclareOption{nousedefaultargs}{\usedefaultprobargsfalse}
\usedefaultprobargsfalse
%    \end{macrocode}
%\end{option}
% 
%\begin{macro}{\prob@showdraftlabel}
%\begin{definition}
%\cs{prob@showdraftlabel}\marg{db name}\marg{label}
%\end{definition}
% Used by \cs{useproblem} to display data base name and problem
% label when in draft mode.
%    \begin{macrocode}
\newcommand*{\prob@showdraftlabel}[2]{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\draftproblemlabel}
%\begin{definition}
%\cs{draftproblemlabel}\marg{db name}\marg{label}
%\end{definition}
% Displays the data base name and label.
%    \begin{macrocode}
\newcommand*{\draftproblemlabel}[2]{[#1,#2]}
%    \end{macrocode}
%\end{macro}
% Draft mode displays the problem label using \cs{draftproblemlabel}
%    \begin{macrocode}
\DeclareOption{draft}{%
\renewcommand*{\prob@showdraftlabel}[2]{\draftproblemlabel{#1}{#2}}}
%    \end{macrocode}
% Final mode:
%    \begin{macrocode}
\DeclareOption{final}{%
\renewcommand*{\prob@showdraftlabel}[2]{}}
%    \end{macrocode}
% Process package options:
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode} 
%\changes{3.02}{2011/12/10}{added xkeyval to required package list}
%    \begin{macrocode}
\RequirePackage{xkeyval}
%    \end{macrocode}
%\begin{macro}{\if@prob@fragile}
%\changes{3.02}{2011/12/10}{new}
%Need a conditional to determine whether \cs{long@collect@body}
%needs to be aware of verbatim contents.
%    \begin{macrocode}
\define@boolkey{probsoln}[@prob@]{fragile}[true]{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\ProbSolnFragileExt}
%\changes{3.02}{2011/12/10}{new}
% The extension used for temporary files dealing with fragile
% contents.
%    \begin{macrocode}
\newcommand*{\ProbSolnFragileExt}{vrb}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\ProbSolnFragileFile}
%\changes{3.02}{2011/12/10}{new}
% The filename used for temporary files dealing with fragile
% contents.
%    \begin{macrocode}
\newcommand*{\ProbSolnFragileFile}{\jobname}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\probsoln@write}
%\changes{3.02}{2011/12/10}{new}
%File handle for temporary files.
%    \begin{macrocode}
\newwrite\probsoln@write
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@startyear}
%\changes{3.03}{2011/01/16}{new}
% The year as at the start of the new academic year. (For example,
% if the academic year starts in September and today is any date
% between 2011-09-01 and 2012-08-30, then the start year is 2011.)
%    \begin{macrocode}
\newcount\probsoln@startyear
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\SetStartYear}
%\changes{3.03}{2011/01/16}{new}
% Provide command to set the starting year manually.
%    \begin{macrocode}
\newcommand*{\SetStartYear}[1]{%
   \probsoln@startyear=#1\relax
   \renewcommand\SetStartMonth[1]{%
      \PackageError{probsoln}{\string\SetStartMonth\space
        can't be used after \string\SetStartYear}{}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\GetStartYear}
% Gets the value of the start year count register:
%    \begin{macrocode}
\newcommand*{\GetStartYear}{\probsoln@startyear}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@startmonth}
%\changes{3.03}{2011/01/16}{new}
% The month starting the academic year. (1=January, 2=February,
% etc).
%    \begin{macrocode}
\newcount\probsoln@startmonth
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\SetStartMonth}
%\changes{3.03}{2011/01/16}{new}
% Define command to set the month starting the academic year. This
% also sets the starting year.
%    \begin{macrocode}
\newcommand*{\SetStartMonth}[1]{%
  \probsoln@startmonth=#1\relax
  \probsoln@startyear=\year\relax
  \ifnum\month<\probsoln@startmonth
    \advance\probsoln@startyear by -1\relax
  \fi
}
%    \end{macrocode}
%\end{macro}
%
% Set the default starting month to 9 (September):
%    \begin{macrocode}
\SetStartMonth{9}
%    \end{macrocode}
%
%\begin{macro}{\probsoln@prev}
%\changes{3.03}{2011/01/16}{new}
% File handle for file containing previous labels.
%    \begin{macrocode}
\newwrite\probsoln@prev
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@used}
%\changes{3.03}{2011/01/16}{new}
% File handle for file containing previous used.
%    \begin{macrocode}
\newwrite\probsoln@used
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@prev@cutoff}
%\changes{3.03}{2011/01/16}{new}
% Cut-off year. Problems excluded if the year they were set is
% greater than the cut-off year.
%    \begin{macrocode}
\newcount\probsoln@prev@cutoff
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@probsoln@usedfilename}
% Stores the file name for the used problems file. (Defaults to
% \cs{jobname}\texttt{.prb})
%    \begin{macrocode}
\newcommand*{\@probsoln@usedfilename}{\jobname.prb}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\SetUsedFileName}
% Set the name of the used problems file.
%    \begin{macrocode}
\newcommand*{\SetUsedFileName}[1]{%
  \renewcommand*{\@probsoln@usedfilename}{#1}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\ClearUsedFile}
%\changes{3.03}{2011/01/16}{new}
% Clear the contents of the used file (\cs{@probsoln@usedfilename})
% and remove corresponding entries from the previous file (as
% specified in \cs{ExcludePreviousFile}).
% Not to be used after \cs{ExcludePreviousFile}.
%    \begin{macrocode}
\newcommand*{\ClearUsedFile}[1]{%
  \probsoln@prev@cutoff=0\relax
  \@probsoln@readprev{#1}%
%    \end{macrocode}
% Only write labels that aren't in the used file.
%    \begin{macrocode}
  \@for\@this@db:=\prob@databases\do{%
    {%
      \edef\@prev@list{\csname probsoln@prev@list@\@this@db\endcsname}%
      \ifdefempty{\@prev@list}%
      {}%
      {%
        \@for\@this@label:=\@prev@list\do{%
          \ifcsundef{@used@problem@\@this@db @\@this@label}%
          {%
            \immediate\write\probsoln@prev{%
              \string\previousproblem{\@this@label}{\@this@db}%
               {\csname @prev@problem@\@this@db @\@this@label\endcsname}}%
          }%
          {}%
        }%
      }%
    }%
  }%
  \immediate\closeout\probsoln@prev
  \immediate\closeout\probsoln@used
  \@disable@exclude@prev
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\ExcludePreviousFile}
%\changes{3.03}{2011/01/16}{new}
% Exclude problems used in the last \meta{n} years.
%    \begin{macrocode}
\newcommand*{\ExcludePreviousFile}[2][3]{%
  \probsoln@prev@cutoff=\probsoln@startyear\relax
  \advance\probsoln@prev@cutoff by -#1\relax
  \@probsoln@readprev{#2}%
  \@write@prev
  \def\ExcludePreviousFile[2][3]{\PackageError{probsoln}{Only one
    instance of \string\ExcludePreviousFile\space allowed}{You've
    already used this command. You are only allowed to use it once}}%
  \def\ClearUsedFile[1]{%
     \PackageError{probsoln}%
       {\string\ClearUsedFile\space may not be used after
          \string\ExcludePreviousFile}{}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@probsoln@readprev}
% Read contents of previous and used files and open for writing.
%    \begin{macrocode}
\newcommand*{\@probsoln@readprev}[1]{%
  \@enable@exclude@prev
  \InputIfFileExists{#1}%
     {\PackageInfo{probsoln}%
        {Excluded problem file `#1' found}}%
     {\PackageInfo{probsoln}%
        {Excluded problem file `#1' not found. A new one will be
            created}}%
  \InputIfFileExists{\@probsoln@usedfilename}%
     {\PackageInfo{probsoln}%
        {Current problems file `\@probsoln@usedfilename' found}}%
     {\PackageInfo{probsoln}%
        {No current problem file `\@probsoln@usedfilename' found. A new one will be created}}%
  \immediate\openout\probsoln@prev=#1
  \immediate\openout\probsoln@used=\@probsoln@usedfilename
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@prev@list}
%\changes{3.03}{2011/01/16}{new}
% Maintain a list of all previous problem labels:
%    \begin{macrocode}
\newcommand*{\probsoln@prev@list@default}{}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@write@prev}
%\changes{3.03}{2011/01/16}{new}
% Write all previous labels to file.
%    \begin{macrocode}
\newcommand*{\@write@prev}{%
  \@for\@this@db:=\prob@databases\do{%
    {%
      \edef\@prev@list{\csname probsoln@prev@list@\@this@db\endcsname}%
      \ifdefempty{\@prev@list}%
      {}%
      {%
        \@for\@this@label:=\@prev@list\do{%
          \immediate\write\probsoln@prev{%
            \string\previousproblem{\@this@label}{\@this@db}%
             {\csname @prev@problem@\@this@db @\@this@label\endcsname}}%
        }%
      }%
    }%
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@enable@exclude@prev}
%\changes{3.03}{2011/01/16}{new}
% Enable commands for excluding previously selected problems.
%    \begin{macrocode}
\newcommand*{\@enable@exclude@prev}{%
%    \end{macrocode}
% Redefine macro that adds used problem to used problem file and
% previous problem file.
%    \begin{macrocode}
  \renewcommand*{\@add@used@problem}[2]{%
    \immediate\write\probsoln@used{\string\usedproblem{##1}{##2}{\number\probsoln@startyear}}%
    \ifcsundef{@prev@problem@##2@##1}%
    {%
      \immediate\write\probsoln@prev{%
          \string\previousproblem{##1}{##2}{\number\probsoln@startyear}}%
      \expandafter\xdef\csname @prev@problem@##2@##1\endcsname{%
          \number\probsoln@startyear}%
    }%
    {%
      \expandafter\ifnum\csname @prev@problem@##2@##1\endcsname
         <\probsoln@startyear
      \immediate\write\probsoln@prev{%
          \string\previousproblem{##1}{##2}{\number\probsoln@startyear}}%
      \expandafter\xdef\csname @prev@problem@##2@##1\endcsname{%
          \number\probsoln@startyear}%
      \fi
    }%
  }%
%    \end{macrocode}
% Redefine macro that fetches the exclusion list. (First argument is
% the macro in which to store the list, the second argument is the
% database.)
%    \begin{macrocode}
  \renewcommand*{\@fetch@excluded@list}[2]{%
    \def##1{}%
    \ifcsdef{probsoln@prev@list@##2}%
    {%
      \edef\@prev@list{\csname probsoln@prev@list@##2\endcsname}%
      \@for\@this@label:=\@prev@list\do{%
%    \end{macrocode}
% If it isn't one of the used problems, it can be added to the
% exclusion list:
%    \begin{macrocode}
       \ifcsundef{@used@problem@##2@\@this@label}%
       {%
%    \end{macrocode}
% It isn't, so label can be added to the exclusion list:
%    \begin{macrocode}
          \ifcsempty{##1}%
          {\edef##1{\@this@label}}%
          {\edef##1{##1,\@this@label}}%
       }%
       {}%
      }%
    }%
    {}%
  }%
%    \end{macrocode}
% Add new previous list for given database:
%    \begin{macrocode}
  \renewcommand*{\@add@newprevlist}[1]{%
     \expandafter\gdef\csname probsoln@prev@list@##1\endcsname{}%
  }%
%    \end{macrocode}
% Redefine macro that closes the exclusion-related files.
%    \begin{macrocode}
  \renewcommand*{\close@probsoln@prev}{%
    \closeout\probsoln@prev
    \closeout\probsoln@used
  }%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@disable@exclude@prev}
%\changes{3.03}{2011/01/16}{new}
% Disable commands for excluding previously selected problems.
%    \begin{macrocode}
\newcommand*{\@disable@exclude@prev}{%
  \renewcommand*{\@add@used@problem}[2]{}%
  \renewcommand*{\@fetch@excluded@list}[2]{\def##1{}}%
  \renewcommand*{\@add@newprevlist}[1]{}%
  \renewcommand*{\close@probsoln@prev}{}%
}
%    \end{macrocode}
%\end{macro}
%
% By default, the commands for excluding previously selected
% problems are disabled.
%\begin{macro}{\@add@used@problem}
%\changes{3.03}{2011/01/16}{new}
% Adds problem to used problems list. (First argument is the label,
% the second argument is the database name.)
%    \begin{macrocode}
\newcommand*{\@add@used@problem}[2]{}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@fetch@excluded@list}
%\changes{3.03}{2011/01/16}{new}
% Fetches the excluded list. (First argument macro in which to store
% the list. The second argument is the database name.)
%    \begin{macrocode}
\newcommand*{\@fetch@excluded@list}[2]{%
  \def#1{}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@add@newprevlist}
%\changes{3.03}{2011/01/16}{new}
% Adds a new previous list for the given database:
%    \begin{macrocode}
\newcommand*{\@add@newprevlist}[1]{}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\close@probsoln@prev}
%\changes{3.03}{2011/01/16}{new}
% Close file used for previous labels
%    \begin{macrocode}
\newcommand*{\close@probsoln@prev}{}
%    \end{macrocode}
%\end{macro}
%
% At the end of the document, close file if required:
%    \begin{macrocode}
\AtEndDocument{\close@probsoln@prev}
%    \end{macrocode}
%
%\begin{macro}{\previousproblem}
%\changes{3.03}{2011/01/16}{new}
% Identifies problem that has been selected and the year it was
% selected. (First argument label, second argument database name,
% third argument year.)
%    \begin{macrocode}
\newcommand*{\previousproblem}[3]{%
  \ifnum#3>\probsoln@prev@cutoff
%    \end{macrocode}
% If data set hasn't been defined, define it:
%    \begin{macrocode}
    \ifcsundef{prob@db@#2}{\prob@newdb{#2}}{}%
%    \end{macrocode}
% Define command that stores the year the problem was used:
%    \begin{macrocode}
    \expandafter\gdef\csname @prev@problem@#2@#1\endcsname{#3}%
%    \end{macrocode}
% Add label to the previous list for this data set:
%    \begin{macrocode}
    \edef\@prev@list{\csname probsoln@prev@list@#2\endcsname}%
    \ifdefempty{\@prev@list}%
    {%
       \expandafter\xdef\csname probsoln@prev@list@#2\endcsname{#1}%
    }%
    {%
       \expandafter\xdef\csname probsoln@prev@list@#2\endcsname{%
         \@prev@list,#1}%
    }%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\usedproblem}
%\changes{3.03}{2011/01/16}{new}
% Don't want to exclude problems that were selected in the previous
% run of this document for the current year, so they need to be
% identified in the aux file.
%    \begin{macrocode}
\newcommand*{\usedproblem}[3]{%
  \ifnum#3=\probsoln@startyear
    \expandafter\def\csname @used@problem@#2@#1\endcsname{#3}%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Databases}
% All the problems are stored in data bases. Each data base 
% \meta{name} is represented as a macro \cs{prob@db@}\meta{name}
% which stores a comma-separated list of labels for each problem
% associated with that data base. Each problem \meta{label} is 
% stored in the macro 
% \cs{prob@data@}\meta{name}"@"\meta{label}. Problems
% loaded from an external file using \cs{loadproblems} are added
% to the specified data base. Any problems that are defined in the
% document or are \cs{input}ed from another file (without the
% use of \cs{loadproblems}) are added to the default data base.
%
% Define the default data base:
%    \begin{macrocode}
\newcommand*{\prob@db@default}{}
%    \end{macrocode}
%
%\begin{macro}{\prob@databases}
% Store a list of all the defined data bases.
%    \begin{macrocode}
\newcommand*{\prob@databases}{default}
%    \end{macrocode}
%\end{macro}
% Each defined database has a list of undisplayed solutions.
%    \begin{macrocode}
\newcommand*{\prob@db@default@solutions}{}
%    \end{macrocode}
%
%\begin{macro}{\prob@newdb}
%\begin{definition}
%\cs{prob@newdb}\marg{name}
%\end{definition}
% Creates a new (empty) data base.
%    \begin{macrocode}
\newcommand*{\prob@newdb}[1]{%
  \ifcsundef{prob@db@#1}%
  {%
    \expandafter\gdef\csname prob@db@#1\endcsname{}%
    \xdef\prob@databases{\prob@databases,#1}%
    \expandafter\gdef\csname prob@db@#1@solutions\endcsname{}%
    \@add@newprevlist{#1}%
  }%
  {%
    \PackageError{probsoln}{Data set `#1' is already defined}%
      {Data set names must be unique}%
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\prob@currentdb}
% Keep a track of the current data base
%    \begin{macrocode}
\newcommand*{\prob@currentdb}{default}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\moveproblem}
%\begin{definition}
%\cs{moveproblem}\marg{label}\marg{source}\marg{target}
%\end{definition}
%    \begin{macrocode}
\newcommand{\moveproblem}[3]{%
\@moveproblem{#1}{#2}{#3}%
\expandafter\let\expandafter\@tmpdblist
 \csname prob@db@#2\endcsname
\expandafter\gdef\csname prob@db@#2\endcsname{}%
\@for\@tmplab:=\@tmpdblist\do{%
 \ifthenelse{\equal{\@tmplab}{#1}}{}{%
   \expandafter\ifx\csname prob@db@#2\endcsname\@empty
     \expandafter\xdef\csname prob@db@#2\endcsname{\@tmplab}%
   \else
     \expandafter\xdef\csname prob@db@#2\endcsname{%
       \csname prob@db@#2\endcsname,%
       \@tmplab}%
   \fi
 }%
}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@moveproblem}
%\begin{definition}
%\cs{@moveproblem}\marg{label}\marg{source}\marg{target}
%\end{definition}
% Moves problem identified by \meta{label} from the data base
% \meta{source} to the data base \meta{target}. (Doesn't remove
% label from \meta{source} --- that needs to be done separately.)
%    \begin{macrocode}
\newcommand*{\@moveproblem}[3]{%
%    \end{macrocode}
% Add label to target data base
%    \begin{macrocode}
  \ifcsempty{prob@db@#3}%
  {%
    \expandafter\xdef\csname prob@db@#3\endcsname{#1}%
  }%
  {%
    \expandafter\xdef\csname prob@db@#3\endcsname{%
      \csname prob@db@#3\endcsname,#1}%
  }%
%    \end{macrocode}
% Redefine \cs{prob@data@}\meta{source}"@"\meta{label} as
% \cs{prob@data@}\meta{target}"@"\meta{label}.
%    \begin{macrocode}
  \edef\do@movedata{%
    \noexpand\global\noexpand\let\expandafter\noexpand
      \csname prob@data@#3@#1\endcsname=%
      \expandafter\noexpand\csname prob@data@#2@#1\endcsname
      \noexpand\global\noexpand\let
      \expandafter\noexpand
      \csname prob@data@#2@#1\endcsname=\noexpand\undefined
    }%
  \do@movedata
%    \end{macrocode}
% Redefine \cs{prob@argN@}\meta{source}"@"\meta{label} as
% \cs{prob@argN@}\meta{target}"@"\meta{label}.
%    \begin{macrocode}
  \edef\do@moveargN{%
    \noexpand\global\noexpand\let\expandafter\noexpand
      \csname prob@argN@#3@#1\endcsname=%
      \expandafter\noexpand\csname prob@argN@#2@#1\endcsname
      \noexpand\global\noexpand\let
      \expandafter\noexpand
      \csname prob@argN@#2@#1\endcsname=\noexpand\undefined
    }%
  \do@moveargN
%    \end{macrocode}
% Redefine \cs{prob@args@}\meta{source}"@"\meta{label} as
% \cs{prob@args@}\meta{target}"@"\meta{label}, if defined.
%    \begin{macrocode}
  \@ifundefined{prob@args@#2@#1}{}{%
    \edef\do@moveargs{%
      \noexpand\global\noexpand\let\expandafter\noexpand
        \csname prob@args@#3@#1\endcsname=%
        \expandafter\noexpand\csname prob@args@#2@#1\endcsname
        \noexpand\global\noexpand\let
        \expandafter\noexpand
        \csname prob@args@#2@#1\endcsname=\noexpand\undefined
      }%
    \do@moveargs
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Defining New Problems}
%\begin{macro}{\prob@newproblem}
%\changes{3.01}{2011/08/22}{added extra argument to supply defaults}
%\begin{definition}
%\cs{prob@newproblem}\marg{n}\marg{db
%name}\marg{label}\marg{definition}\marg{default args}
%\end{definition}
% Define a new problem identified by \meta{label} for data base
% \meta{db name} with the given definition. The problem has \meta{n}
% arguments (each represented by "#1" etc). The arguments may 
% either be appended to \cs{useproblem}\marg{label} or may
% be stored in \cs{prob@args@}\meta{db name}"@"\meta{label} if
% the problem is to be accessed via \cs{foreachproblem}. The
% number of arguments is stored in
% \cs{prob@argN@}\meta{db name}"@"\meta{label}
%    \begin{macrocode}
\newcommand{\prob@newproblem}[5]{%
%    \end{macrocode}
% If the given data base is not defined, create it:
%    \begin{macrocode}
  \@ifundefined{prob@db@#2}{\prob@newdb{#2}}{}%
%    \end{macrocode}
% Check whether this entry has already been defined:
%    \begin{macrocode}
  \@ifundefined{prob@data@#2@#3}%
  {%
%    \end{macrocode}
% Define the new problem and make it global:
%    \begin{macrocode}
    \let\@tmp=\undefined
    \newcommand\@tmp[#1]{#4}%
    \expandafter\global\expandafter\let
       \csname prob@data@#2@#3\endcsname=\@tmp
    \expandafter\xdef\csname prob@argN@#2@#3\endcsname{\number#1}%
    \let\@tmp=\undefined
%    \end{macrocode}
% Add the label to the data base:
%    \begin{macrocode}
    \expandafter\ifx\csname prob@db@#2\endcsname\@empty
      \expandafter\xdef\csname prob@db@#2\endcsname{#3}%
    \else
      \expandafter\xdef\csname prob@db@#2\endcsname{%
        \csname prob@db@#2\endcsname,#3}%
    \fi
%    \end{macrocode}
% If default arguments supplied, set them
%    \begin{macrocode}
    \ifx\@empty#5\@empty
    \else
       \edef\thisprob@currentdb{#2}%
       \edef\thisprob@currentlabel{#3}%
       \expandafter\@setdefaultprobargs\expandafter{#5}%
    \fi
  }%
  {%
    \PackageError{probsoln}{Problem `#3' is already defined for
    data base `#2'}{Problem labels must be unique for each data base}%
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@prob@gobblethree}
% Ignore all three arguments
%    \begin{macrocode}
\newcommand{\@prob@gobblethree}[3]{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@prob@getargs}
%\begin{definition}
%\cs{@prob@getargs}\meta{n}\meta{db name}\meta{label}
%\end{definition}
% Prompt user for \meta{n} arguments for problem \meta{label}
% in data base \meta{db name}.
%    \begin{macrocode}
\newcommand{\@prob@getargs}[3]{%
\message{Problem `#3' (in data base `#2') requires #1 argument(s).^^J
Please specify (e.g. \csname @prob@getargs@eg@\romannumeral#1\endcsname):}%
\read-1to\@tmp
\expandafter\global\expandafter\let
  \csname prob@args@#2@#3\endcsname=\@tmp
}
\def\@prob@getargs@eg@i{{12}}
\def\@prob@getargs@eg@ii{{5}{3}}
\def\@prob@getargs@eg@iii{{4}{5}{2}}
\def\@prob@getargs@eg@iv{{5}{3}{10}{8}}
\def\@prob@getargs@eg@v{{5}{3}{10}{8}{4}}
\def\@prob@getargs@eg@vi{{5}{3}{10}{8}{4}{24}}
\def\@prob@getargs@eg@vii{{5}{3}{10}{8}{4}{24}{32}}
\def\@prob@getargs@eg@viii{{5}{3}{10}{8}{4}{24}{32}{9}}
\def\@prob@getargs@eg@ix{{5}{3}{10}{8}{4}{24}{32}{9}{2}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@prob@do@getargs}
%    \begin{macrocode}
\let\@prob@do@getargs\@prob@gobblethree
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\setprobargs}
%\begin{definition}
%\cs{setprobargs}\oarg{db name}\marg{label}\marg{args}
%\end{definition}
% Sets the arguments for the given problem. Each of the arguments
% within \meta{args} should be grouped, e.g.
% "\setprobargs{label}{{5}{3}}". Inner braces are still required if
% there is only one argument, e.g. "\setprobargs{label}{{25}}"
%    \begin{macrocode}
\newcommand*{\setprobargs}[3][default]{%
  \expandafter\gdef\csname prob@args@#1@#2\endcsname{#3}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@setdefaultprobargs}
% Like \cs{setprobargs} but only sets the arguments if 
% \ics{usedefaultprobargstrue}. The database is given
% by \cs{thisprob@currentdb} (the current database) and
% the problem label is given by \cs{thisprob@currentlabel}.
% This command should only be used within \env{defproblem}.
%    \begin{macrocode}
\newcommand*{\@setdefaultprobargs}[1]{%
  \ifusedefaultprobargs
    \setprobargs[\thisprob@currentdb]{\thisprob@currentlabel}{#1}%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\long@collect@body}
% Need long versions of \cs{collect@body}. These macros are 
% adapted from the macros defined by \sty{amsmath}.
%    \begin{macrocode}
\long\def\long@collect@body#1{%
  \@envbody{\@xp#1\@xp{\the\@envbody}}%
  \edef\process@envbody{\the\@envbody\@nx\end{\@currenvir}}%
  \@envbody\@emptytoks \def\begin@stack{b}%
  \begingroup
  \if@prob@fragile
    \obeylines\obeyspaces
    \@makeother\#
    \@makeother\%
  \fi
  \@xp\let\csname\@currenvir\endcsname\long@collect@@body
  \edef\process@envbody{\@xp\@nx\csname\@currenvir\endcsname}%
  \process@envbody
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\long@addto@envbody}
%\changes{3.01}{2011/08/22}{modified to allow for floats in the body of the
%environment}
%    \begin{macrocode}
\long\def\long@addto@envbody#1{%
  \toks@{#1}%
  \edef\@psn@tmp{\the\@envbody\the\toks@}%
  \global\@envbody\@xp{\@psn@tmp}%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\long@collect@@body}
%    \begin{macrocode}
\long\def\long@collect@@body#1\end#2{%
%    \end{macrocode}
%\changes{3.01}{2011/08/22}{changed \cs{edef} to \cs{protected@edef}}
%    \begin{macrocode}
  \protected@edef\begin@stack{%
    \long@push@begins#1\begin\end \@xp\@gobble\begin@stack
  }%
  \ifx\@empty\begin@stack
    \endgroup
    \@checkend{#2}%
    \long@addto@envbody{#1}%
  \else
    \long@addto@envbody{#1\end{#2}}%
  \fi
  \process@envbody
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\long@push@begins}
%    \begin{macrocode}
\long\def\long@push@begins#1\begin#2{%
  \ifx\end#2\else b\@xp\long@push@begins\fi
}
%    \end{macrocode}
%\end{macro}
%
% Fragile contents are sanitized, written to file and read back in.
% However, we don't want the new line character sanitized. Also, 
% the \env{verbatim} environment doesn't like a space after
% \cs{begin} or \cs{end}, so these need to be replaced.
%\begin{macro}{\@char@M}
%\changes{3.02}{2011/12/10}{new}
% First define a macro that contains the
% newline marker:
%    \begin{macrocode}
{\obeylines
 \gdef\@char@M{^^M}%
}
\@onelevel@sanitize\@char@M
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@beg@env@string}
%\changes{3.02}{2011/12/10}{new}
% Define a macro that contains the begin environment marker:
%    \begin{macrocode}
\def\@beg@env@string{\begin}
\@onelevel@sanitize\@beg@env@string
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@end@env@string}
%\changes{3.02}{2011/12/10}{new}
% Define a macro that contains the end environment marker:
%    \begin{macrocode}
\def\@end@env@string{\end}
\@onelevel@sanitize\@end@env@string
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\def@replace@markers}
%\changes{3.02}{2011/12/10}{new}
% Things start to get a bit complicated here. The substitution
% macros need the markers as part of their definition. The easiest
% way to do this is to define a command (using \cs{edef}) that defines the
% substitution macros. The markers and replacements are the only
% things to be expanded.
%    \begin{macrocode}
\edef\def@replace@markers{%
%    \end{macrocode}
% First define the command that replaces the newline marker:
%    \begin{macrocode}
 \noexpand\def\noexpand\do@replace@charM##1\@char@M##2\noexpand\end@replace@marker{%
    \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
    \noexpand\ifx\noexpand\relax##2\noexpand\relax
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
      \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
    \noexpand\else
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1^^J}%
      \noexpand\let\noexpand\do@replacenext\noexpand\do@replace@charM
    \noexpand\fi
    \noexpand\do@replacenext##2\noexpand\end@replace@marker
  }%
%    \end{macrocode}
% Next define the command that replaces the begin environment marker:
%    \begin{macrocode}
 \noexpand\def\noexpand\doreplace@begverb##1\@beg@env@string##2\noexpand\end@replace@marker{%
    \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
    \noexpand\ifx\noexpand\relax##2\noexpand\relax
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
      \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
    \noexpand\else
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@
        ##1\expandafter\@gobble\string\\begin}%
      \noexpand\let\noexpand\do@replacenext\noexpand\doreplace@begverb
    \noexpand\fi
    \noexpand\do@replacenext##2\noexpand\end@replace@marker
  }%
%    \end{macrocode}
% Next define the command that replaces the end environment marker:
%    \begin{macrocode}
 \noexpand\def\noexpand\doreplace@endverb##1\@end@env@string##2\noexpand\end@replace@marker{%
    \noexpand\expandafter\noexpand\toks@\noexpand\expandafter{\noexpand\replace@text}%
    \noexpand\ifx\noexpand\relax##2\noexpand\relax
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@##1}%
      \noexpand\let\noexpand\do@replacenext\noexpand\replace@mark@noop
    \noexpand\else
      \noexpand\edef\noexpand\replace@text{\noexpand\the\noexpand\toks@
        ##1\expandafter\@gobble\string\\end}%
      \noexpand\let\noexpand\do@replacenext\noexpand\doreplace@endverb
    \noexpand\fi
    \noexpand\do@replacenext##2\noexpand\end@replace@marker
  }%
%    \end{macrocode}
% Finally, define a higher-level command that calls all three of the above:
%    \begin{macrocode}
  \noexpand\def\noexpand\replace@markers##1{%
    \noexpand\def\noexpand\replace@text{}%
    \noexpand\expandafter\noexpand\do@replace@charM##1\@char@M\relax\noexpand\end@replace@marker
    \noexpand\let##1\noexpand\replace@text
    \noexpand\def\noexpand\replace@text{}%
    \noexpand\expandafter\noexpand\doreplace@begverb##1\@beg@env@string\relax\noexpand\end@replace@marker
    \noexpand\let##1\noexpand\replace@text
    \noexpand\def\noexpand\replace@text{}%
    \noexpand\expandafter\noexpand\doreplace@endverb##1\@end@env@string\relax\noexpand\end@replace@marker
    \noexpand\let##1\noexpand\replace@text
  }
}
%    \end{macrocode}
% Define the substitution loop terminator:
%    \begin{macrocode}
\def\replace@mark@noop#1\end@replace@marker{}
%    \end{macrocode}
% Now define all the marker substitution macros:
%    \begin{macrocode}
\def@replace@markers
%    \end{macrocode}
%\end{macro}
%
%\begin{environment}{defproblem}
%\changes{3.01}{2011/08/22}{added second optional argument}
%\changes{3.02}{2011/12/11}{added key-val optional argument}
%\begin{definition}
%"\begin{defproblem}"\oarg{n}\oarg{default args}\marg{label}\oarg{key-val options}
%\end{definition}
% Define a new problem identified by \meta{label} with
% \meta{n} arguments to add to the current data base. Note that
% since the contents of the environment are passed to a command,
% the contents can't contain any verbatim text. 
%    \begin{macrocode}
\newenvironment{defproblem}[1][0]{%
  \edef\@prob@currentargN{\number0#1}%
  \@defproblem@beginenv
}{}
%   \end{macrocode}
% Gather second optional argument and mandatory argument:
%   \begin{macrocode}
\newcommand*{\@defproblem@beginenv}[2][]{%
  \def\@prob@currentdefaultargs{#1}%
  \def\@prob@currentlabel{#2}%
  \@defproblem@beginenv@
}
%    \end{macrocode}
% Get final optional argument and process
%    \begin{macrocode}
\newcommand*{\@defproblem@beginenv@}[1][]{%
  \setkeys{probsoln}{#1}%
  \long@collect@body\prob@do@defproblem
}
%    \end{macrocode}
%\end{environment}
%\begin{macro}{\prob@do@newproblem}
%\begin{definition}
%\cs{prob@do@newproblem}\marg{definition}
%\end{definition}
% Defines a new problem given by \meta{definition}, where
% the number of arguments is given by \cs{@prob@currentargN},
% the label is given by \cs{@prob@currentlabel} and the data base
% is given by \cs{prob@currentdb}.
%    \begin{macrocode}
\newcommand{\prob@do@newproblem}[1]{%
  \if@prob@fragile
    \probsoln@process@fragile{#1}%
    \protected@edef\do@def@new@problem{%
      \noexpand\prob@newproblem
        {\@prob@currentargN}%
        {\prob@currentdb}%
        {\@prob@currentlabel}%
        {%
          \noexpand\probsoln@write@tmp{\@prob@tmp@problem}%
          \noexpand\probsoln@read@tmp
        }%
        {\@prob@currentdefaultargs}%
    }%
  \else
    \toks@{#1}%
    \protected@edef\do@def@new@problem{%
      \noexpand\prob@newproblem
      {\@prob@currentargN}%
      {\prob@currentdb}%
      {\@prob@currentlabel}%
      {\the\toks@}%
      {\@prob@currentdefaultargs}%
    }%
  \fi
  \do@def@new@problem
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\prob@do@defproblem}
% The default action of the \env{defproblem} environment is to
% define a new problem.
%    \begin{macrocode}
\let\prob@do@defproblem=\prob@do@newproblem
%    \end{macrocode}
%\end{macro}
%
%\begin{environment}{onlysolution}
% Define an environment that only displays its contents if the
% solutions should be displayed.
%    \begin{macrocode}
\newenvironment{onlysolution}[1][]{%
   \setkeys{probsoln}{#1}%
   \long@collect@body\do@onlysolution
}{}
%    \end{macrocode}
%\end{environment}
%\begin{macro}{\do@onlysolution}
%\changes{3.02}{2011/12/10}{added \cs{probsoln@do@body}}
%    \begin{macrocode}
\newcommand{\do@onlysolution}[1]{%
  \ifshowanswers
    \probsoln@do@body{#1}%
  \fi
%    \end{macrocode}
% Add to the list of solutions
%    \begin{macrocode}
  \@ifundefined{@prob@currentlabel}
  {}%
  {%
    \expandafter
     \psn@add@unique@label
       \csname prob@db@\prob@currentdb @solutions\endcsname{%
         \@prob@currentlabel
       }%
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\psn@add@unique@label}
%\changes{3.01}{2011/08/22}{new}
%\begin{definition}
%\cs{psn@add@unique@label}\marg{list cs}\marg{label}
%\end{definition}
% Globally adds label to list if not already in list.
%   \begin{macrocode}
\newcommand*{\psn@add@unique@label}[2]{%
  \ifx#1\@empty
    \xdef#1{#2}%
  \else
    \edef\@tmp@label{#2}%
    \expandafter\DTLifinlist\expandafter{\@tmp@label}{#1}%
    {}% ignore
    {\xdef#1{#1,\@tmp@label}}%
  \fi
}
%   \end{macrocode}
%\end{macro}
%
%\begin{macro}{\DTLifinlist}
% This is defined in \sty{datatool}, but there's no sense loading
% the entire package just for this, so define if not already
% defined.
%\changes{3.01}{2011/08/22}{new}
%    \begin{macrocode}
\providecommand{\DTLifinlist}[4]{%
  \def\@dtl@doifinlist##1,#1,##2\end@dtl@doifinlist{%
     \def\@before{##1}%
     \def\@after{##2}%
  }%
  \expandafter\@dtl@doifinlist\expandafter,#2,#1,\@nil
    \end@dtl@doifinlist
  \ifx\@after\@nnil
% not found
    #4%
  \else
% found
    #3%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\begin{environment}{onlyproblem}
% Define an environment that only displays its contents if the
% solutions should not be displayed.
%\changes{3.02}{2011/12/10}{added optional argument.}
%    \begin{macrocode}
\newenvironment{onlyproblem}[1][]{%
   \setkeys{probsoln}{#1}%
   \long@collect@body\do@onlyproblem
}{}
%    \end{macrocode}
%\end{environment}
%\begin{macro}{\do@onlyproblem}
%\changes{3.02}{2011/12/10}{added \cs{probsoln@do@body}}
%    \begin{macrocode}
\newcommand{\do@onlyproblem}[1]{%
  \ifshowanswers
  \else
    \probsoln@do@body{#1}%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@do@body}
% Either does argument or sanitizes, writes and reads.
%\changes{3.02}{2011/12/10}{new}
%    \begin{macrocode}
\newcommand{\probsoln@do@body}[1]{%
  \if@prob@fragile
    \probsoln@process@fragile{#1}%
    \probsoln@write@tmp{\@prob@tmp@problem}%
    \probsoln@read@tmp
  \else
    #1%
  \fi
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@process@fragile}
% Sanitizes and replaces markers. Result stored in
% \cs{@prob@tmp@problem}
%\changes{3.02}{2011/12/10}{new}
%    \begin{macrocode}
\newcommand{\probsoln@process@fragile}[1]{%
  \def\@prob@tmp@problem{#1}%
  \@onelevel@sanitize\@prob@tmp@problem
  \replace@markers\@prob@tmp@problem
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@write@tmp}
%\changes{3.02}{2011/12/10}{new}
% Writes argument to temporary file (including opening and closing
% file.)
%    \begin{macrocode}
\newcommand{\probsoln@write@tmp}[1]{%
  \immediate\openout\probsoln@write=\ProbSolnFragileFile.\ProbSolnFragileExt
  \immediate\write\probsoln@write{#1}%
  \immediate\closeout\probsoln@write
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\probsoln@read@tmp}
%\changes{3.02}{2011/12/10}{new}
% Inputs temporary file.
%    \begin{macrocode}
\newcommand{\probsoln@read@tmp}{%
  \input{\ProbSolnFragileFile.\ProbSolnFragileExt}%
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Using Problems}
%\begin{macro}{\useproblem}
%\begin{definition}
%\cs{useproblem}\oarg{db name}\marg{label}\marg{arg1}\ldots
%\marg{argN}
%\end{definition}
% Use problem identified by \meta{label} in data base
% \meta{db name} where \meta{arg1}\ldots\meta{argN} are the
% arguments to pass to the problem, if the problem was defined
% to take arguments.
%\changes{3.01}{2011/08/22}{changed \cs{@next} to \cs{@useprob@next}}
%    \begin{macrocode}
\newcommand{\useproblem}[2][default]{%
  \def\@prob@currentlabel{#2}%
  \def\prob@currentdb{#1}%
  \prob@showdraftlabel{#1}{#2}%
  \@add@used@problem{#2}{#1}%
  \let\@useprob@next=\relax
  \@ifundefined{prob@data@#1@#2}%
  {%
    \PackageError{probsoln}%
      {Problem `#2' is not defined in data set `#1'}{}%
  }%
  {%
    \def\@useprob@next{\csname prob@data@#1@#2\endcsname}%
  }%
  \@useprob@next
}
%    \end{macrocode}
%\end{macro}
%\subsection{Loading Problems From Another File}
%\begin{macro}{\loadallproblems}
%\begin{definition}
%\cs{loadallproblems}\oarg{db name}\marg{filename}
%\end{definition}
% Loads all the problems defined in \meta{filename} and adds
% them to data base \meta{db name}. (\cs{par} is temporarily 
% disabled to allow for blank lines between problems.)
%    \begin{macrocode}
\newcommand*{\loadallproblems}[2][default]{%
\bgroup
 \let\par\relax
 \edef\prob@currentdb{#1}%
 \input{#2}%
\egroup
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\prob@do@selectedproblem}
% Only define problem if the label is listed in
% \cs{prob@selectedlabels}. (The current label is
% given by \cs{@prob@currentlabel}.)
%\changes{3.01}{2011/08/22}{replaced \cs{IFSubStringInString} with
%\cs{DTLifinlist}}
%    \begin{macrocode}
\newcommand{\prob@do@selectedproblem}[1]{%
  \expandafter\DTLifinlist\expandafter{\@prob@currentlabel}{\prob@selectedlabels}%
  {%
    \prob@do@newproblem{#1}%
  }%
  {}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\loadselectedproblems}
%\begin{definition}
%\cs{loadselectedproblems}\oarg{db name}\marg{list}\marg{filename}
%\end{definition}
% Loads only those problems whose labels are listed in
% \meta{list}.
%    \begin{macrocode}
\newcommand{\loadselectedproblems}[3][default]{%
\bgroup
 \let\par\relax
 \edef\prob@currentdb{#1}%
 \edef\prob@selectedlabels{#2}%
 \let\prob@do@defproblem=\prob@do@selectedproblem
 \input{#3}%
\egroup
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\prob@do@exceptedproblem}
% Only define problem if the label isn't listed in
% \cs{prob@selectedlabels}. (The current label is
% given by \cs{@prob@currentlabel}.)
%    \begin{macrocode}
\newcommand{\prob@do@exceptedproblem}[1]{%
  \expandafter\DTLifinlist\expandafter{\@prob@currentlabel}{\prob@selectedlabels}%
  {}%
  {%
    \prob@do@newproblem{#1}%
  }%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\loadexceptproblems}
%\begin{definition}
%\cs{loadexceptproblems}\oarg{db name}\marg{list}\marg{filename}
%\end{definition}
% Loads only those problems whose labels are not listed in
% \meta{list}.
%\changes{3.01}{2011/08/22}{new}
%    \begin{macrocode}
\newcommand{\loadexceptproblems}[3][default]{%
\bgroup
 \let\par\relax
 \edef\prob@currentdb{#1}%
 \edef\prob@selectedlabels{#2}%
 \let\prob@do@defproblem=\prob@do@exceptedproblem
 \input{#3}%
\egroup
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\prob@add@currentlabel}
% Adds the current label to \cs{prob@selectedlabels} (ignores
% argument.)
%    \begin{macrocode}
\newcommand{\prob@add@currentlabel}[1]{%
\ifx\prob@selectedlabels\@empty
 \xdef\prob@selectedlabels{\@prob@currentlabel}%
\else
 \xdef\prob@selectedlabels{\prob@selectedlabels,\@prob@currentlabel}%
\fi
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\iffirstpass}
% Determines if this is the first pass of the filename when loading
% a data base.
%    \begin{macrocode}
\newif\iffirstpass
\firstpasstrue
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\loadrandomproblems}
%\begin{definition}
%\cs{loadrandomproblems}\oarg{db name}\marg{n}\marg{filename}
%\end{definition}
% Loads \meta{n} randomly selected problems from \meta{filename}.
%\changes{3.03}{2011/01/16}{fetch exclusion list}
%    \begin{macrocode}
\newcommand{\loadrandomproblems}[3][default]{%
  \@fetch@excluded@list{\@excl@list}{#1}%
  \@loadrandomproblems{#1}{#2}{#3}{\@excl@list}%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\loadrandomexcept}
%\begin{definition}
%\cs{loadrandomexcept}\oarg{db
%name}\marg{n}\marg{filename}\marg{exception list}
%\end{definition}
% Loads \meta{n} randomly selected problems from \meta{filename},
% excluding labels listed in \meta{exception list}.
%    \begin{macrocode}
\newcommand{\loadrandomexcept}[4][default]{%
  \@loadrandomproblems{#1}{#2}{#3}{#4}%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@loadrandomproblems}
% Internal workings of \cs{loadrandomproblems} and
% \cs{loadrandomexcept}.
% Needs to input \meta{filename} twice: the first time just gathers
% all the labels, the second time only loads the selected problems.
%    \begin{macrocode}
\newcommand{\@loadrandomproblems}[4]{%
\bgroup
 \let\par\relax
 \def\prob@db@reserved{}%
 \def\prob@currentdb{reserved}%
 \edef\prob@selectedlabels{}%
%    \end{macrocode}
% Collect labels:
%    \begin{macrocode}
 \let\prob@do@defproblem=\prob@add@currentlabel
 \firstpasstrue
%    \end{macrocode}
%\changes{3.05}{2017/07/10}{allow comma-separated list of file names}
%Allow a comma-separated list of file names.
%    \begin{macrocode}
 \@for\@thisfile:=#3\do{\input{\@thisfile}}%
%    \end{macrocode}
% Shuffle labels.
%    \begin{macrocode}
 \@probselN=0\relax
 \@for\@thislabel:=\prob@selectedlabels\do{%
%    \end{macrocode}
%\changes{3.03}{2011/01/16}{ensure arguments expanded in
%\cs{DTLifinlist}}
%    \begin{macrocode}
   \edef\@if@in@list{\noexpand\DTLifinlist{\@thislabel}{#4}}%
   \@if@in@list
   {}%
   {%
     \advance\@probselN by 1\relax
     \expandafter
       \edef\csname @prob@tmp@\romannumeral\@probselN\endcsname{%
         \@thislabel}%
   }%
 }%
%    \end{macrocode}
%\changes{3.03}{2012/01/16}{added exclusion list to warning messages}
%    \begin{macrocode}
 \edef\@do@excludedlist{#4}%
 \ifdefempty{\@do@excludedlist}%
   {}%
   {\def\@do@excludedlist{(Excluded list: #4.) }}%
%    \end{macrocode}
%\changes{3.03}{2012/01/16}{added check for 0 available problems}
%    \begin{macrocode}
 \ifnum\@probselN=0\relax
     \PackageWarning{probsoln}{You have requested
       \number#2\space\space problem(s) but there are no available
       problems in `#3'. \@do@excludedlist No problems will be selected}%
 \else
   \shuffle{@prob@tmp@}{\@probselN}%
   \ifnum\@probselN<#2\relax
%    \end{macrocode}
%\changes{3.01}{2011/08/22}{changed error to warning}
%    \begin{macrocode}
     \ifnum\@probselN=1\relax
       \PackageWarning{probsoln}{You have requested
         \number#2\space\space problem(s) but there is only
         1 problem available in `#3'. \@do@excludedlist
         Only 1 problem will be selected}%
     \else
       \PackageWarning{probsoln}{You have requested
         \number#2\space\space problem(s) but there are only
         \number\@probselN\space problems available in `#3'.
           \@do@excludedlist
           Only \number\@probselN\space problems will be selected}%
     \fi
   \else
     \@probselN=#2\relax
   \fi
 \fi
%    \end{macrocode}
% Store only the first \meta{n} of the shuffled labels.
%    \begin{macrocode}
 \@probN=0\relax
 \def\prob@selectedlabels{}%
 \whiledo{\@probN<\@probselN}{%
   \advance\@probN by 1\relax
   \ifx\prob@selectedlabels\@empty
     \edef\prob@selectedlabels{%
       \csname @prob@tmp@\romannumeral\@probN\endcsname}%
   \else
     \edef\prob@selectedlabels{%
       \prob@selectedlabels,%
       \csname @prob@tmp@\romannumeral\@probN\endcsname}%
   \fi
 }%
%    \end{macrocode}
% Only load selected labels.
%    \begin{macrocode}
 \let\prob@do@defproblem=\prob@do@selectedproblem
 \firstpassfalse
 \@for\@thisfile:=#3\do{\input{\@thisfile}}%
%    \end{macrocode}
% Move them from the reserved data base into the required data base
% in the order specified by \cs{prob@selectedlabels}
%    \begin{macrocode}
\@ifundefined{prob@db@#1}{\prob@newdb{#1}}{}%
\@for\@thislabel:=\prob@selectedlabels\do{%
  \@moveproblem{\@thislabel}{reserved}{#1}%
}%
%    \end{macrocode}
% \cs{prob@selectedlabels} had to be globally defined. It's no
% longer required to undefine it.
%    \begin{macrocode}
 \let\prob@selectedlabels=\undefined
\egroup
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Iterating Through a Data Base}
%\begin{macro}{\foreachproblem}
%\begin{definition}
%\cs{foreachproblem}\oarg{db name}\marg{body}
%\end{definition}
% Does \meta{body} for each problem defined in the data base
% \meta{db name}. Within \meta{body}, the command \cs{thisproblem}
% can be used to do the current problem and the command
% \cs{thisproblemlabel} can be used to access the current label.
%    \begin{macrocode}
\newcommand{\foreachproblem}[2][default]{%
\@ifundefined{prob@db@#1}{%
  \PackageError{probsoln}{Data base `#1' is not defined}{}%
}{%
  \expandafter\let\expandafter\@tmp\csname prob@db@#1\endcsname
  \@for\thisproblemlabel:=\@tmp\do{%
    \expandafter\ifnum
       \csname prob@argN@#1@\thisproblemlabel\endcsname>0\relax
       \@ifundefined{prob@args@#1@\thisproblemlabel}{%
         \expandafter\@prob@getargs
           \csname prob@argN@#1@\thisproblemlabel\endcsname
           {#1}{\thisproblemlabel}}{}%
       \expandafter\let\expandafter\thisproblemargs
         \csname prob@args@#1@\thisproblemlabel\endcsname
    \else
       \let\thisproblemargs\@empty
    \fi
    \expandafter\toks@\expandafter{\thisproblemargs}%
    \edef\thisproblem{\noexpand\useproblem[#1]{\thisproblemlabel}%
       \the\toks@}%
    #2%
  }%
}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\foreachsolution}
%\begin{definition}
%\cs{foreachsolution}\oarg{db name}\marg{body}
%\end{definition}
% Does \meta{body} for each problem defined in the data base
% \meta{db name} that has a solution contained within
% \env{onlysolution}. Within \meta{body}, the command \cs{thisproblem}
% can be used to do the current problem and the command
% \cs{thisproblemlabel} can be used to access the current label.
%    \begin{macrocode}
\newcommand{\foreachsolution}[2][default]{%
\@ifundefined{prob@db@#1}{%
  \PackageError{probsoln}{Data base `#1' is not defined}{}%
}{%
  \expandafter\let\expandafter\@tmp\csname prob@db@#1@solutions\endcsname
  \@for\thisproblemlabel:=\@tmp\do{%
    \ifx\thisproblemlabel\@empty
    \else
      \expandafter\ifnum
         \csname prob@argN@#1@\thisproblemlabel\endcsname>0\relax
         \@ifundefined{prob@args@#1@\thisproblemlabel}{%
           \expandafter\@prob@getargs
             \csname prob@argN@#1@\thisproblemlabel\endcsname
             {#1}{\thisproblemlabel}}{}%
         \expandafter\let\expandafter\thisproblemargs
           \csname prob@args@#1@\thisproblemlabel\endcsname
      \else
         \let\thisproblemargs\@empty
      \fi
      \expandafter\toks@\expandafter{\thisproblemargs}%
      \edef\thisproblem{\noexpand\useproblem[#1]{\thisproblemlabel}%
         \the\toks@}%
      #2%
    \fi
  }%
}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\foreachdataset}
%\begin{definition}
%\cs{foreachdataset}\marg{cmd}\marg{body}
%\end{definition}
% Iterates through all defined data sets. Assigns \meta{cmd} to the 
% name of the data base.
%    \begin{macrocode}
\newcommand{\foreachdataset}[2]{%
\@for#1:=\prob@databases\do{#2}}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Random Numbers}
% First define some registers for later use.
%    \begin{macrocode}
\newcount\@probN \newcount\@probselN \newcount\@rndselctr
\newcount\r@ndcur
\newcount\@ps@randtmp
\r@ndcur=1\relax
%    \end{macrocode}
%\begin{macro}{\PSNrandseed}
% Set the random generator seed
%    \begin{macrocode}
\newcommand*{\PSNrandseed}[1]{%
  \ifnum#1=0\relax
    \PackageWarning{probsoln}{Can't have 0 as random seed, changing to 1}%
    \global\r@ndcur=1\relax
  \else
    \global\r@ndcur=#1\relax
  \fi
  \PackageInfo{probsoln}{Random Seed = \number\r@ndcur}%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\PSNgetrandseed}
%    \begin{macrocode}
\newcommand*{\PSNgetrandseed}[1]{#1=\r@ndcur\relax}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\PSNrand}
% Generate a random integer.
%    \begin{macrocode}
\newcommand*{\PSNrand}{%
  \@ps@randtmp=\r@ndcur
  \multiply\@ps@randtmp by 16811\relax
  \r@ndcur=\@ps@randtmp
  \global\divide\r@ndcur by 39989\relax
  \global\multiply\r@ndcur by 39989\relax
  \advance\@ps@randtmp by -\r@ndcur
  \global\r@ndcur = \@ps@randtmp
  \ifnum\r@ndcur=0\relax
    \global\r@ndcur=1\relax
  \fi
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\PSN@old@rand}
% Random generator used in v3.0 and earlier
%    \begin{macrocode}
\newcommand*{\PSN@old@rand}{%
  \@ps@randtmp=\r@ndcur
  \multiply\@ps@randtmp by 16807\relax
  \r@ndcur=\@ps@randtmp
  \global\divide\r@ndcur by 120001\relax
  \global\multiply\r@ndcur by 120001\relax
  \advance\@ps@randtmp by -\r@ndcur
  \global\r@ndcur = \@ps@randtmp
  \ifnum\r@ndcur=0\relax
    \global\r@ndcur=1\relax
  \fi
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\PSNuseoldrandom}
% Use the old random number generator
%    \begin{macrocode}
\newcommand*{\PSNuseoldrandom}{%
  \let\PSNrand\PSN@old@rand
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\PSNrandom}
%\begin{definition}
%\cs{PSNrandom}\marg{count}\marg{n}
%\end{definition}
% stores a random number from 1 to \meta{n}
% in the TeX count register \meta{count}
%    \begin{macrocode}
\newcommand{\PSNrandom}[2]{%
%    \end{macrocode}
% generate new random number.
%    \begin{macrocode}
  \PSNrand
  #1=\r@ndcur
  \@ps@randtmp=\r@ndcur
%    \end{macrocode}
% now set \meta{count} to (\meta{count} mod \meta{n}) + 1
%    \begin{macrocode}
  \divide\@ps@randtmp by #2\relax
  \multiply\@ps@randtmp by #2\relax
  \advance#1 by -\@ps@randtmp
  \advance#1 by 1\relax
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\random}
%\begin{definition}
% \cs{random}\marg{counter}\marg{a}\marg{b}
%\end{definition}
% Generate a random number in the range $[a,b]$, and store this
% number in the \LaTeX\ counter \meta{counter}.
%    \begin{macrocode}
\newcommand{\random}[3]{%
\ifnum#2=1\relax
  \PSNrandom{\value{#1}}{#3}%
\else
  \@rndselctr=#3%
  \advance\@rndselctr by -#2\relax
  \advance\@rndselctr by 1\relax
  \PSNrandom{\value{#1}}{\@rndselctr}%
  \addtocounter{#1}{#2}%
  \addtocounter{#1}{-1}%
\fi
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\shuffle}
% Shuffle contents of pseudo-array. For example, suppose you have
% the following definitions: "\def\fooi{A}", "\def\fooii{B}" and
% "\def\fooiii{C}", then "\shuffle{foo}{3}" will shuffle the
% definitions, so you may end up with, e.g.\ "\def\fooi{C}", 
% "\def\fooii{A}", "\def\fooiii{B}", or some other variation.
%    \begin{macrocode}
\newcount\@shfctr \newcount\@shfA \newcount\@shfB
\newcommand{\shuffle}[2]{%
  \@shfctr=1\relax
  \whiledo{\@shfctr < 101}%
  {%
    \PSNrandom{\@shfA}{#2}\PSNrandom{\@shfB}{#2}%
    \ifnum\@shfA=\@shfB
    \else
      \edef\@@tmpA{\csname#1\romannumeral\@shfA\endcsname}%
      \let\@tmpA=\@@tmpA
      \edef\@@tmpB{\csname#1\romannumeral\@shfB\endcsname}%
      \let\@tmpB=\@@tmpB
      \expandafter\xdef\csname#1\romannumeral\@shfA\endcsname{\@tmpB}%
      \expandafter\xdef\csname#1\romannumeral\@shfB\endcsname{\@tmpA}%
    \fi
    \advance\@shfctr by 1\relax
  }%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\doforrandN}
%\begin{definition}
% \cs{doforrandN}\marg{n}\marg{cmd}\marg{list}\marg{text}.
%\end{definition}
% A bit like \cs{@for} but only for a random subset of the given 
% list. 
% For example, the following will load one problem each from two 
% out of the three listed files.
%\begin{verbatim}
%\doforrandN{2}{\tmp}{file1,file2,file3}{%
%\loadrandomproblems{1}{\tmp}}
%\end{verbatim}
%
%\changes{3.03}{}{added count register to keep track of level to
%allow for nested loops}
%    \begin{macrocode}
\newcount\@ps@forrand
\newcount\@ps@forrand@level
\newcommand{\doforrandN}[4]{%
  \global\advance\@ps@forrand@level by 1\relax
  {%
    \@ps@forrand=0\relax
    \@for#2:=#3\do{%
      \advance\@ps@forrand by 1\relax
      \expandafter
        \edef\csname @doforrandN@\number\@ps@forrand@level @\romannumeral\@ps@forrand\endcsname{#2}%
    }%
    \ifnum\@ps@forrand<#1\relax
      \PackageError{probsoln}{Can't randomly select \number#1\space item(s)}{You 
      have requested \number#1\space item(s), but there
      are only \number\@ps@forrand\space item(s) in the list: #3}%
    \else
      \shuffle{@doforrandN@\number\@ps@forrand@level @}{\@ps@forrand}%
      \ifnum#1>0\relax
        \@ps@forrand=0\relax
        \loop
          \advance\@ps@forrand by 1\relax
          \edef#2{\csname @doforrandN@\number\@ps@forrand@level @\romannumeral\@ps@forrand\endcsname}%
           #4%
        \ifnum\@ps@forrand<#1\relax
        \repeat
      \fi
    \fi
  }%
  \global\advance\@ps@forrand@level by -1\relax
}
%    \end{macrocode}
%\end{macro}
%\subsection{Compatibility With Older Versions}
% These commands ensure that this version is compatible with
% versions prior to v3.0
%\begin{macro}{\newproblem}
%\changes{3.01}{2011/08/22}{added second optional argument}
% Defines a new problem.
%    \begin{macrocode}
\newcommand*{\newproblem}{\@ifstar\@snewproblem\@newproblem}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@snewproblem}
% Store first optional argument
%    \begin{macrocode}
\newcommand{\@snewproblem}[1][0]{%
  \def\@newprob@argN{#1}%
  \@s@newproblem
}%
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@s@newproblem}
% Define a new problem without a solution
%    \begin{macrocode}
\newcommand{\@s@newproblem}[3][]{%
  \begin{defproblem}[\@newprob@argN][#1]{#2}%
    #3%
  \end{defproblem}%
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@newproblem}
% Store first optional argument
%    \begin{macrocode}
\newcommand{\@newproblem}[1][0]{%
  \def\@newprob@argN{#1}%
  \@ns@newproblem
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@ns@newproblsm}
% Define problem with a solution:
%    \begin{macrocode}
\newcommand{\@ns@newproblem}[4][]{%
  \begin{defproblem}[\@newprob@argN][#1]{#2}%
    #3%
    \begin{onlysolution}%
      \begin{solution}%
        #4%
      \end{solution}%
    \end{onlysolution}%
  \end{defproblem}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\selectallproblems}
%    \begin{macrocode}
\newcommand*{\selectallproblems}[1]{{\loadallproblems[#1]{#1}}%
\foreachproblem[#1]{\PSNitem\thisproblem\endPSNitem}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\selectrandomly}
%    \begin{macrocode}
\newcommand*{\selectrandomly}[2]{%
{\loadrandomproblems[#1]{#2}{#1}}%
\foreachproblem[#1]{\PSNitem\thisproblem\endPSNitem}%
}
%    \end{macrocode}
%\end{macro}
%\begin{environment}{PSNitem}
%    \begin{macrocode}
\newenvironment{PSNitem}{\item}{}
%    \end{macrocode}
%\end{environment}
%
%\subsection{Formatting Commands}
% These commands are provided to format parts of the
% problems/solutions.
%\begin{environment}{solution}
%\changes{3.01}{2011/08/22}{added space after solution tag}
%    \begin{macrocode}
\@ifundefined{solution}{%
  \newenvironment{solution}{\par\noindent\textbf{\solutionname:}\space
    \ignorespaces}{}%
}{}
%    \end{macrocode}
%\end{environment}
%\begin{macro}{\solutionname}
%    \begin{macrocode}
\newcommand*{\solutionname}{Solution}
%    \end{macrocode}
%\end{macro}
% Define an in-line enumeration which uses the enumeration
% environment's counters.
%\begin{environment}{textenum}
%\changes{2007 Oct 6}{2.05}{new}
%\changes{2012/08/23}{3.04}{fixed bug that interfered with depth counter}
%    \begin{macrocode}
\newenvironment{textenum}{%
  \ifnum\@enumdepth>\thr@@
    \@toodeep
  \else
    \advance\@enumdepth by 1\relax
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}%
    \let\@item\@textitem
    \def\@itemlabel{%
      \refstepcounter{\@enumctr}%
      \csname label\@enumctr\endcsname
    }%
    \setcounter{\@enumctr}{0}%
  \fi
  \ignorespaces
}%
{%
}
%    \end{macrocode}
%\end{environment}
%\begin{macro}{\@textitem}
% In-line enumeration item
%    \begin{macrocode}
\def\@textitem[#1]{#1\space\ignorespaces}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\correctitemformat}
% \changes{2007 Oct 6}{2.05}{new}
% Indicates how to format the item label for \cs{correctitem} when
% the solutions are shown. The argument is the label. This
% defaults to placing the argument in a box.
%    \begin{macrocode}
\newcommand*{\correctitemformat}[1]{\fbox{#1}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\incorrectitemformat}
% \changes{2007 Oct 6}{2.05}{new}
% Indicates how to format the item label for \cs{incorrectitem} when
% the solutions are shown. The argument is the label. This
% defaults to just the argument shifted
% by \cs{fboxsep} + \cs{fboxrule} to ensure it aligns
% with the default \cs{correctitemformat}.
%    \begin{macrocode}
\newcommand*{\incorrectitemformat}[1]{%
\hspace{\fboxsep}\hspace{\fboxrule}#1}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\correctitem}
% \changes{2007 Oct 6}{2.05}{new}
%This can be used instead of \cs{item}. If the solutions are
% not shown, it behaves like \cs{item}, otherwise, it's like
% \cs{item}, but the label is formatted according to
% \cs{correctitemformat}.
%    \begin{macrocode}
\newcommand*{\correctitem}{\@inmatherr\correctitem
\@ifnextchar[\@correctitem{\@noitemargtrue\@correctitem[\@itemlabel]}}
%    \end{macrocode}
%    \begin{macrocode}
\def\@correctitem[#1]{%
\ifshowanswers
  \@item[\correctitemformat{#1}]%
\else
  \@item[#1]%
\fi}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\incorrectitem}
% \changes{2007 Oct 6}{2.05}{new}
%This can be used instead of \cs{item}. If the solutions are
% not shown, it behaves like \cs{item}, otherwise, it's like
% \cs{item}, but the label is formatted according to
% \cs{incorrectitemformat}.
%    \begin{macrocode}
\newcommand*{\incorrectitem}{\@inmatherr\incorrectitem
\@ifnextchar[\@incorrectitem{\@noitemargtrue\@incorrectitem[\@itemlabel]}}
%    \end{macrocode}
%    \begin{macrocode}
\def\@incorrectitem[#1]{%
\ifshowanswers
  \@item[\incorrectitemformat{#1}]%
\else
  \@item[#1]%
\fi}
%    \end{macrocode}
%\end{macro}
%\iffalse
%    \begin{macrocode}
%</probsoln.sty>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample-exclude.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{probsoln}
 %uncomment the following line to display the answers
 %\showanswers

 % Set the academic year to be 2011/12 (if omitted assume current
 % academic year):
 \SetStartYear{2011}

 % Set the seed for the random number generator:
 \PSNrandseed{\GetStartYear}

 % Exclude problems that have been used this year or the previous
 % two academic years. (Creates a file called 'excluded.tex' to
 % store labels of used problems. Also creates a file called
 % \jobname.prb that stores labels of problems used in this 
 % document, so they don't get excluded on subsequent runs.)
 \ExcludePreviousFile[3]{excluded}

 % To clear the used problems file (\jobname.prb) of the labels 
 % generated in the previous run, comment the above and uncomment
 % below:
 % \ClearUsedFile{excluded}

 % Load 1 problem randomly selected from the given file
 % and store in database 'firstprinciples':
\loadrandomproblems[firstprinciples]{1}{prob-1stprncp}

 % Load 5 problems randomly selected from the given file
 % and store in database 'easy':
\loadrandomproblems[easy]{5}{prob-easy}

 % Load 2 problems randomly selected from the given file
 % and store in database 'implicit':
\loadrandomproblems[implicit]{2}{prob-implicit}

 % Load 2 problems randomly selected from the given file
 % and store in database 'probspaces':
\loadrandomproblems[probspaces]{2}{prob-probspaces}

 % Load 1 problem randomly selected from the given file
 % and store in database 'mchoice':
\loadrandomproblems[mchoice]{1}{prob-mchoice}

 % Load 1 problem randomly selected from the given file
 % and store in database 'nosoln':
\loadrandomproblems[nosoln]{1}{prob-nosoln}


\begin{document}
\title{Sample Problem Sheet}
\author{Nicola Talbot}
\maketitle

\begin{enumerate}
 % Differentiation from first principles
 \foreachproblem[firstprinciples]{\item \thisproblem}

\item Differentiate the following functions:
\begin{enumerate}
  \foreachproblem[easy]{\item \thisproblem}
\end{enumerate}

 % Implicit differentiation
\foreachproblem[implicit]{\item \thisproblem}

 % Finite probability spaces
\foreachproblem[probspaces]{\item \thisproblem}

 % Multiple choice question
\foreachproblem[mchoice]{\item \thisproblem}

 % select one essay style question that doesn't come with a solution

\foreachproblem[nosoln]{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample-exclude.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{probsoln}
 % uncomment the following line to display the answers
 % \showanswers

 % The following will generate a different set of
 % problems each year
 %\PSNrandseed{\year}

 % Load 1 problem randomly selected from the given file
 % and store in database 'firstprinciples':
\loadrandomproblems[firstprinciples]{1}{prob-1stprncp}

 % Load 5 problems randomly selected from the given file
 % and store in database 'easy':
\loadrandomproblems[easy]{5}{prob-easy}

 % Load 2 problems randomly selected from the given file
 % and store in database 'implicit':
\loadrandomproblems[implicit]{2}{prob-implicit}

 % Load 2 problems randomly selected from the given file
 % and store in database 'probspaces':
\loadrandomproblems[probspaces]{2}{prob-probspaces}

 % Load 1 problem randomly selected from the given file
 % and store in database 'mchoice':
\loadrandomproblems[mchoice]{1}{prob-mchoice}

 % Load 1 problem randomly selected from the given file
 % and store in database 'nosoln':
\loadrandomproblems[nosoln]{1}{prob-nosoln}


\begin{document}
\title{Sample Problem Sheet}
\author{Nicola Talbot}
\maketitle

\begin{enumerate}
 % Differentiation from first principles
 \foreachproblem[firstprinciples]{\item \thisproblem}

\item Differentiate the following functions:
\begin{enumerate}
  \foreachproblem[easy]{\item \thisproblem}
\end{enumerate}

 % Implicit differentiation
\foreachproblem[implicit]{\item \thisproblem}

 % Finite probability spaces
\foreachproblem[probspaces]{\item \thisproblem}

 % Multiple choice question
\foreachproblem[mchoice]{\item \thisproblem}

 % select one essay style question that doesn't come with a solution

\foreachproblem[nosoln]{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{probsoln}
 %uncomment the following line to display the answers
\showanswers

 % The following will generate a different set of
 % problems in subsequent years
 %\PSNrandseed{\year}

 % The following will generate a different set of
 % problems on every run (where each run is at
 % least a minute apart from the previous one)
\PSNrandseed{\time}

\begin{document}
\title{Sample Problem Sheet}
\author{Nicola Talbot}
\maketitle

\begin{enumerate}

\newcounter{numproblems}
 % store a random number from 1 to 3 in the counter numproblems
\random{numproblems}{1}{3}

 % selects 1,2 or 3 problems from 1 of the given files:
\doforrandN{1}{\file}{prob-1stprncp,prob-implicit}{\selectrandomly{\file}{\value{numproblems}}}

\item Differentiate the following functions:
\input{prob-args}% load database
\begin{enumerate}
\newcounter{A}
\newcounter{B}
\newcounter{C}
\random{A}{-4}{5}
\random{B}{-3}{3}
\random{C}{-1}{10}
\item \useproblem{diff:quad}{\arabic{A}}{\arabic{B}}{\arabic{C}}

\random{A}{-5}{5}
 % make sure its not 0
\whiledo{\value{A}=0}{\random{A}{-5}{5}}
\item \useproblem{diff:sin}{\arabic{A}}
\end{enumerate}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample3.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{longtable}
\usepackage{probsoln}

 %\showanswers

\newcounter{problem}
\renewcommand{\PSNitem}{\refstepcounter{problem}%
\theproblem. }
\renewcommand{\endPSNitem}{\\}

\renewenvironment{solution}{}{}

\newcommand{\selected}{\fbox{$\times$}}
\newcommand{\notselected}{\fbox{\phantom{$\times$}}}

\begin{document}
\begin{longtable}{lrrrl}
\bfseries Question & \bfseries A & \bfseries B & 
\bfseries C & \ifshowanswers \bfseries Reason\fi\\
\selectrandomly{prob-tabmchoice}{2}
\end{longtable}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample3.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample4.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{probsoln}

 % Sample file illustrating use of \loadallproblems
 % All problems defined in specified file will be used
 % in order of definition

 \loadallproblems{prob-easy}
 %\loadselectedproblems{diffeasy:gpowh,diffeasy:arccos}{prob-easy}
 %\loadexceptproblems{diffeasy:gpowh,diffeasy:arccos}{prob-easy}

 \showanswers

\begin{document}
Differentiate the following functions:
\begin{enumerate}
  \foreachproblem{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample4.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample5.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{report}

\usepackage{probsoln}

\begin{document}
\hideanswers

\chapter{Easy Problems}
\loadrandomproblems[easyproblems]{5}{prob-easy2}

\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
\begin{enumerate}
\foreachproblem[easyproblems]{\item\label{prob:\thisproblemlabel}\thisproblem}
\end{enumerate}

\chapter{Probability Spaces}
\loadrandomproblems[probspaces]{2}{prob-probspaces2}

\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
\begin{enumerate}
\foreachproblem[probspaces]{\item\label{prob:\thisproblemlabel}\thisproblem}
\end{enumerate}

\appendix
\chapter{Solutions}
\showanswers
\begin{enumerate}
\foreachdataset{\thisdataset}{%
\foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}
}
\end{enumerate}

\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample5.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample6.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
\documentclass{article}

\usepackage[usedefaultargs]{probsoln}

\loadallproblems{prob-args}

\begin{document}
\begin{enumerate}
\foreachproblem{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample6.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample7.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
\documentclass{article}

\usepackage{tikz}
\usepackage{probsoln}

\loadallproblems{prob-mixed}

\begin{document}
\section{Exercises}
\begin{enumerate}
  \foreachproblem{\item\label{q-\thisproblemlabel} \thisproblem}
\end{enumerate}

\section{Solutions}
\showanswers

\begin{enumerate}
  \foreachsolution{\item[\ref{q-\thisproblemlabel}] \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample7.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample8.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
\documentclass{article}

\usepackage{listings}
\usepackage{probsoln}

\setkeys{probsoln}{fragile}

\loadallproblems{prob-verb}

\showanswers
\begin{document}
\begin{enumerate}
  \foreachproblem{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample8.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*sample9.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\documentclass[a4paper]{article}

\usepackage{probsoln}
 % uncomment the following line to display the answers
 % \showanswers

 % Load 10 problems randomly selected from the given files
\loadrandomproblems{10}{prob-1stprncp,prob-easy,prob-implicit,prob-probspaces}

\begin{document}
\title{Sample Problem Sheet}
\author{Nicola Talbot}
\maketitle

\begin{enumerate}
 \foreachproblem{\item \thisproblem}
\end{enumerate}
\end{document}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</sample9.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-1stprncp.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These problems all involve differentiating from 1st principles

\newproblem{dfp:xcube}{%
Differentiate $f(x) = x^3$ with respect to $x$ by first principles.}{%
\begin{eqnarray*}
\frac{dy}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{f(x+\Delta x) - f(x)}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{(x+\Delta x)^3-x^3}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{(x+\Delta x)(x^2+2x\Delta x+(\Delta x)^2)-x^3}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{x^3+3x^2\Delta x+3x(\Delta x)^2+(\Delta x)^3-x^3}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{3x^2\Delta x+3x(\Delta x)^2+(\Delta x)^3}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}3x^2+3x\Delta x + (\Delta x)^2\\
 & = & 3x^2
\end{eqnarray*}}

\newproblem{dfp:Ioverxsq}{%
Differentiate $\displaystyle f(x) = \frac{1}{x^2}$ with respect to $x$ by first principles.}{%
\begin{eqnarray*}
\frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{\frac{1}{(x+\Delta x)^2}-\frac{1}{x^2}}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{\frac{x^2-(x+\Delta x)^2}{x^2(x+\Delta x)^2}}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{x^2-(x^2+2x\Delta x+(\Delta x)^2)}{x^2\Delta x(x+\Delta x)^2}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{-2x\Delta x-(\Delta x)^2}{x^2\Delta x(x+\Delta x)^2}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{-2x-\Delta x}{x^2(x+\Delta x)^2}\\
 & = & \frac{-2x}{x^2x^2}\\
 & = & -\frac{2}{x^3}
\end{eqnarray*}}

\newproblem{dfp:sqrtx}{%
Differentiate from first principles $f(x) = \surd x$}{%
\begin{eqnarray*}
\frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{\sqrt{x+\Delta x}-\surd x}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{(\sqrt{x+\Delta x}-\surd x)(\sqrt{x+\delta x}+\surd x)}{\Delta x(\sqrt{x+\Delta x}+\surd x)}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{x+\Delta x - x}{\Delta x(\sqrt{x+\Delta x}+\surd x)}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{\Delta x}{\Delta x(\sqrt{x+\Delta x}+\Delta x)}\\
 & = & \lim_{\Delta x\rightarrow 0}\frac{1}{\sqrt{x+\Delta x}+\surd x}\\
 & = & \frac{1}{2\surd x}
\end{eqnarray*}}

\newproblem{dfp:cons}{%
Differentiate from first principles $f(x) = c$ where $c$ is a constant.}{%
\begin{eqnarray*}
\frac{df}{dx} & = & \lim_{\Delta x\rightarrow 0}\frac{c-c}{\Delta x}\\
 & = & \lim_{\Delta x\rightarrow 0}0\\
 & = & 0
\end{eqnarray*}}

\newproblem{dfp:cosx}{%
Given 
\begin{eqnarray*}
\lim_{x \rightarrow 0} \frac{\cos x - 1}{x} & = & 0\\
\lim_{x \rightarrow 0} \frac{\sin x}{x} & = & 1
\end{eqnarray*}
differentiate from first principles $f(x) = \cos x$.}{%
\begin{eqnarray*}
\frac{df}{dx} & = & \lim_{\Delta x \rightarrow 0}\frac{f(x + \Delta x) - f(x)}{\Delta x}\\
 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos(x + \Delta x) - \cos(x)}{\Delta x}\\
 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos x\cos\Delta x - \sin x\sin\Delta x - \cos x}{\Delta x}\\
 & = & \lim_{\Delta x \rightarrow 0} \frac{\cos x(\cos\Delta x - 1) - \sin x\sin\Delta x}{\Delta x}\\
 & = & \cos x\lim_{\Delta x \rightarrow 0}\frac{\cos\Delta x - 1}{\Delta x} 
      - \sin x\lim_{\Delta x \rightarrow 0}\frac{\sin\Delta x}{\Delta x}\\
 & = & -1 \qquad\mbox{(using given results)}
\end{eqnarray*}}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-1stprncp.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-args.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These problems require arguments
%
 % Arguments: #1->a_2, #2->a_1 and #3->a_0
 % (Arguments must be integers)
\newcount\ctr
\newproblem[3][{1}{2}{3}]{diff:quad}{%
\(f(x) = 
\ifnum#1=0
\else
\ifnum#1=1\else#1\fi x^2 
\fi
\ifnum#2=0
\else
\ifnum#2>0 \ifnum#1=0 \else + \fi \fi
\ifnum#2=1\else#2\fi x 
\fi
\ifnum#3=0
\else
\ifnum#3>0 \ifnum#2=0 \ifnum#1=0 \else + \fi \else + \fi\fi
#3
\fi\)
}{%
\(f'(x) = 
\ifnum#1=0
\else
\ctr=2
\multiply\ctr by #1
\the\ctr x 
\fi
\ifnum#2=0
\else
\ifnum#2>0 \ifnum#1=0 \else + \fi \fi
#2
\fi
 % print 0 if both #1 and #2 are 0
\ifnum#1=0 \ifnum#2=0 0 \fi\fi 
\)
}

\begin{defproblem}[1][{6}]{diff:sin}
\begin{onlyproblem}
  \(f(x) = \sin(#1x)\)
\end{onlyproblem}
\begin{onlysolution}
  \(f'(x) = #1\cos(#1x)\)
\end{onlysolution}
\end{defproblem}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-args.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-easy.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These are all easy differentiation problems

\newproblem{diffeasy:gpowh}{%
\(f(x) = g(x)^{h(x)}.\)}{%
\begin{eqnarray*}
f(x) & = & e^{\ln g(x)^{h(x)}}\\
 & = & e^{h(x)\ln g(x)}\\
f'(x) & = & e^{h(x)\ln g(x)}(h'(x)\ln g(x) + h(x)\frac{g'(x)}{g(x)})\\
 & = & g(x)^{h(x)}(h'(x)\ln g(x) + \frac{h(x)g'(x)}{g(x)})
\end{eqnarray*}}

\newproblem{diffeasy:arcsin}{%
\(y = \arcsin(x)\)}{%
\[\sin(y) = x\]
diff. w.r.t. $x$:
\begin{eqnarray*}
\cos y \frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{1}{\cos y}\\
 & = & \frac{1}{\sqrt{1 - \sin^2y}}\\
 & = & \frac{1}{\sqrt{1-x^2}}.
\end{eqnarray*}}

\newproblem{diffeasy:arccos}{%
$y = \arccos x$.}{%
\(\cos y = x\)
diff. w.r.t. $x$:
\begin{eqnarray*}
-\sin y \frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{-1}{\sin y}\\
 & = & \frac{-1}{\sqrt{1-\cos^2y}}\\
 & = & \frac{-1}{\sqrt{1-x^2}}
\end{eqnarray*}}

\newproblem{diffeasy:tan}{%
\(y = \tan x\)}{%
\begin{eqnarray*}
y & = & \tan x\\
 & = & \frac{\sin x}{\cos x}\\
\frac{dy}{dx} & = & \frac{\cos x}{\cos x} + \sin x\times\frac{-1}{\cos^2x}\times -\sin x\\
 & = & 1 + \tan^2x\\
 & = & \sec^2x.
\end{eqnarray*}}

\newproblem{diffeasy:arctan}{%
\(y = \arctan x = \tan^{-1}x\)}{%
\[\tan y = x\]
diff w.r.t. $x$:
\begin{eqnarray*}
\sec^2y\frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{1}{\sec^2y}\\
 & = & \frac{1}{1+\tan^2y}\\
 & = & \frac{1}{1+x^2}
\end{eqnarray*}}

\newproblem{diffeasy:cot}{%
\(y = (\tan x)^{-1} = \cot x\)}{%
\begin{eqnarray*}
\frac{dy}{dx} & = & -(\tan x)^{-2}\sec^2x\\
 & = & -\frac{\cos^2x}{\sin^2x}\cdot\frac{1}{\cos^2x}\\
 & = & \frac{-1}{\sin^2x}\\
 & = & -\csc^2x.
\end{eqnarray*}}

\newproblem{diffeasy:cosxsqsinx}{%
$y = \cos(x^2)\sin x$.}{%
\[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]}

\newproblem{diffeasy:xlnx}{%
$y = (x+1)\ln(x+1)$.}{%
\begin{eqnarray*}
\frac{dy}{dx} & = & \ln(x+1) + \frac{x+1}{x+1}\\
 & = & 1 + \ln(x+1).
\end{eqnarray*}}

\newproblem{diffeasy:glng}{%
$f(x) = g(x)\ln(g(x))$.}{%
\begin{eqnarray*}
f'(x) & = & g'(x)\ln(g(x)) + \frac{g(x)}{g(x)}g'(x)\\
 & = & g'(x)(1+\ln(g(x))).
\end{eqnarray*}}

\newproblem{diffeasy:sinx/x}{%
$y = \frac{\sin x}{x}$.}{%
\[\frac{dy}{dx} = \frac{\cos x}{x} - \frac{\sin x}{x^2}\]}

\newproblem{diffeasy:exp4x}{%
  $y = \exp(4x)$
}%
{%
  \[\frac{dy}{dx} = 4\exp(4x)\]
}

\newproblem{diffeasy:exp3x+2}{%
  $y = \exp(3x+2)$
}%
{%
  \[\frac{dy}{dx} = 3\exp(3x+2)\]
}

\newproblem{diffeasy:cubic}{%
  $y=x^3 + 4x^2 - x + 3$
}%
{%
  \[\frac{dy}{dx} = 3x^2 + 8x - 1\]
}

\newproblem{diffeasy:quad}{%
  $y=2x^3 + 6x -1$
}%
{%
  \[\frac{dy}{dx} = 6x + 6 = 6(x+1)\]
}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-easy.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-easy2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These are all easy differentiation problems

\begin{defproblem}{diffeasy:gpowh}%
\begin{onlyproblem}%
\(f(x) = g(x)^{h(x)}.\)%
\end{onlyproblem}%
\begin{onlysolution}%
\begin{eqnarray*}
f(x) & = & e^{\ln g(x)^{h(x)}}\\
 & = & e^{h(x)\ln g(x)}\\
f'(x) & = & e^{h(x)\ln g(x)}(h'(x)\ln g(x) + h(x)\frac{g'(x)}{g(x)})\\
 & = & g(x)^{h(x)}(h'(x)\ln g(x) + \frac{h(x)g'(x)}{g(x)})
\end{eqnarray*}%
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:arcsin}%
\begin{onlyproblem}%
\(y = \arcsin(x)\)%
\end{onlyproblem}%
\begin{onlysolution}%
\[\sin(y) = x\]
diff. w.r.t. $x$:
\begin{eqnarray*}
\cos y \frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{1}{\cos y}\\
 & = & \frac{1}{\sqrt{1 - \sin^2y}}\\
 & = & \frac{1}{\sqrt{1-x^2}}.
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:arccos}%
\begin{onlyproblem}%
$y = \arccos x$.%
\end{onlyproblem}%
\begin{onlysolution}%
\(\cos y = x\)
diff. w.r.t. $x$:
\begin{eqnarray*}
-\sin y \frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{-1}{\sin y}\\
 & = & \frac{-1}{\sqrt{1-\cos^2y}}\\
 & = & \frac{-1}{\sqrt{1-x^2}}
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:tan}%
\begin{onlyproblem}%
\(y = \tan x\)%
\end{onlyproblem}
\begin{onlysolution}%
\begin{eqnarray*}
y & = & \tan x\\
 & = & \frac{\sin x}{\cos x}\\
\frac{dy}{dx} & = & \frac{\cos x}{\cos x} + \sin x\times\frac{-1}{\cos^2x}\times -\sin x\\
 & = & 1 + \tan^2x\\
 & = & \sec^2x.
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:arctan}%
\begin{onlyproblem}%
\(y = \arctan x = \tan^{-1}x\)%
\end{onlyproblem}%
\begin{onlysolution}%
\[\tan y = x\]
diff w.r.t. $x$:
\begin{eqnarray*}
\sec^2y\frac{dy}{dx} & = & 1\\
\frac{dy}{dx} & = & \frac{1}{\sec^2y}\\
 & = & \frac{1}{1+\tan^2y}\\
 & = & \frac{1}{1+x^2}
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:cot}%
\begin{onlyproblem}%
\(y = (\tan x)^{-1} = \cot x\)%
\end{onlyproblem}
\begin{onlysolution}%
\begin{eqnarray*}
\frac{dy}{dx} & = & -(\tan x)^{-2}\sec^2x\\
 & = & -\frac{\cos^2x}{\sin^2x}\cdot\frac{1}{\cos^2x}\\
 & = & \frac{-1}{\sin^2x}\\
 & = & -\csc^2x.
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:cosxsqsinx}%
\begin{onlyproblem}%
$y = \cos(x^2)\sin x$.%
\end{onlyproblem}%
\begin{onlysolution}%
\[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:xlnx}%
\begin{onlyproblem}%
$y = (x+1)\ln(x+1)$.
\end{onlyproblem}%
\begin{onlysolution}%
\begin{eqnarray*}
\frac{dy}{dx} & = & \ln(x+1) + \frac{x+1}{x+1}\\
 & = & 1 + \ln(x+1).
\end{eqnarray*}
\end{onlysolution}
\end{defproblem}

\begin{defproblem}{diffeasy:glng}%
\begin{onlyproblem}%
$f(x) = g(x)\ln(g(x))$.
\end{onlyproblem}
\begin{onlysolution}%
\begin{eqnarray*}
f'(x) & = & g'(x)\ln(g(x)) + \frac{g(x)}{g(x)}g'(x)\\
 & = & g'(x)(1+\ln(g(x))).
\end{eqnarray*}
\end{onlysolution}%
\end{defproblem}

\begin{defproblem}{diffeasy:sinx/x}
\begin{onlyproblem}%
$y = \frac{\sin x}{x}$.
\end{onlyproblem}
\begin{onlysolution}%
\[\frac{dy}{dx} = \frac{\cos x}{x} - \frac{\sin x}{x^2}\]
\end{onlysolution}%
\end{defproblem}

%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-easy2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-implicit.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These are all implicit differentiation problems

\newproblem{imd:circ}{%
Find the gradient of the unit circle ($x^2 + y^2 = 1$).}{%
Differentiating with respect to $x$ gives:
\begin{eqnarray*}
2x + 2y\frac{dy}{dx} & = & 0\\
\frac{dy}{dx} & = & \frac{-2x}{2y}\\
 & = & \frac{-x}{\sqrt{1-x^2}}.
\end{eqnarray*}}

\newproblem{imd:ellipse}{%
Find the gradient of the ellipse given by $4x^2 + 3y^2 = 25$.}{%
Differentiating with respect to $x$ gives:
\begin{eqnarray*}
8x + 6y\frac{dy}{dx} & = & 0\\
\frac{dy}{dx} & = & \frac{-8x}{6y}\\
 & = & \frac{-4x}{\sqrt{25-4x^2}}.
\end{eqnarray*}}

\newproblem{imd:ysq:xcuov2mx}{%
Find $\frac{dy}{dx}$, given
\begin{displaymath}
y^2 = \frac{x^3}{2-x}
\end{displaymath}}{%
Differentiating both sides w.r.t.\ $x$:
\begin{eqnarray*}
2y\frac{dy}{dx} & = & \frac{(2-x)3x^2 - x^3(-1)}{(2-x)^2}\\
 & = & \frac{3x^2(2-x) + x^3}{(2-x)^2}\\
 & = & \frac{6x^2 - 3x^3 + x^3}{(2-x)^2}\\
 & = & \frac{6x^2-2x^3}{(2-x)^2}\\
 & = & 2x^2\frac{3-x}{(2-x)^2}
\end{eqnarray*}
Therefore
\begin{displaymath}
y\frac{dy}{dx} = x^2\frac{3-x}{(2-x)^2}
\end{displaymath}}

\newproblem{imd:exy:IIxay}{%
Differentiate w.r.t.\ $x$:
\begin{displaymath}
e^{xy} = 2x + y
\end{displaymath}}{%
Differentiating both sides w.r.t.\ $x$:
\begin{eqnarray*}
e^{xy}(1y + x\frac{dy}{dx}) & = & 2 + \frac{dy}{dx}\\
xe^{xy}\frac{dy}{dx} - \frac{dy}{dx} & = & 2 - ye^{xy} \\
\frac{dy}{dx}(xe^{xy}-1) & = & 2 - ye^{xy}\\
\frac{dy}{dx} & = & \frac{2-ye^{xy}}{xe^{xy}-1}
\end{eqnarray*}}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-implicit.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-mchoice.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These problems are for multiple choice questions
\newproblem{mc:prod}{%
Which of the following is the derivative of $x\sin(x)$?
(Circle the correct answer.)
\ifthenelse{\boolean{showanswers}}{}%
{\begin{description}
\item[A] $\sin(x)$
\item[B] $x\cos(x)$
\item[C] $\sin(x) + x\cos(x)$
\end{description}
}}{%
\begin{description}
\item[A] $\sin(x)$
\item[B] $x\cos(x)$
\item[\textcircled{C}] $\sin(x) + x\cos(x)$ (product rule).
\end{description}
}

\newproblem{mc:quot}{%
Which of the following is the derivative of $\frac{\sin(x)}{x}$?
(Circle the correct answer.)
\ifthenelse{\boolean{showanswers}}{}%
{\begin{description}
\item[A] $\sin(x)$
\item[B] $\cos(x)$
\item[C] $\frac{\cos(x)x-\sin(x)}{x^2}$
\end{description}
}}{%
\begin{description}
\item[A] $\sin(x)$
\item[B] $\cos(x)$
\item[\textcircled{C}] $\frac{\cos(x)x-\sin(x)}{x^2}$
(quotient rule)
\end{description}
}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-mchoice.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-mixed.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These problems are a mixture of essay-style and questions with
 % answers. One of these problems requires the tikz package

\newproblem*{oop}{Describe what is meant by object-oriented
programming.}

\begin{defproblem}{inheritance}
 Describe what is meant by the term \emph{inheritance} in
 object-oriented programming. Use examples.
\end{defproblem}

\begin{defproblem}{weightedcoin}%
  \begin{onlyproblem}
    A coin is weighted so that heads is four times as likely
    as tails. Find the probability that:
    \begin{textenum}
      \item tails appears,
      \item heads appears
    \end{textenum}%
  \end{onlyproblem}%
  \begin{onlysolution}
    Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
    so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
    \begin{textenum}
      \item $P(T)=\frac{1}{5}$,
      \item $P(H)=\frac{4}{5}$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{defproblem}{validprobspaces}
\begin{onlyproblem}%
Under which of the following functions does
$S=\{a_1,a_2\}$ become a probability space?
\par
\begin{textenum}
\begin{tabular}{ll}
\item $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
&
\item\label{validprobspacescorrect1} $P(a_1)=\frac{3}{4}$,
$P(a_2)=\frac{1}{4}$
\\
\item\label{validprobspacescorrect2} $P(a_1)=1$, $P(a_2)=0$
&
\item $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
\end{tabular}
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}%
\ref{validprobspacescorrect1} and \ref{validprobspacescorrect2}%
\end{onlysolution}
\end{defproblem}

\begin{defproblem}{digraph}
  \begin{onlyproblem}\label{ex:digraph}
  Identify, if any, the sinks and sources of the digraph shown in Figure~\ref{fig:digraph}. 

  \begin{figure}[tbh]
    \centering
      \begin{tikzpicture}[every node/.style={draw,circle}]
         \path (0,0) node (A) {$A$}
               (1,0) node (B) {$B$}
               (0,1) node (C) {$C$};
         \draw[->] (A) -- (B);
         \draw[->] (B) -- (C);
         \draw[->] (A) -- (C);
      \end{tikzpicture}
    \par
    \caption{Digraph for Question~\ref{ex:digraph}}
    \label{fig:digraph}
  \end{figure}
  \end{onlyproblem}
  \begin{onlysolution}
  $A$ is a souce and $C$ is a sink.
  \end{onlysolution}
\end{defproblem}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-mixed.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-newdata.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
\begin{defproblem}{sample}
\begin{onlyproblem}
Differentiate $y=\sin x$
\end{onlyproblem}
\begin{onlysolution}
$y'=\cos x$
\end{onlysolution}
\end{defproblem}

\begin{defproblem}[1]{sample2}
\begin{onlyproblem}
Differentiate $y = \sin(#1x)$
\end{onlyproblem}
\begin{onlysolution}
$y'=#1\cos #1x$
\end{onlysolution}
\end{defproblem}

\begin{defproblem}{sample3}
\begin{onlyproblem}
Differentiate $y = x^2$.
\end{onlyproblem}
\begin{onlysolution}
$y' = 2x$
\end{onlysolution}
\end{defproblem}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-newdata.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-nosoln.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % these problems don't have solutions

\newproblem*{oop}{Describe what is meant by object-oriented
programming.}

\newproblem*{inheritance}{Describe what is meant by the term 
\emph{inheritance} in object-oriented programming. Use examples.}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-nosoln.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-probspaces.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % Finite probability spaces
\newproblem{weightedcoin}{%
A coin is weighted so that heads is four times as likely
as tails. Find the probability that:
\begin{textenum}
\item tails appears,
\item heads appears
\end{textenum}}{%
Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
\begin{textenum}
\item $P(T)=\frac{1}{5}$,
\item $P(H)=\frac{4}{5}$
\end{textenum}}

\newproblem*{validprobspaces}{%
Under which of the following functions does 
$S=\{a_1,a_2\}$ become a probability space?
\par
\begin{textenum}
\begin{tabular}{ll}
\incorrectitem $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
&
\correctitem $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
\\
\correctitem $P(a_1)=1$, $P(a_2)=0$
&
\incorrectitem $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
\end{tabular}
\end{textenum}
}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-probspaces.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-probspaces2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % Finite probability spaces
\begin{defproblem}{weightedcoin}
\begin{onlyproblem}%
A coin is weighted so that heads is four times as likely
as tails. Find the probability that:
\begin{textenum}
\item tails appears,
\item heads appears
\end{textenum}
\end{onlyproblem}
\begin{onlysolution}%
Let $p=P(T)$, then $P(H)=4p$. We require $P(H)+P(T)=1$,
so $4p+p=1$, hence $p=\frac{1}{5}$. Therefore:
\begin{textenum}
\item $P(T)=\frac{1}{5}$,
\item $P(H)=\frac{4}{5}$
\end{textenum}
\end{onlysolution}
\end{defproblem}

\begin{defproblem}{validprobspaces}
\begin{onlyproblem}%
Under which of the following functions does 
$S=\{a_1,a_2\}$ become a probability space?
\par
\begin{textenum}
\begin{tabular}{ll}
\item $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$
&
\item\label{validprobspacescorrect1} $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$
\\
\item\label{validprobspacescorrect2} $P(a_1)=1$, $P(a_2)=0$
&
\item $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$
\end{tabular}
\end{textenum}
\end{onlyproblem}%
\begin{onlysolution}%
\ref{validprobspacescorrect1} and \ref{validprobspacescorrect2}%
\end{onlysolution}
\end{defproblem}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-probspaces2.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-tabmchoice.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
 % This file is public domain
 %
 % These problems are designed to be placed in a
 % tabular environment
 %
\newproblem{tab:1}{%
What is $(3+2)\times5$? &
25 \ifshowanswers\selected\else\notselected\fi &
13 \notselected &
10 \notselected &
}{Brackets come first}%

\newproblem{tab:2}{%
What is $-1+2\times3$? &
3 \notselected &
-7 \notselected &
5 \ifshowanswers\selected\else\notselected\fi &
}{Multiplication comes first}%
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-tabmchoice.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*prob-verb.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
\begin{defproblem}{code-helloworld}
This problem has a code fragment.
\begin{onlyproblem}
\lstset{language=Java}
\begin{lstlisting}
public class HelloWorld
{
  public static void main(String[] args)
  {
     System.out.println("Hello World!");
  }
}
\end{lstlisting}
\end{onlyproblem}
\begin{onlysolution}
\lstset{language=Java}
\begin{lstlisting}
public class HelloWorld
{
  public static void main(String[] args)
  {
     System.out.println("Hello "
      + (args.length==0 ? "anon" : args[0])+"!");
  }
}
\end{lstlisting}
\end{onlysolution}
\end{defproblem}
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%</prob-verb.tex>
%    \end{macrocode}
%\fi
%\Finale
\endinput