summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikzmark/tikzmark.dtx
blob: 6169f8176fcaeea769e73bdebf068204ad7683fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
tikzmark --- remembering absolute positioning with TikZ
E-mail: loopspace@mathforge.org
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

The tikzmark package defines a command to "remember" a position
on a page for later (or earlier) use, primarily (but not
exclusively) with TikZ.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
tikzmark --- remembering absolute positioning with TikZ.
E-mail: loopspace@mathforge.org
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2011-2021 by Andrew Stacey <loopspace@mathforge.org>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Andrew Stacey.

This work consists of the file  tikzmark.dtx
and the derived files           tikzmark.ins,
                                tikzmark.pdf,
                                tikzlibrarytikzmark.code.tex, and
                                tikzmarklibrarylistings.code.tex
                                tikzmarklibraryhighlighting.code.tex
                                tikzmarklibraryams.code.tex

\endpostamble
\usedir{tex/latex/tikzmark}
\generate{
  \file{tikzlibrarytikzmark.code.tex}{\from{\jobname.dtx}{tikzlibrary}}
}
\generate{
  \file{tikzmarklibrarylistings.code.tex}{\from{\jobname.dtx}{listings}}
}
\generate{
  \file{tikzmarklibraryhighlighting.code.tex}{\from{\jobname.dtx}{highlighting}}
}
\generate{
  \file{tikzmarklibraryams.code.tex}{\from{\jobname.dtx}{ams}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/tikzmark}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/demopkg}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
%\usepackage{morefloats}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{fit,arrows.meta,tikzmark,shadows,decorations.pathreplacing,calc}
\usetikzmarklibrary{ams,listings,highlighting}

%\usepackage[numbered]{hypdoc}
\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9} 
 
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
 \lstset{
  gobble=2,
  breakatwhitespace=true,
  breaklines=true,
  language=[LaTeX]TeX,
  basicstyle=\small\ttfamily,
  keepspaces=true,
  columns=fullflexible
}
\usepackage{fancyvrb}

\usepackage[hyperindex=false]{hyperref}
                                
\newenvironment{example}
  {\VerbatimEnvironment
   \begin{VerbatimOut}[gobble=2]{example.out}}
  {\end{VerbatimOut}
   \begin{center}
%   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{.9\linewidth}
     \lstinputlisting{example.out}
   \end{minipage}}

   \fbox{\begin{minipage}{.9\linewidth}
     \input{example.out}
   \end{minipage}}
\end{center}
}

\newenvironment{justexample}
  {\VerbatimEnvironment
   \begin{VerbatimOut}[gobble=2]{example.out}}
  {\end{VerbatimOut}
   \begin{center}
%   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{.9\linewidth}
     \lstinputlisting{example.out}
   \end{minipage}}
\end{center}
}

  \newcommand\balloon[4]{%
    \pgfmathtruncatemacro\firstline{%
      #3-1
    }%
    \iftikzmark{line-#2-\firstline-start}{%
      \iftikzmark{line-#2-#3-first}{%
        \xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic cs:line-#2-#3-first})}%
      }{%
        \iftikzmark{line-#2-#3-start}{%
          \xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic cs:line-#2-#3-start})}%
        }{%
          \xdef\blines{(pic cs:line-#2-\firstline-start)}%
        }%
      }%
    }{%
      \xdef\blines{}%
    }%
    \foreach \k in {#3,...,#4} {%
      \iftikzmark{line-#2-\k-first}{%
        \xdef\blines{\blines (pic cs:line-#2-\k-first) }
      }{}
      \iftikzmark{line-#2-\k-end}{%
        \xdef\blines{\blines (pic cs:line-#2-\k-end) }
      }{}
    }%
    \ifx\blines\empty
    \else
    \edef\temp{\noexpand\tikz[remember         picture,overlay]{\noexpand\node[fit={\blines},balloon] (#1) {}}}%
    \temp
    \fi
  }
  \tikzset{
    line/.style={
      draw,
      rounded corners=3pt,
      -latex
    },
    balloon/.style={
      draw,
      fill=blue!20,
      opacity=0.4,
      inner sep=4pt,
      rounded corners=2pt
    },
    comment/.style={
      draw,
      fill=blue!70,
      text=white,
      text width=3cm,
      minimum height=1cm,
      rounded corners,
      drop shadow,
      align=left,
      font=\scriptsize
    },
  }

\DisableCrossrefs
% \CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{1891}
%
% \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         \~}
%
%
% \changes{1.0}{2012/11/08}{Converted to DTX file}
% \changes{1.1}{2013/04/22}{Fix bug relating to active semi-colon}
% \changes{1.2}{2016/04/07}{Tikzmark works inside tikzpicture, added easy suffix and prefix for tikzmarks}
% \changes{1.3}{2017/06/01}{Tikzmark no longer uses semi-colon (too many issues with catcodes) and fix for beamer-awareness}
% \changes{1.4}{2017/10/29}{Bug fixes: spurious characters in aux file, tikzmark prefix/suffix ignored in iftikzmark test}
% \changes{1.5}{2018/09/09}{Reintroduced the original command (now as tikzmarknode) with a mathchoice hack for different math modes}
% \changes{1.6}{2018/10/18}{Added the ability to save node information between runs and between TeX documents}
% \changes{1.7}{2019/05/07}{Added conditions to test if a tikzmark is on a particular page}
% \changes{1.8}{2019/10/04}{Fixed some bugs with subnode and tikzmarknode inside maths}
% \changes{1.10}{2021/02/16}{Tikzmarknode is now prefix and suffix aware, and added a test to see if a picture id has been saved to the aux file for times when pictures are thrown away, eg in AMS's text command}
% \changes{1.11}{2021/08/16}{Tikzmark inside a tikzpicture was not applying any surrounding transformation before saving the coordinates.  Code for adjusting a pic's location based on an internal coordinate.}
% \changes{1.12}{2021/08/24}{Experimental library for creating pseudo-nodes around AMSMath equation alignment boxes.}
% \changes{1.14}{2022/01/28}{Reimplementing the highlighting code using LaTeX3 hooks.}
% \changes{1.15}{2022/08/24}{Improvements to highlighting code}
%
% \DoNotIndex{\newcommand,\newenvironment}
% \pdfstringdefDisableCommands{%
%  \def\\{}%
%  \def\url#1{<#1>}%
% }
%
% \GetFileInfo{tikzlibrarytikzmark.code.tex}
% \providecommand*{\url}{\texttt}
% \title{The \textsf{tikzmark} package}
% \author{Andrew Stacey \\ \url{loopspace@mathforge.org}}
% \date{\fileversion~from \filedate}
%
%
% \maketitle
%
% \section{Introduction}
%
% The \Verb+\tikzmark+ macro burst onto the scene in a blaze of glory on \href{http://tex.stackexchange.com}{TeX-SX}.
% Since then, it has proved embarrassingly (to its original author) popular.
% The idea behind it is extremely simple: that the machinery underneath TikZ provides a way to ``mark'' a point on a page for further use.
% This functionality is already provided by several other packages.
% The point of this one is that as TikZ can provide this feature, if already loading TikZ then it makes sense to use the TikZ version than another version.
% Moreover, if the goal is to use these marks with some TikZ code then this version is already set up for that purpose (not that it would be exactly difficult to add this to any of the other implementations).
%
% \section{Use}
%
% Using the \Verb+\tikzmark+ is extremely simple.
% You need to load the \Verb+tikz+ package and then load \Verb+tikzmark+ as a \Verb+tikzlibrary+.
% Thus in your preamble you should have something like:
%
% \begin{lstlisting}
% \usepackage{tikz}
% \usetikzlibrary{tikzmark}
% \end{lstlisting}
%
% In your document, you can now type \Verb+\tikzmark{<name>}+ at a point that you want to remember.
% This will save a mark with name \Verb+<name>+ for use later (or earlier).
% To use it in a \Verb+\tikz+ or \Verb+tikzpicture+, simply use the \Verb+pic+ coordinate system:
%
% \begin{lstlisting}
% \tikz[remember picture] \draw[overlay] (0,0) -- (pic cs:<name>);
% \end{lstlisting}
%
% There are two important points to note:
%
% \begin{enumerate}
% \item The enveloping \Verb+\tikz+ or \Verb+tikzpicture+ must have the key \Verb+remember picture+ set.
%
% This is because of how TikZ coordinates work.
% The coordinates inside a TikZ picture are relative to its origin, so that origin can move around on the page and not affect the internals of the picture.
% To use a point outside the picture, therefore, the current picture not only has to know where that point is on the page it also has to know where it itself is on the page.
% Hence the \Verb+remember picture+ key must be set.
% \item The drawing command must have the \Verb+overlay+ key set (or be in a scope or picture where it is set).
% 
% This is to keep the bounding box of the current picture under control.
% Otherwise, it would grow to encompass the remembered point as well as the current picture.
% (This isn't necessary if the remembered point is inside the current picture.)
% \end{enumerate}
%
% 
%
% \section{History}
%
% I wrote the original \Verb+\tikzmark+ macro in 2009 for use in lecture slides prepared with the \Verb+beamer+ package.
% Its original definition was:
%
% \begin{lstlisting}
% \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
% \end{lstlisting}
%
% Its first use was in the (inelegant) code:
%
% \begin{lstlisting}
% \begin{frame}
% \frametitle{Structure of Continuous Functions}
%
% \begin{tikzpicture}[overlay, remember picture]
% \useasboundingbox (0,0);
% \draw<2-|trans: 0|handout: 0>[red,->] (bsp) .. controls +(-1,-1) and ($(cnvs.north)+(1,1)$) .. ($(cnvs.north)+(0,1)$) .. controls ($(cnvs.north)+(-1,1)$) and +(-1,0) .. (cnvs.north);
% \draw<3-|trans: 0|handout: 0>[green!50!black,->] (cplt) .. controls +(-1,-1) and +(-1,0) .. (mcplt.north);
% \draw<4-|trans: 0|handout: 0>[blue,->] (norm) .. controls +(-1,-.5) and ($(nvs.north)+(0,1.5)$) .. ($(nvs.north)+(0,1.5)$) .. controls ($(nvs.north)+(-1.5,1.5)$) and +(-1.5,0) .. (nvs.north);
% \draw<5-|trans: 0|handout: 0>[purple,->] (vector) .. controls +(-1,-1) and ($(vsp.north)+(2,2)$) .. ($(vsp.north)+(0,2)$) .. controls ($(vsp.north)+(-2,2)$) and +(-2,0) .. (vsp.north);
% \end{tikzpicture}
%
% \begin{theorem}
% \centering
% \(\big(C([0,1],\R),d_\infty\big)\) \\
% is a \\
% \alert{Banach\tikzmark{bsp} space}
% \end{theorem}
%
% \pause
% \bigskip
%
% \begin{itemize}
% \item[\tikzmark{cnvs}] {\color<.(2)->{green!50!black}Comp\tikzmark{cplt}lete} {\color<.(3)->{blue}nor\tikzmark{norm}med} {\color<.(4)->{purple}vector\tikzmark{vector} space}.
%
% \bigskip
% \bigskip
% \pause
%
% \begin{itemize}[<+->]
% \item[\tikzmark{mcplt}] {\color{green!50!black}Cauchy sequences converge.}
% \medskip
% \item[\tikzmark{nvs}] {\color{blue}Metric from a norm.}
% \medskip
% \item[\tikzmark{vsp}] {\color{purple}Functions behave like vectors.}
% \end{itemize}
% \end{itemize}
%
% \end{frame}
% \end{lstlisting}
%
% This produced, on the final slide, Figure~\ref{fig:tikzmarkex}.
%
% \begin{figure}
% \centering
% \IfFileExists{tikzmarkExample.pdf}{\includegraphics{tikzmarkExample}}{}
% \caption{First use of tikzmark}
% \label{fig:tikzmarkex}
% \end{figure}
%
% Its first appearance on \href{http://tex.stackexchange.com}{TeX-SX} was in an \href{http://tex.stackexchange.com/a/316/86}{answer} to a question about how to put overlapping braces on a mathematical text.
% This was in July 2010.
% The opening statement of the answer was not overly encouraging: ``This may not be the best solution\dots''.
% And for a macro that would go on to become quite ubiquitous, its initial appearance only garnered it 2 votes.
%
% However, it started out in life as a useful macro for me and as such I found more uses for it in my own code and thus more opportunity for using it to answer questions on TeX-SX.
% The one that seems to have been where it got noticed came in \href{http://tex.stackexchange.com/a/1570/86}{August 2010}, again about putting braces in text but in a more complicated fashion.
% From this answer, it got picked up, picked over, and picked apart.
% A common use was in highlighting or adding marks to text.
%
% Gradually, as it got used, it developed.
% A major revision dates from an answer given in \href{http://tex.stackexchange.com/a/50054/86}{March 2012} where the question was actually about \Verb+\tikzmark+.
% This version added two important features: a TikZ coordinate system for referencing saved marks directly and the ability to refer to marks earlier in the document than they are defined (the mechanism for remembering points uses the \Verb+aux+ file anyway so this was more about exposing the information earlier than anything complicated).
% Then in October 2012 there was a  \href{http://tex.stackexchange.com/q/79121/86}{question} where it would have been useful to remember which page the mark was on and a \href{http://tex.stackexchange.com/q/79762/86}{question} where for some reason using the \Verb+\tikz+ macro didn't work so the \Verb+\pgfmark+ macro was introduced.
%
% By this point, the \Verb+\tikzmark+ command had morphed considerably from its original definition.
% Experience has shown that on the TeX-SX site it has continued to be used in its original form as well as its current form.
% I've therefore taken the decision to reintroduce a form of the original command, now called \Verb+\tikzmarknode+.
% It goes beyond the original version in that it uses some \Verb+\mathchoice+ trickery (inspired by \href{https://tex.stackexchange.com/a/122419/86}{this answer} from Heiko Oberdiek) to hopefully correctly choose the correct math style.
%
% The original reason for not using nodes inside \Verb+\tikzmark+ was to be able to use the information from a \Verb+\tikzmark+ before the point where it was defined (via information saved into the \Verb+aux+ file).
% Thanks to a \href{https://tex.stackexchange.com/a/415862/86}{question on TeX-SX} about saving node information, I've developed code that solves that issue with nodes.
% As it fits in the general concept of this package, I've added that code to the \Verb+\tikzmark+ package.
%
% \section{Usage}
%
% This package defines the following commands and usable stuff.
%
% \subsection{Core Commands}
%
% \begin{enumerate}
% \item \Verb+\tikzmark+\oarg{drawing command}\marg{name}
%
% The mandatory argument is the name of the mark to be used to refer back to this point later.
%
% The \Verb+\tikzmark+ command can take an optional parameter which is some drawing command that can be put in a \Verb+\tikz ... ;+ command.
% This drawing command can be used to place a node or something similar at the marked point, or to set some \Verb+\tikzset+ keys.
% Sometimes this can be useful.
% Note, though, that if this is used to define an offset coordinate then this will only be available in the document \emph{after} the \Verb+\tikzmark+ command, even on later runs.
%
% If the \Verb+beamer+ class is loaded then this command is made overlay-aware.
%
% \item \Verb+\tikzmark+\marg{name}\marg{coordinate}
%
% v1.2 of the \Verb+tikzmark+ package introduced a new variant of \Verb+\tikzmark+ which works inside a \Verb+tikzpicture+.
% One feature of \Verb+\tikzmark+ which isn't part of TikZ's normal coordinate remembering system is the ability to use a \Verb+\tikzmark+ coordinate before it is defined (due to the use of the \Verb+aux+ file).
% This is potentially useful to have inside a \Verb+tikzpicture+ and so it is now possible to use \Verb+\tikzmark+ inside a \Verb+tikzpicture+.
% The syntax is slightly different as we need to specify the coordinates of a point to remember.
%
% This was inspired by the question \href{http://tex.stackexchange.com/q/295903/86}{Refer to a node in tikz that will be defined ``in the future'' (two passes)?} on TeX-SX.
%
% \item \Verb+\pgfmark+\marg{name}
%
% This is a more basic form of the \Verb+\tikzmark+ which doesn't use any of the \Verb+\tikz+ overhead.
% One advantage of this command is that it doesn't create an \Verb+hbox+.
% It does, however, insert a \Verb+whatsit+ into the stream so it will, for example, stop two vertical spaces either side of it being merged.
% This can't be avoided.
% 
% If the \Verb+beamer+ class is loaded then this command is made overlay-aware.
%
% \item \Verb+\iftikzmark+\marg{name}\marg{true code}\marg{false code}
%
% This is a conditional to test if a particular mark is available.
% It executes \Verb+true code+ if it is and \Verb+false code+ if not.
%
% \item \Verb+\iftikzmarkexists+\marg{name}
%
% This is a conditional to test if a particular mark is available which works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
%
% \item \Verb+\iftikzmarkoncurrentpage+\marg{name}
%
% This is a conditional to test if a particular mark is on the current page; it works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
%
% \item \Verb+\iftikzmarkonpage+\marg{name}\marg{page}
%
% This is a conditional to test if a particular mark is on a given page; it works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
%
% \item \Verb+\tikzmarknode+\oarg{options}\marg{name}\marg{contents}
%
% This is a reincarnation of the original \Verb+\tikzmark+ command which places its contents inside a \Verb+\tikz+ node.  It also defines a \Verb+tikzmark+ with the same name.  Using a sneaky trick with \Verb+\mathchoice+, it works inside a math environment.  The spacing either side might not be quite right as although it detects the math style it doesn't got beyond that. The \Verb+options+ are passed to the node.
%
% Two styles are attempted, one on the surrounding picture and one on the node, which are:
%
% \begin{itemize}
% \item \Verb+every tikzmarknode picture+
% \item \Verb+every tikzmarknode+
% \end{itemize}
%
% To refer to the \emph{node}, use usual TikZ coordinates.
% To refer to the underlying \emph{tikzmark}, use the special tikzmark coordinates (see below).
%
% \item \Verb+(pic cs:<name>)+ or \Verb+(pic cs:<name>,<coordinate>)+
%
% This is the method for referring to a position remembered by \Verb+\tikzmark+ (or \Verb+\pgfmark+) as a coordinate in a \Verb+tikzpicture+ environment (or \Verb+\tikz+ command).
% If the extra \Verb+coordinate+ is specified then this is used in case the mark \Verb+name+ has not yet been defined (this can be useful for defining code that does something sensible on the first run).
%
% \item \Verb+/tikz/save picture id=<name>+
%
% This is the TikZ key that is used by \Verb+\tikzmark+ to actually save the connection between the name and the picture coordinate.
% It can be used on an arbitrary picture to save its origin as a tikzmark.
%
% \item \Verb+/tikz/check picture id+
%
% There are circumstances where, behind the scenes, a tikzpicture is actually placed in a box and processed several times (often this involves \Verb+\mathchoice+).
% In such a situation, when defining nodes then the last one ``wins'' in that each node remembers the id of the last processed picture.
% However, only the one that is actually used has its location remembered on the page (since the others don't have a position).
% This can lead to the situation whereby a node becomes disassociated from its picture and so using it for later reference fails.
% This key tries to get around that situation by checking the \Verb+aux+ file to see if the current picture was actually typeset last time (by checking for the presence of the remembered location) and if it finds that it wasn't, it quietly appends the string \Verb+discard-+ to each node name.
% The idea being that the version of the picture that is actually typeset will not have this happen and so its nodes ``survive''.
%
% \item \Verb+/tikz/maybe define node=#1+
%
% The previous key can lead to undefined nodes on the first time that the picture is processed.
% Using this key will ensure that the specified node is aliased to its \Verb+discard-+ version providing it doesn't already exist.
% This is purely to get rid of pointless error messages, and also should only be used in conjunction with \Verb+check picture id+.
%
% Note that due to the order in which code gets executed, \Verb+check picture id+ should be before any \Verb+maybe define node+ keys.
%
% \item \Verb+/tikz/if picture id=#1#2#3+
%
% This is a key equivalent of the \Verb+\iftikzmark+ command.
%
% \item \Verb+/tikz/if tikzmark on current page=#1#2#3+
%
% This is a key equivalent of the \Verb+\iftikzmarkoncurrentpage+ command.
% If true, the keys in \Verb+#2+ are executed, otherwise the keys in \Verb+#3+.
%
% \item \Verb+/tikz/if tikzmark on page=#1#2#3#4+
%
% This is a key equivalent of the \Verb+\iftikzmarkonpage+ command.
%
% \item \Verb+/tikz/next page+, \Verb+/tikz/next page vector+
%
% It is possible to refer to a mark on a different page to the current page.
% When this is done, the mark is offset by a vector stored in the key \Verb+/tikz/next page vector+.
% The key \Verb+/tikz/next page+ can be used to set this to certain standard vectors by specifying where the ``next page'' is considered as lying corresponding to the current page.
% Possible values are (by default) \Verb+above+, \Verb+below+, \Verb+left+, \Verb+right+, and \Verb+ignore+.
% (The last one sets the vector to the zero vector.)
%
% Previous versions of \Verb+tikzmark+ tried to make this work correctly with the mark being on, say, \(5\) pages further on but this got too fiddly so this version just pretends that the mark is on the next or previous page and points to it as appropriate.
%
% \item \Verb+/tikz/tikzmark prefix=<prefix>+  and \Verb+/tikz/tikzmark suffix=<suffix>+
%
% These keys allow for the automatic addition of a prefix and/or suffix to each \Verb+\tikzmark+ name.
% The prefix and suffix are added both at time of definition and of use, so providing one is in the same scope there is no difference in at the user level when using prefixes and suffixes.
% What it can be useful for is to make the \Verb+\tikzmark+ names unique.
% In particular, if the \Verb+beamer+ class is loaded then an automatic suffix is added corresponding to the overlay.
% This means that if a slide consists of several overlays with \Verb+\tikzmark+s on them, and the positions of the \Verb+\tikzmark+s move then the resulting pictures should look right. 
% Without the automatic suffix, only the final positions of the marks would be used throughout.
%
% This was inspired by the question \href{http://tex.stackexchange.com/q/302517/86}{using tikzmark subnode with overlays beamer} on TeX-SX.
%
% \end{enumerate}
%
% \subsection{Pic and Scope Positioning}
%
% \Verb+scope anchor+, \Verb+pic anchor+, and \Verb+surround pic+.
%
% These keys can be used to enable advanced positioning of \Verb+scope+s and \Verb+pic+s.
% The standard positioning of a \Verb+pic+ places its internal origin at the location specified on the \Verb!\pic! command.
% This is more limited than what is available to a \Verb+node+ whereby any of the defined anchors can be placed at the given position.
% The key \Verb+pic anchor+ allows a little more flexibility to \Verb+pic+ positioning by allowing a \Verb+pic anchor+ to be defined and used as the point to place at the given position.
%
% When invoking the \Verb!pic! the key \Verb!pic anchor={coordinate}! can be used to specify a point inside the \Verb!pic! to use as the anchor.
% This point is evaluated inside the \Verb!pic! so if using a node then the node name should be specified as if inside the \Verb!pic!.
%
% The node positioning syntax, things like \Verb!below! and \Verb!below=5pt of!, sets the anchor of the following node.
% Using \Verb!pic anchor! without a coordinate uses this anchor on the bounding box of the pic when positioning the pic.
%
% Internally, this works by adjusting the location of the \Verb!pic!'s surrounding scope.
% So the code can equally be used on \Verb!scope!s.
% For a \Verb!scope!, use the \Verb!scope anchor! version on the scope directly.
% The keys \Verb!name! and \Verb!anchor! can be used on the scope as if on a \Verb!node! with the same effect on the positioning.
%
% The key \Verb!surround pic! saves the bounding box of the pic as if it were the boundary of a rectangular node, using the name of the pic as the name of the node.
%
% This was inspired by the questions \href{https://tex.stackexchange.com/q/185279/86}{Anchoring TiKZ pics} and \href{https://tex.stackexchange.com/q/567245/86}{Reposition Tikz Scope After Size Known}.
%
% \subsection{Subnodes}
%
% \Verb+\subnode[options]{name}{content}+
%
% This produces a pseudo-node named \Verb+name+ around the \Verb+content+.
% The design purpose of this is to create a ``subnode'' inside a TikZ node.
% As far as TikZ is concerned, the contents of a node is just a box.
% It therefore does not know anything about it beyond its external size and so cannot easily determine the coordinates of pieces inside.
% The \Verb+\subnode+ command boxes its contents and saves the position of that box and its dimensions.
% This information is stored in the same way that PGF stores the necessary information about a node.
% It is therefore possible to use ordinary node syntax (within a \Verb+tikzpicture+) to access this information.
% Thus after \Verb+\node {a \subnode{a}{sub} node};+ it is possible to use \Verb+a+ as a node.
% The \Verb+options+ are passed to the node construction mechanism, but note that the only sensible options are those that affect the size and shape of the node: drawing options are ignored (except in so far as they affect the size -- as an example, \Verb+line width+ affects the node size).
%
% There are two important points to make about this.
% The first is that, as with all the \Verb+tikzmark+ macros, the information is always one compilation old.
% The second is that the pseudo-node is purely about coordinates: the  path information is not used and the contents are not moved.
% This is partly for reasons of implementation: the pseudo-node is constructed when TikZ is not in ``picture mode''.
% But also interleaving the background path of the pseudo-node and any containing node would be problematic and so is best left to the user.
%
% The simplest way to turn a pseudo-node into a more normal node is to use the \Verb+fit+ library.
% Using the above example, \Verb+\node[fit=(a),draw,inner sep=0pt] {};+ would draw a rectangle around the word \Verb+sub+ of exactly the same size as would appear had a normal node been created.
%
% Using a sneaky trick with \Verb+\mathchoice+, \Verb+subnode+ works inside a math environment.
% The spacing either side might not be quite right as although it detects the math style it doesn't got beyond that.
%
% Note that because of the way that this works, the outer \Verb!tikzpicture! must have the \Verb!remember picture! option set.
%
% \subsection{Node saving}
%
% The node saving system takes the information stored about a node and saves it for later use.
% That later use can be in the same document, in which case it should be saved just to the memory of the current TeX process, or it can be used earlier in the same document or another document altogether (in particular, if the nodes are defined in a \Verb+tikzpicture+ that has been externalised, this can be used to import the node information into the main file) in which cases the node data is saved to a file.
%
% When working with files, nodes are saved and restored in bulk.
% When working in memory, nodes are saved and restored in named lists.
% Nodes are not actually saved until the end of the tikzpicture in which they are defined, meaning that if saving to memory then all the nodes in a tikzpicture will belong to the same list.
%
% The keys for working with saving and restoring nodes are as follows.
%
% \begin{itemize}
% \item \Verb+save node+,  \Verb+save node=<name>+
%
% This is the key that indicates a node to be saved.
% The version with no argument is to be used directly in the keys for a node and it saves that node.
% With an argument then it saves a node that has been declared somewhere in the current tikz picture (it may not always be convenient to issue the \Verb+save node+ key directly on the node itself).
% Since the list is saved up to the end of the picture, this can be invoked before the node is defined.
%
% \item \Verb+\SaveNode[group name]{name}+
%
% This command is for outside a tikzpicture and saves the named node directly.
% The optional argument is a group name for saving to a group.
% If this is not specified then the node is saved to a file.
%
% \item \Verb+set node group=<name>+
%
% Nodes are grouped together into a list that can be saved either to a file or for use later on in the document.
% This sets the name for the current group.
%
% \item \Verb+restore nodes from list=<name>+
%
% This restores the node information from the named list to the current \Verb+tikzpicture+.
% This is required both for when the node information comes from a file or from earlier in the same document.
%
% \item \Verb+save nodes to file+
%
% This is a \Verb+true/false+ key which determines whether to save the node information to a file.
%
% \item \Verb+set saved nodes file name=<name>+
%
% This sets the file name for the saved nodes (the extension will be \Verb+.nodes+.
% The default is to use the current \TeX\ filename.
% This is set globally, and once the file is opened then changing the name will have no effect.
% (The file is not opened until it is actually needed to avoid creating empty files unnecessarily.)
%
% \item \Verb+restore nodes from file=<name>+
%
% This loads the node information from the file into the current document.
%
% The \Verb+<name>+ can have the syntax \Verb+[options]{name}+, where \Verb+options+ can be used to influence how the nodes are restored.
% The key \Verb+transform saved nodes+ (see below) can be given here.
% Another useful key is the \Verb+name prefix+ key which is applied to all restored nodes.
%
% \item \Verb+transform saved nodes+
%
% A particular use-case for restoring saved nodes is to safely include one \Verb+tikzpicture+ inside another by creating an image out of the inner picture and including it back in as a picture inside a node.
% In that situation, restoring the nodes from the inner picture can make it possible to refer to coordinates from the inner picture to the outer one.
% If there is a transformation in place on the containing node, this key applies that transformation to all the nodes in the inner picture.
%
% \end{itemize}
%
% \section{Examples}
%
% The \Verb+\tikzmark+ command has been used in numerous answers on \href{http://tex.stackexchange.com}{TeX-SX}.
%
% \subsection{Basic Examples}
%
% A simple example of the \Verb+\tikzmark+ macro is the following.
%
% \begin{example}
% \tikzset{tikzmark prefix=ex1-}
% \[
%  \tikzmark{a} e^{i \pi/2} = i
% \]
%
% This\tikz[remember picture,overlay,baseline=0pt] \draw[->] (0,1em) to[bend left] ([shift={(-1ex,1ex)}]pic cs:a); is an important equation.
% \end{example}
%
% \begin{example}
% \tikzset{tikzmark prefix=ex2-}
% \begin{itemize}
% \item A first item,\tikzmark{b}
% \item A second item,\tikzmark{c}
% \item A third item.\tikzmark{d}
% \end{itemize}
% \begin{tikzpicture}[remember picture,overlay]
% \draw[decorate,decoration={brace}] ({pic cs:c} |- {pic cs:b}) +(0,1em) -- node[right,inner sep=1em] {some items} ({pic cs:c} |- {pic cs:d});
% \end{tikzpicture}
% \end{example}
%
% \begin{example}
% \tikzset{tikzmark prefix=ex3-}
% \begin{tikzpicture}[remember picture]
% \node (a) at (0,0) {This has a \subnode{sub}{subnode} in it};
% \draw[->] (0,-1) to[bend right] (sub);
% \end{tikzpicture}
% \end{example}
%
% An example using \Verb+\tikzmark+ inside a \Verb+tikzpicture+
%
% \begin{example}
% \tikzset{tikzmark prefix=ex4-}
% \begin{tikzpicture}[remember picture,overlay]
% \draw[->,line width=1mm,cyan] (pic cs:a) to[bend left] (pic cs:b);
% \end{tikzpicture}
%
% By placing the \tikzmark{a}code before the marks, the arrow goes under the subsequent text and picture.
%
% \begin{tikzpicture}
% \filldraw[fill=gray] (0,0) circle[radius=1cm];
% \tikzmark{b}{(-1,-1)}
% \end{tikzpicture}
% \end{example}
%
% The \Verb+\tikmarknode+ puts a node around some text, which can be referred to later, and adds a \Verb+\tikzmark+ at its origin.
%
% \begin{example}
% \tikzset{tikzmark prefix=ex5-}
% Putting a node around \tikzmarknode{txt}{some text} means we can connect text together, including in maths:
% \[
% \tikzmarknode{a}{\sum_{k=1}^n} k^{\tikzmarknode{b}{2}}
% \]
%
% \begin{tikzpicture}[remember picture,overlay]
% \draw[->] (txt) -- (a);
% \draw[->] (a.south) to[out=-90,in=-45] (b.south east);
% \end{tikzpicture}
% \end{example}
%
% The syntax for saving node data is illustrated by the following example.
%
% File \Verb+firstpicture.tex+:
%
% \begin{justexample}
% \documentclass[tikz,border=10pt]{standalone}
% \usetikzlibrary{tikzmark,shapes.geometric}
% \begin{document}
% \begin{tikzpicture}[save nodes to file]
% \node[draw,rotate=-30,save node](1) at (-2,0) {1};
% \draw[->] (0,0) -- (1);
% \node[draw,ellipse,save node] (c) at (current bounding box.center) {};
% \end{tikzpicture}
% \end{document}
% \end{justexample}
%
% File \Verb+secondpicture.tex+:
%
% \begin{justexample}
% \documentclass[tikz,border=10pt]{standalone}
% \usetikzlibrary{tikzmark,shapes.geometric}
% \begin{document}
% \begin{tikzpicture}[save nodes to file]
% \node[draw,rotate=-70,save node] (2) at (2,0) {2};
% \draw[->] (0,0) -- (2);
% \node[draw,ellipse,save node] (c) at (current bounding box.center) {};
% \end{tikzpicture}
% \end{document}
% \end{justexample}
%
% Main file:
%
% \begin{justexample}
% \documentclass{article}
% \usepackage{tikz}
% \usetikzlibrary{tikzmark}
% 
% \begin{document}
% \begin{tikzpicture}
% 
% \node[draw,
%   rotate=30,
%   restore nodes from file={[transform saved nodes,name prefix=pic-1-]{firstpicture}}
% ] (a-1) at (-2,-3) {\includegraphics{firstpicture.pdf}};
% 
% \node[draw,
%   rotate=70,
%   restore nodes from file={[transform saved nodes,name prefix=pic-2-]{secondpicture}}
% ] (a-2) at (+2,+2) {\includegraphics{secondpicture.pdf}};
% 
% \draw[red] (pic-1-1.north west) --  (pic-1-1.north east) --  (pic-1-1.south east) --  (pic-1-1.south west) -- cycle;
% \draw[red] (pic-2-2.north west) --  (pic-2-2.north east) --  (pic-2-2.south east) --  (pic-2-2.south west) -- cycle;
% 
% \node[red] at (pic-1-1) {1};
% \node[red] at (pic-2-2) {2};
% 
% \draw (a-1) circle[radius=5pt];
% \draw (a-2) circle[radius=5pt];
% 
% \draw (pic-1-1) -- (pic-2-2);
% \end{tikzpicture}
% \end{document}
% \end{justexample}
%
% This produces:
%
% \begin{center}
% \fbox{\IfFileExists{tikzrefextnodes.pdf}{\includegraphics{tikzrefextnodes}}{}}
% \end{center}
%
% \section{Additional Libraries}
%
% Some of the more ambitious uses of \Verb!\tikzmark! involve a fair bit of extra code and so are worth gathering in to extra libraries of their own.
% These can be loaded via \Verb+\usetikzmarklibrary+.
%
% At present, there are three libraries: one for code listings which works with the \Verb+listings+ package, one for AMSMath equations, and one for highlighting.
%
% \subsection{Code Listings}
%
% If the \Verb+listings+ package has been loaded then issuing
%
% \Verb+\usetikzmarklibrary{listings}+
%
% \noindent will load in some code to add marks to \Verb+lstlisting+ environments.
% This code places a mark at three places on a line of code in a \Verb+listings+ environment.
% The marks are placed at the start of the line, the first non-whitespace character, and the end of the line (if the line is blank the latter two are not placed).
% (This has not been extensively tested, it works by adding code to various ``hooks'' that are made available by the \Verb+listings+ package; it is quite possible that the hooks chosen are both wrong and insufficient to cover all desired cases.)
%
%
% These are inspired by questions such as \href{http://tex.stackexchange.com/q/79762/86}{Marking lines in listings} and \href{http://tex.stackexchange.com/q/86309/86}{Macros for code annotations}.
%
% In more detail, the \Verb+listings+ library places lots of marks around the code.
% The marks are:
%
% \begin{itemize}
% \item \Verb+line-<name>-<number>-start+ at the start of each line.
% \item \Verb+line-<name>-<number>-end+ at the end of each line.
% \item \Verb+line-<name>-<number>-first+ at the first non-space character of the line (assuming it exists).
% \end{itemize}
%
% The line numbers \emph{should} match up with the line numbers in the code in that any initial offset is also applied.
%
% Not every mark is available on every line.
% If a line is blank, in particular, it will only have a \Verb+start+ mark.
% The following example shows this, where the red dots are the \Verb+start+, the blue are \Verb+end+, and the green are \Verb+first+.
%
% \begin{example}
% \tikzset{tikzmark prefix=ex6-}
% \begin{tikzpicture}[remember picture]
% \foreach \k in {0,...,7} {
% \iftikzmark{line-code-\k-start}{\fill[red,overlay] (pic cs:line-code-\k-start) circle[radius=4pt];}{\message{No start for \k}}
% \iftikzmark{line-code-\k-end}{\fill[blue,overlay] (pic cs:line-code-\k-end) circle[radius=2pt];}{\message{No end for \k}}
% \iftikzmark{line-code-\k-first}{\fill[green,overlay] (pic cs:line-code-\k-first) circle[radius=2pt];}{\message{No first for \k}}
% }
% \draw[->,overlay] (0,0) -- (pic cs:line-code-5-first);
% \draw[->,overlay] (0,0) -- (pic cs:line-code-5-start);
% \draw[->,overlay] (0,0) -- (pic cs:line-code-5-end);
% \node[above] at (0,0) {Line 5};
% \end{tikzpicture}
% 
% \begin{lstlisting}[language=c,name=code,numbers=left]
%   #include <stdio.h>
% 
%   int main(void)
%   {
%       printf("hello, world\n");
%       return 0;
%   }
% \end{lstlisting}
% \end{example}
%
% This example puts a fancy node behind certain lines of the code, computing the necessary extents.
%
% \begin{example}
% \balloon{comment}{more code}{3}{3}
% \balloon{comment}{more code}{7}{8}
% \begin{lstlisting}[language=c,name=more code,numbers=left,firstnumber=3]
%   #include <stdio.h>
%   
%   int main(void)
%   {
%       printf("hello, world\n");
%       return 0;
%   }
% \end{lstlisting}
% \end{example}
%
% \subsection{AMS Equation Environments}
%
% \textbf{This is an experimental library.}
%
% If the \Verb+amsmath+ package has been loaded then issuing
%
% \Verb+\usetikzmarklibrary{ams}+
%
% \noindent loads some code that places pseudo-nodes around the boxes that are used in AMSMath's various equation alignment environments, such as \Verb!align! and \Verb!gather!.
% These environments work by constructing boxes with each of the pieces of the equations that are then put together into the grid.
% This library hooks in to the unboxing code, before the box is typeset then it measures it and stores that information in various macros as if it were a TikZ node.
% The aim is that this doesn't disturb the placement, but as far as TikZ is concerned then there is a node there that can be referred to later.
%
% As it is experimental, even if this library is loaded then it isn't automatically switched on.
% To do that, use either the \Verb!tikzmarkmath! environment or the \Verb!\tikzmarkmath! command.
% Each has an optional argument which is a prefix for the node names (the default is \Verb!equation!).
% The node names are then of the form \Verb!<prefix>-<number>!.
% The numbering is held in a counter called \Verb!tikzmarkequation! and is reset when the command is invoked or the environment is started.
% As usual, redefining \Verb!\thetikzmarkequation! changes the styling of the \Verb!<number>!.
%
% To disable the marking, either end the environment or use \Verb!\endtikzmarkmath!.
% The ending command explicitly removes the hook rather than rely on \TeX\ groupings.
% It also prints out the number of nodes created to the log file and terminal.
% This can be useful with figuring out which nodes to use, since the box that this library hooks into is used many times.
% For example, equation numbers are included with this.
%
% The box is also used when assembling a \Verb!\sqrt[3]{4}! command, and as that uses \Verb!\mathchoice! then there are more boxes created than used.
% So the count of number of nodes created can be more than are actually there.
%
% \begin{example}
% \begin{tikzmarkmath}[pythagoras]
% 
% \begin{gather}
% a^2 = b^2 + c^2
% \end{gather}
% 
% \begin{gather}
% a = \sqrt[2]{b^2 + c^2}
% \end{gather}
% \end{tikzmarkmath}
% 
% \begin{tikzpicture}[remember picture, overlay]
% \foreach \k in {1,2,3,7,8} {
%   \draw[red]  (pic cs:pythagoras-\k) -- ++(135:1) node[draw,red,circle,font=\tiny,above left] {\k};
%   \node[draw,blue,fit=(pythagoras-\k),inner sep=0pt] {};
% }
% \end{tikzpicture}
% % \end{example}
%
%
% \subsection{Highlighting}
%
% I've returned to the highlighting library.
% The \LaTeX3 hook mechanism makes a couple of things possible that were tricky before.
%
% The idea of the highlighting mechanism is to use two \Verb!\tikzmark!s to mark a start and end of a region to be highlighted.
% The region is considered to be formed by lines of text, with the first mark at the baseline of the start and the second at the baseline of the end.
%
% The highlighting itself is done by inserting code in the shipout routine before the page itself is laid out.
% So the highlighting is on a separate layer to the text itself, which can be either behind or in front of the text layer.
% The hook mechanisem also makes it relatively simple to support page breaks between the start and end of highlighting.
%
% Since the highlighting is separate to the flow of the text, it doesn't make sense to use an environment to mark the start and end of the highlighting so instead there are two commands: \Verb!\StartHighlighting[options]! and \Verb!\StopHighlighting!, or a single command \Verb!\Highlight[options]{text}! that just highlights the \Verb!text!.
% At the moment, nesting highlighting is not supported.
%
% The optional argument to \Verb!\StartHighlighting! (or \Verb!\Highlight!) consists of key-value pairs that control the behaviour of the highlighted region.
% There are particular keys in the \Verb!/tikz/highlighter! family which control the size of the highlighted region.
%
% The keys are as follows:
%
% \begin{itemize}
% \item \texttt{direction}
% \item \texttt{layer}
% \item \texttt{initial height}
% \item \texttt{initial depth}
% \item \texttt{initial offset}
% \item \texttt{final height}
% \item \texttt{final depth}
% \item \texttt{final offset}
% \item \texttt{left margin}
% \item \texttt{right margin}
% \item \texttt{height}
% \item \texttt{depth}
% \item \texttt{offset}
% \item \texttt{margin}
% \end{itemize}
%
% The highlighting code draws a region which can be styled with standard TikZ keys, more of which in a moment.
% Although it is a single region, the \emph{intention} is to simulate using an actual highlighter.
% The first key, \Verb!direction!, is used to draw the region as if the highlighter were used in a particular direction.
% The options are \Verb!horizontal!, \Verb!vertical!, or \Verb!box!.
% The default is \Verb!horizontal!.
%
% The second key, \Verb!layer!, determines whether the highlighter is rendered on the \Verb!background! or \Verb!foreground! layer.
% Using the \Verb!background! layer puts the highlighting underneath the text, which will make the text easier to read.
% The \Verb!foreground! option puts the highlighting over the text, which can be used to fade the text.
% The default is \Verb!background!.
%
% The shape of the region depends on a few things, such as whether the highlighting starts and ends on the same line.
%
% \begin{tikzpicture}[>=Latex]
% \fill (0,0) circle[radius=2pt];
% \fill (4,0) circle[radius=2pt];
% \draw (-1,-1) rectangle (5,1);
% \draw[dashed] (0,0) -- (4,0);
% \draw[<->] (0,0) -- +(-1,0);
% \draw[<->] (0,0) -- +(0,1);
% \draw[<->] (4,0) -- +(0,-1);
% \draw[<->] (4,0) -- +(1,0);
% \draw[<-] (-.5,0) to[out=90,in=-90] +(-1,1.5) node[above] {initial offset};
% \draw[<-] (4.5,0) to[out=90,in=-90] +(1,1.5) node[above] {final offset};
% \draw[<-] (0,.5) to[out=0,in=180] +(1,1.5) node[right] {initial height};
% \draw[<-] (4,-.5) to[out=0,in=180] +(1,-1.5) node[right] {final depth};
% \node[anchor=base] at (2,0) {Single line};
% \end{tikzpicture}
%
% \begin{tikzpicture}[>=Latex]
% \fill (0,0) circle[radius=2pt];
% \draw (-1,-1) rectangle (4,1);
% \draw[dashed] (0,0) -- (4,0);
% \draw[<->] (0,0) -- +(-1,0);
% \draw[<->] (0,0) -- +(0,1);
% \draw[<->] (0,0) -- +(0,-1);
% \draw[<-] (-.5,0) to[out=90,in=-90] +(-1,1.5) node[above] {initial offset};
% \draw[<-] (0,.5) to[out=0,in=180] +(1,1.5) node[right] {initial height};
% \draw[<-] (0,-.5) to[out=0,in=180] +(1,-1.5) node[right] {initial depth};
% \node[anchor=base] at (2,0) {Split line};
% \begin{scope}[yshift=-3cm,xshift=-5cm]
% \fill (4,0) circle[radius=2pt];
% \draw (0,-1) rectangle (5,1);
% \draw[dashed] (0,0) -- (4,0);
% \draw[<->] (4,0) -- +(1,0);
% \draw[<->] (4,0) -- +(0,1);
% \draw[<->] (4,0) -- +(0,-1);
% \draw[<-] (4.5,0) to[out=90,in=180] +(2.5,0) node[right] {final offset};
% \draw[<-] (4,.5) to[out=180,in=0] +(-1,1.5) node[left] {final height};
% \draw[<-] (4,-.5) to[out=0,in=180] +(1.5,-1) node[right] {final depth};
% \node[anchor=base] at (2,0) {Split line};
% \end{scope}
% \end{tikzpicture}
%
% \begin{tikzpicture}[>=Latex]
% \fill (0,0) circle[radius=2pt];
% \fill (4,0) circle[radius=2pt];
% \fill (-4,-1) circle[radius=2pt];
% \fill (4,-1) circle[radius=2pt];
% \fill (-4,-2) circle[radius=2pt];
% \fill (0,-2) circle[radius=2pt];
% \draw (-1,0) -- (-1,1) -- (5,1) -- (5,-2) -- (1,-2) -- (1,-3) -- (-5,-3) -- (-5,0) -- cycle;
% \draw[dashed] (0,0) -- (4,0);
% \draw[dashed] (-4,-1) -- (4,-1);
% \draw[dashed] (-4,-2) -- (0,-2);
% \draw[<->] (0,0) -- +(-1,0);
% \draw[<->] (0,0) -- +(0,1);
% \draw[<->] (4,0) -- +(1,0);
% \draw[<->] (0,-2) -- +(1,0);
% \draw[<->] (0,-2) -- +(0,-1);
% \draw[<->] (-4,-2) -- +(-1,0);
% \draw[<-] (-.5,0) to[out=90,in=0] +(-1,.5) node[left] {initial offset};
% \draw[<-] (0,.5) to[out=0,in=180] +(1,0) node[right] {initial height};
% \draw[<-] (4.5,0) to[out=90,in=-90] +(1,1.5) node[above] {right margin};
% \draw[<-] (.5,-2) to[out=-90,in=180] +(1,-.5) node[right] {final offset};
% \draw[<-] (0,-2.5) to[out=180,in=0] +(-1,0) node[left] {final depth};
% \draw[<-] (-4.5,-2) to[out=90,in=-90] +(-1,1.5) node[above] {left margin};
% \node[anchor=base] at (0,-1) {Multiple lines};
% \end{tikzpicture}
%
% The \texttt{vertical} regions and the \texttt{box} are defined similarly.
% With the vertical regions then the meaning of the \Verb!height!, \Verb!depth!, and \Verb!offset! are rotated \(90^\circ\), and the vertial regions don't stretch to the page boundaries.
% The \texttt{box} region is always a rectangle.
%
% Once the region is defined, it can be styled using options directly on the \Verb!StartHighlighting! or \Verb!\Highlight! command and by using the following styles:
%
% \begin{itemize}
% \item \texttt{every highlight picture}
% \item \texttt{every <direction> highlight picture}
% \item \texttt{every <layer> highlight picture}
% \item \texttt{every highlight path}
% \item \texttt{every <direction> highlight path}
% \item \texttt{every <layer> highlight path}
% \item \texttt{highlight path}
% \item \texttt{<direction> highlight path}
% \item \texttt{<layer> highlight path}
% \end{itemize}
%
% The \Verb!picture! keys are for the surrounding \Verb!tikzpicture!, while the \Verb!path! keys are for the path itself.
%
% Lastly, a word about scoping the options.
% Since the code that actually renders the highlighting is processed when the page is shipped out, it may well be that the settings in force when the highlighting was defined have changed.
% The keys that adjust the size of the region (in the \Verb!highlighter! family) are saved at the moment of invocation but keys such as the colour or whether to fill or draw the path are not.
% Therefore, it is wise to use styles that persist to set the rendering styles.
%
% \begin{example}
% The sun was shining on the sea, shining with all its might.
% \StartHighlighting[fill=cyan!50]
% And this was very odd because it was the middle of the night.
% \StopHighlighting
% The moon was up there sulkily because she thought the sun had no
% business to be there after the day was done.
% \StartHighlighting[fill=magenta!50]
% ``It's very rude of him,'' she said, ``to come and spoil the fun.''
% \StopHighlighting

% \noindent The sun was shining on the sea, shining with all its might.
% And this was very odd because it was the middle of the night.
% \StartHighlighting[fill=yellow!50]
% The moon was up there sulkily because she thought the sun had no business to be there after the day was done\StopHighlighting.
% ``It's very rude of him,'' she said, ``to come and spoil the fun.''
% \end{example}
%
% \section{Acknowledgements}
%
% The \Verb+\tikzmark+ macro has been used and abused by many users of \href{http://tex.stackexchange.com}{TeX-SX}.
% Of particular note (but in no particular order) are \href{https://tex.stackexchange.com/users/4301/peter-grill}{Peter Grill}, \href{https://tex.stackexchange.com/users/3954/gonzalo-medina}{Gonzalo Medina}, \href{https://tex.stackexchange.com/users/13304/claudio-fiandrino}{Claudio Fiandrino}, \href{https://tex.stackexchange.com/users/3235/percusse}{percusse}, and \href{https://tex.stackexchange.com/users/121799/marmot}{marmot}.
% I would also like to mention \href{https://tex.stackexchange.com/users/1090/david-carlisle}{David Carlisle} whose knowledge of TikZ continues to astound us all.
% 
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*tikzlibrary>
% \fi
% \subsection{Main Code}
%
%
% The \Verb+save nodes+ code uses \LaTeX3.
%    \begin{macrocode}
\ProvidesFile{tikzlibrarytikzmark.code.tex}[%
  2022/08/24
  v1.15
  TikZ library for marking positions in a document]
\RequirePackage{expl3, l3keys2e, xparse}
%    \end{macrocode}
%
%    \begin{macrocode}
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
%    \end{macrocode}
% Not totally happy with using \Verb+every picture+ here as it's too easily overwritten by the user.
% Maybe it would be better to patch \Verb+endtikzpicture+ directly.
%    \begin{macrocode}
  every picture/.append style={%
    execute at end picture={%
      \ifpgfrememberpicturepositiononpage%
      \edef\pgf@temp{%
        \noexpand\write\noexpand\pgfutil@auxout{%
          \string\savepicturepage%
          {\pgfpictureid}{\noexpand\arabic{page}}%
        }%
      }%
      \pgf@temp
      \fi%
    },
  },
%    \end{macrocode}
% There are times when some code is executed and then discarded, such as in \Verb+\mathchoice+.
% This can seriously mess with how TikZ pictures are remembered as the last \Verb+pgfpictureid+ to be \emph{processed} is the one that is used, but it is the one that is \emph{used} that is recorded in the \Verb+aux+ file.
% This isn't particularly a tikzmark issue, but does come up from time to time with tikzmark as it's all about remembering locations.
%
% In actual fact, it only occurs with \Verb+\tikzmarknode+ since the issue is about how nodes are associated with pictures.
%
% The solution is to check to see if the \Verb+pgfpictureid+ has been recorded in the \Verb+aux+ file and if it hasn't, quietly prefix the node names with a discard term.
% This needs to be used \emph{after} \Verb+remember picture+ has been invoked.
% It probably messes with some other stuff so should only be used under controlled conditions, such as \Verb+\tikzmarknode+.
%    \begin{macrocode}
  check picture id/.code={
    \ifpgfrememberpicturepositiononpage
    \@ifundefined{pgf@sys@pdf@mark@pos@\pgfpictureid}{%
      \tikzset{%
        name prefix/.get=\tzmk@name@prefix,
        name prefix/.prefix=discard-,
        execute at end picture={%
          \tikzset{name prefix/.expand once=\tzmk@name@prefix}%
        },
      }%
    }{}%
    \fi
  },
%    \end{macrocode}
% We also want a failsafe that quietly handles the case where the document hasn't been compiled enough times (once) to get the information into the \Verb+aux+ file.
% There will already be messages about needing reruns so we don't need to add to that.
% We simply ensure that the node exists.
%    \begin{macrocode}
  maybe define node/.style={%
    execute at end picture={%
      \ifpgfrememberpicturepositiononpage
      \@ifundefined{pgf@sh@pi@\tikz@pp@name{#1}}{%
        \pgfnodealias{\tikz@pp@name{#1}}{discard-\tikz@pp@name{#1}}%
      }{}%
      \fi
    }%
  },
%    \end{macrocode}
% The positions are already recorded in the \Verb+aux+ file, all we really need to do is provide them with better names.
%    \begin{macrocode}
  save picture id/.code={%
      \protected@write\pgfutil@auxout{}{%
      \string\savepointas%
      {\tikzmark@pp@name{#1}}{\pgfpictureid}{0pt}{0pt}}%
  },
%    \end{macrocode}
% Provides a way to test if a picture has already been saved (in particular, can avoid errors on first runs)
%    \begin{macrocode}
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  },
%    \end{macrocode}
% Page handling
%    \begin{macrocode}
  next page/.is choice,
  next page vector/.initial={\pgfqpoint{0pt}{0pt}},
  next page/below/.style={%
    next page vector={\pgfqpoint{0pt}{-\the\paperheight}}%
  },
  next page/above/.style={%
    next page vector={\pgfqpoint{0pt}{\the\paperheight}}%
  },
  next page/left/.style={%
    next page vector={\pgfqpoint{-\the\paperwidth}{0pt}}%
  },
  next page/right/.style={%
    next page vector={\pgfqpoint{\the\paperwidth}{0pt}}%
  },
  next page/ignore/.style={%
    next page vector={\pgfqpoint{0pt}{0pt}}%
  },
  if tikzmark on current page/.code n args={3}{%
    \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
      \pgfkeysalso{#3}%
    }{%
      \@ifundefined{%
        save@pg@\csname save@pt@\tikzmark@pp@name{#1}\endcsname
      }{%
        \pgfkeysalso{#3}%
      }{%
        \ifnum\csname save@pg@%
        \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
        \endcsname=\the\value{page}\relax%
        \pgfkeysalso{#2}%
        \else
        \pgfkeysalso{#3}%
        \fi
      }%
    }%
  },
  if tikzmark on page/.code n args={4}{%
    \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
      \pgfkeysalso{#4}%
    }{%
      \@ifundefined{%
        save@pg@\csname save@pt@\tikzmark@pp@name{#1}@label\endcsname%
      }{%
        \pgfkeysalso{#4}%
      }{%
        \ifnum\csname save@pg@%
        \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
        \endcsname=#2\relax%
        \pgfkeysalso{#3}%
        \else
        \pgfkeysalso{#4}%
        \fi
      }%
    }%
  },
%    \end{macrocode}
% Prefix and suffix for tikzmark names, shamelessly borrowed from the main tikz code  
%    \begin{macrocode}
  tikzmark prefix/.initial=,%
  tikzmark suffix/.initial=,%
  tikzmark clear ixes/.style={
    tikzmark prefix={},
    tikzmark suffix={}
  },
%    \end{macrocode}
% Tikzmarks can be used to adjust the position of a scope or pic so that an internally defined coordinate is used to locate the scope or pic.
%
% The key used to adjust the location is \Verb!scope anchor={coordinate}! for \Verb!scope!s and \Verb!pic anchor={coordinate}! for \Verb!pic!s, where \Verb!coordinate! is evaluated internally to the \Verb!scope! or \Verb!pic!, so can use node names.
%    \begin{macrocode}
  scope anchor location/.initial={(0,0)},
  scope anchor location/.default=@auto,
  pic anchor/.style={
    scope anchor location={#1},
    next pic/.append style={
      adjust scope position,
    }
  },
  scope anchor/.style={
    scope anchor location={#1},
    adjust scope position,
  },
%    \end{macrocode}
% The code that does the adjustment is added to the \Verb!pic! on its enclosing scope using the \Verb!every pic! key.
%    \begin{macrocode}  
  adjust scope position/.code={%
    \pgfutil@ifundefined{tikz@fig@name}%
    {\let\tikz@fig@name=\pgfutil@empty}{}%
    \tikz@resetexpandcount%
    \tikz@fig@mustbenamed
    \pgfkeysgetvalue{/tikz/scope anchor location}\tkzmk@anchor
    \ifx\tkzmk@anchor\tikz@auto@text
    \tikzset{local bounding box/.expanded=\tikz@fig@name}%
    \def\tkzmk@anchor{(\tikz@fig@name.\tikz@anchor)}%
    \fi
    \tikz@scan@one@point
    \pgfutil@firstofone(pic cs:\tikz@fig@name-origin)\relax
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \tikz@scan@one@point
    \pgfutil@firstofone(pic cs:\tikz@fig@name-anchor)\relax
    \advance\pgf@xa by -\pgf@x
    \advance\pgf@ya by -\pgf@y
    \tikzset{
      shift={(\the\pgf@xa,\the\pgf@ya)},
      execute at end scope={%
        \tikzmark{\tikz@fig@name-origin}{(0,0)}%
        \tikzmark{\tikz@fig@name-anchor}{\tkzmk@anchor}%
      }
    }
  },
%    \end{macrocode}
%
% To install this code on a pic, we hook in to the pic's enclosing scope using the \Verb+every pic+ key.
% To avoid this bubbling down to pics within pics, we clear it once it has been executed.
% So any code that triggers this adjustment adds \Verb!adjust pic position! to the !next pic! style.
%    \begin{macrocode}
  every pic/.append style={
    next pic/.try,
    next pic/.style={}
  },
%    \end{macrocode}
%  
% This code remembers the bounding box of a pic, saving it as if it were a node.
%    \begin{macrocode}
  save pic bounding box/.code={
    \tikz@fig@mustbenamed
    \tikzset{local bounding box/.expanded=\tikz@fig@name}
  },
  surround pic/.style={
    next pic/.append style={
      save pic bounding box
    }
  },
}
%    \end{macrocode}
%
% \begin{macro}{\tikzmark@pp@name}
%    \begin{macrocode}
\def\tikzmark@pp@name#1{%
  \csname pgfk@/tikz/tikzmark prefix\endcsname%
  #1%
  \csname pgfk@/tikz/tikzmark suffix\endcsname%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\savepointas}
% This is what gets written to the \Verb+aux+ file.
%    \begin{macrocode}
\def\savepointas#1#2#3#4{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
  \expandafter\gdef\csname save@pt@#1@offset\endcsname%
  {\pgfqpoint{#3}{#4}}%
}
\def\savepicturepage#1#2{%
  \expandafter\gdef\csname save@pg@#1\endcsname{#2}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikzmarkalias}
% Alias a tikzmark to another name (used in tikzmarknode).
% The alias is saved to the aux-file so that it is available prior to the definition.
% The private one doesn't use the prefix-suffix for greater internal flexibility.
% The public one does.
%    \begin{macrocode}
\def\@tikzmarkalias#1#2{%
  \@ifundefined{save@pt@#2}{}{%
    \pgf@node@gnamelet{save@pt@#1}{save@pt@#2}%
    \pgf@node@gnamelet{save@pt@#1@offset}{save@pt@#2@offset}%
    \protected@write\pgfutil@auxout{}{%
      \string\savepointas%
      {#1}{\csname save@pt@#2\endcsname}%
      \expandafter\expandafter\expandafter
      \@gobble\csname save@pt@#2@offset\endcsname
    }%
  }%
}
\def\tikzmarkalias#1#2{%
  \@tikzmarkalias{\tikzmark@pp@name{#1}}{\tikzmark@pp@name{#2}}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tmk@labeldef}
% Auxiliary command for the coordinate system.
%    \begin{macrocode}
\def\tmk@labeldef#1,#2\@nil{%
  \edef\tmk@label{\tikzmark@pp@name{#1}}%
  \def\tmk@def{#2}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{pic}
% This defines the new coordinate system.
%    \begin{macrocode}
\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \expandafter\tikz@scan@one@point
    \expandafter\pgfutil@firstofone\tmk@def\relax
  }{%
    \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}%
    \save@orig@pic%
    \pgfsys@getposition{\pgfpictureid}\save@this@pic%
    \pgf@process{\pgfpointorigin\save@this@pic}%
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgf@process{\pgfpointorigin\save@orig@pic}%
    \advance\pgf@x by -\pgf@xa
    \advance\pgf@y by -\pgf@ya
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgf@process%
    {\pgfpointorigin\csname save@pt@\tmk@label @offset\endcsname}%
    \advance\pgf@xa by \pgf@x
    \advance\pgf@ya by \pgf@y
      \@ifundefined{save@pg@\csname save@pt@\tmk@label\endcsname}{}{%
        \@ifundefined{save@pg@\pgfpictureid}{}{%
          \pgfkeysvalueof{/tikz/next page vector}%
        \edef\tmk@pg{%
          \the\numexpr \csname save@pg@%
          \csname save@pt@\tmk@label\endcsname\endcsname%
          -
          \csname save@pg@\pgfpictureid\endcsname\relax%
        }%
        \ifnum \tmk@pg > 0 \relax
        \advance \pgf@xa by \pgf@x\relax
        \advance \pgf@ya by \pgf@y\relax
        \fi
        \ifnum \tmk@pg < 0 \relax
        \advance \pgf@xa by -\pgf@x\relax
        \advance \pgf@ya by -\pgf@y\relax
        \fi
        }%
      }%
      \pgf@x=\pgf@xa
      \pgf@y=\pgf@ya
    \pgftransforminvert
    \pgf@pos@transform{\pgf@x}{\pgf@y}%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikzmark}
% The active/non-active semi-colon is proving somewhat hazardous to \Verb+\tikzmark+ (see  \href{http://tex.stackexchange.com/q/110014/86}{ Tikzmark and french seem to conflict} and \href{http://tex.stackexchange.com/q/335485/86}{Clash between tikzmark, babel package (french) and babel tikzlibrary}) so \Verb+\tikzmark+ now uses the brace-delimited version of the \Verb+\tikz+ command.
%
% This version is for when we're outside a tikzpicture environment
%    \begin{macrocode}
\newcommand\tikzmark@outside[2][]{%
\tikzset{external/export next/.try=false}%
\tikz[remember picture with id=#2]{#1}%
}
%    \end{macrocode}
% This is for when we're inside a tikzpicture environment
%    \begin{macrocode}
\def\tikzmark@inside#1#2{%
  \tikzset{remember picture}%
  \tikz@resetexpandcount%
  \tikz@scan@one@point\pgfutil@firstofone#2\relax
  \pgf@pos@transform{\pgf@x}{\pgf@y}%
  \protected@write\pgfutil@auxout{}{%
    \string\savepointas%
    {\tikzmark@pp@name{#1}}{\pgfpictureid}{\the\pgf@x}{\the\pgf@y}}%
}
%    \end{macrocode}
% And finally, the ultimate invoker:
%    \begin{macrocode}
\def\tikzmark{%
  \ifx\pgfpictureid\@undefined
  \let\tikzmark@next=\tikzmark@outside
  \else
  \relax
  \ifx\scope\tikz@origscope\relax
  \let\tikzmark@next=\tikzmark@outside
  \else
  \let\tikzmark@next=\tikzmark@inside
  \fi
  \fi
  \tikzmark@next%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pgfmark}
%    \begin{macrocode}
\newcommand\pgfmark[1]{%
  \bgroup
  \global\advance\pgf@picture@serial@count by1\relax%
  \edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
  \pgfsys@markposition{\pgfpictureid}%
  \edef\pgf@temp{%
    \noexpand\write\noexpand\pgfutil@auxout{%
      \string\savepicturepage
      {\pgfpictureid}{\noexpand\arabic{page}}%
    }%
  }%
  \pgf@temp
  \protected@write\pgfutil@auxout{}{%
    \string\savepointas
    {\tikzmark@pp@name{#1}}{\pgfpictureid}{0pt}{0pt}}%
  \egroup
}
%    \end{macrocode}
% \end{macro}
%
%
% If the beamer class is used, make the commands overlay aware.
% \begin{macro}{\tikzmark<>}
%    \begin{macrocode}
\@ifclassloaded{beamer}{
  \renewcommand<>{\tikzmark@outside}[2][]{%
    \only#3{\beameroriginal{\tikzmark@outside}[{#1}]{#2}}%
  }
  \renewcommand<>{\tikzmark@inside}[2]{%
    \only#3{\beameroriginal{\tikzmark@inside}{#1}{#2}}%
  }
}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pgfmark<>}
%    \begin{macrocode}
\@ifclassloaded{beamer}{
  \renewcommand<>{\pgfmark}[1]{\only#2{\beameroriginal{\pgfmark}{#1}}}
}{}
%    \end{macrocode}
% \end{macro}
%
% If beamer is loaded, add a suffix based on the frame number
%    \begin{macrocode}
\@ifclassloaded{beamer}{
  \tikzset{
    tikzmark suffix=-\the\beamer@slideinframe
  }
}{}
%    \end{macrocode}
%
%
% \begin{macro}{\iftikzmark}
%    \begin{macrocode}
\newif\iftikzmark@
\newcommand\iftikzmark[3]{%
    \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
      #3%
    }{%
      #2%
    }%
}%
%    \end{macrocode}
%
% A version suitable for \Verb+\if ... \else ... \fi+.
%    \begin{macrocode}
\newcommand\iftikzmarkexists[1]{%
  \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
    \tikzmark@false%
  }{%
    \tikzmark@true%
  }%
  \iftikzmark@
}%
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\iftikzmarkonpage}
%    \begin{macrocode}
\newcommand\iftikzmarkonpage[2]{%
  \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
    \tikzmark@false
  }{%
    \@ifundefined{save@pg@%
      \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
    }{%
      \tikzmark@false
    }{%
      \ifnum\csname save@pg@%
      \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
      \endcsname=#2\relax%
      \tikzmark@true
      \else
      \tikzmark@false
      \fi
    }%
  }%
  \iftikzmark@
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\iftikzmarkoncurrentpage}
%    \begin{macrocode}
\newcommand\iftikzmarkoncurrentpage[1]{%
  \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
    \tikzmark@false
  }{%
    \@ifundefined{save@pg@%
      \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
    }{%
      \tikzmark@false
    }{%
      \ifnum\csname save@pg@%
      \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
      \endcsname=\the\value{page}\relax%
      \tikzmark@true
      \else
      \tikzmark@false
      \fi
    }%
  }%
  \iftikzmark@
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subnode}
% Note: much of this code was inevitably adapted from the node defining code in the TikZ/PGF sources.
%
% The \Verb!\pgfmark! applies the current tikzmark prefix/suffix.
% The current node prefix/suffix is applied by using the \Verb!name=! key.
%    \begin{macrocode}
\def\subnode@#1#2#3{%
  \begingroup
  \pgfmark{#2}%
  \setbox\pgfnodeparttextbox=\hbox\bgroup #3\egroup
  \tikzset{every subnode/.try,#1,name=#2}%
  \pgfpointorigin
  \tikz@scan@one@point\pgfutil@firstofone(pic cs:#2)\relax
  \advance\pgf@x by .5\wd\pgfnodeparttextbox
  \advance\pgf@y by .5\ht\pgfnodeparttextbox
  \advance\pgf@y by -.5\dp\pgfnodeparttextbox
  \pgftransformshift{}%
  \setbox\@tempboxa=\hbox\bgroup
  {%
    \let\pgf@sh@savedmacros=\pgfutil@empty% MW
    \let\pgf@sh@savedpoints=\pgfutil@empty%
    \def\pgf@sm@shape@name{rectangle}% CJ % TT added prefix!
    \pgf@sh@s@rectangle%
    \pgf@sh@savedpoints%
    \pgf@sh@savedmacros% MW
    \pgftransformshift{%
      \pgf@sh@reanchor{rectangle}{center}%
      \pgf@x=-\pgf@x%
      \pgf@y=-\pgf@y%
    }%
    \expandafter\pgfsavepgf@process
    \csname pgf@sh@sa@\tikz@fig@name\endcsname{%
      \pgf@sh@reanchor{rectangle}{center}% FIXME : this is double work!
    }%
    % Save the saved points and the transformation matrix
    \edef\pgf@node@name{\tikz@fig@name}%
    \ifx\pgf@node@name\pgfutil@empty%
    \else%
    \expandafter\xdef
    \csname pgf@sh@ns@\pgf@node@name\endcsname{rectangle}%
    \edef\pgf@sh@@temp{%
      \noexpand\gdef\expandafter
      \noexpand\csname pgf@sh@np@\pgf@node@name\endcsname}%
    \expandafter\pgf@sh@@temp\expandafter{%
      \pgf@sh@savedpoints}%
    \edef\pgf@sh@@temp{%
      \noexpand\gdef\expandafter
      \noexpand\csname pgf@sh@ma@\pgf@node@name\endcsname}% MW
    \expandafter\pgf@sh@@temp\expandafter{\pgf@sh@savedmacros}% MW
    \pgfgettransform\pgf@temp
    \expandafter\xdef
    \csname pgf@sh@nt@\pgf@node@name\endcsname{\pgf@temp}%
    \expandafter\xdef
    \csname pgf@sh@pi@\pgf@node@name\endcsname{\pgfpictureid}%
    \fi%
  }%
  \egroup
  \box\pgfnodeparttextbox
  \endgroup
}

\newcommand\subnode[3][]{%
  \ifmmode
  \mathchoice{%
    \subnode@{#1}{#2-d}{\(\displaystyle #3\)}%
  }{%
    \subnode@{#1}{#2-t}{\(\textstyle #3\)}%
  }{%
    \subnode@{#1}{#2-s}{\(\scriptstyle #3\)}%
  }{%
    \subnode@{#1}{#2-ss}{\(\scriptscriptstyle #3\)}%
  }%
  \let\pgf@nodecallback\pgfutil@gobble
  \def\tzmk@prfx{pgf@sys@pdf@mark@pos@pgfid}%
  \edef\tzmk@pic{\tzmk@prfx\the\pgf@picture@serial@count}
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-1\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-2\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-3\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \pgfutil@ifundefined{pgf@sh@ns@\tikz@pp@name{#2}}{%
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
  }{}%
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-d}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-d}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-s}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-s}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-ss}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-ss}}%
  \fi
  \else
    \subnode@{#1}{#2}{#3}%
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikzmarknode}
% The \Verb+\tikzmark+ macro has changed considerably since its first inception, but there does still seem to be a use for the original version which put stuff inside a node.  This command reintroduces that command.
%
% It does its best to work inside a math environment by a sneaky trick involving \Verb+\mathchoice+: the \Verb+remember picture+ key means that only the picture id of the typeset box is saved to the aux file.  So comparing the possible picture ids of the four options with the one read from the aux file, we can figure out which box was actually used.
%    \begin{macrocode}
\def\tikzmarknode@#1#2#3{%
\tikzset{external/export next/.try=false}%
  \tikz[%
    remember picture,
    save picture id={#2},
    check picture id,
    maybe define node={#2},
    baseline=(#2.base),
    every tikzmarknode picture/.try
  ] {
    \node[
      anchor=base,
      inner sep=0pt,
      minimum width=0pt,
      name={#2},
      node contents={#3},
      every tikzmarknode/.try,
      #1
  ]}%
}

\newcommand\tikzmarknode[3][]{%
  \ifmmode
  \mathchoice{%
    \tikzmarknode@{#1}{#2-d}{\(\displaystyle #3\)}%
  }{%
    \tikzmarknode@{#1}{#2-t}{\(\textstyle #3\)}%
  }{%
    \tikzmarknode@{#1}{#2-s}{\(\scriptstyle #3\)}%
  }{%
    \tikzmarknode@{#1}{#2-ss}{\(\scriptscriptstyle #3\)}%
  }%
  \let\pgf@nodecallback\pgfutil@gobble
  \def\tzmk@prfx{pgf@sys@pdf@mark@pos@pgfid}%
  \edef\tzmk@pic{\tzmk@prfx\the\pgf@picture@serial@count}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-1\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-2\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \edef\tzmk@pic%
  {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-3\relax}%
  \expandafter\ifx\csname\tzmk@pic\endcsname\relax
  \pgfutil@ifundefined{pgf@sh@ns@\tikz@pp@name{#2}}{%
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
  }{}%
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-d}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-d}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-s}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-s}}%
  \fi
  \else
    \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-ss}}%
    \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-ss}}%
  \fi
  \else
    \tikzmarknode@{#1}{#2}{#3}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikzmark@box}
% This macro takes a name and a box.
% It pretends that there is a tight-fitting rectangular PGF node around that box with the given name, and saves the required information so that that node can be used later on in a tikzpicture drawing.
%
% It does not actually build a node, and it doesn't create a TikZ drawing.
% Rather, it measures the box and uses that information to define the various macros that store the information about the node.
%
% Apart from assigning a load of macros, it does also place a \Verb+\pgfmark+ just before the box.
% This is needed to be able to locate the node on the page.
%
% The command is defined with an \Verb!@! because it is more likely to be used in other packages than by a user.
%    \begin{macrocode}
\def\tikzmark@box#1#2{%
  \begingroup
  \pgfmark{#1}%
  \let\pgfnodeparttextbox=#2%
  \edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
  \def\tikz@fig@name{#1}%
  \pgfpointorigin
  \advance\pgf@x by .5\wd\pgfnodeparttextbox
  \advance\pgf@y by .5\ht\pgfnodeparttextbox
  \advance\pgf@y by -.5\dp\pgfnodeparttextbox
  \pgftransformshift{}%
  \setbox\@tempboxa=\hbox\bgroup
  {%
    \tikzset{
      inner sep=0pt, 
      minimum size=0pt, 
      outer sep=0pt, 
      anchor=base
    }%
    \let\pgf@sh@savedmacros=\pgfutil@empty% MW
    \let\pgf@sh@savedpoints=\pgfutil@empty
    \def\pgf@sm@shape@name{rectangle}% CJ % TT added prefix!
    \pgf@sh@s@rectangle
    \pgf@sh@savedpoints
    \pgf@sh@savedmacros% MW
    \pgftransformshift{%
      \pgf@sh@reanchor{rectangle}{center}%
      \pgf@x=-\pgf@x
      \pgf@y=-\pgf@y
    }%
    \expandafter\pgfsavepgf@process
    \csname pgf@sh@sa@\tikz@fig@name\endcsname{%
      \pgf@sh@reanchor{rectangle}{center}% FIXME : this is double work!
    }%
    % Save the saved points and the transformation matrix
    \edef\pgf@node@name{\tikz@fig@name}%
    \ifx\pgf@node@name\pgfutil@empty
    \else
    \expandafter\xdef
    \csname pgf@sh@ns@\pgf@node@name\endcsname{rectangle}%
    \edef\pgf@sh@@temp{%
      \noexpand\gdef\expandafter
      \noexpand\csname pgf@sh@np@\pgf@node@name\endcsname}%
    \expandafter\pgf@sh@@temp\expandafter{%
      \pgf@sh@savedpoints}%
    \edef\pgf@sh@@temp{%
      \noexpand\gdef\expandafter
      \noexpand\csname pgf@sh@ma@\pgf@node@name\endcsname}% MW
    \expandafter\pgf@sh@@temp\expandafter{\pgf@sh@savedmacros}% MW
    \pgfgettransform\pgf@temp
    \expandafter\xdef
    \csname pgf@sh@nt@\pgf@node@name\endcsname{\pgf@temp}%
    \expandafter\xdef
    \csname pgf@sh@pi@\pgf@node@name\endcsname{\pgfpictureid}%
    \fi
  }%
  \egroup
  \endgroup
  \box#2%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\usetikzmarklibrary}
%    \begin{macrocode}
\def\usetikzmarklibrary{%
  \pgfutil@ifnextchar[{\use@tikzmarklibrary}{\use@@tikzmarklibrary}%
  }%}
\def\use@tikzmarklibrary[#1]{\use@@tikzmarklibrary{#1}}
\def\use@@tikzmarklibrary#1{%
    \edef\pgf@list{#1}%
    \pgfutil@for\pgf@temp:=\pgf@list\do{%
      \expandafter\pgfkeys@spdef
      \expandafter\pgf@temp\expandafter{\pgf@temp}%
    \ifx\pgf@temp\pgfutil@empty
    \else
      \expandafter\ifx
      \csname tikzmark@library@\pgf@temp @loaded\endcsname\relax%
      \expandafter\global\expandafter\let%
      \csname tikzmark@library@\pgf@temp @loaded\endcsname
      =\pgfutil@empty%
      \expandafter\edef
      \csname tikzmark@library@#1@atcode\endcsname{\the\catcode`\@}
      \expandafter\edef
      \csname tikzmark@library@#1@barcode\endcsname{\the\catcode`\|}
      \catcode`\@=11
      \catcode`\|=12
      \pgfutil@InputIfFileExists{tikzmarklibrary\pgf@temp.code.tex}{}{
        \PackageError{tikzmark}{
          I did not find the tikzmark extras library '\pgf@temp'.}{}
      }%
      \catcode`\@=\csname tikzmark@library@#1@atcode\endcsname
      \catcode`\|=\csname tikzmark@library@#1@barcode\endcsname
      \fi%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
%
% The \Verb+save node+ code is written in \LaTeX3.
%
%    \begin{macrocode}  
\ExplSyntaxOn
\cs_new_protected:Nn \tikzmark_tl_put_right_braced:Nn
 {
  \tl_put_right:Nn #1 { { #2 } }
 }
\cs_generate_variant:Nn \tikzmark_tl_put_right_braced:Nn { NV, cV, cv, Nx, cx }
%    \end{macrocode}
%
% This is how we handle return values from functions
%    \begin{macrocode}
\tl_new:N \g__sn_output_tl
%    \end{macrocode}  
% We save our information in a ``property list'', which is L3's
% version of an associative array or dictionary.  They keys will give
% the ability to store several groups of nodes and restore them at
% will.
%    \begin{macrocode}  
\prop_new:N \g__sn_prop
%    \end{macrocode}  
% We'll need a couple of spare token lists
%    \begin{macrocode}  
\tl_new:N \l__sn_tmpa_tl
\tl_new:N \l__sn_tmpb_tl
%    \end{macrocode}  
%
% Another useful token list
%    \begin{macrocode}
\tl_new:N \l__open_bracket_tl
\tl_set:Nn  \l__open_bracket_tl {[} %]
%    \end{macrocode}
%
% This token list is used for our current node group name
%    \begin{macrocode}
\tl_new:N \l__sn_group_tl
%    \end{macrocode}
%
% We store up the nodes in a list and save them at the end of a given tikzpicture.
% Has to be global as we're often in a group.    
%    \begin{macrocode}    
\clist_new:N \g__sn_nodes_clist
%    \end{macrocode}
%
% This boolean is for whether we save to a file or not.
%    \begin{macrocode}
\bool_new:N \l__sn_file_bool
%    \end{macrocode}
%
% This boolean is for whether we are in the preamble or not.
%    \begin{macrocode}
\bool_new:N \g__sn_preamble_bool
\bool_gset_true:N \g__sn_preamble_bool
%    \end{macrocode}
%
% Key interface for setting some of the options
%    \begin{macrocode}
\keys_define:nn {tikzmark / save nodes}
{
  file .bool_set:N = \l__sn_file_bool,
  group  .tl_set:N = \l__sn_group_tl,
}
%    \end{macrocode}
%
%    
%    \begin{macrocode}    
\msg_new:nnn {tikzmark} {no file} {File~ "#1"~ doesn't~ exist.}
\msg_new:nnn {tikzmark} {loading nodes} {Loading~ nodes~ from~ "#1".}
%    \end{macrocode}
%
% Dimensions and token lists for shifting
%    \begin{macrocode}    
\dim_new:N \l__sn_x_dim
\dim_new:N \l__sn_y_dim
\dim_new:N \l__sn_xa_dim
\dim_new:N \l__sn_ya_dim
\tl_new:N \l__sn_centre_tl

\tl_new:N \l__sn_transformation_tl
\tl_set:Nn \l__sn_transformation_tl {{1}{0}{0}{1}{0pt}{0pt}}
%    \end{macrocode}
%
% Set up a stream for saving the nodes data to a file
%    \begin{macrocode}    
\iow_new:N \g__sn_stream
\bool_new:N \g__sn_stream_bool
\tl_new:N \g__sn_filename_tl
\tl_gset:Nx \g__sn_filename_tl {\c_sys_jobname_str}    

\cs_new_nopar:Npn \sn_open_stream:
{
  \bool_if:NF \g__sn_stream_bool
  {
    \iow_open:Nn \g__sn_stream {\tl_use:N \g__sn_filename_tl .nodes}
    \bool_gset_true:N \g__sn_stream_bool
  }
}

\AtEndDocument
{
  \ExplSyntaxOn
  \bool_if:NT \g__sn_stream_bool
  {
    \iow_close:N \g__sn_stream
  }
  \ExplSyntaxOff
}
%    \end{macrocode}
%
% LaTeX3 wrappers around some PGF functions (to avoid @-catcode issues)
%    \begin{macrocode}
\makeatletter
\cs_set_eq:NN \tikz_set_node_name:n \tikz@pp@name
\cs_set_eq:NN \tikz_fig_must_be_named: \tikz@fig@mustbenamed

\cs_new_nopar:Npn \tikz_scan_point:n #1
{
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
}

\cs_new_nopar:Npn \tikz_scan_point:NNn #1#2#3
{
  \tikz@scan@one@point\pgfutil@firstofone#3\relax
  \dim_set_eq:NN #1 \pgf@x
  \dim_set_eq:NN #2 \pgf@y
}

\makeatother
\cs_generate_variant:Nn \tikz_scan_point:n {V}
\cs_generate_variant:Nn \tikz_scan_point:NNn {NNV}
%    \end{macrocode}
%
% \begingroup
% \catcode`_=12
% \begin{macro}{\process_node:Nn}
% This is the command that actually does the work.  It constructs a
% token list which contains the code that will restore the node data
% when invoked.  The two arguments are the token list to store this in
% and the node name to be saved.
%    \begin{macrocode}
\cs_new_nopar:Npn \__sn_process_node:n #1
{
  \group_begin:
%    \end{macrocode}
% Clear our token list
%    \begin{macrocode}
  \tl_clear:N \l__sn_tmpa_tl
%    \end{macrocode}
% Set the centre of the picture
%    \begin{macrocode}  
  \tikz_scan_point:NNn \l__sn_x_dim \l__sn_y_dim
  {(current~ bounding~ box.center)}
  \dim_set:Nn \l__sn_x_dim {-\l__sn_x_dim}
  \dim_set:Nn \l__sn_y_dim {-\l__sn_y_dim}
  \tl_set:Nx \l__sn_centre_tl {
    {1}{0}{0}{1}{\dim_use:N \l__sn_x_dim}{\dim_use:N \l__sn_y_dim}
  }
%    \end{macrocode}  
% Test to see if the node has been defined
%    \begin{macrocode}
  \tl_if_exist:cT {pgf@sh@ns@#1}
  {
%    \end{macrocode}
% The node information is stored in a series of macros of the form
% \Verb+\pgf@sh@XX@nodename+ where XX is one of the following.
%    \begin{macrocode}
    \clist_map_inline:nn {ns,np,ma,pi}
    {
%    \end{macrocode}  
% Our token list will look like:
%
% \Verb+\tl_set:cn {pgf@sh@XX@nodename}+ {<current contents of that macro>}
%
% This will restore \Verb+\pgf@sh@XX@nodename+ to its current value
% when this list is invoked.
%
% This part puts the \Verb+\tl_set:cn {pgf@sh@XX@nodename}+ in place
%    \begin{macrocode}
      \tl_put_right:Nn \l__sn_tmpa_tl
      {
        \tl_gset:cn {pgf@sh@##1@ \tikz_set_node_name:n{#1} }
      }
%    \end{macrocode}
% Now we put the current contents in place.  We're doing this in
% an expansive context to get at the contents.  The \Verb+\exp_not:v+
% part takes the current value of \Verb+\pgf@sh@XX@nodename+ and puts
% it in place, preventing further expansion.
%    \begin{macrocode}
      \tl_if_exist:cTF {pgf@sh@##1@#1}
      {
        \tl_put_right:Nx \l__sn_tmpa_tl {
          {\exp_not:v {pgf@sh@##1@ \tikz_set_node_name:n {#1}}}
        }
      }
      {
        \tl_put_right:Nx \l__sn_tmpa_tl {{}}
      }
    }
    \tl_put_right:Nn \l__sn_tmpa_tl
    {
      \tl_gset:cn {pgf@sh@nt@ \tikz_set_node_name:n{#1} }
    }
    \compose_transformations:NVv
    \l__sn_tmpb_tl \l__sn_centre_tl {pgf@sh@nt@#1}
    \tl_put_right:Nx \l__sn_tmpa_tl {{\exp_not:V \l__sn_tmpb_tl}}
    \tl_put_right:Nn \l__sn_tmpa_tl {
      \transform_node:Nn \l__sn_transformation_tl {
        \tikz_set_node_name:n{#1}
      }
    }
  }
%    \end{macrocode}  
% Once we've assembled our token list, we store it in the given
% token list
%    \begin{macrocode}
  \tl_gset_eq:NN \g__sn_output_tl \l__sn_tmpa_tl
  \group_end:
}
\cs_new_protected_nopar:Npn \process_node:Nn #1#2
{
  \__sn_process_node:n {#2}
  \tl_set_eq:NN #1 \g__sn_output_tl
  \tl_gclear:N \g__sn_output_tl
}
\cs_new_protected_nopar:Npn \process_gnode:Nn #1#2
{
  \__sn_process_node:n {#2}
  \tl_gset_eq:NN #1 \g__sn_output_tl
  \tl_gclear:N \g__sn_output_tl
}
%    \end{macrocode}  
% \end{macro}
%
% \begin{macro}{\save_nodes_to_list:nn}
% Save the nodes to a list, given a key
%    \begin{macrocode}
\cs_new_nopar:Npn \save_nodes_to_list:nn #1#2
{
  \tl_clear:N \l__sn_tmpa_tl
  \clist_map_inline:nn {#2}
  {
    \process_node:Nn \l__sn_tmpb_tl {##1}
    \tl_put_right:NV \l__sn_tmpa_tl \l__sn_tmpb_tl
  }
  \prop_gput:NnV \g__sn_prop {#1} \l__sn_tmpa_tl
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\save_nodes_to_file:n}
% Save the nodes to a file
%    \begin{macrocode}
\cs_generate_variant:Nn \iow_now:Nn {NV}
\cs_new_nopar:Npn \save_nodes_to_file:n #1
{
  \sn_open_stream:
  \clist_map_inline:nn {#1}
  {
    \process_node:Nn \l__sn_tmpa_tl {##1}
%    \end{macrocode}  
% Save the token list to the nodes file so that on reading it back in, we restore the node definitions
%    \begin{macrocode}
    \iow_now:Nx \g__sn_stream
    {
      \iow_newline:
      \exp_not:V \l__sn_tmpa_tl
    }
  }
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\cs_generate_variant:Nn \save_nodes_to_list:nn {VV, Vn}
\cs_generate_variant:Nn \save_nodes_to_file:n {V}
%    \end{macrocode}
%
% \begin{macro}{\restore_nodes_from_list:n}
%    \begin{macrocode}
\cs_new_nopar:Npn \restore_nodes_from_list:n #1
{
%    \end{macrocode}
% Restoring nodes is simple: look in the property list for the key
% and if it exists, invoke the macro stored there.
%    \begin{macrocode}  
  \prop_get:NnNT \g__sn_prop {#1} \l__sn_tmpa_tl
  {
    \tl_use:N \l__sn_tmpa_tl
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\restore_nodes_from_file:n}
%    \begin{macrocode}
\cs_new_nopar:Npn \restore_nodes_from_file:n #1
{
  \file_if_exist:nTF {#1.nodes}
  {
    \msg_log:nnn {tikzmark} {loading nodes} {#1}
    \ExplSyntaxOn
    \file_input:n {#1.nodes}
    \ExplSyntaxOff
  }
  {
    \msg_warning:nnn {tikzmark} {no file} {#1}
  }
}
\cs_generate_variant:Nn \restore_nodes_from_file:n {x}
\AtBeginDocument{\bool_gset_false:N \g__sn_preamble_bool}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\compose_transformations:Nnn}
% Compose PGF transformations \Verb+#2 * #3+, storing the result in \Verb+#1+

% I think the PGF Manual might be incorrect.  It implies that the
% matrix is stored row-major, but experimentation implies column-major.
%
% That is, \Verb+{a}{b}{c}{d}{s}{t}+ is:
%
% \[
% \begin{bmatrix} a & c \\ b & d \end{bmatrix}
% \]
%
%    \begin{macrocode}
\cs_new_nopar:Npn \compose_transformations:Nnn #1#2#3
{
  \tl_gset:Nx #1
  {
    {\fp_eval:n {
        \tl_item:nn {#2} {1}
        * \tl_item:nn {#3} {1}
        +
        \tl_item:nn {#2} {3}
        * \tl_item:nn {#3} {2}
      }
    }
    {\fp_eval:n {
        \tl_item:nn {#2} {2}
        * \tl_item:nn {#3} {1}
        +
        \tl_item:nn {#2} {4}
        * \tl_item:nn {#3} {2}
      }
    }
    {\fp_eval:n {
        \tl_item:nn {#2} {1}
        * \tl_item:nn {#3} {3}
        +
        \tl_item:nn {#2} {3}
        * \tl_item:nn {#3} {4}
      }
    }
    {\fp_eval:n {
        \tl_item:nn {#2} {2}
        * \tl_item:nn {#3} {3}
        +
        \tl_item:nn {#2} {4}
        * \tl_item:nn {#3} {4}
      }
    }
    {\fp_to_dim:n {
        \tl_item:nn {#2} {1}
        * \tl_item:nn {#3} {5}
        +
        \tl_item:nn {#2} {3}
        * \tl_item:nn {#3} {6}
        +
        \tl_item:nn {#2} {5}
      }
    }
    {\fp_to_dim:n {
        \tl_item:nn {#2} {2}
        * \tl_item:nn {#3} {5}
        +
        \tl_item:nn {#2} {4}
        * \tl_item:nn {#3} {6}
        +
        \tl_item:nn {#2} {6}
      }
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\cs_generate_variant:Nn \compose_transformations:Nnn
{cVv,NVv,NVn,NvV,NnV}
%    \end{macrocode}
%
% \begin{macro}{\transform_node:Nn}
%    \begin{macrocode}
\cs_new_nopar:Npn \transform_node:Nn #1#2
{
  \compose_transformations:cVv {pgf@sh@nt@#2} #1 {pgf@sh@nt@#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\set_transform_from_node:n}
%    \begin{macrocode}
\cs_new_nopar:Npn \set_transform_from_node:n #1
{
  \tl_set_eq:Nc \l__sn_transformation_tl {pgf@sh@nt@#1}
  \tikz_scan_point:NNn \l__sn_x_dim \l__sn_y_dim {(#1.center)}
    
  \dim_set:Nn \l__sn_x_dim {
    \l__sn_x_dim - \tl_item:cn {pgf@sh@nt@#1}{5}
  }
  \dim_set:Nn \l__sn_y_dim {
    \l__sn_y_dim - \tl_item:cn {pgf@sh@nt@#1}{6}
  }
    
  \compose_transformations:NnV  \l__sn_transformation_tl {
    {1}{0}{0}{1}{\dim_use:N \l__sn_x_dim}{\dim_use:N \l__sn_y_dim}
  } \l__sn_transformation_tl
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\cs_generate_variant:Nn \set_transform_from_node:n {v}
%    \end{macrocode}
%
% Set the TikZ keys for access to the above commands.
%    \begin{macrocode}
\tikzset{
  set~ saved~ nodes~ file~ name/.code={
    \tl_gset:Nx \g__sn_filename_tl {#1}    
  },
  transform~ saved~ nodes/.code={
    \set_transform_from_node:v {tikz@last@fig@name}
  },
  set~ node~ group/.code={
    \tl_set:Nn \l__sn_group_tl {#1}
    \pgfkeysalso{
      execute~ at~ end~ scope={
        \maybe_save_nodes:
      }
    }
  },
%    \end{macrocode}
% Are we saving to a file?
%    \begin{macrocode}  
  save~ nodes~ to~ file/.code={
    \tl_if_eq:nnTF {#1}{false}
    {
      \bool_set_false:N \l__sn_file_bool
    }
    {
      \bool_set_true:N \l__sn_file_bool
    }
    \pgfkeysalso{
      execute~ at~ end~ scope={
        \maybe_save_nodes:
      }
    }
  },
%    \end{macrocode}  
% Append current node or named node to the list of nodes to be saved
%    \begin{macrocode}
  save~ node/.code={
    \tl_if_eq:nnTF {#1} {\pgfkeysnovalue}
    {
      \tikz_fig_must_be_named:
      \pgfkeysalso{
        append~ after~ command={
          \pgfextra{
            \clist_gput_right:Nv \g__sn_nodes_clist {tikz@last@fig@name}
          }
        }
      }
    }
    {
      \clist_gput_right:Nn \g__sn_nodes_clist {#1}
    }
  },
%    \end{macrocode}  
% Restore nodes from file
%    \begin{macrocode}
  restore~ nodes~ from~ file/.code={
    \bool_if:NTF \g__sn_preamble_bool
    {
      \restore_nodes_from_file:x {#1}
    }
    {
      \tikz_fig_must_be_named:
      \pgfkeysalso{append~ after~ command={
          \pgfextra{
            \scope
            \split_argument:NNn \tikzset \restore_nodes_from_file:x {#1}
            \endscope
          }
        }
      }
    }
  },
  restore~ nodes~ from~ file/.default = \g__sn_filename_tl,
%    \end{macrocode}  
% Restore nodes from list
%    \begin{macrocode}
  restore~ nodes~ from~ list/.code={
    \tikz_fig_must_be_named:
    \pgfkeysalso{append~ after~ command={
        \pgfextra{
          \scope
          \split_argument:NNn \tikzset \restore_nodes_from_list:n {#1}
          \endscope
        }
      }
    }
  }
}
\cs_generate_variant:Nn \clist_gput_right:Nn {Nv}
%    \end{macrocode}
%
% \begin{macro}{\split_argument:NNn}
%    \begin{macrocode}
\cs_new_nopar:Npn \split_argument:NNn #1#2#3
{
  \tl_set:Nx \l__sn_tmpa_tl {\tl_head:n {#3}}
  \tl_if_eq:NNTF \l__sn_tmpa_tl \l__open_bracket_tl
  {
    \split_argument_aux:NNp #1#2#3
  }
  {
    #2 {#3}
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\split_argument_aux:NNp}
%    \begin{macrocode}
\cs_new_nopar:Npn \split_argument_aux:NNp #1#2[#3]#4
{
  #1 {#3}
  #2 {#4}
}
%    \end{macrocode}  
% \end{macro}
%
% \begin{macro}{\maybe_save_nodes:}
%    \begin{macrocode}
\cs_new_nopar:Npn \maybe_save_nodes:
{
  \clist_if_empty:NF \g__sn_nodes_clist
  {
    \bool_if:NTF \l__sn_file_bool
    {
      \save_nodes_to_file:V \g__sn_nodes_clist
    }
    {
      \tl_if_empty:NF \l__sn_group_tl
      {
        \save_nodes_to_list:VV \l__sn_group_tl \g__sn_nodes_clist
      }
    }
    \clist_gclear:N \g__sn_nodes_clist
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\SaveNode}
% Command for saving a node outside a TikZ picture.
%    \begin{macrocode}
\DeclareDocumentCommand \SaveNode { o m }
{
  \group_begin:
  \IfNoValueF {#1}
  {
    \keys_set:nn {tikzmark / save nodes}
    {
      file=false,
      group=#1
    }
  }
  \bool_if:NTF \l__sn_file_bool
  {
    \save_nodes_to_file:n {#2}
  }
  {
    \tl_if_empty:NF \l__sn_group_tl
    {
      \save_nodes_to_list:Vn \l__sn_group_tl {#2}
    }
  }
  \group_end:
}
%    \end{macrocode}
% \end{macro}
% \endgroup
%    \begin{macrocode}
\ExplSyntaxOff
%    \end{macrocode}  
% \iffalse
%</tikzlibrary>
% \fi
%
% \subsection{Listings}
%
% \iffalse
%<*listings>
% \fi
%
% From \url{http://tex.stackexchange.com/q/79762/86}
%
%    \begin{macrocode}
\@ifpackageloaded{listings}{%
%    \end{macrocode}
%
% \begin{macro}{\iflst@linemark}
% A conditional to help with placing the mark at the first non-whitespace character.
% Should be set to true so that we notice the first line of the code.
%    \begin{macrocode}
  \newif\iflst@linemark
  \lst@linemarktrue
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{EveryLine}
% This hook places the mark at the start of the line.
%    \begin{macrocode}
\lst@AddToHook{EveryLine}{%
  \begingroup
  \advance\c@lstnumber by 1\relax
  \pgfmark{line-\lst@name-\the\c@lstnumber-start}%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{EOL}
% This hook places the mark at the end of the line and resets the conditional for placing the first mark.
%    \begin{macrocode}
\lst@AddToHook{EOL}{\pgfmark{line-\lst@name-\the\c@lstnumber-end}%
\global\lst@linemarktrue
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{OutputBox}
% Experimenting shows that this is the right place to set the mark at the first non-whitespace character.
% But we only want to do this once per line.
%    \begin{macrocode}
\lst@AddToHook{OutputBox}{%
  \iflst@linemark
  \pgfmark{line-\lst@name-\the\c@lstnumber-first}%
  \global\lst@linemarkfalse
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikzmk@lst@fnum}
% An auxiliary macro to figure out if the \Verb+firstnumber+ key was set.
% If so, it has the form \Verb+<number>\relax+.
% If not, it expands to a single token.
%    \begin{macrocode}
\def\tkzmk@lst@fnum#1\relax#2\@STOP{%
  \def\@test{#2}%
  \ifx\@test\@empty
  \def\tkzmk@lst@start{0}%
  \else
  \@tempcnta=#1\relax
  \advance\@tempcnta by -1\relax
  \def\tkzmk@lst@start{\the\@tempcnta}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{Init}
% Adds a mark at the start of the listings environment.
%    \begin{macrocode}
\lst@AddToHook{Init}{%
  \expandafter\tkzmk@lst@fnum\lst@firstnumber\relax\@STOP
  \pgfmark{line-\lst@name-\tkzmk@lst@start-start}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
}{%
  \PackageError{tikzmark listings}%
  {The listings package has not been loaded.}{}
}
%    \end{macrocode}
% \iffalse
%</listings>
% \fi
%
% \subsection{AMS Math}
%
% This tikzmark library defines a routine that puts a pseudo-node (using \Verb+\tikzmark@box+) around all the pieces used in constructing the various math environments that the AMS Math package provides, such as \Verb+gather+ and \Verb+align+.
% All of these (and their labels) work by putting various pieces into a box and then typesetting that box in the cells of an \Verb+halign+.
% By using \Verb+\tikzmark@box+, this can be infiltrated to put nodes around each of those boxes as it is placed.
%
% \iffalse
%<*ams>
% \fi
%    \begin{macrocode}
\@ifpackageloaded{amsmath}{%
%    \end{macrocode}
%
% \begin{macro}{tikzmarkmath}
%
% Defines an environment in which any AMS mathematical aligned environments get nodes around each piece of their contents.
%
% Start by saving the original \Verb+\boxz@+ command. 
%    \begin{macrocode}
  \let\tikzmark@ams@boxz@=\boxz@
%    \end{macrocode}
%
% We'll need a counter to keep track of the nodes.
%    \begin{macrocode}
  \newcounter{tikzmarkequation}
%    \end{macrocode}
%
% The nodes will be labelled \Verb!<name>-<number>!.
% By default the name is \Verb!equation! but this can be customised.
%    \begin{macrocode}
  \def\tikzmark@ams@name{equation}
%    \end{macrocode}
%
% This is the substitute command.
% I don't know if the \Verb=\ifmeasuring@= actually does anything, but it's here just in case at the moment.
%    \begin{macrocode}
  \def\tikzmark@boxz@{%
    \ifmeasuring@
    \tikzmark@ams@boxz@
    \else
    \stepcounter{tikzmarkequation}%
    \tikzmark@box{\tikzmark@ams@name-\thetikzmarkequation}{\z@}%
    \fi
  }
%    \end{macrocode}
%
% This is the environment that sets the node name and swaps out the box code.
% At the end of the environment we swap back the code so that the commands can be used as standalone \Verb+\tikzmarkmath+ and \Verb+\endtikzmarkmath+ in occasions when it isn't appropriate to use an environment (for example, if it crosses sections, or if it is wanted to turn on this feature for an entire document).
% At the end of the environment, the number of nodes is written out to the terminal and log file to make it easier to keep track.
%    \begin{macrocode}
  \newenvironment{tikzmarkmath}[1][equation]{%
    \def\tikzmark@ams@name{#1}%
    \setcounter{tikzmarkequation}{0}%
    \let\boxz@=\tikzmark@boxz@
  }{%
    \let\boxz@=\tikzmark@ams@boxz@
    \message{%
      Tikzmark math environment
      \tikzmark@ams@name\space had
      \the\value{tikzmarkequation} nodes in it
    }%
  }
%    \end{macrocode}
% \end{macro}
% \begin{macrocode}
}{%
  \PackageError{tikzmark AMS}%
  {The amsmath package has not been loaded.}%
  {}
}
%    \end{macrocode}
% \iffalse
%</ams>
% \fi
%
% \iffalse
%<*highlighting>
% \fi
%
% \subsection{Highlighting}
%
% An early use of \Verb!\tikzmark! was to add highlighting to text by drawing over or under the text between two tikzmarks, for example the question \href{http://tex.stackexchange.com/q/46434/86}{How to "highlight" text/formulas with tikz?}.
%
% I was never totally happy with the overall mechanism, so didn't include it in the main tikzmark package.
% Recently, I had occasion to revisit it and by using the new \LaTeX3 hook facility I got something that I was sufficiently happy with to add to the main package.
%
% The key idea is to hook into the \texttt{shipout/background} routine to insert the highlighting behind the text.
% This allows us to draw the highlighting before the page is laid out and so is under the text.
%
% \LaTeX3 makes life just that little bit easier.
%    \begin{macrocode}
\ExplSyntaxOn
%    \end{macrocode}
%
% Since the code that draws the highlighting will probably be very separate from the code that defines it, when storing the highlighting code then we want to expand the tikzmark full name.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \tikzmark_fix_name:Nn #1#2
{
  \tl_set:Nx #1 {\tikzmark@pp@name{#2}}
}
%    \end{macrocode}
%
% \begin{macro}{\StartHighlighting,\EndHighlighting,\Highlight}
% These are the user interfaces for highlighting a section.
% The first command inserts the drawing code into the relevant hook and places a tikzmark at the current location.
% The second command indicates when the highlighting should stop.
% The third is a short cut for highlighting its argument.
%
% These are commands rather than an environment to allow it to span, for example, different parts of an aligned equation.
%    \begin{macrocode}
\tl_new:N \g__tikzmark_highlighter_tl
\tl_set:Nn \g__tikzmark_highlighter_tl {tikzmark~ highlighter~}
\int_new:N \g__tikzmark_highlighter_int
\tl_new:N \l__tikzmark_start_tl
\tl_new:N \l__tikzmark_end_tl
\tl_new:N \l__tikzmark_highlighter_name_tl
\tl_new:N \l__tikzmark_tmpa_tl
\tl_new:N \l__tikzmark_tmpb_tl
\tl_new:N \l__tikzmark_tmpc_tl

\cs_new_protected_nopar:Npn \tikzmark_bake_highlighter:N #1
{
  \tl_clear:N #1
  \clist_map_inline:nn {direction,layer}
  {
    \tl_put_right:Nx #1 {
      /tikz/highlighter/##1=\pgfkeysvalueof{/tikz/highlighter/##1},
    }
  }
  \clist_map_inline:nn {
    initial~ height,
    initial~ depth,
    initial~ offset,
    final~ height,
    final~ depth,
    final~ offset,
    left~ margin,
    right~ margin,
    top~ margin,
    bottom~ margin,
  }
  {
    \tl_put_right:Nx #1 {
      /tikz/highlighter/##1=\dim_eval:n {\pgfkeysvalueof{/tikz/highlighter/##1}},
    }
  }
}

\cs_new_protected_nopar:Npn \tikzmark_start_highlighting:n #1
{
  \int_gincr:N \g__tikzmark_highlighter_int
  \tl_set:Nx \l__tikzmark_highlighter_name_tl
  {
    \tl_use:N \g__tikzmark_highlighter_tl
    \int_use:N \g__tikzmark_highlighter_int
  }
  \tl_set:Nn \l__tikzmark_tmpb_tl
  {
    every~ highlighter/.try,
  }
  \tikzmark_bake_highlighter:N \l__tikzmark_tmpc_tl
  \tl_put_right:NV \l__tikzmark_tmpb_tl \l__tikzmark_tmpc_tl
  \tl_put_right:Nn \l__tikzmark_tmpb_tl {#1}
  \tikzmark_process_highlighting:VV
  \l__tikzmark_tmpb_tl
  \l__tikzmark_highlighter_name_tl
  \tikzmark{highlight-start-\tl_use:N \l__tikzmark_highlighter_name_tl}
}
\cs_new_protected_nopar:Npn \tikzmark_end_highlighting:
{
  \tl_set:Nx \l__tikzmark_highlighter_name_tl
  {
    \tl_use:N \g__tikzmark_highlighter_tl
    \int_use:N \g__tikzmark_highlighter_int
  }
  \tikzmark{highlight-end-\tl_use:N \l__tikzmark_highlighter_name_tl}
}

\NewDocumentCommand \StartHighlighting {O{}}
{%
  \tikzmark_start_highlighting:n {#1}
}
\NewDocumentCommand \StopHighlighting {}
{%
  \tikzmark_end_highlighting:
}
\NewDocumentCommand \Highlight {O{} m}
{%
  \tikzmark_start_highlighting:n {#1}
  #2
  \tikzmark_end_highlighting:
}
%    \end{macrocode}
% \end{macro}
%
% The following code inserts the drawing command into the shipout hook.
%    \begin{macrocode}
%    \end{macrocode}
% We need an ordinary colon, rather than a \LaTeX3 one
%    \begin{macrocode}
\tl_const:Nx \c__tikzmark_colon_tl
{
  \char_generate:nn {`:} {12}
}

\cs_generate_variant:Nn \hook_gput_next_code:nn {nV}
%    \end{macrocode}
% 
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \tikzmark_highlight_or_shunt:nnnn #1#2#3#4
{
%    \end{macrocode}
% First, test to check if the tikzmarks are actually defined yet,
% if not then bail out.
%    \begin{macrocode}
  \bool_lazy_all:nT
  {
    {\tl_if_exist_p:c {save@pt@\tikzmark@pp@name{#2}}}
    {\tl_if_exist_p:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}}
    {\tl_if_exist_p:c {save@pt@\tikzmark@pp@name{#3}}}
    {\tl_if_exist_p:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#3}}}}
  }
  {
%    \end{macrocode}
% Okay, so all the tikzmarks are defined. Now see if we're on the
% right page.  Is our start tikzmark in the future?
%    \begin{macrocode}
    \int_compare:nTF
    {
      \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}
      >
      \the\value{page}
    }
    {
%    \end{macrocode}
% It is, so we just punt our highlighting down the line
%    \begin{macrocode}
      \hook_gput_next_code:nn {#1} {
        \tikzmark_highlight_or_shunt:nnnn {#1}{#2}{#3}{#4}
      }
    }
    {
%    \end{macrocode}
% It isn't, so we have some highlighting to do.
% We need to build our highlighting code.
%    \begin{macrocode}
      \tl_set:Nn \l__tikzmark_tmpa_tl {#4}
%    \end{macrocode}
% Is our starting tikzmark on \emph{this} page?
%    \begin{macrocode}
      \int_compare:nTF
      {
        \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}
        =
        \the\value{page}
      }
      {
%    \end{macrocode}
% It is, so we use the starting tikzmark as our first coordinate.
%    \begin{macrocode}
        \tl_put_right:Nx \l__tikzmark_tmpa_tl
        {
          {
            pic~ cs
            \tl_use:N \c__tikzmark_colon_tl
            #2
          }
        }
      }
      {
%    \end{macrocode}
% It isn't, so we use the north west corner of the page
%    \begin{macrocode}
        \tl_put_right:Nn \l__tikzmark_tmpa_tl
        {
          {
            page.north~ west
          }
        }
      }
%    \end{macrocode}
% Is our ending tikzmark on \emph{this} page?
%    \begin{macrocode}
      \int_compare:nTF
      {
        \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#3}}}
        =
        \the\value{page}
      }
      {
%    \end{macrocode}
% It is, so we use the ending tikzmark as our second coordinate.
%    \begin{macrocode}
        \tl_put_right:Nx \l__tikzmark_tmpa_tl
        {
          {
            pic~ cs
            \tl_use:N \c__tikzmark_colon_tl
            #3
          }
        }
      }
      {
%    \end{macrocode}
% It isn't, so we use the south east corner of the page, and
% we have to shunt the code to the next page.
%    \begin{macrocode}
        \tl_put_right:Nn \l__tikzmark_tmpa_tl
        {
          {
            page.south~ east
          }
        }
        \hook_gput_next_code:nn {#1} {
          \tikzmark_highlight_or_shunt:nnnn {#1}{#2}{#3}{#4}
        }
      }
%    \end{macrocode}
% We've built our highlighting code, now's time to execute it.
%    \begin{macrocode}
      \tl_use:N \l__tikzmark_tmpa_tl
    }
  }
}
%    \end{macrocode}
% 
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \tikzmark_process_highlighting:nn #1#2
{
  \pgfkeys{/tikz/highlighter/configuration/.activate~ family}
  \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}

  \tikzmark_fix_name:Nn \l__tikzmark_start_tl {highlight-start-#2}
  \tikzmark_fix_name:Nn \l__tikzmark_end_tl {highlight-end-#2}
  \tl_set:Nx \l__tikzmark_tmpa_tl {\pgfkeysvalueof{/tikz/highlighter/direction}}
  \tl_clear:N \l__tikzmark_tmpb_tl
  \tl_clear:N \l__tikzmark_tmpc_tl
  \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {vertical}
  {
    \tl_put_right:Nn \l__tikzmark_tmpb_tl
    {
      \vl@draw
    }
  }
  {
    \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {box}
    {
      \tl_put_right:Nn \l__tikzmark_tmpb_tl
      {
        \box@draw
      }
    }
    {
      \tl_put_right:Nn \l__tikzmark_tmpb_tl
      {
        \hl@draw
      }
    }
  }
  
  \tl_put_right:Nn \l__tikzmark_tmpb_tl
  {
    {tikzmark~ clear~ ixes,#1}
  }

  \tl_set:Nx \l__tikzmark_tmpa_tl {\pgfkeysvalueof{/tikz/highlighter/layer}}
  \tl_set:Nn \l__tikzmark_tmpc_tl
  {
    \tikzmark_highlight_or_shunt:nnnn
  }
  \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {foreground}
  {
    \tl_put_right:Nn \l__tikzmark_tmpc_tl {{shipout/foreground}}
  }
  {
    \tl_put_right:Nn \l__tikzmark_tmpc_tl {{shipout/background}}
  }

  \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_start_tl
  \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_end_tl
  \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_tmpb_tl
 
  \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {foreground}
  {
    \hook_gput_next_code:nV {shipout/foreground} \l__tikzmark_tmpc_tl
  }
  {
    \hook_gput_next_code:nV {shipout/background} \l__tikzmark_tmpc_tl
  }
}
\cs_generate_variant:Nn  \tikzmark_process_highlighting:nn {nV,VV}
\ExplSyntaxOff
%    \end{macrocode}
%
% The command that draws the horizontal highligher or fader.
% This fills a shape determined by two coordinates assumed to be (in effect) on the baseline of the start and end of the region to be highlighted.
%    \begin{macrocode}
\def\hl@draw#1#2#3{%
  \pgfkeys{/tikz/highlighter/configuration/.activate family}
  \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
  \begin{tikzpicture}[
    remember picture,
    overlay,
    highlight picture action,
    #1,
  ]%
%
  \page@node
%
  \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
  \pgf@ya=\pgf@y
  \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
  \pgf@yb=\pgf@y
%
  \ifdim\pgf@ya=\pgf@yb
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
  \pgfkeysvalueof{/tikz/highlighter/initial height})
  coordinate (start);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
  -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
  coordinate (end);
%
  \path[
    highlight action,
    #1
  ] (start) rectangle (end);
%
  \else
%
  \path (page.east)
  ++(\pgfkeysvalueof{/tikz/highlighter/right margin},0pt)
  coordinate (east);
%
  \path (page.west)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/left margin},0pt)
  coordinate (west);
%
  \pgfmathsetlength\pgf@x{%
    \pgfkeysvalueof{/tikz/highlighter/initial height}%
  }%
%
  \advance\pgf@yb by \pgf@x\relax
%
  \pgfmathsetlength\pgf@x{%
    -1*\pgfkeysvalueof{/tikz/highlighter/final depth}%
  }%
%
  \advance\pgf@ya by \pgf@x\relax
%
  \ifdim\pgf@yb>\pgf@ya
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
  \pgfkeysvalueof{/tikz/highlighter/initial height}) 
  coordinate (start);
%
  \path (#2)
  ++(0pt,-1*\pgfkeysvalueof{/tikz/highlighter/final depth}) 
  coordinate (end);
%
  \path[
    highlight action,
    #1
  ] (start) rectangle (end -| east);
%
  \path (#3)
  ++(0pt,\pgfkeysvalueof{/tikz/highlighter/initial height}) 
  coordinate (start);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
  -1*\pgfkeysvalueof{/tikz/highlighter/final depth}) 
  coordinate (end);
%
  \path[
    highlight action,
    #1
  ] (start -| west) rectangle (end);
%
  \else
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
  \pgfkeysvalueof{/tikz/highlighter/initial height}) 
  coordinate (tl);
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
  -1*\pgfkeysvalueof{/tikz/highlighter/initial depth}) 
  coordinate (start);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
  -1*\pgfkeysvalueof{/tikz/highlighter/final depth}) 
  coordinate (end);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
  \pgfkeysvalueof{/tikz/highlighter/final height}) 
  coordinate (mr);
%
  \path[
    highlight action,
    #1
  ] (start) -- (tl) -- (tl -| east) -- (mr -| east) -- (mr) --
  (end) -- (end -| west) -- (start -| west) -- cycle;
%
  \fi
  \fi
  \end{tikzpicture}%
}
%    \end{macrocode}
%
% This one draws a box.
%    \begin{macrocode}
\def\box@draw#1#2#3{%
  \pgfkeys{/tikz/highlighter/configuration/.activate family}
  \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
  \begin{tikzpicture}[
    remember picture,
    overlay,
    highlight picture action,
    #1,
  ]%
%
  \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
  \pgf@xa=\pgf@x
  \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
  \pgf@xb=\pgf@x
%
  \def\tkmk@high@bscale{1}%
  \ifdim\pgf@xa>\pgf@xb
  \def\tkmk@high@bscale{-1}%
  \fi
%
  \path (#2)
  ++({\tkmk@high@bscale*(-1)*\pgfkeysvalueof{/tikz/highlighter/initial offset}},
  \pgfkeysvalueof{/tikz/highlighter/initial height})
  coordinate (start);
%
  \path (#3)
  ++(\tkmk@high@bscale*\pgfkeysvalueof{/tikz/highlighter/final offset},
  -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
  coordinate (end);
%
  \path[
    highlight action,
    #1
  ] (start) rectangle (end);
  \end{tikzpicture}%
}
%    \end{macrocode}
%
% In this one the region is defined vertically.
%    \begin{macrocode}
\def\vl@draw#1#2#3{%
  \pgfkeys{/tikz/highlighter/configuration/.activate family}
  \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
  \begin{tikzpicture}[
    remember picture,
    overlay,
    highlight picture action,
    #1,
  ]%
%
  \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
  \pgf@ya=\pgf@y
  \pgf@xa=\pgf@x
  \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
  \pgf@yb=\pgf@y
  \pgf@xb=\pgf@x
%
  \pgfmathsetlength\pgf@y{%
    \pgfkeysvalueof{/tikz/highlighter/initial offset}%
  }%
  \advance\pgf@yb by \pgf@y
  \pgfmathsetlength\pgf@y{%
    -1*\pgfkeysvalueof{/tikz/highlighter/final offset}%
  }%
  \advance\pgf@ya by \pgf@y
%
  \ifdim\pgf@yb>\pgf@ya
%
  \ifdim\pgf@xa>\pgf@xb
%
  \path (#2)
  ++(\pgfkeysvalueof{/tikz/highlighter/initial height},
  \pgfkeysvalueof{/tikz/highlighter/initial offset})
  coordinate (start);
%
  \path (#3)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/final depth},
  -1*\pgfkeysvalueof{/tikz/highlighter/final offset})
  coordinate (end);
%  
  \else
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial depth},
  \pgfkeysvalueof{/tikz/highlighter/initial offset})
  coordinate (start);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final height},
  -1*\pgfkeysvalueof{/tikz/highlighter/final offset})
  coordinate (end);
%  
  \fi
%
  \path[
    highlight action,
    #1
  ] (start) rectangle (end);
%
  \else
%
  \path (#2)
  ++(\pgfkeysvalueof{/tikz/highlighter/initial height},0)
  coordinate (tr);
%
  \path (#2)
  ++(0,\pgfkeysvalueof{/tikz/highlighter/initial offset})
  coordinate (start);
%
  \path (#2)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial depth},0)
  coordinate (tl);
%
  \path (#3)
  ++(\pgfkeysvalueof{/tikz/highlighter/final height},0)
  coordinate (br);
%
  \path (#3)
  ++(0,-1*\pgfkeysvalueof{/tikz/highlighter/final offset})
  coordinate (end);
%
  \path (#3)
  ++(-1*\pgfkeysvalueof{/tikz/highlighter/final depth},0)
  coordinate (bl);
%
  \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
  \pgf@xa=\pgf@x
  \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
  \pgf@xb=\pgf@x
%
  \ifdim\pgf@xa<\pgf@xb
%
  \path[
    highlight action,
    #1
  ] (tl) |- (start) -| (tr) -| (br) |- (end) -| (bl) -| cycle;
%
  \else
%
  \path[
    highlight action,
    #1
  ] (tl) |- (start) -| (tr) |- (br) |- (end) -| (bl) |- cycle;
%
  \fi
%
  \fi
  \end{tikzpicture}
}
%    \end{macrocode}
%
% These set various options.
%    \begin{macrocode}
\tikzset{%
  /tikz/highlighter/.is family,
  /tikz/highlighter/.unknown/.code={%
    \let\tk@searchname=\pgfkeyscurrentname%
    \pgfkeysalso{%
      /tikz/\tk@searchname=#1
    }
  },
  every highlight path/.style={
    fill=yellow!50,
    rounded corners,
  },
  every foreground highlight path/.style={
    fill opacity=.5,
  },
  highlight picture action/.style={
    every highlight picture/.try,
    every \pgfkeysvalueof{/tikz/highlighter/direction} highlight picture/.try,
    every \pgfkeysvalueof{/tikz/highlighter/layer} highlight picture/.try,
  },
  highlight action/.style={
    every highlight path/.try,
    every \pgfkeysvalueof{/tikz/highlighter/direction} highlight path/.try,
    every \pgfkeysvalueof{/tikz/highlighter/layer} highlight path/.try,
    highlight path/.try,
    \pgfkeysvalueof{/tikz/highlighter/direction} highlight path/.try,
    \pgfkeysvalueof{/tikz/highlighter/layer} highlight path/.try,
  },
  /tikz/highlighter/.cd,
  direction/.initial=horizontal,
  layer/.initial=background,
  direction/.default=horizontal,
  layer/.default=background,
  initial height/.initial=\baselineskip,
  initial depth/.initial=.5ex,
  initial offset/.initial=.5\baselineskip,
  final height/.initial=\baselineskip,
  final depth/.initial=.5ex,
  final offset/.initial=.5\baselineskip,
  left margin/.initial=.5\baselineskip,
  right margin/.initial=.5\baselineskip,
  top margin/.initial=.5\baselineskip,
  bottom margin/.initial=-.5\baselineskip,
  height/.style={
    initial height=#1,
    final height=#1
  },
  depth/.style={
    initial depth=#1,
    final depth=#1
  },
  offset/.style={
    initial offset=#1,
    final offset=#1
  },
  margin/.style={
    left margin=#1,
    right margin=#1,
    top margin=#1,
    bottom margin=#1,
  },
  /tikz/highlighter/configuration/.is family,
  /tikz/highlighter/direction/.belongs to family=/tikz/highlighter/configuration,
  /tikz/highlighter/layer/.belongs to family=/tikz/highlighter/configuration,
}
%    \end{macrocode}
%
%
%    \begin{macrocode}
\def\page@node{
  \path (current page.north west)
  ++(\hoffset + 1in + \oddsidemargin + \leftskip,
  -\voffset - 1in - \topmargin - \headheight - \headsep)
  node[
    minimum width=\textwidth - \leftskip - \rightskip,
    minimum height=\textheight,
    anchor=north west,
    line width=0mm,
    inner sep=0pt,
    outer sep=0pt,
  ] (page) {};
}
%    \end{macrocode}
%
%
%
% \iffalse
%</highlighting>
% \fi
%
% \Finale
\endinput