summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/babel-contrib/hebrew/hebrew.dtx
blob: 19f0fbf0fc854c4ba03f3c64add4f50e10301a26 (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
% \iffalse meta-comment
%
% Copyright 1989-2005 Johannes L. Braams and any individual authors
% listed elsewhere in this file.  All rights reserved.
% 
% This file is part of the Babel system.
% --------------------------------------
% 
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Johannes Braams.
% 
% The list of all files belonging to the Babel system is
% given in the file `manifest.bbl. See also `legal.bbl' for additional
% information.
% 
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
% \fi
% \CheckSum{3345}
%
% \iffalse meta-comment
%% Hebrew language definition and additional packages.
%% Copyright (C) 1997 -- 2005 Boris Lavva.
%
%% Babel package for LaTeX version 2e
%% Copyright (C) 1989 -- 2005 by Johannes Braams,
%%                            TeXniek
%%                            All rights reserved.
%<*calendar>
%% TeX & LaTeX macros for computing Hebrew date from Gregorian one
%% Copyright (C) 1991 by Michail Rozman, misha@iop.tartu.ew.su
%%
%</calendar>
% \fi
%
%
% \iffalse
%<hebrew>\ProvidesFile{hebrew.ldf}
%<rightleft>\ProvidesFile{rlbabel.def}
%<calendar>\ProvidesPackage{hebcal}
%<*driver>
\ProvidesFile{hebrew.drv}
%</driver>
% \fi
% \ProvidesFile{hebrew.dtx}
        [2005/03/30 v2.3h %
% \iffalse
%<hebrew>         Hebrew language definition from the babel system
%<rightleft>         Right-to-Left support from the babel system
%<calendar>         Hebrew calendar
%<driver>         Driver file for hebrew support
% \fi
    Hebrew language support from the babel system]
%
% \iffalse
% \subsection{A driver for this document}
%
% The next bit of code contains the documentation driver file for
% \TeX{}, i.e., the file that will produce the documentation you are
% currently reading. It will be extracted from this file by the \dst{}
%  program.
%
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\providecommand\babel{\textsf{babel}}
\providecommand\file[1]{\texttt{#1}}
\makeatletter
%    \end{macrocode}
%
%    The code lines are numbered within sections,
%    \begin{macrocode}
\@addtoreset{CodelineNo}{section}
\renewcommand\theCodelineNo{%
  \reset@font\scriptsize\thesection.\arabic{CodelineNo}}
%    \end{macrocode}
%    which should also be visible in the index; hence this
%    redefinition of a macro from \file{doc.sty}.
%    \begin{macrocode}
\renewcommand\codeline@wrindex[1]{\if@filesw
        \immediate\write\@indexfile
            {\string\indexentry{#1}%
            {\number\c@section.\number\c@CodelineNo}}\fi}
%    \end{macrocode}
%
%    The glossary environment is used or the change log, but its
%    definition needs changing for this document.
%    \begin{macrocode}
\renewenvironment{theglossary}{%
    \glossary@prologue%
    \GlossaryParms \let\item\@idxitem \ignorespaces}%
   {}
\makeatother
\DisableCrossrefs
\CodelineIndex
\RecordChanges
\title{Hebrew language support from the \babel\ system}
\author{Boris Lavva}
\date{Printed \today}
\begin{document}
   \maketitle
   \tableofcontents
   \DocInput{hebrew.dtx}
   \DocInput{hebinp.dtx}
   \DocInput{hebrew.fdd}
   \DocInput{heb209.dtx}
   \clearpage
   \def\filename{index}
   \PrintIndex
   \clearpage
   \def\filename{changes}
   \PrintChanges
\end{document}
%</driver>
%    \end{macrocode}
% \fi
%
% \providecommand\babel{\textsf{babel}}
% \providecommand\dst{\textsc{docstrip}}
% \providecommand\file[1]{\texttt{#1}}
% \providecommand\pkg[1]{\texttt{#1}}
% \providecommand\XeT{X\kern-.125em\lower.5ex\hbox{E}\kern-.1667emT\@}
% \providecommand\scrunch{\setlength{\itemsep}{-.05in}}
% \GetFileInfo{hebrew.dtx}
%
% \changes{hebrew~0.1}{??/??/??}{%
%    Preliminary \LaTeX\ Hebrew option (by Sergio Fogel)}
% \changes{hebrew~0.2}{??/??/??}{%
%    Corrections and additions (by Rama Porrat)}
% \changes{hebrew~0.6}{??/??/??}{Additions (by Yael Dubinsky)}
% \changes{hebrew~1.2}{??/??/??}{%
%    Bilingual tables, penalties, documentation and more changes
%    (by Yaniv Bargury)}
% \changes{hebrew~1.30}{1992/05/15}{%
%    Font selection, various (by Alon Ziv)}
% \changes{hebrew~1.31}{1993/02/22}{Bug fixes (by Alon Ziv)}
% \changes{hebrew~1.32}{1993/03/10}{Made font-change command 
%    for numbers `\cs{protect}'ed (by Alon Ziv)}
% \changes{hebrew~1.33}{1993/03/11}{%
%    Made \cs{refstepcounter} work using \cs{@ltor} (by Alon Ziv)}
% \changes{hebrew~1.34}{1993/03/22}{%
%    Moved font loading to another file. Added \cs{mainsec}. 
%    Made all text strings be produced by control codes (similar to
%    \LaTeX 2.09 Mar '92). Fixed \cs{noindent} (by Alon Ziv)}
% \changes{hebrew~1.35}{1993/03/22}{%
%    Moved the texts to a file selected by the current encoding 
%    (by Alon Ziv)}
% \changes{hebrew~1.36}{1993/03/24}{Use \TeX\ tricks to redefine 
%    \cs{theXXXX} without keeping old definitions.
%    Use only \cs{@eng} for direction/font change (removed \cs{@ltor}).
%    Switched from use of \cs{mainsec} to code taken from \babel\
%    system (by Alon Ziv)}
% \changes{hebrew~1.37}{1993/03/28}{%
%    Use \cs{add@around} in defining font size commands. Small bug
%    fixes (by Alon Ziv)}
% \changes{hebrew~1.38}{1993/04/20}{%
%    \cs{everypar} changed so that \cs{noindent} works unmodified 
%    (by Alon Ziv, thanks to Chris Rowley)}
% \changes{hebrew~1.39}{1993/08/10}{%
%    Redefined primitive sectioning commands. Changed \cs{include} so
%    it finds \texttt{.h}, \texttt{.xet}, and \texttt{.ltx} files (no
%    extension needed). Reinstated use of \cs{@ltor} (by Alon Ziv)}
% \changes{hebrew~1.40}{1993/09/01}{Added the \cs{@brackets} hack
%    (by Alon Ziv)}
% \changes{hebrew~1.41}{1993/09/09}{%
%    Reworked towards using NFSS2. Changed some macro names to be more
%    logical: renamed \cs{@ltor} to \cs{@number}, \cs{@eng} to
%    \cs{@latin}, and (in \texttt{hebrew.ldf}) \cs{@heb} to
%    \cs{@hebrew} (by Alon Ziv)}
% \changes{hebrew~1.42}{1993/09/22}{%
%    Made list environments work better. Fixed thebibliography
%    environment (by Alon Ziv)}
% \changes{hebrew~2.0a}{1998/01/01}{%
%    Completely rewritten for \LaTeXe\ and \babel\ support. Various
%    input and font encodings (with NFSS2) are supported too. The
%    original \pkg{hebrew.sty} is divided to a number of packages and
%    definition files for better readability and extensibility. Added
%    some user- and code-level documentation inside the \texttt{.dtx}
%    and \texttt{.fdd} files, and \LaTeX -driven installation with
%    \pkg{hebrew.ins} (by Boris Lavva)}
% \changes{hebrew~2.1}{2000/11/23}{%
%    corrections from Sivan Toledo: sender name in letter, and section name in
%    headings. (by Tzafrir Cohen)}
% \changes{hebrew~2.2}{2000/12/11}{%
%    renamed hebrew letters to heb* (e.g.: alef renamed to hebalef)
%    (by Tzafrir Cohen)}
% \changes{hebrew~2.3}{2001/02/27}{
%    added several \cs{@ifclassloaded}\{slides\} to allow the use of the
%    slides class. (by Tzafrir Cohen)}
% \changes{hebrew~2.3a}{2001/07/09}{
%    The documentation should now be built fine (broken since at least 
%    2.1, and probably 2.0) (by Tzafrir Cohen)}
% \changes{hebrew~2.3b}{2001/08/16}{
%    minor clean-ups. The documentation builds now with no warnings.
%    Also removed \cs{R} from the caption macro (added in 2.1)
%    Added internal \cs{@ensure@L} and \cs{@ensure@R} 
%    (Is there a real need for them? Maybe should they be exposed?)
%    (by Tzafrir Cohen)}
% \changes{hebrew~2.3c}{2001/10/05}{
%    a temporary fix to the \cs{gim} macro. Should be replaced by stuff 
%    from hebcal.
%    (by Tzafrir Cohen)}
% \changes{hebrew~2.3d}{2002/01/04}{
%    Initial support for the prosper class. Added \cs{arabicnorl} .
%    (by Tzafrir Cohen)}
% \changes{hebrew~2.3e}{2002/08/09}{
%    Removing hebtech from this distriution (not relevant to babel),
%    added \cs{HeblatexEncoding}. some docs cleanup
%    (by Tzafrir Cohen)}
% \changes{hebrew~2.3f}{2002/12/26}{
%    redefined \cs{list} instead of redefining every environment 
%    that uses it. some pscolor handling, removed HeblatexEncoding 
%    (don't use 2.3e) (by Tzafrir Cohen)}
% \changes{hebrew~2.3g}{2003/06/05}{
%    Reimplemented the printing of Hebrew numerals and Hebrew
%    counters; modified \pkg{hebcal.sty} to use this implementation
%    when typesetting Hebrew dates; added option |full| to package
%    \pkg{hebcal}; also removed some gratuitous
%    spaces inserted by \pkg{hebcal.sty} by adding comment marks.
%    CAUTION: the changes to \pkg{hebcal.sty} make it dependent on
%    \pkg{babel} and not useable as a stand-alone package. Is this a
%    problem? (by Ron Artstein)}
%
% \section{The Hebrew language}\label{sec:hebrew}
%
%    The file \file{\filename}\footnote{The Hebrew language support
%    files described in this section have version number \fileversion\
%    and were last revised on \filedate.} provides the following
%    packages and files for Hebrew language support:
%    \begin{description}
%    \item[\file{hebrew.ldf}] file defines all the language-specific
%    macros for the Hebrew language. 
%    \item[\file{rlbabel.def}] file is used by |hebrew.ldf| for
%    bidirectional versions of the major \LaTeX{} commands and
%    environments. It is designed to be used with other right-to-left
%    languages, not only with Hebrew.
%    \item[\pkg{hebcal.sty}] package defines a set of macros for
%    computing Hebrew date from Gregorian one.
%    \end{description}
%
%    Additional Hebrew input and font encoding definition files that
%    should be included and used with \file{hebrew.ldf} are:
%    \begin{description}
%    \item[\file{hebinp.dtx}] provides Hebrew input encodings, such as
%          ISO 8859-8, MS Windows codepage 1255 or IBM PC codepage 862
%          (see Section~\ref{sec:hebinp} on page~\pageref{sec:hebinp}).
%    \item[\file{hebrew.fdd}] contains Hebrew font encodings, related
%          font definition files and \pkg{hebfont} package that
%          provides Hebrew font switching commands (see
%          Section~\ref{sec:hebfdd} on page~\pageref{sec:hebfdd} for
%          further details).
%    \end{description}
%
%    \LaTeX~2.09 compatibility files are included with
%    \file{heb209.dtx} and gives possibility to compile existing
%    \LaTeX~2.09 Hebrew documents with small (if any) changes (see
%    Section~\ref{sec:heb209} on page~\pageref{sec:heb209} for
%    details).
%
%    Finally, optional document class \pkg{hebtech} may be useful for
%    writing theses and dissertations in both Hebrew and English (and
%    any other languages included with \babel). It designed to meet
%    requirements of the Graduate School of the Technion --- Israel
%    Institute of Technology. 
%
%    \emph{As of version 2.3e hebtech is no longer distributed together
%    with heblatex. It should be part of a new "hebclasses" package}
%
% \subsection{Acknowledgement}
%
%    The following people have contributed to Hebrew package in one
%    way or another, knowingly or unknowingly. In alphabetical order:
%    Irina Abramovici, Yaniv Bargury, Yael Dubinsky, Sergio Fogel,
%    Dan Haran, Rama Porrat, Michail Rozman, Alon Ziv.
%
%    Tatiana Samoilov and Vitaly Surazhsky found a number of serious
%    bugs in preliminary version of Hebrew package.
%
%    A number of other people have contributed comments and
%    information. Specific contributions are acknowledged within the
%    document.
%
%    I want to thank my wife, Vita, and son, Mishka, for their
%    infinite love and patience.
%
%    If you made a contribution and I haven't mentioned it, don't
%    worry, it was an accident. I'm sorry. Just tell me and I will add
%    you to the next version.
%
% \StopEventually{}
%
% \subsection{The {\normalfont\dst{}} modules}
%
%    The following modules are used in the implementation to direct
%    \dst{} in generating external files:
% \begin{center}
% \begin{tabular}{@{}ll}
%   driver    & produce a documentation driver file \\[4pt]
%   hebrew    & produce Hebrew language support file\\
%   rightleft & create right-to-left support file\\
%   calendar  & create Hebrew calendar package
% \end{tabular}
% \end{center}
%    A typical \dst{} command file would then have entries like:
%    \begin{quote}
%       |\generateFile{hebrew.ldf}{t}{\from{hebrew.dtx}{hebrew}}|
%    \end{quote}
%
% \subsection{Hebrew language definitions}
%
%    The macro |\LdfInit| takes care of preventing that this file is
%    loaded more than once, checking the category code of the |@|
%    sign, etc.
%    \begin{macrocode}
%<*hebrew>
\LdfInit{hebrew}{captionshebrew}
%    \end{macrocode}
%
%    When this file is read as an option, i.e., by the |\usepackage|
%    command, |hebrew| will be an `unknown' language, in which case we
%    have to make it known. So we check for the existence of
%    |\l@hebrew| to see whether we have to do something here. 
%
%    \begin{macrocode}
\ifx\l@hebrew\@undefined
  \@nopatterns{Hebrew}%
  \adddialect\l@hebrew0
\fi
%    \end{macrocode}
%
%  \begin{macro}{\hebrewencoding}
%    \emph{FIX DOCS REGARDING 8BIT}
%
%    Typesetting Hebrew texts implies that a special input and output
%    encoding needs to be used. Generally, the user may choose
%    between different available Hebrew encodings provided. The
%    current support for Hebrew uses all available fonts from the
%    Hebrew University of Jerusalem encoded in `old-code' 7-bit
%    encoding also known as Israeli Standard SI-960. We define for
%    these fonts the Local Hebrew Encoding |LHE| (see the file
%    |hebrew.fdd| for more details), and the |LHE| encoding definition
%    file should be loaded by default.
%
%    Other fonts are available in windows-cp1255 (a superset of ISO-8859-8
%    with nikud). For those, the encoding |HE8| should be used. Such fonts
%    are, e.g., windows' TrueType fonts (once cnverted to Type1 or MetaFont)
%    and IBM's Type1 fonts.
%
%    However, if an user wants to use another font encoding, for
%    example, cyrillic encoding T2 and extended latin encoding T1, ---
%    he/she has to load the corresponding file \emph{before} the
%    \pkg{hebrew} package. This may be done in the following way:
%    \begin{quote}
%      |\usepackage[LHE,T2,T1]{fontenc}|\\
%      |\usepackage[hebrew,russian,english]{babel}|
%    \end{quote}
%    We make sure that the |LHE| encoding is known to \LaTeX{} at end
%    of this package.
%
%    Also note that if you want to use the encoding |HE8| , you should define 
%    the following in your document, \emph{before loading babel}:
%    \begin{quote}
%      |\def\HeblatexEncoding{HE8}|\\
%      |\def\HeblatexEncodingFile{he8enc}|
%    \end{quote}
% \changes{hebrew-2.3h}{2004/02/20}{Make LHE the default encoding for
%    compatibility reasons}
%    \begin{macrocode}
\providecommand{\HeblatexEncoding}{LHE}%
\providecommand{\HeblatexEncodingFile}{lheenc}%
\newcommand{\heblatex@set@encoding}[2]{
}
\AtEndOfPackage{%
  \@ifpackageloaded{fontenc}{%
    \@ifl@aded{def}{%
      \HeblatexEncodingFile}{\def\hebrewencoding{\HeblatexEncoding}}{}%
  }{%
    \input{\HeblatexEncodingFile.def}%
    \def\hebrewencoding{\HeblatexEncoding}%
  }}
%    \end{macrocode}
%  \end{macro}
%
%    We also need to load inputenc package with one of the Hebrew
%    input encodings. By default, we set up the |8859-8| codepage.
%    If an user wants to use many input encodings in the same
%    document, for example, the MS Windows Hebrew codepage |cp1255|
%    and the standard IBM PC Russian codepage |cp866|, he/she has to
%    load the corresponding file \emph{before} the hebrew package
%    too. This may be done in the following way:
%    \begin{quote}
%      |\usepackage[cp1255,cp866]{inputenc}|\\
%      |\usepackage[hebrew,russian,english]{babel}|
%    \end{quote}
%
%    An user can switch input encodings in the document using the
%    command |\inputencoding|, for example, to use the |cp1255|:
%    \begin{quote}
%       |\inputencoding{cp1255}|
%    \end{quote}
%    \begin{macrocode}
\AtEndOfPackage{%
  \@ifpackageloaded{inputenc}{}{\RequirePackage[8859-8]{inputenc}}}
%    \end{macrocode}
%
%    The next step consists of defining commands to switch to (and
%    from) the Hebrew language.
%
%  \begin{macro}{\hebrewhyphenmins}
%    This macro is used to store the correct values of the hyphenation
%    parameters |\lefthyphenmin| and |\righthyphenmin|. They are set
%    to~2.
% \changes{hebrew~2.0b}{2000/09/22}{Now use \cs{providehyphenmins} to
%    provide a default value}
%    \begin{macrocode}
\providehyphenmins{\CurrentOption}{\tw@\tw@}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\captionshebrew}
%    The macro |\captionshebrew| replaces all captions used in the four
%    standard document classes provided with \LaTeXe with their Hebrew
%    equivalents.
% \changes{hebrew-2.0b}{2000/09/20}{Added \cs{glossaryname}}
%    \begin{macrocode}
\addto\captionshebrew{%
  \def\prefacename{\@ensure@R{\hebmem\hebbet\hebvav\hebalef}}%
  \def\refname{\@ensure@R{\hebresh\hebshin\hebyod\hebmem\hebtav\ %
    \hebmem\hebqof\hebvav\hebresh\hebvav\hebtav}}%
  \def\abstractname{\@ensure@R{\hebtav\hebqof\hebtsadi\hebyod\hebresh}}%
  \def\bibname{\@ensure@R{\hebbet\hebyod\hebbet\heblamed\hebyod\hebvav%
    \hebgimel\hebresh\hebpe\hebyod\hebhe}}%
  \def\chaptername{\@ensure@R{\hebpe\hebresh\hebqof}}%
  \def\appendixname{\@ensure@R{\hebnun\hebsamekh\hebpe\hebhet}}%
  \def\contentsname{\@ensure@R{%
    \hebtav\hebvav\hebkaf\hebfinalnun\ %
    \hebayin\hebnun\hebyod\hebyod\hebnun\hebyod\hebfinalmem}}%
  \def\listfigurename{\@ensure@R{%
    \hebresh\hebshin\hebyod\hebmem\hebtav\ %
    \hebalef\hebyod\hebvav\hebresh\hebyod\hebfinalmem}}%
  \def\listtablename{\@ensure@R{%
    \hebresh\hebshin\hebyod\hebmem\hebtav\
    \hebtet\hebbet\heblamed\hebalef\hebvav\hebtav}}%
  \def\indexname{\@ensure@R{\hebmem\hebpe\hebtav\hebhet}}%
  \def\figurename{\@ensure@R{\hebalef\hebyod\hebvav\hebresh}}%
  \def\tablename{\@ensure@R{\hebtet\hebbet\heblamed\hebhe}}%
  \def\partname{\@ensure@R{\hebhet\heblamed\hebqof}}%
  \def\enclname{\@ensure@R{\hebresh\hebtsadi"\hebbet}}%
  \def\ccname{\@ensure@R{\hebhe\hebayin\hebtav\hebqof\hebyod%
    \hebfinalmem}}%
  \def\headtoname{\@ensure@R{\hebalef\heblamed}}%
  \def\pagename{\@ensure@R{\hebayin\hebmem\hebvav\hebdalet}}%
  \def\psname{\@ensure@R{\hebnun.\hebbet.}}%
  \def\seename{\@ensure@R{\hebresh\hebalef\hebhe}}%
  \def\alsoname{\@ensure@R{\hebresh\hebalef\hebhe \hebgimel%
    \hebmemesof}}%
  \def\proofname{\@ensure@R{\hebhe\hebvav\hebkaf\hebhet\hebhe}}
  \def\glossaryname{\@ensure@L{Glossary}}% <-- Needs translation
}
%    \end{macrocode}
% \end{macro}
%  \begin{macro}{\slidelabel}
%    Here we fix the macro |slidelabel| of the seminar package. Note 
%    that this still won't work well enough when overlays will be 
%    involved
%    \begin{macrocode}
\@ifclassloaded{seminar}{%
  \def\slidelabel{\bf \if@rl\R{\hebshin\hebqof\hebfinalpe{} \theslide}%
                      \else\L{Slide \theslide}%
                      \fi}%
}{}
%    \end{macrocode}
% \end{macro}
%
%    Here we provide an user with translation of Gregorian dates
%    to Hebrew. In addition, the \pkg{hebcal} package can be used
%    to create Hebrew calendar dates.
%
%  \begin{macro}{\hebmonth}
%    The macro |\hebmonth{|\emph{month}|}| produces month names in
%    Hebrew.
%    \begin{macrocode}
\def\hebmonth#1{%
  \ifcase#1\or \hebyod\hebnun\hebvav\hebalef\hebresh\or %
     \hebpe\hebbet\hebresh\hebvav\hebalef\hebresh\or %
     \hebmem\hebresh\hebfinaltsadi\or %
     \hebalef\hebpe\hebresh\hebyod\heblamed\or %
     \hebmem\hebalef\hebyod\or \hebyod\hebvav\hebnun\hebyod\or %
     \hebyod\hebvav\heblamed\hebyod\or %
     \hebalef\hebvav\hebgimel\hebvav\hebsamekh\hebtet\or %
     \hebsamekh\hebpe\hebtet\hebmem\hebbet\hebresh\or %
     \hebalef\hebvav\hebqof\hebtet\hebvav\hebbet\hebresh\or %
     \hebnun\hebvav\hebbet\hebmem\hebbet\hebresh\or %
     \hebdalet\hebtsadi\hebmem\hebbet\hebresh\fi}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\hebdate}
%    The macro |\hebdate{|\emph{day}|}{|\emph{month}|}{|\emph{year}|}|
%    translates a given Gregorian date to Hebrew.
%    \begin{macrocode}
\def\hebdate#1#2#3{%
  \beginR\beginL\number#1\endL\ \hebbet\hebmonth{#2}
         \beginL\number#3\endL\endR}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\hebday}
%    The macro |\hebday| will replace |\today| command when in Hebrew
%    mode.
%    \begin{macrocode}
\def\hebday{\hebdate{\day}{\month}{\year}}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\datehebrew}
%    The macro |\datehebrew| redefines the command |\today| to produce
%    Gregorian dates in Hebrew. It uses the macro |\hebday|.
%    \begin{macrocode}
\def\datehebrew{\let\today=\hebday}
%    \end{macrocode}
% \end{macro}
%
%    The macro |\extrashebrew| will perform all the extra definitions
%    needed for the Hebrew language. The macro |\noextrashebrew|
%    is used to cancel the actions of |\extrashebrew|.
%
% \begin{macro}{\extrashebrew}
%    We switch font encoding to Hebrew and direction to
%    right-to-left. We cannot use the regular language switching
%    commands (for example, |\sethebrew| and |\unsethebrew| or
%    |\selectlanguage{hebrew}|), when in restricted horizontal mode,
%    because it will result in \emph{unbalanced} |\beginR| or
%    |\beginL| primitives.
%    Instead, in \TeX 's restricted horizontal mode, the
%    |\L{|\emph{latin text}|}| and |\R{|\emph{hebrew text}|}|, or
%    |\embox{|\emph{latin text}|}| and |\hmbox{|\emph{hebrew text}|}|
%    should be used.
%
%    Hence, we use |\beginR| and |\beginL| switching commands only
%    when not in restricted horizontal mode.
%    \begin{macrocode}
\addto\extrashebrew{%
  \tohebrew%
  \ifhmode\ifinner\else\beginR\fi\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\noextrashebrew}
%    The macro |\noextrashebrew| is used to cancel the actions of
%    |\extrashebrew|. We switch back to the previous font encoding and
%    restore left-to-right direction.
%    \begin{macrocode}
\addto\noextrashebrew{%
  \fromhebrew%
  \ifhmode\ifinner\else\beginL\fi\fi}
%    \end{macrocode}
% \end{macro}
%
%    Generally, we can switch to- and from- Hebrew by means of
%    standard \babel -defined commands, for example,
%    \begin{quote}
%       |\selectlanguage{hebrew}|
%    \end{quote}
%    or
%    \begin{quote}
%       |\begin{otherlanguage}{hebrew}|\\
%       \hspace*{1.5em} some Hebrew text\\
%       |\end{otherlanguage}|
%    \end{quote}
%    Now we define two additional commands that offer the possibility
%    to switch to and from Hebrew language. These commands are
%    backward compatible with the previous versions of
%    \pkg{hebrew.sty}.
%
%  \begin{macro}{\sethebrew}
%  \begin{macro}{\unsethebrew}
%    The command |\sethebrew| will switch from the current font encoding
%    to the hebrew font encoding, and from the current direction of
%    text to the right-to-left mode. The command |\unsethebrew| switches
%    back.
%
%    Both commands use standard right-to-left switching macros
%    |\setrllanguage{|\emph{ r2l language name}|}| and
%    |\unsetrllanguage{|\emph{r2l language name}|}|, that
%    defined in the \file{rlbabel.def} file.
%    \begin{macrocode}
\def\sethebrew{\setrllanguage{hebrew}}
\def\unsethebrew{\unsetrllanguage{hebrew}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\hebrewtext}
%  \begin{macro}{\nohebrewtext}
%    The following two commands are \emph{obsolete} and work only
%    in \LaTeX 2.09 compatibility mode. They are synonyms of
%    |\sethebrew| and |\unsethebrew| defined above.
%    \begin{macrocode}
\if@compatibility
  \let\hebrewtext=\sethebrew
  \let\nohebrewtext=\unsethebrew
\fi
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\tohebrew}
%  \begin{macro}{\fromhebrew}
%    These two commands change only the current font encoding to- and
%    from- Hebrew encoding. Their implementation uses
%    |\@torl{|\emph{language name}|}| and |\@fromrl| macros defined in
%    \file{rlbabel.def} file. Both commands may be useful \emph{only}
%    for package and class writers, not for regular users.
%    \begin{macrocode}
\def\tohebrew{\@torl{hebrew}}%
\def\fromhebrew{\@fromrl}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\@hebrew}
%    Sometimes we need to preserve Hebrew mode without knowing in
%    which environment we are located now. For these cases, the
%    |\@hebrew{|\emph{hebrew text}|}| macro will be useful. Not that
%    this macro is similar to the |\@number| and |\@latin| macros
%    defined in \file{rlbabel.def} file.
%    \begin{macrocode}
\def\@@hebrew#1{\beginR{{\tohebrew#1}}\endR}
\def\@hebrew{\protect\@@hebrew}
%    \end{macrocode}
%  \end{macro}
%
%  \subsubsection{Hebrew numerals}
%
%    We provide commands to print numbers in the traditional
%    notation using Hebrew letters. We need commands that print 
%    a Hebrew number from a decimal input, as well as commands 
%    to print the value of a counter as a Hebrew number.  
%  \begin{macro}{\if@gim@apost}
%  \begin{macro}{\if@gim@final}
%    Hebrew numbers can be written in various styles: with or without
%    apostrophes, and with the letters kaf, mem, nun, pe, tsadi as either
%    final or initial forms when they are the last letters in the
%    sequence. We provide two flags to set the style options.
%    \begin{macrocode}
\newif\if@gim@apost  % whether we print apostrophes
\newif\if@gim@final  % whether we use final or initial letters
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{\hebrewnumeral}
%  \begin{macro}{\Hebrewnumeral}
%  \begin{macro}{\Hebrewnumeralfinal}
%    The commands that print a Hebrew number 
%    must specify the style locally: relying on a global style
%    option could cause a counter to
%    print in an inconsistent manner---for instance, page numbers
%    might appear in different styles if the global style option
%    changed mid-way through a document.
%    The commands only allow three of the four possible flag
%    combinations (I do not know of a use that requires the
%    combination of final letters and no apostrophes --RA).
%
%    Each command sets the style flags and calls |\@hebrew@numeral|.
%    Double braces are used in order to protect the values of
%    |\@tempcnta| and |\@tempcntb|, which are changed by this call;
%    they also keep the flag assignments local (this is not important
%    because the global values are never used).
%    \begin{macrocode}
\newcommand*{\hebrewnumeral}[1]      % no apostrophe, no final letters
 {{\@gim@finalfalse\@gim@apostfalse\@hebrew@numeral{#1}}}
\newcommand*{\Hebrewnumeral}[1]      % apostrophe, no final letters
 {{\@gim@finalfalse\@gim@aposttrue\@hebrew@numeral{#1}}}
\newcommand*{\Hebrewnumeralfinal}[1] % apostrophe, final letters
 {{\@gim@finaltrue\@gim@aposttrue\@hebrew@numeral{#1}}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{\alph}
%  \begin{macro}{\@alph}
%  \begin{macro}{\Alph}
%  \begin{macro}{\@Alph}
%  \begin{macro}{\Alphfinal}
%  \begin{macro}{\@Alphfinal}
%    Counter-printing commands are based on the above commands. The
%    natural name for the counter-printing commands is |\alph|, because
%    Hebrew numerals are the only way to represent numbers with
%    Hebrew letters (kaf always means~20, never~11). Hebrew has no
%    uppercase letters, hence no need for the familiar meaning of |\Alph|;
%    we therefore define |\alph| to print counters as Hebrew numerals
%    without apostrophes, and |\Alph| to print with apostrophes. A third
%    form, |\Alphfinal|, is provided to print with apostrophes and final
%    letters, as is required for Hebrew year designators. The commands
%    |\alph| and |\Alph| are defined in \pkg{latex.ltx}, and we only
%    need to redefine the internal commands |\@alph| and 
%    |\@Alph|; for |\Alphfinal| we need to provide both a wrapper and
%    an internal command. 
%    The counter printing commands are made semi-robust: without the
%    |\protect|, commands like |\theenumii| break (I'm not quite clear
%    on why this happens, --RA); at the same time, we cannot make the 
%    commands too robust (e.g.~with |\DeclareRobustCommand|) because
%    this would enter the command name rather than its value into
%    files like |.aux|, |.toc| etc\@.
%    The old meanings of meaning of |\@alph| and |\@Alph| are saved
%    upon entering Hebrew mode and restored upon exiting it.
%    \begin{macrocode}
\addto\extrashebrew{%
  \let\saved@alph=\@alph%
  \let\saved@Alph=\@Alph%
  \renewcommand*{\@alph}[1]{\protect\hebrewnumeral{\number#1}}%
  \renewcommand*{\@Alph}[1]{\protect\Hebrewnumeral{\number#1}}%
  \def\Alphfinal#1{\expandafter\@Alphfinal\csname c@#1\endcsname}%
  \providecommand*{\@Alphfinal}[1]{\protect\Hebrewnumeralfinal{\number#1}}}
\addto\noextrashebrew{%
  \let\@alph=\saved@alph%
  \let\@Alph=\saved@Alph}
%    \end{macrocode}
%    Note that |\alph| (without apostrophes) is already the
%    appropriate choice for the second-level enumerate label, and
%    |\Alph| (with apostrophes) is an appropriate choice for appendix;
%    however, the default \LaTeX\ labels need to be redefined for
%    appropriate cross-referencing, see below.
%    \LaTeX\ default class files specify |\Alph| for
%    the fourth-level enumerate level, this should probably be changed.
%    Also, the way labels get flushed left by default looks inappropriate
%    for Hebrew numerals, so we should redefine |\labelenumii| as well
%    as |\labelenumiv| (presently not implemented).
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{\theenumii}
%  \begin{macro}{\theenumiv}
%  \begin{macro}{\label}
%    Cross-references to counter labels need to be printed according
%    to the language environment in which a label was issued, not
%    the environment in which it is called: for example, a label~(1b) 
%    issued in a Latin environment should be referred to as~(1b) in a
%    Hebrew text, and label~(2dalet) issued in a Hebrew environment
%    should be referred to as~(2dalet) in a Latin text. This was the
%    unanimous opinion in a poll sent to the Ivri\TeX\ list. 
%    We therefore redefine |\theenumii| and |\theenumiv|, so that an
%    explicit language instruction gets written to the |.aux| file.
%    \begin{macrocode}
\renewcommand{\theenumii}
   {\if@rl\protect\hebrewnumeral{\number\c@enumii}%
    \else\protect\L{\protect\@@alph{\number\c@enumii}}\fi}
\renewcommand{\theenumiv}
   {\if@rl\protect\Hebrewnumeral{\number\c@enumiv}%
    \else\protect\L{\protect\@@Alph{\number\c@enumiv}}\fi}
%    \end{macrocode}
%    We also need to control for the font and direction in which a
%    counter label is printed. Direction is straightforward: a Latin
%    label like~(1b) should be written left-to-right when called in a
%    Hebrew text, and a Hebrew label like~(2dalet) should be written
%    right-to-left when called in a Latin text. The font question is
%    more delicate, because we should decide whether the numerals
%    should be typeset in the font of the language enviroment in which
%    the label was issued, or that of the environment in which it is
%    called. 
%    \begin{itemize}
%     \item
%      A purely numeric label like~(23) looks best if it is set in the
%      font of the surrounding language.
%     \item
%      But a mixed alphanumeric label like~(1b) lookes weird if
%      the~`1' is taken from the Hebrew font; likewise, (2dalet) looks
%      weird if the~`2' is taken from a Latin font.
%     \item
%      Finally, mixing the two possibilities is worst, because a
%      single Hebrew sentence referring to examples~(1b) and~(2) would
%      take the~`1' from the Latin font and the~`2' from the Hebrew
%      font, and this looks really awful. (It is also very hard to
%      implement). 
%    \end{itemize}
%    In light of the conflicting considerations it seems like there's
%    no perfect solution. I have chosen to implement the top option,
%    where numerals are taken from the font of the surrounding
%    language, because it seems to me that reference to purely numeric
%    labels is the most common, so this gives a good solution to the
%    majority of cases and a mediocre solution to the minority.
%
%    We redefine the |\label| command which writes to the
%    |.aux| file. Depending on the language environment we issue
%    appropriate |\beginR/L|$\cdots$|\endR/L| commands to control the
%    direction without affecting the font. Since these commands do not
%    affect the value of |\if@rl|, we cannot use the macro
%    |\@brackets| to determine the correct brackets to be used with
%    |\p@enumiii|; instead, we let the language environment determine an
%    explicit definition.
%    \begin{macrocode}
\def\label#1{\@bsphack
  \if@rl
    \def\p@enumiii{\p@enumii)\theenumii(}%
    \protected@write\@auxout{}%
         {\string\newlabel{#1}{{\beginR\@currentlabel\endR}{\thepage}}}%
  \else
    \def\p@enumiii{\p@enumii(\theenumii)}%
    \protected@write\@auxout{}%
         {\string\newlabel{#1}{{\beginL\@currentlabel\endL}{\thepage}}}%
  \fi
  \@esphack}
%    \end{macrocode}
%    NOTE: it appears that the definition of |\label| is
%    language-independent and thus belongs in \pkg{rlbabel.def}, but
%    this is not the case. The decision to typeset label numerals
%    in the font of the surrounding language is reasonable for Hebrew,
%    because mixed-font (1b) and (2dalet) are somewhat acceptable. The
%    same may not be acceptable for Arabic, whose numeral glyphs are
%    radically different from those in the Latin fonts. The decision
%    about the direction may also be different for Arabic, which is
%    more right-to-left oriented than Hebrew (two examples: dates like
%    15/6/2003 are written left-to-right in Hebrew but right-to-left
%    in Arabic; equations like $1+2=3$ are written left-to-right in
%    Hebrew but right-to-left in Arabic elementary school textbooks
%    using Arabic numeral glyphs). My personal hunch is that a label
%    like~(1b) in an Arabic text would be typeset left-to-right if
%    the~`1' is a Western glyph, but right-to-left if the~`1' is an
%    Arabic glyph. But this is just a guess, I'd have to ask Arab
%    typesetters to find the correct answer. --RA.
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{\appendix}
%    The following code provides for the proper printing of appendix
%    numbers in tables of contents. Section and chapter headings are
%    normally bilingual: regardless of the text language, the author
%    supplies each section/chapter with two headings---one for the
%    Hebrew table of contents and one for the Latin table of contents.
%    It makes sense that the label should be a Latin letter in the
%    Latin table of contents and a Hebrew letter in the Hebrew table
%    of contents. The definition is similar to that of |\theenumii|
%    and |\theenumiv| above, but additional |\protect| commands ensure
%    that the entire condition is written the |.aux| file. The
%    appendix number will therefore be typeset according to the
%    environment in which it is used rather than issued: a Hebrew
%    number (with apostrophes) in a Hebrew environment and a Latin
%    capital letter in a Latin environment (the command 
%    |\@@Alph| is set in \pkg{rlbabel.def} to hold the default meaning
%    of \LaTeX\ [latin] |\@Alph|, regardless of the mode in which it is
%    issued). The net result is that
%    the second appendix will be marked with~`B' in the Latin table of
%    contents and with `bet' in the Hebrew table of contents; the mark
%    in the main text will depend on the language of the appendix itself.
%    \begin{macrocode}
\@ifclassloaded{letter}{}{%
\@ifclassloaded{slides}{}{%
  \let\@@appendix=\appendix%
  \@ifclassloaded{article}{%
    \renewcommand\appendix{\@@appendix%
      \renewcommand\thesection
        {\protect\if@rl\protect\Hebrewnumeral{\number\c@section}%
         \protect\else\@@Alph\c@section\protect\fi}}}
   {\renewcommand\appendix{\@@appendix%
      \renewcommand\thechapter
        {\protect\if@rl\protect\Hebrewnumeral{\number\c@chapter}%
         \protect\else\@@Alph\c@chapter\protect\fi}}}}}
%    \end{macrocode}
%    QUESTION: is this also the appropriate way to refer to an
%    appendix in the text, or should we retain the original label the
%    same way we did with |enumerate| labels? 
%    ANOTHER QUESTION: are similar redefinitions needed for other
%    counters that generate texts in bilingual lists like |.lof/.fol|
%    and |.lot/.tol|? --RA.
%  \end{macro}
%  \begin{macro}{\@hebrew@numeral}
%    The command |\@hebrew@numeral| prints a Hebrew number. The groups
%    of thousands, millions, billions are separated by apostrophes and
%    typeset without apostrophes or final letters; the remainder
%    (under 1000) is typeset conventionally, with the selected styles
%    for apostrophes and final letters. 
%    The function calls on |\gim@no@mil| to typeset each
%    three-digit block. The algorithm 
%    is recursive, but the maximum recursion depth is~4 because \TeX\
%    only allows numbers up to $2^{31}-1 = 2{,}147{,}483{,}647$.
%    The typesetting routine is wrapped in |\@hebrew| in order to
%    ensure that numbers are always typeset in Hebrew mode.
%
%    Initialize: |\@tempcnta| holds the value, |\@tempcntb| is used for
%    calculations.
%    \begin{macrocode}
\newcommand*{\@hebrew@numeral}[1]
{\@hebrew{\@tempcnta=#1\@tempcntb=#1\relax
 \divide\@tempcntb by 1000
%    \end{macrocode}
%    If we're under 1000, call |\gim@nomil|
%    \begin{macrocode}
 \ifnum\@tempcntb=0\gim@nomil\@tempcnta\relax
%    \end{macrocode}
%    If we're above 1000 then force no apostrophe and no final letter
%    styles for the value above~1000, recur for the value above~1000,
%    add an apostrophe, and call |\gim@nomil| for the remainder.
%    \begin{macrocode}
 \else{\@gim@apostfalse\@gim@finalfalse\@hebrew@numeral\@tempcntb}'%
      \multiply\@tempcntb by 1000\relax
      \advance\@tempcnta by -\@tempcntb\relax
      \gim@nomil\@tempcnta\relax
 \fi
}}
%    \end{macrocode}
%    NOTE: is it the case that 15,000 and 16,000 are written as
%    yod-he and yod-vav, rather than tet-vav and tet-zayin? This
%    vaguely rings a bell, but I'm not certain. If this is the case,
%    then the current behavior is incorrect and should be changed. --RA.
%  \end{macro}
%  \begin{macro}{\gim@nomil}
%    The command |\gim@nomil| typesets an integer between 0~and~999
%    (for~0 it typesets nothing). The code has been modified from the
%    old |hebcal.sty|
%    (appropriate credits---Boris Lavva and Michail Rozman ?).
%    |\@tempcnta| holds the total value that remains to be typeset.
%    At each stage we find the highest valued letter that is 
%    less than or equal to |\@tempcnta|, and call on |\gim@print| to
%    subtract this value and print the letter.
%
%    Initialize: |\@tempcnta| holds the value, there is no previous
%    letter. 
%    \begin{macrocode}
\newcommand*{\gim@nomil}[1]{\@tempcnta=#1\@gim@prevfalse
%    \end{macrocode}
% Find the hundreds digit.
%    \begin{macrocode}
  \@tempcntb=\@tempcnta\divide\@tempcntb by 100\relax % hundreds digit
  \ifcase\@tempcntb                     % print nothing if no hundreds
     \or\gim@print{100}{\hebqof}%
     \or\gim@print{200}{\hebresh}%
     \or\gim@print{300}{\hebshin}%
     \or\gim@print{400}{\hebtav}%
     \or\hebtav\@gim@prevtrue\gim@print{500}{\hebqof}%
     \or\hebtav\@gim@prevtrue\gim@print{600}{\hebresh}%
     \or\hebtav\@gim@prevtrue\gim@print{700}{\hebshin}%
     \or\hebtav\@gim@prevtrue\gim@print{800}{\hebtav}%
     \or\hebtav\@gim@prevtrue\hebtav\gim@print{900}{\hebqof}%
  \fi
%    \end{macrocode}
%    Find the tens digit. The numbers 15 and 16 are traditionally
%    printed as tet-vav ($9+6$) and tet-zayin ($9+7$) to avoid
%    spelling the Lord's name.
%    \begin{macrocode}
  \@tempcntb=\@tempcnta\divide\@tempcntb by 10\relax      % tens digit
  \ifcase\@tempcntb                         % print nothing if no tens
      \or                                   % number between 10 and 19
              \ifnum\@tempcnta = 16 \gim@print {9}{\hebtet}% tet-zayin
         \else\ifnum\@tempcnta = 15 \gim@print {9}{\hebtet}% tet-vav
         \else                      \gim@print{10}{\hebyod}%
              \fi % \@tempcnta = 15
              \fi % \@tempcnta = 16
%    \end{macrocode}
%    Initial or final forms are selected according to the current
%    style option; |\gim@print| will force a non-final letter in
%    non-final position by means of a local style change.
%    \begin{macrocode}
      \or\gim@print{20}{\if@gim@final\hebfinalkaf\else\hebkaf\fi}%
      \or\gim@print{30}{\heblamed}%
      \or\gim@print{40}{\if@gim@final\hebfinalmem\else\hebmem\fi}%
      \or\gim@print{50}{\if@gim@final\hebfinalnun\else\hebnun\fi}%
      \or\gim@print{60}{\hebsamekh}%
      \or\gim@print{70}{\hebayin}%
      \or\gim@print{80}{\if@gim@final\hebfinalpe\else\hebpe\fi}%
      \or\gim@print{90}{\if@gim@final\hebfinaltsadi\else\hebtsadi\fi}%
  \fi
%    \end{macrocode}
%    Print the ones digit.
%    \begin{macrocode}
  \ifcase\@tempcnta                         % print nothing if no ones
      \or\gim@print{1}{\hebalef}%
      \or\gim@print{2}{\hebbet}%
      \or\gim@print{3}{\hebgimel}%
      \or\gim@print{4}{\hebdalet}%
      \or\gim@print{5}{\hebhe}%
      \or\gim@print{6}{\hebvav}%
      \or\gim@print{7}{\hebzayin}%
      \or\gim@print{8}{\hebhet}%
      \or\gim@print{9}{\hebtet}%
  \fi
}
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\gim@print}
%  \begin{macro}{\if@gim@prev}
%    The actual printing routine typesets a digit with the appropriate
%    apostrophes: if a number sequence consists of a
%    single letter then it is followed by a single apostrophe, and if
%    it consists of more than one letter then a double
%    apostrophe is inserted before the last letter.
%    We typeset the letters one at a time, keeping a flag that tells
%    us if any previous letters had been typeset.
%    \begin{macrocode}
\newif\if@gim@prev % flag if a previous letter has been typeset
%    \end{macrocode}
%    For each letter, we
%    first subtract its value from the total. Then, 
%    \begin{itemize}
%     \item
%      if the result is zero then this is the last letter; we check
%      the flag to see if this is the only letter and print it with
%      the appropriate apostrophe;
%     \item
%      if the result is not zero then there remain additional letters
%      to be typeset; we print without an apostrophe and set the
%      `previous letter' flag. 
%    \end{itemize}
%    |\@tempcnta| holds the total value that remains to be typeset.
%    We first deduct the letter's value from |\@tempcnta|,
%    so |\@tempcnta| is zero if and only if this is the last letter.
%    \begin{macrocode}
\newcommand*{\gim@print}[2]{%   #2 is a letter, #1 is its value.
  \advance\@tempcnta by -#1\relax% deduct the value from the remainder
%    \end{macrocode}
%    If this is the last letter, we print with the appropriate
%    apostrophe (depending on the style option):
%    if there is a preceding letter, print |"x| if the style calls for
%    apostrophes, |x| if it doesn't;
%    otherwise, this is the only letter: print |x'| if the style calls
%    for apostrophes, |x| if it doesn't.
%    \begin{macrocode}
  \ifnum\@tempcnta=0% if this is the last letter
     \if@gim@prev\if@gim@apost"\fi#2%
     \else#2\if@gim@apost'\fi\fi%
%    \end{macrocode}
%    If this is not the last letter: print a non-final form (by
%    forcing a local style option) and set the `previous letter' flag.
%    \begin{macrocode}
  \else{\@gim@finalfalse#2}\@gim@prevtrue\fi}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\hebr}
%  \begin{macro}{\gim}
%    The older Hebrew counter commands |\hebr| and |\gim| are retained
%    in order to keep older documents from breaking. They are set to
%    be equivalent to |\alph|, and their use is deprecated. Note that
%    |\hebr| gives different results than it had in the past---it
%    now typesets 11 as yod-alef rather than kaf.
%    \begin{macrocode}
\let\hebr=\alph
\let\gim=\alph
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%    For backward compatibility with `older' \pkg{hebrew.sty}
%    packages, we define Hebrew equivalents of some useful \LaTeX\
%    commands. Note, however, that 8-bit macros defined in Hebrew
%    are no longer supported.
%    \begin{macrocode}
\def\hebcopy{\protect\R{\hebhe\hebayin\hebtav\hebqof}}
\def\hebincl{\protect\R{\hebresh\hebtsadi"\hebbet}}
\def\hebpage{\protect\R{\hebayin\hebmem\hebvav\hebdalet}}
\def\hebto{\protect\R{\hebayin\hebdalet}}
%    \end{macrocode}
%    |\hadgesh| produce ``poor man's bold'' (heavy printout), when
%    used with normal font glyphs. It is advisable to use bold font
%    (for example, \emph{Dead Sea}) instead of this macro.
%    \begin{macrocode}
\def\hadgesh#1{\leavevmode\setbox0=\hbox{#1}%
  \kern-.025em\copy0\kern-\wd0
  \kern.05em\copy0\kern-\wd0
  \kern-.025em\raise.0433em\box0 }
%    \end{macrocode}
%    |\piska| and |\piskapiska| sometimes used in `older' hebrew
%    sources, and should not be used in \LaTeXe.
%    \begin{macrocode}
\if@compatibility
  \def\piska#1{\item{#1}\hangindent=-\hangindent}
  \def\piskapiska#1{\itemitem{#1}\hangindent=-\hangindent}
\fi
%    \end{macrocode}
%    The following commands are simply synonyms for the standard ones,
%    provided with \LaTeXe.
%    \begin{macrocode}
\let\makafgadol=\textendash
\let\makafanak=\textemdash
\let\geresh=\textquoteright
\let\opengeresh=\textquoteright
\let\closegeresh=\textquoteleft
\let\openquote=\textquotedblright
\let\closequote=\textquotedblleft
\let\leftquotation=\textquotedblright
\let\rightquotation=\textquotedblleft
%    \end{macrocode}
%
%    We need to ensure that Hebrew is used as the default
%    right-to-left language at |\begin{document}|. The mechanism of
%    defining the |\@rllanguagename| is the same as in \babel 's
%    |\languagename|: the last right-to-left language in the
%    |\usepackage{babel}| line is set as the default right-to-left
%    language at document beginning.
%
%    For example, the following code:
%    \begin{quote}
%       |\usepackage[russian,hebrew,arabic,greek,english]{babel}|
%    \end{quote}
%    will set the Arabic language as the default right-to-left
%    language and the English language as the default language.
%    As a result, the commands |\L{}| and |\embox{}| will use English
%    and |\R{}| and |\hmbox{}| will use Arabic by default. These
%    defaults can be changed with the next |\sethebrew| or
%    |\selectlanguage{|\emph{language name}|}| command.
%    \begin{macrocode}
\AtBeginDocument{\def\@rllanguagename{hebrew}}
%    \end{macrocode}
%    
%    The macro |\ldf@finish| takes care of looking for a configuration
%    file, setting the main language to be switched on at
%    |\begin{document}| and resetting the category code of |@| to its
%    original value.
%    \begin{macrocode}
\ldf@finish{hebrew}
%</hebrew>
%    \end{macrocode}
%
% \subsection{Right to left support}
%
%    This file \pkg{rlbabel.def} defines necessary bidirectional macro
%    support for \LaTeXe. It is designed for use not only with Hebrew,
%    but with any Right-to-Left languages, supported by \babel. The
%    macros provided in this file are language and encoding
%    independent.
%
%    Right-to-left languages will use \TeX\ extensions, namely \TeX\
%    primitives |\beginL|, |\endL| and |\beginR|, |\endR|, currently
%    implemented only in $\varepsilon$-\TeX\ and in \TeX{-}{-}\XeT.
%
%    If $\varepsilon$-\TeX\ is used, we should switch it to the
%    \emph{enhanced} mode:
%    \begin{macrocode}
%<*rightleft>
\ifx\TeXXeTstate\undefined\else%
   \TeXXeTstate=1
\fi
%    \end{macrocode}
%
%    Note, that $\varepsilon$-\TeX 's format file should be created
%    for \emph{extended} mode. Mode can be checked by running
%    $\varepsilon$-\TeX\ on some \TeX{} file, for example:
%    \begin{quote}
%    |This is e-TeX, Version 3.14159-1.1 (Web2c 7.0)|\\
%    |entering extended mode|
%    \end{quote}
%    The second line should be \texttt{entering extended mode}.
%
%    We check if user uses Right-to-Left enabled engine instead of
%    regular Knuth's \TeX:
%    \begin{macrocode}
\ifx\beginL\@undefined%
   \newlinechar`\^^J
   \typeout{^^JTo avoid this error message,^^J%
     run TeX--XeT or e-TeX engine instead of regular TeX.^^J}
   \errmessage{Right-to-Left Support Error: use TeX--XeT or e-TeX
     engine}%
\fi
%    \end{macrocode}
%
% \subsubsection{Switching from LR to RL mode and back}
% 
%    \cs{@torl} and \cs{@fromrl} are called each time the horizontal
%    direction changes. They do all that is necessary besides changing
%    the direction. Currently their task is to change the encoding
%    information and mode (condition \cs{if@rl}). They should not
%    normally be called by users: user-level macros, such as
%    \cs{sethebrew} and \cs{unsethebrew}, as well as \babel 's
%    \cs{selectlanguage} are defined in language-definition files and
%    should be used to change default language (and direction). 
%    
%    Local direction changing commands (for small pieces of text):
%    |\L{}|, |\R{}|, |\embox{}| and |\hmbox{}| are defined below in
%    this file in language-independent manner.
%
% \begin{macro}{\if@rl}
%    \begin{description}\scrunch
%       \item[|\@rltrue|] means that the main mode is currently
%                          Right-to-Left.
%       \item[|\@rlfalse|] means that the main mode is currently
%                          Left-to-Right.
%    \end{description}
%    \begin{macrocode}
\newif\if@rl
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@rlmain}
%    This is the main direction of the document. Unlike |\if@rl|
%    it is set once and never changes.
%    \begin{description}\scrunch
%       \item[|\@rltrue|]  means that the document is Right-to-Left.
%       \item[|\@rlfalse|] means that the document is Left-to-Right.
%    \end{description}
%    Practically |\if@rlmain| is set according to the value of |\if@rl|
%    in the beginning of the run.
%    \begin{macrocode}
\AtBeginDocument{% Here we set the main document direction
  \newif\if@rlmain% 
  \if@rl% e.g: if the options to babel were [english,hebrew]
    \@rlmaintrue%
  \else%  e.g: if the options to babel were [hebrew,english]
    \@rlmainfalse%
  \fi%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@torl}
%    Switches current direction to Right-to-Left: saves current
%    Left-to-Right encoding in |\lr@encodingdefault|, sets required
%    Right-to-Left language name in |\@rllanguagename| (similar to
%    \babel 's |\languagename|) and changes derection.
%
%    The Right-to-Left language encoding should be defined in |.ldf|
%    file as special macro created by concatenation of the language
%    name and string \texttt{encoding}, for example, for Hebrew it
%    will be |\hebrewencoding|.
%    \begin{macrocode}
\DeclareRobustCommand{\@torl}[1]{%
  \if@rl\else%
     \let\lr@encodingdefault=\encodingdefault%
  \fi%
  \def\@rllanguagename{#1}%
  \def\encodingdefault{\csname#1encoding\endcsname}%
  \fontencoding{\encodingdefault}%
  \selectfont%
  \@rltrue}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@fromrl}
%    Opposite to |\@torl|, switches current direction to
%    Left-to-Right: restores saved Left-to-Right encoding
%    (|\lr@encodingdefault|) and changes direction.
%    \begin{macrocode}
\DeclareRobustCommand{\@fromrl}{%
  \if@rl%
     \let\encodingdefault=\lr@encodingdefault%
  \fi%
  \fontencoding{\encodingdefault}%
  \selectfont%
  \@rlfalse}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\selectlanguage}
%    This standard \babel 's macro should be redefined to support
%    bidirectional tables. We divide |\selectlanguage| implementation
%    to two parts, and the first part calls the second
%    |\@@selectlanguage|.
%    \begin{macrocode}
\expandafter\def\csname selectlanguage \endcsname#1{%
  \edef\languagename{%
    \ifnum\escapechar=\expandafter`\string#1\@empty
    \else \string#1\@empty\fi}%
  \@@selectlanguage{\languagename}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@selectlanguage}
%    This new internal macro redefines a final part of the standard
%    \babel 's |\select|\-|language| implementation.
%
%    Standard \LaTeX\ provides us with 3 tables: Table of Contents
%    (|.toc|), List of Figures (|.lof|), and List of Tables
%    (|.lot|). In multi-lingual texts mixing Left-to-Right languages
%    with Right-to-Left ones, the use of various directions in one
%    table results in very ugly output. Therefore, these 3 standard
%    tables will be used now only for Left-to-Right languages, and we
%    will add 3 Right-to-Left tables (their extensions are simply
%    reversed ones): RL Table of Contents (|.cot|), RL List of Figures
%    (|.fol|), and RL List of Tables (|.lof|).
%    \begin{macrocode}
\def\@@selectlanguage#1{%
  \select@language{#1}%
  \if@filesw
     \protected@write\@auxout{}{\string\select@language{#1}}%
     \if@rl%
       \addtocontents{cot}{\xstring\select@language{#1}}%
       \addtocontents{fol}{\xstring\select@language{#1}}%
       \addtocontents{tol}{\xstring\select@language{#1}}%     
     \else%
       \addtocontents{toc}{\xstring\select@language{#1}}%
       \addtocontents{lof}{\xstring\select@language{#1}}%
       \addtocontents{lot}{\xstring\select@language{#1}}%
     \fi%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setrllanguage}
% \begin{macro}{\unsetrllanguage}
%    The |\setrllanguage| and |\unsetrllanguage| pair of macros is
%    proved to very useful in bilingual texts, for example, in
%    Hebrew-English texts. The language-specific commands, for example,
%    |\sethebrew| and |\unsethebrew| use these macros as basis.
%    
%    Implementation saves and restores other language in
%    |\other@languagename| variable, and uses internal macro
%    |\@@selectlanguage|, defined above, to switch between languages.
%    \begin{macrocode}
\let\other@languagename=\languagename
\DeclareRobustCommand{\setrllanguage}[1]{%
   \if@rl\else%
     \let\other@languagename=\languagename%
   \fi%
     \def\languagename{#1}%
     \@@selectlanguage{\languagename}}
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareRobustCommand{\unsetrllanguage}[1]{%
   \if@rl%
     \let\languagename=\other@languagename%
   \fi
   \@@selectlanguage{\languagename}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\L}
% \begin{macro}{\R}
% \begin{macro}{\HeblatexRedefineL}
%    Macros for changing direction, originally taken from TUGboat.
%    Usage: |\L{|\emph{Left to Right text}|}| and |\R{|\emph{Right to
%    Left text}|}|. Numbers should also be enclosed in |\L{}|, as in
%    |\L{123}|.
%
%    Note, that these macros do not receive language name as
%    parameter. Instead, the saved |\@rllanguagename| will be
%    used. We assume that each Right-to-Left language defines
%    |\to|\emph{languagename} and |\from|\emph{languagename} macros in
%    language definition file, for example, for Hebrew: |\tohebrew|
%    and |\fromhebrew| macros in \pkg{hebrew.ldf} file.
%
%    The macros \cs{L} and \cs{R} include `protect' to to make them robust and
%    allow use, for example, in tables.
%
%    Due to the fact that some packages have different definitions for \cs{L}
%    the macro |\HeblatexRedefineL| is provided to overide them. This may
%    be required with hyperref, for instance.
%    \begin{macrocode}
\let\next=\
\def\HeblatexRedefineL{%
  \def\L{\protect\pL}%
}
\HeblatexRedefineL
\def\pL{\protect\afterassignment\moreL \let\next= }
\def\moreL{\bracetext \aftergroup\endL \beginL\csname
  from\@rllanguagename\endcsname}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\R{\protect\pR}
\def\pR{\protect\afterassignment\moreR \let\next= }
\def\moreR{\bracetext \aftergroup\endR \beginR\csname
  to\@rllanguagename\endcsname}
\def\bracetext{\ifcat\next{\else\ifcat\next}\fi
  \errmessage{Missing left brace has been substituted}\fi \bgroup}
\everydisplay{\if@rl\aftergroup\beginR\fi }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@ensure@R}
% \begin{macro}{\@ensure@L}
%    Two small internal macros, a-la |\ensuremath|
%    \begin{macrocode}
\def\@ensure@R#1{\if@rl#1\else\R{#1}\fi}
\def\@ensure@L#1{\if@rl\L{#1}\else#1\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    Take care of Right-to-Left indentation in every paragraph.
%    Originally, \cs{noindent} was redefined for right-to-left by
%    Yaniv Bargury, then the implementation was rewritten by Alon Ziv
%    using an idea by Chris Rowley: \cs{noindent} now works
%    unmodified.
%    \begin{macrocode}
\def\rl@everypar{\if@rl{\setbox\z@\lastbox\beginR\usebox\z@}\fi}
\let\o@everypar=\everypar
\def\everypar#1{\o@everypar{\rl@everypar#1}}
%    \end{macrocode}
%
% \begin{macro}{\hmbox}
% \begin{macro}{\embox}
%    Useful vbox commands. All text in math formulas is best enclosed
%    in these: LR text in |\embox| and RL text in |\hmbox|. |\mbox{}|
%    is useless for both cases, since it typesets in Left-to-Right
%    even for Right-to-Left languages (additions by Yaniv Bargury). 
%    \begin{macrocode}
\newcommand{\hmbox}[1]{\mbox{\R{#1}}}
\newcommand{\embox}[1]{\mbox{\L{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@brackets}
%    When in Right-to-Left mode, brackets should be swapped. This
%    macro receives 3 parameters: left bracket, content, right
%    bracket. Brackets can be square brackets, braces, or
%    parentheses.
%    \begin{macrocode}
\def\@brackets#1#2#3{\protect\if@rl #3#2#1\protect\else
  #1#2#3\protect\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@number}
% \begin{macro}{\@latin}
%    \cs{@number} preserves numbers direction from Left to Right.
%    \cs{@latin} in addition switches current encoding to the latin.
%    \begin{macrocode}
\def\@@number#1{\ifmmode\else\beginL\fi#1\ifmmode\else\endL\fi}
\def\@@latin#1{\@@number{{\@fromrl#1}}}
\def\@number{\protect\@@number}
\def\@latin{\protect\@@latin}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Counters}
% 
%     To make counter references work in Right to Left text, we need
%     to surround their original definitions with an
%     |\@number{|\ldots|}| or |\@latin{|\ldots|}|. Note, that
%     language-specific counters, such as \cs{hebr} or \cs{gim} are
%     provided with language definition file.
%
%    We start with saving the original definitions:
%    \begin{macrocode}
\let\@@arabic=\@arabic
\let\@@roman=\@roman
\let\@@Roman=\@Roman
\let\@@alph=\@alph
\let\@@Alph=\@Alph
%    \end{macrocode}
%
% \begin{macro}{\@arabic}
% \begin{macro}{\@roman}
% \begin{macro}{\@Roman}
%    Arabic and roman numbers should be from Left to Right. In
%    addition, roman numerals, both lower- and upper-case should be in
%    latin encoding.
%    \begin{macrocode}
\def\@arabic#1{\@number{\@@arabic#1}}
\def\@roman#1{\@latin{\@@roman#1}}
\def\@Roman#1{\@latin{\@@Roman#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\arabicnorl}
% This macro preserves the original definition of |\arabic|
% (overrides the overriding of |\@arabic|)
%    \begin{macrocode}
\def\arabicnorl#1{\expandafter\@@arabic\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\make@lr}
%    In Right to Left documents all counters defined in the standard
%    document classes \emph{article}, \emph{report} and \emph{book}
%    provided with \LaTeXe, such as |\thesection|, |\thefigure|,
%    |\theequation| should be typed as numbers from left to right. To
%    ensure direction, we use the following
%    |\make@lr{|\emph{counter}|}| macro:
%    \begin{macrocode}
\def\make@lr#1{\begingroup
    \toks@=\expandafter{#1}%
    \edef\x{\endgroup
  \def\noexpand#1{\noexpand\@number{\the\toks@}}}%
  \x}
%    \end{macrocode}
%
%    \begin{macrocode}
\@ifclassloaded{letter}{}{%
  \@ifclassloaded{slides}{}{%
    \make@lr\thesection
    \make@lr\thesubsection
    \make@lr\thesubsubsection
    \make@lr\theparagraph
    \make@lr\thesubparagraph
    \make@lr\thefigure
    \make@lr\thetable
  }
  \make@lr\theequation
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Preserving logos}
% 
%    Preserve \TeX, \LaTeX\ and \LaTeXe\ logos.
% \begin{macro}{\TeX}
%    \begin{macrocode}
\let\@@TeX\TeX
\def\TeX{\@latin{\@@TeX}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\LaTeX}
%    \begin{macrocode}
\let\@@LaTeX\LaTeX
\def\LaTeX{\@latin{\@@LaTeX}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\LaTeXe}
%    \begin{macrocode}
\let\@@LaTeXe\LaTeXe
\def\LaTeXe{\@latin{\@@LaTeXe}}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{List environments}
%
%    List environments in Right-to-Left languages, are ticked and
%    indented from the right instead of from the left. All the
%    definitions that caused indentation are revised for Right-to-Left
%    languages. \LaTeX\ keeps track on the indentation with the
%    \cs{leftmargin} and \cs{rightmargin} values.
%
% \begin{macro}{list}
%    Thus we need to override the definition of the |\list| macro: when
%    in RTL mode, the right margins are the begining of the line.
%    \begin{macrocode}
\def\list#1#2{%
  \ifnum \@listdepth >5\relax
    \@toodeep
  \else
    \global\advance\@listdepth\@ne
  \fi
  \rightmargin\z@
  \listparindent\z@
  \itemindent\z@
  \csname @list\romannumeral\the\@listdepth\endcsname
  \def\@itemlabel{#1}%
  \let\makelabel\@mklab
  \@nmbrlistfalse
  #2\relax
  \@trivlist
  \parskip\parsep
  \parindent\listparindent
  \advance\linewidth -\rightmargin
  \advance\linewidth -\leftmargin
%    \end{macrocode}
%    The only change in the macro is the |\if@rl| case:
%    \begin{macrocode}
  \if@rl
    \advance\@totalleftmargin \rightmargin
  \else
    \advance\@totalleftmargin \leftmargin
  \fi
  \parshape \@ne \@totalleftmargin \linewidth
  \ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\labelenumii}
% \begin{macro}{\p@enumiii}
%    The \cs{labelenumii} and \cs{p@enumiii} commands use
%    \emph{parentheses}. They are revised to work Right-to-Left with
%    the help of \cs{@brackets} macro defined above.
%    \begin{macrocode}
\def\labelenumii{\@brackets(\theenumii)}
\def\p@enumiii{\p@enumii\@brackets(\theenumii)}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Tables of moving stuff}
%
%    Tables of moving arguments: table of contents (|toc|), list of
%    figures (|lof|) and list of tables (|lot|) are handles here. These
%    three default \LaTeX\ tables will be used now exclusively for
%    Left to Right stuff.
%
%    Three additional Right-to-Left tables: RL table of contents
%    (|cot|), RL list of figures (|fol|), and RL list of tables
%    (|tol|) are added.
%    These three tables will be used exclusively for Right to
%    Left stuff.
%
% \begin{macro}{\@tableofcontents}
% \begin{macro}{\@listoffigures}
% \begin{macro}{\@listoftables}
%    We define 3 new macros similar to the standard \LaTeX\ tables,
%    but with one parameter --- table file extension. These macros
%    will help us to define our additional tables below.
%    \begin{macrocode}
\@ifclassloaded{letter}{}{% other
\@ifclassloaded{slides}{}{% other
  \@ifclassloaded{article}{% article
    \newcommand\@tableofcontents[1]{%
      \section*{\contentsname\@mkboth%
        {\MakeUppercase\contentsname}%
        {\MakeUppercase\contentsname}}%
      \@starttoc{#1}}
    \newcommand\@listoffigures[1]{%
      \section*{\listfigurename\@mkboth%
        {\MakeUppercase\listfigurename}%
        {\MakeUppercase\listfigurename}}%
      \@starttoc{#1}}
    \newcommand\@listoftables[1]{%
      \section*{\listtablename\@mkboth%
        {\MakeUppercase\listtablename}%
        {\MakeUppercase\listtablename}}%
      \@starttoc{#1}}}%
  {% else report or book
    \newcommand\@tableofcontents[1]{%
      \@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn%
      \fi\chapter*{\contentsname\@mkboth%
        {\MakeUppercase\contentsname}%
        {\MakeUppercase\contentsname}}%
      \@starttoc{#1}\if@restonecol\twocolumn\fi}
    \newcommand\@listoffigures[1]{%
      \@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn%
      \fi\chapter*{\listfigurename\@mkboth%
        {\MakeUppercase\listfigurename}%
        {\MakeUppercase\listfigurename}}%
      \@starttoc{#1}\if@restonecol\twocolumn\fi}
    \newcommand\@listoftables[1]{%
      \if@twocolumn\@restonecoltrue\onecolumn\else\@restonecolfalse\fi%
      \chapter*{\listtablename\@mkboth%      
        {\MakeUppercase\listtablename}%
        {\MakeUppercase\listtablename}}%
      \@starttoc{#1}\if@restonecol\twocolumn\fi}}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\lrtableofcontents}
% \begin{macro}{\lrlistoffigures}
% \begin{macro}{\lrlistoftables}
%    Left-to-Right tables are called now |\lr|\emph{xxx} and defined
%    with the aid of three macros defined above (extensions |toc|,
%    |lof|, and |lot|).
%    \begin{macrocode}
  \newcommand\lrtableofcontents{\@tableofcontents{toc}}%
  \newcommand\lrlistoffigures{\@listoffigures{lof}}%
  \newcommand\lrlistoftables{\@listoftables{lot}}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\rltableofcontents}
% \begin{macro}{\rllistoffigures}
% \begin{macro}{\rllistoftables}
%    Right-to-Left tables will be called |\rl|\emph{xxx} and defined
%    with the aid of three macros defined above (extensions |cot|,
%    |fol|, and |tol|).
%    \begin{macrocode}
  \newcommand\rltableofcontents{\@tableofcontents{cot}}%
  \newcommand\rllistoffigures{\@listoffigures{fol}}%
  \newcommand\rllistoftables{\@listoftables{tol}}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tableofcontents}
% \begin{macro}{\listoffigures}
% \begin{macro}{\listoftables}
%    Let |\|\emph{xxx} be |\rl|\emph{xxx} if the current direction is
%    Right-to-Left and |\lr|\emph{xxx} if it is Left-to-Right.
%    \begin{macrocode}
  \renewcommand\tableofcontents{\if@rl\rltableofcontents%
                                \else\lrtableofcontents\fi}
  \renewcommand\listoffigures{\if@rl\rllistoffigures%
                              \else\lrlistoffigures\fi}
  \renewcommand\listoftables{\if@rl\rllistoftables%
                             \else\lrlistoftables\fi}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@dottedtocline}
%    The following makes problems when making a Right-to-Left tables,
%    since it uses \cs{leftskip} and \cs{rightskip} which are both
%    mode dependent.
%    \begin{macrocode}
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\if@rl\rightskip\else\leftskip\fi #2\relax 
      \if@rl\leftskip\else\rightskip\fi \@tocrmarg \parfillskip
      -\if@rl\leftskip\else\rightskip\fi
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\if@rl\rightskip\else\leftskip\fi \@tempdima 
     \null\nobreak\hskip -\if@rl\rightskip\else\leftskip\fi
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor \beginL#5\endL}%
     \par}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@part}
%    This standard macro was redefined for table of contents since it
%    uses \cs{rightskip} which is mode dependent.
%    \begin{macrocode}
\@ifclassloaded{letter}{}{% other
\@ifclassloaded{slides}{}{% other
\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \begingroup
      \setlength\@tempdima{3em}%
      \parindent \z@ \if@rl\leftskip\else\rightskip\fi \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries #1\hfil \hb@xt@\@pnumwidth{\hss#2}}\par
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@part}
%    Part is redefined to support new Right-to-Left table of contents
%    (|cot|) as well as the Left-to-Right one (|toc|).
%    \begin{macrocode}
\@ifclassloaded{article}{% article class
  \def\@part[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
      \addcontentsline{cot}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
      \addcontentsline{cot}{part}{#1}%
    \fi
    {\parindent \z@ \raggedright
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >\m@ne
       \Large\bfseries \partname~\thepart
       \par\nobreak
     \fi
     \huge \bfseries #2%
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}%
}{% report and book classes
  \def\@part[#1]#2{%
    \ifnum \c@secnumdepth >-2\relax
      \refstepcounter{part}%
      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
      \addcontentsline{cot}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
      \addcontentsline{cot}{part}{#1}%
    \fi
    \markboth{}{}%
    {\centering
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname~\thepart
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
     \@endpart}}
%    \end{macrocode} 
% \end{macro}
%
% \begin{macro}{\@sect}
%    Section was redefined from the \pkg{latex.ltx} file. It is
%    changed to support both Left-to-Right (|toc|) and Right-to-Left
%    (|cot|) table of contents simultaneously.
%    \begin{macrocode}
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
    \addcontentsline{cot}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}%
      \addcontentsline{cot}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
%    \end{macrocode} 
% \end{macro}
%
% \begin{macro}{\@caption}
%    Caption was redefined from the \pkg{latex.ltx} file. It is
%    changed to support Left-to-Right list of figures and list of
%    tables (|lof| and |lot|) as well as new Right-to-Left lists 
%    (|fol| and |tol|) simultaneously.
%    \begin{macrocode}
\long\def\@caption#1[#2]#3{%
  \par
  \addcontentsline{\csname ext@#1\endcsname}{#1}%
    {\protect\numberline{\csname the#1\endcsname}%
    {\ignorespaces #2}}%
  \def\@fignm{figure}
  \ifx#1\@fignm\addcontentsline{fol}{#1}%
     {\protect\numberline{\csname the#1\endcsname}%
     {\ignorespaces #2}}\fi%
  \def\@tblnm{table}
  \ifx#1\@tblnm\addcontentsline{tol}{#1}%
     {\protect\numberline{\csname the#1\endcsname}%
     {\ignorespaces #2}}\fi%
  \begingroup
    \@parboxrestore
    \if@minipage
      \@setminipage
    \fi
    \normalsize
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}
%    \end{macrocode} 
% \end{macro}
%
% \begin{macro}{\l@chapter}
%    This standard macro was redefined for table of contents since it
%    uses \cs{rightskip} which is mode dependent.
%    \begin{macrocode}
\@ifclassloaded{letter}{}{%
\@ifclassloaded{slides}{}{%
  \@ifclassloaded{article}{}{%
    \renewcommand*\l@chapter[2]{%
      \ifnum \c@tocdepth >\m@ne
      \addpenalty{-\@highpenalty}%
      \vskip 1.0em \@plus\p@
      \setlength\@tempdima{1.5em}%
      \begingroup
         \parindent \z@ \if@rl\leftskip\else\rightskip\fi \@pnumwidth
         \parfillskip -\@pnumwidth
         \leavevmode \bfseries
         \advance\if@rl\rightskip\else\leftskip\fi\@tempdima
         \hskip -\if@rl\rightskip\else\leftskip\fi
         #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss#2}\par
         \penalty\@highpenalty
      \endgroup
      \fi}}}}
%    \end{macrocode} 
% \end{macro}
%
% \begin{macro}{\l@section}
% \begin{macro}{\l@subsection}
% \begin{macro}{\l@subsubsection}
% \begin{macro}{\l@paragraph}
% \begin{macro}{\l@subparagraph}
%    The toc entry for section did not work in article style.
%    Also it does not print dots, which is funny when most of your
%    work is divided into sections.
%
%    It was revised to use |\@dottedtocline| as in \pkg{report.sty}
%    (by Yaniv Bargury) and was updated later for all kinds of
%    sections (by Boris Lavva).
%    \begin{macrocode}
\@ifclassloaded{article}{%
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\renewcommand*\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}
\renewcommand*\l@subsubsection{\@dottedtocline{3}{7.0em}{4.1em}}
\renewcommand*\l@paragraph{\@dottedtocline{4}{10em}{5em}}
\renewcommand*\l@subparagraph{\@dottedtocline{5}{12em}{6em}}}{}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Two-column mode}
%
%    This is the support of \texttt{twocolumn} option for the standard
%    \LaTeXe\ classes.
%    The following code was originally borrowed from the Arab\TeX\
%    package, file \pkg{latexext.sty}, copyright by Klaus Lagally,
%    Institut fuer Informatik, Universitaet Stuttgart. It was updated
%    for this package by Boris Lavva.
%
% \begin{macro}{\@outputdblcol}
% \begin{macro}{\set@outputdblcol}
% \begin{macro}{rl@outputdblcol}
%    First column is \cs{@leftcolumn} will be shown at the right side,
%    Second column is \cs{@outputbox} will be shown at the left side. 
%    
%    |\set@outputdblcol| IS CURRENTLY DISABLED. TODO: REMOVE IT [tzafrir]
%    \begin{macrocode}
\let\@@outputdblcol\@outputdblcol
%\def\set@outputdblcol{%
%  \if@rl\renewcommand{\@outputdblcol}{\rl@outputdblcol}%
%  \else\renewcommand{\@outputdblcol}{\@@outputdblcol}\fi}
\renewcommand{\@outputdblcol}{%
  \if@rlmain%
    \rl@outputdblcol%
  \else%
    \@@outputdblcol%
  \fi%
}
\newcommand{\rl@outputdblcol}{%
  \if@firstcolumn
    \global \@firstcolumnfalse
    \global \setbox\@leftcolumn \box\@outputbox
  \else
    \global \@firstcolumntrue
    \setbox\@outputbox \vbox {\hb@xt@\textwidth {%
                              \hskip\columnwidth%
                              \hfil\vrule\@width\columnseprule\hfil
                              \hb@xt@\columnwidth {%
                                \box\@leftcolumn \hss}%
                              \hb@xt@\columnwidth {%
                                \hskip-\textwidth%
                                \box\@outputbox \hss}%
                              \hskip\columnsep%
                              \hskip\columnwidth}}%
    \@combinedblfloats
    \@outputpage
    \begingroup
      \@dblfloatplacement
      \@startdblcolumn
      \@whilesw\if@fcolmade \fi
        {\@outputpage
         \@startdblcolumn}%
    \endgroup
 \fi}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Footnotes}
%
% \begin{macro}{\footnoterule}
%    The Right-to-Left footnote rule is simply reversed default
%    Left-to-Right one. Footnotes can be used in RL or LR main 
%    modes, but changing mode while a footnote is pending is still 
%    unsolved.
%    \begin{macrocode}
\let\@@footnoterule=\footnoterule
\def\footnoterule{\if@rl\hb@xt@\hsize{\hss\vbox{\@@footnoterule}}%
                  \else\@@footnoterule\fi} 
%    \end{macrocode} 
% \end{macro}
%
% \subsubsection{Headings and two-side support}
%
%    When using \texttt{headings} or \texttt{myheadings} modes, we
%    have to ensure that the language and direction of heading is the
%    same as the whole chapter/part of the document. This is
%    implementing by setting special variable \cs{headlanguage} when
%    starting new chapter/part.
%
%    In addition, when selecting the \texttt{twoside} option (default in
%    \texttt{book} document class), the LR and RL modes need to be set
%    properly for things on the heading and footing. This is done
%    here too.
%
% \begin{macro}{ps@headings}
% \begin{macro}{ps@myheadings}
% \begin{macro}{headeven}
% \begin{macro}{headodd}
%    First, we will support the standard \pkg{letter} class:
%    \begin{macrocode}
\@ifclassloaded{letter}{%
  \def\headodd{\protect\if@rl\beginR\fi\headtoname{}
               \ignorespaces\toname
               \hfil \@date
               \hfil \pagename{} \thepage\protect\if@rl\endR\fi}
  \if@twoside
     \def\ps@headings{%
         \let\@oddfoot\@empty\let\@evenfoot\@empty
         \def\@oddhead{\select@language{\headlanguage}\headodd}
         \let\@evenhead\@oddhead}
  \else
     \def\ps@headings{%
         \let\@oddfoot\@empty
         \def\@oddhead{\select@language{\headlanguage}\headodd}}
  \fi  
  \def\headfirst{\protect\if@rl\beginR\fi\fromlocation \hfill %
                 \telephonenum\protect\if@rl\endR\fi}
  \def\ps@firstpage{%
     \let\@oddhead\@empty
     \def\@oddfoot{\raisebox{-45\p@}[\z@]{%
        \hb@xt@\textwidth{\hspace*{100\p@}%
          \ifcase \@ptsize\relax
             \normalsize
          \or
             \small
          \or
             \footnotesize
          \fi
        \select@language{\headlanguage}\headfirst}}\hss}}
%
  \renewcommand{\opening}[1]{%
     \let\headlanguage=\languagename%
     \ifx\@empty\fromaddress%
        \thispagestyle{firstpage}%
        {\raggedleft\@date\par}%
     \else  % home address
        \thispagestyle{empty}%
        {\raggedleft
        \if@rl\begin{tabular}{@{\beginR\csname%
          to\@rllanguagename\endcsname}r@{\endR}}\ignorespaces
           \fromaddress \\*[2\parskip]%
           \@date \end{tabular}\par%
        \else\begin{tabular}{l}\ignorespaces
           \fromaddress \\*[2\parskip]%
           \@date \end{tabular}\par%
        \fi}%
     \fi
     \vspace{2\parskip}%
     {\raggedright \toname \\ \toaddress \par}%
     \vspace{2\parskip}%
     #1\par\nobreak}
}
%    \end{macrocode}
%    Then, the \pkg{article}, \pkg{report} and \pkg{book} document
%    classes are supported. Note, that in one-sided mode
%    \cs{markright} was changed to \cs{markboth}.
%    \begin{macrocode}
{% article, report, book
  \def\headeven{\protect\if@rl\beginR\thepage\hfil\rightmark\endR
                \protect\else\thepage\hfil{\slshape\leftmark}
                \protect\fi}
  \def\headodd{\protect\if@rl\beginR\leftmark\hfil\thepage\endR
               \protect\else{\slshape\rightmark}\hfil\thepage
               \protect\fi}
  \@ifclassloaded{article}{% article
    \if@twoside   % two-sided
       \def\ps@headings{%
         \let\@oddfoot\@empty\let\@evenfoot\@empty
         \def\@evenhead{\select@language{\headlanguage}\headeven}%
         \def\@oddhead{\select@language{\headlanguage}\headodd}%
         \let\@mkboth\markboth
         \def\sectionmark##1{%
           \markboth {\MakeUppercase{%
               \ifnum \c@secnumdepth >\z@
                  \thesection\quad
               \fi
               ##1}}{}}%
         \def\subsectionmark##1{%
           \markright{%
             \ifnum \c@secnumdepth >\@ne
                \thesubsection\quad
             \fi
        ##1}}}
    \else          % one-sided
       \def\ps@headings{%
         \let\@oddfoot\@empty
         \def\@oddhead{\headodd}%
         \let\@mkboth\markboth
         \def\sectionmark##1{%
           \markboth{\MakeUppercase{%
               \ifnum \c@secnumdepth >\m@ne
                  \thesection\quad
               \fi
               ##1}}{\MakeUppercase{%
               \ifnum \c@secnumdepth >\m@ne
                  \thesection\quad
               \fi
               ##1}}}}
    \fi
%
    \def\ps@myheadings{%
      \let\@oddfoot\@empty\let\@evenfoot\@empty
      \def\@evenhead{\select@language{\headlanguage}\headeven}%
      \def\@oddhead{\select@language{\headlanguage}\headodd}%
      \let\@mkboth\@gobbletwo
      \let\sectionmark\@gobble
      \let\subsectionmark\@gobble
  }}{% report and book
    \if@twoside  % two-sided
       \def\ps@headings{%
         \let\@oddfoot\@empty\let\@evenfoot\@empty
         \def\@evenhead{\select@language{\headlanguage}\headeven}
         \def\@oddhead{\select@language{\headlanguage}\headodd}
         \let\@mkboth\markboth
         \def\chaptermark##1{%
           \markboth{\MakeUppercase{%
               \ifnum \c@secnumdepth >\m@ne
                  \@chapapp\ \thechapter. \ %
               \fi
               ##1}}{}}%
         \def\sectionmark##1{%
           \markright {\MakeUppercase{%
               \ifnum \c@secnumdepth >\z@
                  \thesection. \ %
               \fi
               ##1}}}}
    \else  % one-sided
       \def\ps@headings{%
         \let\@oddfoot\@empty
         \def\@oddhead{\select@language{\headlanguage}\headodd}
         \let\@mkboth\markboth
         \def\chaptermark##1{%
           \markboth{\MakeUppercase{%
               \ifnum \c@secnumdepth >\m@ne
                  \@chapapp\ \thechapter. \ %
               \fi
               ##1}}{\MakeUppercase{%
               \ifnum \c@secnumdepth >\m@ne
                  \@chapapp\ \thechapter. \ %
               \fi
               ##1}}}}
    \fi
    \def\ps@myheadings{%
      \let\@oddfoot\@empty\let\@evenfoot\@empty
      \def\@evenhead{\select@language{\headlanguage}\headeven}%
      \def\@oddhead{\select@language{\headlanguage}\headodd}%
      \let\@mkboth\@gobbletwo
      \let\chaptermark\@gobble
      \let\sectionmark\@gobble
  }}}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    
%    \subsubsection{Postscript Porblems}
%    Any command that is implemented by PostScript directives, e.g
%    commands from the ps-tricks package, needs to be fixed, because the
%    PostScript directives are being interpeted after the document has been
%    converted by \TeX to visual Hebrew (DVI, PostScript and PDF have visual
%    Hebrew). 
%
%    For instance: Suppose you wrote in your document: 
%
%    |\textcolor{cyan}{some ltr text}| 
%
%    This would be interpeted by \TeX to something like:
%
%    |[postscript:make color cyan]some LTR text[postscript:make color black]|
%
%
%    However, with the bidirectionality support we get:
%
%    |\textcolor{cyan}{\hebalef\hebbet}|
%
%    Translated to: 
%
%    |[postscript:make color black]{bet}{alef}[postscript:make color cyan]|
%
%    While we want:
%
%    |[postscript:make color cyan]{bet}{alef}[postscript:make color black]|
%
%    The following code will probably work at least with code that stays in the
%    same line:
%    \begin{macro}{@textcolor}
% \begin{macrocode}
\AtBeginDocument{%
  %I assume that \@textcolor is only defined by the package color
  \ifx\@textcolor\@undefined\else% 
    % If that macro was defined before the beginning of the document,
    % that is: the package was loaded: redefine it with bidi support
    \def\@textcolor#1#2#3{% 
      \if@rl%
        \beginL\protect\leavevmode{\color#1{#2}\beginR#3\endR}\endL%
      \else%
        \protect\leavevmode{\color#1{#2}#3}%
      \fi%
    }%
  \fi%
}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\thetrueSlideCounter}
%    This macro probably needs to be overriden for when using |prosper|,
%    (waiting for feedback. Tzafrir)
%    \begin{macrocode}
\@ifclassloaded{prosper}{%
  \def\thetrueSlideCounter{\arabicnorl{trueSlideCounter}}
}{}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Miscellaneous internal \LaTeX\ macros}
%
% \begin{macro}{\raggedright}
% \begin{macro}{\raggedleft}
%    \cs{raggedright} was changed from \pkg{latex.ltx} file to support
%    Right-to-Left mode, because of the bug in its implementation.
%    \begin{macrocode}
\def\raggedright{%
  \let\\\@centercr
  \leftskip\z@skip\rightskip\@flushglue
  \parindent\z@\parfillskip\z@skip}
%    \end{macrocode} 
%    Swap meanings of \cs{raggedright} and \cs{raggedleft} in
%    Right-to-Left mode.
%    \begin{macrocode}
\let\@@raggedleft=\raggedleft
\let\@@raggedright=\raggedright
\renewcommand\raggedleft{\if@rl\@@raggedright%
                         \else\@@raggedleft\fi}
\renewcommand\raggedright{\if@rl\@@raggedleft%
                          \else\@@raggedright\fi}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
%
% \begin{macro}{\author}
%    \cs{author} is inserted with \texttt{tabular} environment, and
%    will be used in restricted horizontal mode. Therefore we have to
%    add explicit direction change command when in Right-to-Left
%    mode.
%    \begin{macrocode}
\let\@@author=\author
\renewcommand{\author}[1]{\@@author{\if@rl\beginR #1\endR\else #1\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\MakeUppercase}
% \begin{macro}{\MakeLowercase}
%    There are no uppercase and lowercase letters in most
%    Right-to-Left languages, therefore we should redefine
%    \cs{MakeUppercase} and \cs{MakeLowercase} \LaTeXe\ commands.
%    \begin{macrocode}
\let\@@MakeUppercase=\MakeUppercase
\def\MakeUppercase#1{\if@rl#1\else\@@MakeUppercase{#1}\fi}
\let\@@MakeLowercase=\MakeLowercase
\def\MakeLowercase#1{\if@rl#1\else\@@MakeLowercase{#1}\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\underline}
%    We should explicitly use \cs{L} and \cs{R} commands in
%    \cs{underline}d text.
%    \begin{macrocode}
\let\@@@underline=\underline
\def\underline#1{\@@@underline{\if@rl\R{#1}\else #1\fi}}
%    \end{macrocode}
% \end{macro}
%
%    \cs{undertext} was added for \LaTeX 2.09 compatibility mode.
%    \begin{macrocode}
\if@compatibility
   \let\undertext=\underline
\fi
%    \end{macrocode} 
%
% \begin{macro}{\@xnthm}
% \begin{macro}{\@opargbegintheorem}
%    The following has been inserted to correct the appearance of the
%    number in \cs{newtheorem} to reorder theorem number components. A
%    similar correction  in the definition of \cs{@opargbegintheorem}
%    was added too.
%    \begin{macrocode}
\def\@xnthm#1#2[#3]{%
  \expandafter\@ifdefinable\csname #1\endcsname
  {\@definecounter{#1}\@addtoreset{#1}{#3}%
    \expandafter\xdef\csname the#1\endcsname{\noexpand\@number
      {\expandafter\noexpand\csname the#3\endcsname \@thmcountersep
        \@thmcounter{#1}}}%
    \global\@namedef{#1}{\@thm{#1}{#2}}%
    \global\@namedef{end#1}{\@endtheorem}}}
%
\def\@opargbegintheorem#1#2#3{%
  \trivlist
      \item[\hskip \labelsep{\bfseries #1\ #2\ 
          \@brackets({#3})}]\itshape}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@chapter}
% \begin{macro}{\@schapter}
%    The following was added for pretty printing of the chapter
%    numbers, for supporting Right-to-Left tables (\texttt{cot},
%    \texttt{fol}, and \texttt{tol}), to save \cs{headlanguage}
%    for use in running headers, and to start two-column mode
%    depending on chapter's main language.
%    \begin{macrocode}
\@ifclassloaded{article}{}{%
  % For pretty priniting
  \def\@@chapapp{Chapter}
  \def\@@thechapter{\@@arabic\c@chapter}
  \def\@chapter[#1]#2{%
    \let\headlanguage=\languagename%
    %\set@outputdblcol%
    \ifnum \c@secnumdepth >\m@ne
       \refstepcounter{chapter}%
       \typeout{\@@chapapp\space\@@thechapter.}%
       \addcontentsline{toc}{chapter}%
       {\protect\numberline{\thechapter}#1}
       \addcontentsline{cot}{chapter}%
       {\protect\numberline{\thechapter}#1}
    \else
       \addcontentsline{toc}{chapter}{#1}%
       \addcontentsline{cot}{chapter}{#1}%
    \fi
    \chaptermark{#1}
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{fol}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \addtocontents{tol}{\protect\addvspace{10\p@}}%
    \if@twocolumn
       \@topnewpage[\@makechapterhead{#2}]%
    \else
       \@makechapterhead{#2}%
       \@afterheading
    \fi}
  %
  \def\@schapter#1{%
    \let\headlanguage=\languagename%
    %\set@outputdblcol%    
    \if@twocolumn
       \@topnewpage[\@makeschapterhead{#1}]%
    \else
       \@makeschapterhead{#1}%
       \@afterheading
    \fi}}
%    \end{macrocode} 
% \end{macro}
% \end{macro}
%
% \begin{macro}{\appendix}
%    Changed mainly for pretty printing of appendix numbers, and to
%    start two-column mode with the right language (if needed).
%    \begin{macrocode}
\@ifclassloaded{letter}{}{% other
\@ifclassloaded{slides}{}{% other
  \@ifclassloaded{article}{% article
    \renewcommand\appendix{\par
      \setcounter{section}{0}%
      \setcounter{subsection}{0}%
      \renewcommand\thesection{\@Alph\c@section}}
  }{% report and book
    \renewcommand\appendix{\par
      %\set@outputdblcol%
      \setcounter{chapter}{0}%
      \setcounter{section}{0}%
      \renewcommand\@chapapp{\appendixname}%
      % For pretty priniting
      \def\@@chapapp{Appendix}%
      \def\@@thechapter{\@@Alph\c@chapter}
      \renewcommand\thechapter{\@Alph\c@chapter}}}}}
%    \end{macrocode} 
% \end{macro}
%
% \subsubsection{Bibliography and citations}
%
% \begin{macro}{\@cite}
% \begin{macro}{\@biblabel}
% \begin{macro}{\@lbibitem}
%    Citations are produced by the macro
%    |\@cite{|\emph{LABEL}|}{|\emph{NOTE}|}|. Both the citation label
%    and the note is typeset in the current direction. We have to use
%    \cs{@brackets} macro in \cs{@cite} and \cs{@biblabel} macros. In
%    addition, when using \emph{alpha} or similar bibliography style,
%    the \cs{@lbibitem} is used and have to be update to support bot
%    Right-to-Left and Left-to-Right citations.
%
%    \begin{macrocode}
\def\@cite#1#2{\@brackets[{#1\if@tempswa , #2\fi}]}
\def\@biblabel#1{\@brackets[{#1}]}
\def\@lbibitem[#1]#2{\item[\@biblabel{#1}\hfill]\if@filesw
      {\let\protect\noexpand
       \immediate
       \if@rl\write\@auxout{\string\bibcite{#2}{\R{#1}}}%
       \else\write\@auxout{\string\bibcite{#2}{\L{#1}}}\fi%
      }\fi\ignorespaces}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{environment}{thebibliography}
%    Use \cs{rightmargin} instead of \cs{leftmargin} when in RL mode.
%    \begin{macrocode}
\@ifclassloaded{letter}{}{% other
\@ifclassloaded{slides}{}{% other
\@ifclassloaded{article}{%
  \renewenvironment{thebibliography}[1]
  {\section*{\refname\@mkboth%
      {\MakeUppercase\refname}%
      {\MakeUppercase\refname}}%
    \list{\@biblabel{\@arabic\c@enumiv}}%
    {\settowidth\labelwidth{\@biblabel{#1}}%
      \if@rl\leftmargin\else\rightmargin\fi\labelwidth
      \advance\if@rl\leftmargin\else\rightmargin\fi\labelsep
      \@openbib@code
      \usecounter{enumiv}%
      \let\p@enumiv\@empty
      \renewcommand\theenumiv{\@arabic\c@enumiv}}%
    \sloppy
    \clubpenalty4000
    \@clubpenalty \clubpenalty
    \widowpenalty4000%
    \sfcode`\.\@m}
  {\def\@noitemerr
    {\@latex@warning{Empty `thebibliography' environment}}%
     \endlist}}%
{\renewenvironment{thebibliography}[1]{%
    \chapter*{\bibname\@mkboth%
      {\MakeUppercase\bibname}%
      {\MakeUppercase\bibname}}%
    \list{\@biblabel{\@arabic\c@enumiv}}%
    {\settowidth\labelwidth{\@biblabel{#1}}%
      \if@rl\leftmargin\else\rightmargin\fi\labelwidth
      \advance\if@rl\leftmargin\else\rightmargin\fi\labelsep
      \@openbib@code
      \usecounter{enumiv}%
      \let\p@enumiv\@empty
      \renewcommand\theenumiv{\@arabic\c@enumiv}}%
    \sloppy
    \clubpenalty4000
    \@clubpenalty \clubpenalty
    \widowpenalty4000%
    \sfcode`\.\@m}
  {\def\@noitemerr
    {\@latex@warning{Empty `thebibliography' environment}}%
     \endlist}}}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\@verbatim}
%    All kinds of verbs (\cs{verb},\cs{verb*},\texttt{verbatim} and
%    \texttt{verbatim*}) now can be used in Right-to-Left mode. Errors
%    in latin mode solved too.
%    \begin{macrocode}
\def\@verbatim{%
  \let\do\@makeother \dospecials%
  \obeylines \verbatim@font \@noligs}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@makecaption}
%    Captions are set always centered. This allows us to use bilingual
%    captions, for example: |\caption{\R{RLtext} \\ \L{LRtext}}|,
%    which will be formatted as:
%    \begin{center}
%    Right to left caption here (RLtext) \\
%    Left to right caption here (LRtext)
%    \end{center}
%    See also \cs{bcaption} command below.
%    \begin{macrocode}
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip%
  \begin{center}%
    #1: #2%
  \end{center} \par%
  \vskip\belowcaptionskip}
%    \end{macrocode} 
% \end{macro}
%
% \subsubsection{Additional bidirectional commands}
%
%    \begin{itemize}
%    \item Section headings are typeset with the default global
%    direction.
%    \item Text in section headings in the reverse language \emph{do
%    not} have to be protected for the reflection command, as in:
%    |\protect\L{|\emph{Latin Text}|}|, because \cs{L} and \cs{R} are
%    robust now.
%    \item Table of contents, list of figures and list of tables
%    should be typeset with the \cs{tableofcontents},
%    \cs{listoffigures} and \cs{listoftables} commands respectively.
%    \item The above tables will be typeset in the main direction (and
%    language) in effect where the above commands are placed.
%    \item Only 2 tables of each kind are supported: one for
%    Right-to-Left and another for Left-to-Right directions.
%    \end{itemize}
%
%    How to include line to both tables? One has to use bidirectional
%    sectioning commands as following:
%    \begin{enumerate}
%    \item Use the |\b|\emph{xxx} version of the sectioning commands
%    in the text instead of the |\|\emph{xxx} version (\emph{xxx} is
%    one of: \texttt{part}, \texttt{chapter}, \texttt{section}, 
%    \texttt{subsection}, \texttt{subsubsection}, \texttt{caption}).
%    \item Syntax of the |\b|\emph{xxx} command is
%        |\b|\emph{xxx}|{|\emph{RL text}|}{|\emph{LR text}|}|.
%    Both arguments are typeset in proper direction by default (no
%    need to change direction for the text inside).
%    \item The section header inside the document will be typeset in
%    the global direction in effect at the time. i.e. The |{|\emph{RL
%    text}|}| will be typeset if Right-to-Left mode is in effect and
%    |{|\emph{LR text}|}| otherwise.
%    \end{enumerate}
%
% \begin{macro}{\bpart}
%    \begin{macrocode}
\newcommand{\bpart}[2]{\part{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bchapter}
%    \begin{macrocode}
\newcommand{\bchapter}[2]{\chapter{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bsection}
%    \begin{macrocode}
\newcommand{\bsection}[2]{\section{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bsubsection}
%    \begin{macrocode}
\newcommand{\bsubsection}[2]{\subsection{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bsubsubsection}
%    \begin{macrocode}
\newcommand{\bsubsubsection}[2]{\subsubsection{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bcaption}
%    \begin{macrocode}
\newcommand{\bcaption}[2]{%
  \caption[\protect\if@rl \R{#1}\protect\else \L{#2}\protect\fi]{%
    \if@rl\R{#1}\protect\\ \L{#2}
    \else\L{#2}\protect\\ \R{#1}\fi}}
%    \end{macrocode}
% \end{macro}
%
%    The following definition is a modified version of \cs{bchapter}, meant
%    as a bilingual twin for \cs{chapter*} and \cs{section*}
%    (added by Irina Abramovici).
%
% \begin{macro}{\bchapternn}
%    \begin{macrocode}
\newcommand{\bchapternn}[2]{\chapter*{\protect\if@rl% 
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bsectionnn}
%    \begin{macrocode}
\newcommand{\bsectionnn}[2]{\section*{\protect\if@rl%
    #1 \protect\else #2 \protect\fi}}
%    \end{macrocode}
% \end{macro}
%
%    Finally, at end of \babel\ package, the \cs{headlanguage} and
%    two-column mode will be initialized according to the current
%    language.
%    \begin{macrocode}
\AtEndOfPackage{\rlAtEndOfPackage}
%
\def\rlAtEndOfPackage{%
  \global\let\headlanguage=\languagename%\set@outputdblcol%
}
%</rightleft>
%    \end{macrocode}
%
% \subsection{Hebrew calendar}
%
%    The original version of the package \pkg{hebcal.sty}\footnote{The
%    following description of \pkg{hebcal} package is based on the
%    comments included with original source by the author, Michail
%    Rozman.} for \TeX\ and \LaTeX2.09, entitled ``\TeX{} \& \LaTeX{}
%    macros for computing Hebrew date from Gregorian one'' was created
%    by Michail Rozman, |misha@iop.tartu.ew.su|\footnote{Please direct
%    any comments, bug reports, questions, etc. about the package to
%    this address.}
%
%    \begin{tabular}{@{}lr@{}c@{}ll}
%    Released: &Tammuz 12, 5751&--&June 24, 1991   &\\
%    Corrected:&Shebat 10, 5752&--&January 15, 1992&by Rama Porrat\\
%    Corrected:&Adar II 5, 5752&--&March 10, 1992  &by Misha\\
%    Corrected:&Tebeth, 5756   &--&January 1996    &Dan Haran\\
%              &&&&(haran@math.tau.ac.il)
%    \end{tabular}
%
%    The package was adjusted for \babel{} and \LaTeXe{} by Boris
%    Lavva.
%
%    Changes to the printing routine (only) by Ron Artstein, June 1,
%    2003.
%
%    This package should be included \emph{after} the \pkg{babel} with
%    \pkg{hebrew} option, as following:
%    \begin{quote}
%       |\documentclass[|\ldots|]{|\ldots|}|\\
%       |\usepackage[hebrew,|\ldots|,|\emph{other languages}|,|
%                            \ldots|]{babel}|\\
%       |\usepackage{hebcal}|
%    \end{quote}
%
%    Two main user-level commands are provided by this package:
%
%    \DescribeMacro{\Hebrewtoday}
%    Computes today's Hebrew date and prints it. If we are presently
%    in Hebrew mode, the date will be printed in Hebrew, otherwise ---
%    in English (like Shebat 10, 5752).
%
%    \DescribeMacro{\Hebrewdate}
%    Computes the Hebrew date from the given Gregorian date and
%    prints it. If we are presently in Hebrew mode, the date will be
%    printed in Hebrew, otherwise --- in English (like Shebat 10,
%    5752). An example of usage is shown below:
%    \begin{quote}
%       |\newcount\hd \newcount\hm \newcount\hy|\\
%       |\hd=10 \hm=3 \hy=1992|\\
%       |\Hebrewdate{\hd}{\hm}{\hy}|
%    \end{quote}
%
%    \DescribeMacro{full}
%    The package option |full| sets the flag |\@full@hebrew@year|,
%    which causes years from the current millenium to be printed with
%    the thousands digit (he-tav-shin-samekh-gimel). Without this 
%    option, thousands are not printed for the current millenium.
%    NOTE: should this be a command option rather than a package
%    option? --RA.
%
% \subsubsection{Introduction}
%
%    The Hebrew calendar is inherently complicated: it is lunisolar --
%    each year starts close to the autumn equinox, but each month must
%    strictly start at a new moon.  Thus Hebrew calendar must be
%    harmonized simultaneously with both lunar and solar events. In
%    addition, for reasons of the religious practice, the year cannot
%    start on Sunday, Wednesday or Friday.
%
%    For the full description of Hebrew calendar and for the list of
%    references see:
%    \begin{quote}
%      Nachum Dershowitz and Edward M. Reingold,
%      \emph{``Calendarical Calculations''}, Software--Pract.Exper.,
%      vol. 20 (9), pp.899--928 (September 1990).
%    \end{quote}
%    |C| translation of |LISP| programs from the above article
%    available from Mr. Wayne Geiser, |geiser%pictel@uunet.uu.net|.
%
%    The 4\textsuperscript{th} distribution (July 1989) of hdate/hcal
%    (Hebrew calendar programs similar to UNIX date/cal) by Mr. Amos
%    Shapir, |amos@shum.huji.ac.il|, contains short and very clear
%    description of algorithms.
%
% \subsubsection{Registers, Commands, Formatting Macros}
%
%    The command |\Hebrewtoday| produces today's date for Hebrew
%    calendar. It is similar to the standard \LaTeXe{} command
%    |\today|. In addition three numerical registers |\Hebrewday|,
%    |\Hebrewmonth| and |\Hebrewyear| are set.
%    For setting this registers without producing of date string
%    command |\Hebrewsetreg| can be used.
%
%    The command 
%    |\Hebrewdate{|\emph{Gday}|}{|\emph{Gmonth}|}{|\emph{Gyear}|}|
%    produces Hebrew calendar date corresponding to Gregorian date 
%    |Gday.Gmonth.Gyear|. Three numerical registers |\Hebrewday|,
%    |\Hebrewmonth| and |\Hebrewyear| are set.
%
%    For converting arbitrary Gregorian date |Gday.Gmonth.Gyear|
%    to Hebrew date |Hday.Hmonth.Hyear| without producing date string
%    the command:
%    \begin{center}
%      |\HebrewFromGregorian{|\emph{Gday}|}{|\emph{Gmonth}|}{|%
%      \emph{Gyear}|}{|\emph{Hday}|}{|\emph{Hmonth}|}{|\emph{Hyear}|}|
%    \end{center}
%    can be used.
%
%    \begin{macrocode}
%<*calendar>
\newif\if@full@hebrew@year
\@full@hebrew@yearfalse
\DeclareOption{full}{\@full@hebrew@yeartrue}
\ProcessOptions
\newcount\Hebrewday  \newcount\Hebrewmonth   \newcount\Hebrewyear
%    \end{macrocode}
%
% \begin{macro}{\Hebrewdate}
%    Hebrew calendar date corresponding to Gregorian date
%    |Gday.Gmonth.Gyear|. If Hebrew (right-to-left) fonts \& macros
%    are not loaded, we have to use English format.
%    \begin{macrocode}
\def\Hebrewdate#1#2#3{%
    \HebrewFromGregorian{#1}{#2}{#3}
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \ifundefined{if@rl}% 
       \FormatForEnglish{\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \else%
       \FormatDate{\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Hebrewtoday}
%    Today's date in Hebrew calendar.
%    \begin{macrocode}
\def\Hebrewtoday{\Hebrewdate{\day}{\month}{\year}}
\let\hebrewtoday=\Hebrewtoday
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Hebrewsetreg}
%    Set registers: today's date in hebrew calendar.
%    \begin{macrocode}
\def\Hebrewsetreg{%
    \HebrewFromGregorian{\day}{\month}{\year}
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FormatDate}
%    Prints a Hebrew calendar date |Hebrewday.Hebrewmonth.Hebrewyear|.
%    \begin{macrocode}
\def\FormatDate#1#2#3{%
        \if@rl%
            \FormatForHebrew{#1}{#2}{#3}%
        \else%
            \FormatForEnglish{#1}{#2}{#3}
        \fi}
%    \end{macrocode}
% \end{macro}
%
%    To prepare another language version of Hebrew calendar commands,
%    one should change or add commands here.
%
%    We start with Hebrew language macros.
% \begin{macro}{\HebrewYearName}
%    Prints Hebrew year as a Hebrew number. Disambiguates strings by
%    adding lamed-pe-gimel to years of the first Jewish millenium and
%    to years divisible by 1000. Suppresses the thousands digit in the
%    current millenium unless the package option |full| is selected.
%    NOTE: should this be provided as a command option rather than a
%    package option? --RA.
%    \begin{macrocode}
\def\HebrewYearName#1{{%
   \@tempcnta=#1\divide\@tempcnta by 1000\multiply\@tempcnta by 1000
   \ifnum#1=\@tempcnta\relax % divisible by 1000: disambiguate
     \Hebrewnumeralfinal{#1}\ )\heblamed\hebpe"\hebgimel(%
   \else % not divisible by 1000
     \ifnum#1<1000\relax     % first millennium: disambiguate
       \Hebrewnumeralfinal{#1}\ )\heblamed\hebpe"\hebgimel(%
     \else 
       \ifnum#1<5000
         \Hebrewnumeralfinal{#1}%
       \else
         \ifnum#1<6000 % current millenium, print without thousands
           \@tempcnta=#1\relax
           \if@full@hebrew@year\else\advance\@tempcnta by -5000\fi
           \Hebrewnumeralfinal{\@tempcnta}%
         \else % #1>6000
           \Hebrewnumeralfinal{#1}%
         \fi
       \fi
     \fi
   \fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewMonthName}
%    The macro |\HebrewMonthName{|\emph{month}|}{|\emph{year}|}|
%    returns the name of month in the `year'.
%    \begin{macrocode}
\def\HebrewMonthName#1#2{%
    \ifnum #1 = 7 %
    \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap \hebalef\hebdalet\hebresh\ \hebbet'%
           \else \hebalef\hebdalet\hebresh%
        \fi%
    \else%
        \ifcase#1%
           % nothing for 0           
           \or\hebtav\hebshin\hebresh\hebyod%
           \or\hebhet\hebshin\hebvav\hebfinalnun%
           \or\hebkaf\hebsamekh\heblamed\hebvav%
           \or\hebtet\hebbet\hebtav%
           \or\hebshin\hebbet\hebtet%
           \or\hebalef\hebdalet\hebresh\ \hebalef'%
           \or\hebalef\hebdalet\hebresh\ \hebbet'%
           \or\hebnun\hebyod\hebsamekh\hebfinalnun%
           \or\hebalef\hebyod\hebyod\hebresh%
           \or\hebsamekh\hebyod\hebvav\hebfinalnun%
           \or\hebtav\hebmem\hebvav\hebzayin%
           \or\hebalef\hebbet%
           \or\hebalef\heblamed\hebvav\heblamed%
        \fi%
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewDayName}
%    Name of day in Hebrew letters (gimatria).
%    \begin{macrocode}
\def\HebrewDayName#1{\Hebrewnumeral{#1}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\FormatForHebrew}
%    The macro |\FormatForHebrew{|\emph{hday}|}{|\emph{hmonth}
%    |}{|\emph{hyear}|}| returns the formatted Hebrew date in Hebrew
%    language.
%    \begin{macrocode}
\def\FormatForHebrew#1#2#3{%
  \HebrewDayName{#1}~\hebbet\HebrewMonthName{#2}{#3},~%
  \HebrewYearName{#3}}
%    \end{macrocode}
% \end{macro}
%
%    We continue with two English language macros for Hebrew calendar.
% \begin{macro}{\HebrewMonthNameInEnglish}
%    The macro |\HebrewMonthNameInEnglish{|\emph{month}|}{|%
%    \emph{year}|}| is similar to |\Hebrew|\-|Month|\-|Name| described
%    above. It returns the name of month in the Hebrew `year' in
%    English. 
%    \begin{macrocode}
\def\HebrewMonthNameInEnglish#1#2{%
    \ifnum #1 = 7%
    \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap Adar II\else Adar\fi%
    \else%
        \ifcase #1%
            % nothing for 0
            \or Tishrei%
            \or Heshvan%
            \or Kislev%
            \or Tebeth%
            \or Shebat%
            \or Adar I%
            \or Adar II%
            \or Nisan%
            \or Iyar%
            \or Sivan%
            \or Tammuz%
            \or Av%
            \or Elul%
        \fi
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FormatForEnglish}
%    The macro |\FormatForEnglish{|\emph{hday}|}{|\emph{hmonth}
%    |}{|\emph{hyear}|}| is similar to |\Format|\-|For|\-|Hebrew|
%    macro described above and returns the formatted Hebrew date in
%    English.
%    \begin{macrocode}
\def\FormatForEnglish#1#2#3{%
    \HebrewMonthNameInEnglish{#2}{#3} \number#1,\ \number#3}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Auxiliary Macros}
%
%    \begin{macrocode}
\newcount\@common
%    \end{macrocode}
% \begin{macro}{\Remainder}
%    |\Remainder{|\emph{a}|}{|\emph{b}|}{|\emph{c}|}| calculates 
%    $c = a\%b == a-b\times\frac{a}{b}$
%    \begin{macrocode}
\def\Remainder#1#2#3{%
    #3 = #1%                   %  c = a
    \divide #3 by #2%          %  c = a/b
    \multiply #3 by -#2%       %  c = -b(a/b)
    \advance #3 by #1}%        %  c = a - b(a/b)
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\newif\if@Divisible
%    \end{macrocode}
% \begin{macro}{\CheckIfDivisible}
%    |\CheckIfDivisible{|\emph{a}|}{|\emph{b}|}| sets
%    |\@Divisibletrue| if $a\%b == 0$
%    \begin{macrocode}
\def\CheckIfDivisible#1#2{%
    {%
      \countdef\tmp = 0% \tmp == \count0 - temporary variable
      \Remainder{#1}{#2}{\tmp}%
      \ifnum \tmp = 0%
          \global\@Divisibletrue%
      \else%
          \global\@Divisiblefalse%
      \fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifundefined}
%    From the \TeX book, ex. 7.7: 
%    \begin{quote}
%       |\ifundefined{|\emph{command}|}<true text>\else<false text>\fi|
%    \end{quote}
%    \begin{macrocode}
\def\ifundefined#1{\expandafter\ifx\csname#1\endcsname\relax}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Gregorian Part}
%
%    \begin{macrocode}
\newif\if@GregorianLeap
%    \end{macrocode}
% \begin{macro}{\IfGregorianLeap}
%    Conditional which is true if Gregorian `year' is a leap year:
%    $((year\%4==0)\wedge(year\%100\neq 0))\vee(year\%400==0)$
%    \begin{macrocode}
\def\IfGregorianLeap#1{%
    \CheckIfDivisible{#1}{4}%
    \if@Divisible%
        \CheckIfDivisible{#1}{100}%
        \if@Divisible%
            \CheckIfDivisible{#1}{400}%
            \if@Divisible%
                \@GregorianLeaptrue%
            \else%
                \@GregorianLeapfalse%
            \fi%
        \else%
            \@GregorianLeaptrue%
        \fi%
    \else%
        \@GregorianLeapfalse%
    \fi%
    \if@GregorianLeap}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\GregorianDaysInPriorMonths}
%    The macro |\GregorianDaysInPriorMonths{|\emph{month}|}{|^^A
%    \emph{year}|}{|\emph{days}|}| calculates the number of days in
%    months prior to `month' in the `year'.
%    \begin{macrocode}
\def\GregorianDaysInPriorMonths#1#2#3{%
    {%
        #3 = \ifcase #1%
               0 \or%             % no month number 0
               0 \or%
              31 \or%
              59 \or%
              90 \or%
             120 \or%
             151 \or%
             181 \or%
             212 \or%
             243 \or%
             273 \or%
             304 \or%
             334%
        \fi%
        \IfGregorianLeap{#2}%
            \ifnum #1 > 2%        % if month after February
                \advance #3 by 1% % add leap day
            \fi%
        \fi%
        \global\@common = #3}%
    #3 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\GregorianDaysInPriorYears}
%    The macro |\GregorianDaysInPriorYears{|\emph{year}|}{|^^A
%    \emph{days}|}| calculates the number of days in years prior to
%    the `year'.
%    \begin{macrocode}
\def\GregorianDaysInPriorYears#1#2{%
     {%
         \countdef\tmpc = 4%      % \tmpc==\count4
         \countdef\tmpb = 2%      % \tmpb==\count2
         \tmpb = #1%              %
         \advance \tmpb by -1%    %
         \tmpc = \tmpb%           % \tmpc = \tmpb = year-1
         \multiply \tmpc by 365%  % Days in prior years =
         #2 = \tmpc%              % = 365*(year-1) ...
         \tmpc = \tmpb%           %
         \divide \tmpc by 4%      % \tmpc = (year-1)/4
         \advance #2 by \tmpc%    % ... plus Julian leap days ...
         \tmpc = \tmpb%           %
         \divide \tmpc by 100%    % \tmpc = (year-1)/100
         \advance #2 by -\tmpc%   % ... minus century years ...
         \tmpc = \tmpb%           %
         \divide \tmpc by 400%    % \tmpc = (year-1)/400
         \advance #2 by \tmpc%    % ... plus 4-century years.
         \global\@common = #2}%
    #2 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AbsoluteFromGregorian}
%    The macro |\AbsoluteFromGregorian{|\emph{day}|}{|\emph{month}^^A
%    |}{|\emph{year}|}{|\emph{absdate}|}| calculates the absolute date
%    (days since $01.01.0001$) from Gregorian date |day.month.year|.
%    \begin{macrocode}
\def\AbsoluteFromGregorian#1#2#3#4{%
    {%
        \countdef\tmpd = 0%       % \tmpd==\count0
        #4 = #1%                  % days so far this month
        \GregorianDaysInPriorMonths{#2}{#3}{\tmpd}%
        \advance #4 by \tmpd%     % add days in prior months
        \GregorianDaysInPriorYears{#3}{\tmpd}%
        \advance #4 by \tmpd%     % add days in prior years
        \global\@common = #4}%
    #4 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Hebrew Part}
%
%    \begin{macrocode}
\newif\if@HebrewLeap
%    \end{macrocode}
% \begin{macro}{\CheckLeapHebrewYear}
%    Set |\@HebrewLeaptrue| if Hebrew `year' is a leap year, i.e.\ if
%    $(1+7\times year)\%19 < 7$ then \emph{true} else \emph{false}
%    \begin{macrocode}
\def\CheckLeapHebrewYear#1{%
    {%
        \countdef\tmpa = 0%       % \tmpa==\count0
        \countdef\tmpb = 1%       % \tmpb==\count1
%
        \tmpa = #1%
        \multiply \tmpa by 7%
        \advance \tmpa by 1%
        \Remainder{\tmpa}{19}{\tmpb}%
        \ifnum \tmpb < 7%         % \tmpb = (7*year+1)%19
            \global\@HebrewLeaptrue%
        \else%
            \global\@HebrewLeapfalse%
        \fi}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewElapsedMonths}
%    The macro |\HebrewElapsedMonths{|\emph{year}|}{|\emph{months}|}|
%    determines the number of months elapsed from the Sunday prior to
%    the start of the Hebrew calendar to the mean conjunction of
%    Tishri of Hebrew `year'.
%    \begin{macrocode}
\def\HebrewElapsedMonths#1#2{%
    {%
        \countdef\tmpa = 0%       % \tmpa==\count0
        \countdef\tmpb = 1%       % \tmpb==\count1
        \countdef\tmpc = 2%       % \tmpc==\count2
%
        \tmpa = #1%               %
        \advance \tmpa by -1%     %
        #2 = \tmpa%               % #2 = \tmpa = year-1
        \divide #2 by 19%         % Number of complete Meton cycles
        \multiply #2 by 235%      % #2 = 235*((year-1)/19)
%
        \Remainder{\tmpa}{19}{\tmpb}% \tmpa = years%19-years this cycle
        \tmpc = \tmpb%            %
        \multiply \tmpb by 12%    %
        \advance #2 by \tmpb%     % add regular months this cycle
%
        \multiply \tmpc by 7%     %
        \advance \tmpc by 1%      %
        \divide \tmpc by 19%      % \tmpc = (1+7*((year-1)%19))/19 -
%                                 %  number of leap months this cycle
        \advance #2 by \tmpc%     %  add leap months
%
        \global\@common = #2}%
    #2 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewElapsedDays}
%    The macro |\HebrewElapsedDays{|\emph{year}|}{|\emph{days}|}|
%    determines the number of days elapsed from the Sunday prior to
%    the start of the Hebrew calendar to the mean conjunction of
%    Tishri of Hebrew `year'.
%    \begin{macrocode}
\def\HebrewElapsedDays#1#2{%
    {%
        \countdef\tmpa = 0%       % \tmpa==\count0
        \countdef\tmpb = 1%       % \tmpb==\count1
        \countdef\tmpc = 2%       % \tmpc==\count2
%
        \HebrewElapsedMonths{#1}{#2}%
        \tmpa = #2%               %
        \multiply \tmpa by 13753% %
        \advance \tmpa by 5604%   % \tmpa=MonthsElapsed*13758 + 5604
        \Remainder{\tmpa}{25920}{\tmpc}% \tmpc == ConjunctionParts
        \divide \tmpa by 25920%
%
        \multiply #2 by 29%
        \advance #2 by 1%
        \advance #2 by \tmpa%     %  #2 = 1 + MonthsElapsed*29 +
%                                 %          PartsElapsed/25920
        \Remainder{#2}{7}{\tmpa}% %  \tmpa == DayOfWeek
        \ifnum \tmpc < 19440%
            \ifnum \tmpc < 9924%
            \else%                % New moon at 9 h. 204 p. or later
                \ifnum \tmpa = 2% % on Tuesday ...
                    \CheckLeapHebrewYear{#1}% of a common year
                    \if@HebrewLeap%
                    \else%
                        \advance #2 by 1%
                    \fi%
                \fi%
            \fi%
            \ifnum \tmpc < 16789%
            \else%                 % New moon at 15 h. 589 p. or later
                \ifnum \tmpa = 1%  % on Monday ...
                    \advance #1 by -1%
                    \CheckLeapHebrewYear{#1}% at the end of leap year
                    \if@HebrewLeap%
                        \advance #2 by 1%
                    \fi%
                \fi%
            \fi%
        \else%
            \advance #2 by 1%      %  new moon at or after midday
        \fi%
%
        \Remainder{#2}{7}{\tmpa}%  %  \tmpa == DayOfWeek
        \ifnum \tmpa = 0%          %  if Sunday ...
            \advance #2 by 1%
        \else%                     %
            \ifnum \tmpa = 3%      %  Wednesday ...
                \advance #2 by 1%
            \else%
                \ifnum \tmpa = 5%  %  or Friday
                     \advance #2 by 1%
                \fi%
            \fi%
        \fi%
        \global\@common = #2}%
    #2 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DaysInHebrewYear}
%    The macro |\DaysInHebrewYear{|\emph{year}|}{|\emph{days}|}|
%    calculates the number of days in Hebrew `year'.
%    \begin{macrocode}
\def\DaysInHebrewYear#1#2{%
    {%
        \countdef\tmpe = 12%   % \tmpe==\count12
%
        \HebrewElapsedDays{#1}{\tmpe}%
        \advance #1 by 1%
        \HebrewElapsedDays{#1}{#2}%
        \advance #2 by -\tmpe%
        \global\@common = #2}%
    #2 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewDaysInPriorMonths}
%    The macro |\HebrewDaysInPriorMonths{|\emph{month}|}{|^^A
%    \emph{year}|}{|\emph{days}|}| calculates the nu\-mber of days in
%    months prior to `month' in the `year'.
%    \begin{macrocode}
\def\HebrewDaysInPriorMonths#1#2#3{%
    {%
        \countdef\tmpf= 14%    % \tmpf==\count14
%
        #3 = \ifcase #1%       % Days in prior month of regular year
               0 \or%          % no month number 0
               0 \or%          % Tishri
              30 \or%          % Heshvan
              59 \or%          % Kislev
              89 \or%          % Tebeth
             118 \or%          % Shebat
             148 \or%          % Adar I
             148 \or%          % Adar II
             177 \or%          % Nisan
             207 \or%          % Iyar
             236 \or%          % Sivan
             266 \or%          % Tammuz
             295 \or%          % Av
             325 \or%          % Elul
             400%              % Dummy
        \fi%
        \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap%            % in leap year
            \ifnum #1 > 6%         % if month after Adar I
                \advance #3 by 30% % add  30 days
            \fi%
        \fi%
        \DaysInHebrewYear{#2}{\tmpf}%
        \ifnum #1 > 3%
            \ifnum \tmpf = 353%    %
                \advance #3 by -1% %
            \fi%                   %  Short Kislev
            \ifnum \tmpf = 383%    %
                \advance #3 by -1% %
            \fi%                   %
        \fi%
%
        \ifnum #1 > 2%
            \ifnum \tmpf = 355%    %
                \advance #3 by 1%  %
            \fi%                   %  Long Heshvan
            \ifnum \tmpf = 385%    %
                \advance #3 by 1%  %
            \fi%                   %
        \fi%
        \global\@common = #3}%
    #3 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AbsoluteFromHebrew}
%    The macro |\AbsoluteFromHebrew{|\emph{day}|}{|\emph{month}^^A
%    |}{|\emph{year}|}{|\emph{absdate}|}| calculates the absolute date
%    of Hebrew date |day.month.year|.
%    \begin{macrocode}
\def\AbsoluteFromHebrew#1#2#3#4{%
    {%
        #4 = #1%
        \HebrewDaysInPriorMonths{#2}{#3}{#1}%
        \advance #4 by #1%         % Add days in prior months this year
        \HebrewElapsedDays{#3}{#1}%
        \advance #4 by #1%         % Add days in prior years
        \advance #4 by -1373429%   % Subtract days before Gregorian
        \global\@common = #4}%     %   01.01.0001
    #4 = \@common}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\HebrewFromGregorian}
%    The macro |\HebrewFromGregorian{|\emph{Gday}|}{|\emph{Gmonth}^^A
%    |}{|\emph{Gyear}|}{|\emph{Hday}|}{|\emph{Hmonth}|}|\-|{|^^A
%    \emph{Hyear}|}| evaluates Hebrew date |Hday|, |Hmonth|, |Hyear|
%    from Gregorian date |Gday|, |Gmonth|, |Gyear|.
%    \begin{macrocode}
\def\HebrewFromGregorian#1#2#3#4#5#6{%
    {%
        \countdef\tmpx= 17%        % \tmpx==\count17
        \countdef\tmpy= 18%        % \tmpy==\count18
        \countdef\tmpz= 19%        % \tmpz==\count19
%
        #6 = #3%                   %
        \global\advance #6 by 3761%  approximation from above
        \AbsoluteFromGregorian{#1}{#2}{#3}{#4}%
        \tmpz = 1  \tmpy = 1%
        \AbsoluteFromHebrew{\tmpz}{\tmpy}{#6}{\tmpx}%
        \ifnum \tmpx > #4%              %
            \global\advance #6 by -1% Hyear = Gyear + 3760
            \AbsoluteFromHebrew{\tmpz}{\tmpy}{#6}{\tmpx}%
        \fi%                            %
        \advance #4 by -\tmpx%     % Days in this year
        \advance #4 by 1%          %
        #5 = #4%                   %
        \divide #5 by 30%          % Approximation for month from below
        \loop%                     % Search for month
            \HebrewDaysInPriorMonths{#5}{#6}{\tmpx}%
            \ifnum \tmpx < #4%
                \advance #5 by 1%
                \tmpy = \tmpx%
        \repeat%
        \global\advance #5 by -1%
        \global\advance #4 by -\tmpy}}
%</calendar>
%    \end{macrocode}
% \end{macro}
%
% \Finale
%%
%% \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         \~}
%%
\endinput