summaryrefslogtreecommitdiff
path: root/macros/generic/tokcycle/tokcycle-examples.tex
blob: 891741bd4bed5733fc45622282bf80c76faa23fe (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
\documentclass[10pt]{article}
\usepackage[margin=1.3in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,xcolor,manfnt,lmodern}
\colorlet{bred}{red!90!blue}
\usepackage{tikz}
% MACROS: REDEFINE TO ALLOW \cytoks TO ESCAPE SCOPE OF exampleA
\aftertokcycle{\global\cytoks\expandafter{\the\cytoks}}
% ENVIRONMENTS: REDEFINE TO ALLOW \cytoks TO ESCAPE SCOPE OF exampleA
\makeatletter\renewcommand\tcenvscope{\global\cytoks}\makeatother

\def\ddbend{\setbox0=\hbox{\dbend\dbend}\leavevmode\llap{%
  \raisebox{10pt}{\box0~}}}

\usepackage[skins,listings]{tcolorbox}

\newtcblisting{exampleA}[2][]{%
  colframe=red!70!yellow!50!black,
  colback=red!75!yellow!5!white,
  coltitle=red!50!yellow!3!white,
  bicolor,colbacklower=white,
  top=0mm,bottom=0mm,left=0mm,right=0mm,
  arc=1mm,boxrule=1pt,
  fonttitle=\sffamily\bfseries,
  sidebyside,
  title=#2,#1}

\newtcblisting{exampleB}[2][]{%
  colframe=red!50!yellow!50!black,
  colback=red!50!yellow!5!white,%white,
  coltitle=red!50!yellow!3!white,
  bicolor,colbacklower=white,%red!50!yellow!5!white,
  fonttitle=\sffamily\bfseries,
  sidebyside,text and listing,
  top=0mm,bottom=0mm,left=0mm,right=0mm,
  arc=1mm,boxrule=1pt,
  title=#2,#1}

\newtcblisting{exampleC}[2][]{%
  colframe=cyan!70!yellow!50!black,
  colback=cyan!30!yellow!5!white,%white,
  coltitle=cyan!50!yellow!3!white,
  fonttitle=\sffamily\bfseries,
  listing only,
  top=0mm,bottom=0mm,left=0mm,right=0mm,
  arc=1mm,boxrule=1pt,
  title=#2,#1}

\def\altdecytoks{\par\medskip\begingroup\noindent\macname{cytoks}
  \ \textit{alt}detokenization:\\\raggedright\footnotesize\ttfamily
  \expandafter\altdetokenize\expandafter{\the\cytoks}\normalsize\par\endgroup}

\newcommand\TokCycle{\textsf{tokcycle}}

\parindent0pt
\parskip0pt
\begin{document}
{\centering\LARGE \TokCycle{} Package Examples\par\smallskip
\large\today\par}

\tableofcontents

%%%%%%%%%%%%%% SUPPORT MACRO

\newcommand\macname[1]{\texttt{\char92#1}} 

\newcommand\underdot[1]{\ooalign{#1\cr\hfil{\raisebox{-5pt}{.}}\hfil}}

\newcounter{colorindex}

\newcommand\restorecolor{\setcounter{colorindex}{100}}

\newcommand\reducecolor[1]{%
  \color{red!\thecolorindex!cyan}%
  \addtocounter{colorindex}{-#1}%
  \ifnum\thecolorindex<1\relax\setcounter{colorindex}{1}\fi
}

\newif\ifmacro
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    { \hspace{0pt plus 3em minus .3ex}}
    {#1}%
  \unskip
\endgroup}

\newcommand\plusl[1]{\char\numexpr`#1+1\relax}

\newcommand\vowelcap[1]{%
  \ifx a#1A\else
  \ifx e#1E\else
  \ifx i#1I\else
  \ifx o#1O\else
  \ifx u#1U\else
  #1\fi\fi\fi\fi\fi
}

\newcommand\spaceouttext[2]{%
  \tokcycle
    {\addcytoks{##1\nobreak\hspace{#1}}}%
    {\processtoks{##1}}
    {\addcytoks{##1}}%
    {\addcytoks{##1\hspace{#1}}}
    {#2}%
  \the\cytoks\unskip}

\newcommand\growdim[2]{%
\tokcycle{\addcytoks{##1}}
         {\addcytoks{#1\dimexpr##1}}
         {\addcytoks{##1}}
         {\addcytoks{##1}}{%
    #2}%
\the\cytoks}

\newcommand\nextcap[1]{%
       \edef\tmp{#1}%
       \tctestifx{-#1}{\def\capnext{T}}{}%
       \tctestifcon{\if T\capnext}%
         {\tctestifcon{\ifcat\tmp A}%
           {\uppercase{#1}\def\capnext{F}}%
           {#1}}%
         {#1}%
}

\newcommand\TitleCase[1]{%
  \def\capnext{T}
  \tokcycle
    {\addcytoks{\nextcap{##1}}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{##1\def\capnext{T}}}
    {#1}%
  \the\cytoks
}

\tokcycleenvironment\spaceBgone
    {\addcytoks{##1}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{\hspace{.2pt plus .2pt minus .8pt}}}%


\tokcycleenvironment\remaptext
    {\addcytoks[x]{\tcremap{##1}}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{##1}}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
  \csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am   \tcmapto bf   \tcmapto cz   \tcmapto de   \tcmapto ey
\tcmapto fl   \tcmapto gx   \tcmapto hb   \tcmapto ic   \tcmapto jn
\tcmapto ki   \tcmapto lr   \tcmapto mh   \tcmapto nt   \tcmapto ok
\tcmapto ps   \tcmapto qa   \tcmapto ro   \tcmapto sq   \tcmapto tw
\tcmapto uj   \tcmapto vp   \tcmapto wd   \tcmapto xg   \tcmapto yu
\tcmapto zv

\tokcycleenvironment\greek%     NEW TOKCYCLE ENVIRONMENT:
{\addcytoks[4]{\tcremap{##1}}}% HOW TO TREAT CHARACTERS
{\processtoks{##1}}%            HOW TO TREAT GROUP CONTENTS
{\addcytoks{##1}}%              HOW TO TREAT MACROS
{\addcytoks{##1}}%              HOW TO TREAT SPACES

\newcommand\findinstring[2]{\begingroup%
  \stripgroupingtrue
  \setcounter{runcount}{0}%
  \tokcycle
    {\nextctltok{##1}}
    {\nextctltok{\opengroup}\processtoks{##1}\nextctltok{\closegroup}}
    {\nextctltok{##1}}
    {\nextctltok{\tcspace}}
    {#1}%
  \edef\numlet{\theruncount}%
  \expandafter\def\expandafter\searchword\expandafter{\the\cytoks}%
%
  \aftertokcycle{\matchfound}%
  \setcounter{runcount}{0}%
  \def\matchfound{F}%
  \tokcycle
    {\nextcmptok{##1}}
    {\nextcmptok{\opengroup}\processtoks{##1}\nextcmptok{\closegroup}}
    {\nextcmptok{##1}}
    {\nextcmptok{\tcspace}}
    {#2}%
\endgroup}
\newcounter{runcount}
\makeatletter
\newcommand\rotcytoks[1]{\cytoks\expandafter\expandafter\expandafter{%
  \expandafter\tc@gobble\the\cytoks#1}}
\makeatother
\newcommand\testmatch[1]{\ifx#1\searchword\gdef\matchfound{T}\fi}%
\newcommand\rotoradd[2]{\stepcounter{runcount}%
  \ifnum\theruncount>\numlet\relax#1\else#2\fi
  \expandafter\def\expandafter\tmp\expandafter{\the\cytoks}}
\newcommand\nextcmptok[1]{\rotoradd{\rotcytoks{#1}}{\addcytoks{#1}}\testmatch{\tmp}}
\newcommand\nextctltok[1]{\stepcounter{runcount}\addcytoks{#1}}

\newcommand\detokenizespacetok[1]{\tctestifcon{\ifimplicittok}%
  {\tctestifcon{\ifactivetok}{\theactivespace}{\string#1}}{\detokenize{#1}}}

\newcounter{sigd}
\newcounter{trailingz}
\newcommand\sigdigits[1]{%
  \setcounter{sigd}{0}% SIGNIFICANT DIGIT COUNT
  \setcounter{trailingz}{0}% TRAILING ZERO COUNT
  \def\founddot{F}% MADE T WHEN DECIMAL HAS BEEN 1ST LOCATED
  \def\zerosig{F}% MADE T WHEN ZERO BECOMES SIGNIFICANT
  \tokcycle% CYCLE THROOUGH EACH TOKEN
  {\tctestifx{.##1}%
   {\if T\founddot\throwerr\fi\def\founddot{T}}% IF .
   {\tctestifnum{`##1>`/}%
    {\tctestifnum{`##1<`:}%
     {\tctestifnum{##1>0}%
      {\stepcounter{sigd}\def\zerosig{T}\setcounter{trailingz}{0}}% IF 1-9
      {\if T\zerosig\stepcounter{sigd}\fi\stepcounter{trailingz}}% IF 0
     }%
     {\throwerr}% IF ASCII > `9
    }%
    {\throwerr}% IF ASCII < `0
   }%
  }% APPLY ABOVE LOGIC FOR CHAR TOKENS
  {\throwerr}% IF BRACES
  {\throwerr}% IF CONTROL SEQUENCE
  {}% IGNORE SPACES
  {#1}% THE ARGUMENT
  \addtocounter{sigd}{-\thetrailingz}% SUBTRACT TRAILING ZEROS
  \expandafter\ifx\expandafter\relax\detokenize\expandafter{\the\cytoks}%
  \relax\else\setcounter{sigd}{-1}\fi% CHECK FOR INVALID INPUT
  \thesigd
}
\newcommand\throwerr{\addcytoks{##1}}% ADD INVALID TOKS TO \cytoks

\newcounter{Tcount}
\xtokcycleenvironment\capTive% ENVIRONMENT NAME
{\tctestifx{t##1}{\stepcounter{Tcount}\addcytoks{T}}{\addcytoks{##1}}}% CHAR. DIRECTIVE
{\processtoks{##1}}% GROUP DIRECTIVE
{\addcytoks{##1}}% MACRO DIRECTIVE
{\addcytoks{##1}}% SPACE DIRECTIVE
{\setcounter{Tcount}{0}\color{red}\tcafterenv{\ Here}}% INITIALIZATION CODE
{\theTcount{} t's capitalized: \addcytoks{!!}}% CLOSE-OUT CODE

\newcounter{wordcount}
\newcounter{lettercount}
\newif\ifinword
\newif\ifrunningcount
\newcommand\dumpword{%
  \addcytoks[1]{\accumword}%
  \tctestifcon\ifinword{\stepcounter{wordcount}%
    \ifrunningcount\addcytoks[x]{$^{\thewordcount,\thelettercount}$}\fi}{}%
  \inwordfalse\def\accumword{}}
\makeatletter
\newcommand\addletter[1]{%
  \tctestifcatnx A#1{\stepcounter{lettercount}\inwordtrue}{\dumpword}%
  \tc@defx\accumword{\accumword#1}}
\makeatother

\xtokcycleenvironment\countem
  {\addletter{##1}}
  {\processtoks{##1}\dumpword}
  {\dumpword\addcytoks{##1}}
  {\dumpword\addcytoks{##1}}
  {\def\accumword{}\setcounter{wordcount}{0}\setcounter{lettercount}{0}}
  {\dumpword\ifrunningcount\else\tcafterenv{\par(Wordcount=\thewordcount,
    Lettercount=\thelettercount)}\fi}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Examples, examples, and more examples}

Often, the best way to learn a new tool is to see examples of
  it being used.
Here, a number of examples are gathered that span the spectrum 
  of \TokCycle{} usage.

\subsection{Application basics}

\subsubsection{Using the CGMS directives}

Apply different directives to Characters (under-dot), Groups
  (visible braces), Macros (boxed, detokenized), and 
  Spaces (visible space).

\begin{exampleC}[]{The \macname{underdot} macro}
\newcommand\underdot[1]{\ooalign{#1\cr\hfil{\raisebox{-5pt}{.}}\hfil}}
\end{exampleC}


\begingroup
\begin{exampleA}[lefthand width=7.8cm]{}
\tokcycle{\addcytoks{\underdot{#1}}}
         {\addcytoks{\{}\processtoks{#1}
          \addcytoks{\}}}
         {\addcytoks{\fbox{\detokenize{#1}}}}
         {\addcytoks{\textvisiblespace}}
{This \textit{is \textbf{a}} test.}
\the\cytoks
\end{exampleA}

\endgroup

\subsubsection{Escaping text}

Text between two successive escape characters is bypassed
  by \TokCycle{} and instead echoed to the output register.
Default escape character is $|$.  One can change it with
  \macname{settcEscapechar} macro.

\begingroup

\begin{exampleC}[]{The unexpandable \macname{plusl} macro to increase
 the ASCII value of tokens by 1}
\newcommand\plusl[1]{\char\numexpr`#1+1\relax}
\end{exampleC}

\begin{exampleA}[lefthand width=5cm]{Escaping text and macro parameters 
  in the input stream}
\tokcycle
{\addcytoks{\plusl{#1}}}
{\processtoks{#1}}
{\addcytoks{#1}}
{\addcytoks{#1}}
{This \fbox{code is a test 
  |(I can also escape text)|}
  of |\rule{1em}{.5em}|
  {\bfseries mine}.}
\the\cytoks
\end{exampleA}

\endgroup

\subsubsection{Unexpandable, unexpanded, and expanded Character 
  directives}

\begingroup
This section concerns the issue of whether the characters of the input
  stream are transformed before or after being placed in the 
  output token list (\macname{cytoks}).

\medskip
Transform characters (+1 ASCII) via unexpandable macro:\medskip

\begin{exampleA}[lefthand width=8.3cm]
  {Unexpandable Character directive}
\tokcycle
{\addcytoks{\plusl{#1}}}
{\processtoks{#1}}
{\addcytoks{#1}}
{\addcytoks{#1}}{%
  This \textit{code \textup{is} a test} of mine.}
\the\cytoks
\end{exampleA}

\altdecytoks

Note how the above detokenization shows the actual transformation
  instructions involving \macname{plusl}.
This is done because \macname{plusl} is not expandable.

\par\noindent\hrulefill\medskip
\endgroup

\noindent\begin{minipage}[t]{\textwidth}
Capitalize vowels (but don't expand the character directive)

\begingroup

\begin{exampleC}[]{The expandable\macname{vowelcap} macro}
\newcommand\vowelcap[1]{%
  \ifx a#1A\else
  \ifx e#1E\else
  \ifx i#1I\else
  \ifx o#1O\else
  \ifx u#1U\else
  #1\fi\fi\fi\fi\fi
}
\end{exampleC}

\begin{exampleA}[lefthand width=8.3cm]
  {Not expanded Character directive}
\tokcycle
{\addcytoks{\vowelcap{#1}}}
{\processtoks{#1}}
{\addcytoks{#1}}
{\addcytoks{#1}}{%
  This \textit{code \textup{is} a test} of mine.}
\the\cytoks
\end{exampleA}

\altdecytoks 

\medskip
The above detokenization also shows the transformation macro,
  \macname{vowelcap}, as part of \macname{cytoks}.
But because \macname{vowelcap} is expandable, we did not need to
  leave it at this stage, as the next example shows.

\par\noindent\hrulefill\medskip
\endgroup
\end{minipage}

\begingroup
Capitalize vowels (expanding the character directive)

\begin{exampleA}[lefthand width=8.3cm]
  {Expanded Character directive}
\tokcycle
{\addcytoks[x]{\vowelcap{#1}}}
{\processtoks{#1}}
{\addcytoks{#1}}
{\addcytoks{#1}}{%
  This \textit{code \textup{is} a test} of mine.}
\the\cytoks
\end{exampleA}

\altdecytoks

\medskip
Here, we see no vestiges of \macname{vowelcap} in \macname{cytoks}, 
  but only the transformed result.
\endgroup

\subsubsection{Unexpanded vs. pre-expanded input stream}

\begingroup
\Characterdirective{\addcytoks[x]{\vowelcap{#1}}}

\noindent\begin{minipage}[t]{\textwidth}
\begin{exampleA}[lefthand width=5.5cm]{Normal token cycle (input stream
  not pre-expanded)}
\tokcycle
{\addcytoks[x]{\vowelcap{#1}}}
{\processtoks{#1}}
{\addcytoks{#1}}
{\addcytoks{#1}}%
{This \fbox{code 
  is a test \today} of 
  {\bfseries mine}.}
\the\cytoks
\end{exampleA}

\altdecytoks

\medskip
In the above case, vowel-capping is not applied to the date, because
  \macname{today} remains unexpanded in the \macname{cytoks} token list.

\noindent\hrulefill\strut
\end{minipage}

Note that, when pre-expanding the input stream, one must 
  \macname{noexpand} the macros that are \textit{not} 
  to be pre-expanded.

\begin{exampleA}[lefthand width=5.5cm]{Pre-\macname{expanded} token cycle
  input stream}
\expandedtokcyclexpress
{This \noexpand\fbox{code 
  is a test \today} of 
  {\noexpand\bfseries mine}.}
\the\cytoks
\end{exampleA}

\altdecytoks

\medskip
Here, we see the date subjected to vowel-capping, because the \TokCycle{}
  input was expanded prior to the token cycle.
Thus \macname{today} no longer appears \textit{explicitly} in the 
  \macname{cytoks} token list.
Its pre-expansion into character tokens is now subject to the 
  \macname{vowelcap}.

\endgroup

\subsection{Grouping}

Differentiating explicit groups, e.g., \{...\}, from
  implicit groups, e.g. \macname{bgroup}...\macname{egroup},
  is done automatically by \TokCycle.
The user has options on how \TokCycle{} should treat these tokens.
The desired options are to be set prior to the \TokCycle{} invocation.


\subsubsection{Treatment options for implicit groups}

\begingroup

The macro
\macname{stripimplicitgroupingcase} can take three possible
  integer arguments: 0 (default) to automatically place unaltered
  implicit group tokens in the output register; 1 to strip
  implicit group tokens from the output; or $-$1 to instead pass the
  implicit group tokens to the Character directive (as implicit
  tokens) for separate processing (typically, when detokenization
  is desired).

\begin{exampleA}[lefthand width=6cm]{Using 
  \macname{stripimplicitgroupingcase} to affect treatment of 
  implicit grouping tokens}
\resettokcycle
\Characterdirective{\addcytoks[x]{%
  \vowelcap{#1}}}
\def\z{Announcement: 
  {\bfseries\bgroup\itshape 
  Today \egroup it is} \today, 
  a Wednesday}
\expandafter\tokencyclexpress\z
\endtokencyclexpress\medskip

\detokenize\expandafter{\the\cytoks}
\bigskip

\stripimplicitgroupingcase{1}
\expandafter\tokencyclexpress\z
\endtokencyclexpress\medskip

\detokenize\expandafter{\the\cytoks}
\end{exampleA}

%\tokcyclexpress{Announcement: {\bfseries\bgroup\itshape 
%  Today \egroup it is} \today, a Wednesday.}\the\cytoks

%\detokenize\expandafter{\the\cytoks}

\endgroup

\subsubsection{Treatment options for explicit groups}

\begingroup

For explicit group tokens, e.g., \{ \}, there are only two 
  options to be had.
These are embodied in the if-condition \macname{ifstripgrouping} 
  (default \macname{stripgroupingfalse}).
Regardless of which condition is set, the tokens within the explicit
  group are still passed to the Group directive for processing.

\medskip
Permutations of the following code are used in the subsequent examples.
Group stripping, brought about by \macname{stripgroupingtrue},
  involves removing the grouping braces from around the group.
The choice of \macname{processtoks} vs.\@ \macname{addcytoks}
  affects whether the tokens inside the group are recommitted
  to \TokCycle{} for processing, or are merely sent to the output
  register in their original unprocessed form.

\medskip
Note that, in these examples, underdots and visible spaces
  will only appear on characters and spaces that have been directed 
  to the Character and Space directives, respectively.
Without \macname{processtoks}, that will not occur to tokens
  \textit{inside} of groups.

\begin{exampleB}[listing only,lefthand width = \dimexpr\textwidth-19pt]
{Code permutations on group stripping and inner-group token processing}
\stripgroupingfalse  OR  \stripgroupingtrue
\tokcycle{\addcytoks{\underdot{#1}}}
         {\processtoks{#1}}  OR  {\addcytoks{#1}}}
         {\addcytoks{#1}}
         {\addcytoks{\textvisiblespace}}
{This \fbox{is a \fbox{token}} test.}
\the\cytoks
\end{exampleB}


\begin{exampleB}[text only,  colback=white]%
{\macname{stripgroupingfalse \string\processtoks} }
\stripgroupingfalse% RETAIN GROUPING (PACKAGE DEFAULT)
\tokcycle{\addcytoks{\underdot{#1}}}
         {\processtoks{#1}}% GROUP TOKENS ARE PROCESSED
         {\addcytoks{#1}}
         {\addcytoks{\textvisiblespace}}{%
This \fbox{is a \fbox{token}} test.}
\the\cytoks
\end{exampleB} 

\begin{exampleB}[text only,  colback=white]%
{\macname{stripgroupingfalse \string\addcytoks} }
\tokcycle{\addcytoks{\underdot{#1}}}
         {\addcytoks{#1}}% GROUP TOKENS OUTPUT DIRECTLY
         {\addcytoks{#1}}
         {\addcytoks{\textvisiblespace}}{%
This \fbox{is a \fbox{token}} test.}
\the\cytoks
\end{exampleB}

\begin{exampleB}[text only,  colback=white]%
{\macname{stripgroupingtrue \string\processtoks} }
\stripgroupingtrue% DANGEROUS AS IT REMOVES GROUPING FROM ARGUMENTS
\tokcycle{\addcytoks{{\underdot{#1}}}}% EXTRA GROUPING TO COMPENSATE
         {\processtoks{#1}}% GROUP TOKENS PROCESSED
         {\addcytoks{#1}}
         {\addcytoks{\textvisiblespace}}{%
This \fbox{is a \fbox{token}} test.}
\the\cytoks
\end{exampleB}

\begin{exampleB}[text only,  colback=white]%
{\macname{stripgroupingtrue \string\addcytoks} }
\stripgroupingtrue% DANGEROUS AS IT REMOVES GROUPING FROM ARGUMENTS
\tokcycle{\addcytoks{\underdot{#1}}}
         {\addcytoks{#1}}% GROUP TOKENS OUTPUT DIRECTLY
         {\addcytoks{#1}}
         {\addcytoks{\textvisiblespace}}{%
This \fbox{is a \fbox{token}} test.}
\the\cytoks
\end{exampleB} 

Note that the content of groups can be altogether eliminated if 
  \textit{neither} \macname{processtoks\{\#1\}} nor 
  \macname{addcytoks\{\#1\}} are used in the Group directive.



\endgroup

\subsubsection{Group nesting}

\begingroup

\begin{exampleC}[]{The \macname{reducecolor} and \macname{restorecolor}
  macros}
\newcounter{colorindex}
\newcommand\restorecolor{\setcounter{colorindex}{100}}
\newcommand\reducecolor[1]{%
  \color{red!\thecolorindex!cyan}%
  \addtocounter{colorindex}{-#1}%
  \ifnum\thecolorindex<1\relax\setcounter{colorindex}{1}\fi}
\end{exampleC}

\begin{exampleA}[lefthand width=6cm]{Group nesting is no impediment to
  tokcycle}
\restorecolor
\tokcycle
  {\addcytoks{(#1)}}
  {\addcytoks{\reducecolor{11}}%
   \addcytoks{[}\processtoks{#1}%
   \addcytoks{]}}
  {\addcytoks{#1}}
  {}{%
  {1{{3{{5{{7{{9{1{}0}}8}}6}}4}}2}}}
\the\cytoks
\end{exampleA}
Note, above, that \TokCycle{} does not rely on an argument-based nesting
  algorithm that would otherwise be limited to 9 embedded groups.
\endgroup

\subsubsection{The use of \macname{groupedcytoks} for more sophisticated
  group processing}

\begingroup

\begin{sloppypar}
By default, \TokCycle{} processes groups such that whatever additions to the
  \macname{cytoks} token list you accomplish in the Group directive will be 
  encased in an explicit grouped scope.
This is the token-cycle behavior with the default \macname{stripgroupingfalse} 
  declaration.
However, for more sophisticated tasks associated with the processing of
  group content, one may override the default (by way of 
  \macname{stripgroupingtrue}) and manually dictate the groupings associated 
  with the Group directive, with the use of \macname{groupedcytoks\{...\}}.
\end{sloppypar}

\medskip In the following example, tasks A and D are performed \textit{outside}
  of the explicit grouping otherwise introduced with \macname{groupedcytoks},
  whereas tasks B and C fall inside of that explicit group.
The fact that, in the output, \texttt{d} is not italic confirms the 
  fact that task D occurs after the conclusion of the explicit \macname{cytoks} 
  group that encompasses the italic `bxc'.
The fact that none of \texttt{a}, \texttt{b}, \texttt{c}, nor \texttt{d} are
  encased in an \macname{fbox} indicates that they were not processed through
  the Character directive, but arose from direct additions to \macname{cytoks},
  by way of their tasks.

\begin{exampleA}[lefthand width=10cm]{Use of the \macname{groupedcytoks}
  macro}
\stripgroupingtrue
\Characterdirective{\addcytoks{\fbox{#1}}}
\Groupdirective{\taskA%
  \groupedcytoks{\taskB\processtoks{#1}\taskC}%
  \taskD}
\def\taskA{\addcytoks{\bgroup\LARGE a}}
\def\taskB{\addcytoks{\itshape b}}
\def\taskC{\addcytoks{c}}
\def\taskD{\addcytoks{d\egroup}}

\tokcyclexpress{x\textbf{x}x}
\the\cytoks
\end{exampleA}

The final disposition of the \macname{cytoks} token list shows the effects of
  \macname{stripgroupingtrue} and \macname{groupedcytoks} more explicitly:

\altdecytoks

\medskip
Without \macname{stripgroupingtrue}, all tasks, A,B,C, and D, would have
  been contained within the explicit \texttt{\{...\}} group.

\endgroup



\subsection{Direct use of \TokCycle}

The \TokCycle{} macros and pseudo-environments
  (in regular or \texttt{xpress} form) may be invoked 
  directly from the document, without
  being first encapsulated within a separate macro or environment.

\subsubsection{Modifying counters as part of the Character
  directive}

\begingroup

\begin{exampleA}[lefthand width=8.5cm]{Using a period token (.) to
  reset a changing color every sentence}
\restorecolor
\tokencycle
  {\addcytoks{\bgroup\reducecolor{3}#1\egroup}%
   \ifx.#1\addcytoks{\restorecolor}\fi}
  {\processtoks{#1}}
  {\addcytoks{#1}}
  {\addcytoks{#1}}%
This right \textit{here is a sentence in italic}.  
And \textbf{here we have another sentence in bold}.

{\scshape Now in a new paragraph, the sentence 
is long.} Now, it is short.
\endtokencycle
\end{exampleA}

\endgroup

\subsection{Macro encapsulation of \TokCycle}

\subsubsection{Spacing out text}

\begingroup

\begin{exampleC}[]{The \macname{spaceouttext} macro}
\newcommand\spaceouttext[2]{%
  \tokcycle
    {\addcytoks{##1\nobreak\hspace{#1}}}%
    {\processtoks{##1}}
    {\addcytoks{##1}}%
    {\addcytoks{##1\hspace{#1}}}
    {#2}%
  \the\cytoks\unskip}
\end{exampleC}

\begin{exampleA}[lefthand width=6cm]{\macname{spaceouttext} demo}
\spaceouttext{3pt plus 3pt}{This 
  \textit{text \textbf{is} 
  very} spaced out}. Back 
  to regular text.

\spaceouttext{1.5pt}{This 
  \textit{text \textbf{is} 
  somewhat} spaced out}. 
  Back to regular text.
\end{exampleA}

Note that embedded macros such as \macname{textit} and \macname{textbf}
  do not adversely affect the capability of the \macname{spaceouttext}
  macro.
\endgroup


\subsubsection{Alternate presentation of detokenized content}

\begingroup
This macro attempts to give a more natural presentation of 
  \macname{detokenize}'d material.
It is \textbf{not} to be confused as a replacement for
  \macname{detokenize}.
In certain applications, it may offer a more pleasingly formatted
  typesetting of detokenized material.

\medskip It is an unusual application of \TokCycle{} in that it does not
  actually use the \macname{cytoks} token register to collect
  its output.
This is only possible because all macros in the input stream
  are detokenized, rather than executed.

\medskip

\begin{exampleC}[]{The \macname{altdetokenize }\ macro}
\newif\ifmacro
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    { \hspace{0pt plus 3em minus .3ex}}
    {#1}%
  \unskip
\endgroup}
\end{exampleC}

\let\mac\relax
\begin{exampleA}[lefthand width=6.3cm]
  {\macname{altdetokenize} demo}
\string\altdetokenize: \\
\texttt{\altdetokenize{a\mac a \mac2 {\mac}\mac{a\mac\mac}\mac}}!

versus \string\detokenize: \\ 
   \texttt{\detokenize{a\mac a \mac2 {\mac}\mac{a\mac\mac}\mac}}!
\end{exampleA}
\endgroup


\subsubsection{Capitalize all words, including compound and 
  parenthetical words}

\begingroup

\begin{exampleC}[]{The \macname{Titlecase} and \texttt{\string\nextcap} macros}
\newcommand\TitleCase[1]{%
  \def\capnext{T}
  \tokcycle
    {\addcytoks{\nextcap{##1}}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{##1\def\capnext{T}}}
    {#1}%
  \the\cytoks
}
\newcommand\nextcap[1]{%
       \edef\tmp{#1}%
       \tctestifx{-#1}{\def\capnext{T}}{}%
       \tctestifcon{\if T\capnext}%
         {\tctestifcon{\ifcat\tmp A}%
           {\uppercase{#1}\def\capnext{F}}%
           {#1}}%
         {#1}%
}
\end{exampleC}

\begin{exampleA}[]{A demo of \macname{Titlecase} showing raw (escaped)
  input and processed output}
\TitleCase{%
|here, {\bfseries\today{}, is [my]}
  really-big-test  
  (\textit{capitalizing} words).|

 here, {\bfseries\today{}, is [my]} 
  really-big-test 
  (\textit{capitalizing} words).}
\end{exampleA}

\endgroup

\subsubsection{Scaling rule dimensions}
\begingroup

\ddbend This example only applies if one can guarantee that
  the input stream will contain only text and rules...

\begin{exampleC}[]{The \macname{growdim} macro}
\newcommand\growdim[2]{%
\tokcycle{\addcytoks{##1}}
         {\addcytoks{#1\dimexpr##1}}
         {\addcytoks{##1}}
         {\addcytoks{##1}}{%
    #2}%
\the\cytoks}
\end{exampleC}

\begin{exampleA}[]{Using \TokCycle{} to change \macname{rule} dimensions}
\growdim{2}{This rule is exactly 4pt: 
  \rule|{4pt}{4pt}| , whereas this
  rule is 2x bigger than 4pt: 
  \rule{4pt}{4pt} .}\par
\growdim{4}{This rule is exactly 5pt: 
  \rule|{5pt}{5pt}| , whereas this
  rule is 4x bigger than 5pt: 
  \rule{5pt}{5pt} .}
\end{exampleA}

\endgroup

\subsubsection{String search, including non-regex material}

\begingroup

\begin{exampleC}[]{The \macname{findinstring} macro for string searches}
\newcommand\findinstring[2]{\begingroup%
  \stripgroupingtrue
  \setcounter{runcount}{0}%
  \tokcycle
    {\nextctltok{##1}}
    {\nextctltok{\opengroup}\processtoks{##1}\nextctltok{\closegroup}}
    {\nextctltok{##1}}
    {\nextctltok{\tcspace}}
    {#1}%
  \edef\numlet{\theruncount}%
  \expandafter\def\expandafter\searchword\expandafter{\the\cytoks}%
%
  \aftertokcycle{\matchfound}%
  \setcounter{runcount}{0}%
  \def\matchfound{F}%
  \tokcycle
    {\nextcmptok{##1}}
    {\nextcmptok{\opengroup}\processtoks{##1}\nextcmptok{\closegroup}}
    {\nextcmptok{##1}}
    {\nextcmptok{\tcspace}}
    {#2}%
\endgroup}
\newcounter{runcount}
\makeatletter
\newcommand\rotcytoks[1]{\cytoks\expandafter\expandafter\expandafter{%
  \expandafter\tc@gobble\the\cytoks#1}}
\makeatother
\newcommand\testmatch[1]{\ifx#1\searchword\gdef\matchfound{T}\fi}%
\newcommand\rotoradd[2]{\stepcounter{runcount}%
  \ifnum\theruncount>\numlet\relax#1\else#2\fi
  \expandafter\def\expandafter\tmp\expandafter{\the\cytoks}}
\newcommand\nextcmptok[1]{\rotoradd{\rotcytoks{#1}}{\addcytoks{#1}}\testmatch{\tmp}}
\newcommand\nextctltok[1]{\stepcounter{runcount}\addcytoks{#1}}
\end{exampleC}

\begin{exampleA}[lefthand width=11.5cm]{Demo of the 
  \macname{findinstring} macro}
1. \findinstring{this}{A test of the times}
   \findinstring{the} {A test of the times}\par
2. \findinstring{This is}{Here, This is a test}
   \findinstring{Thisis} {Here, This is a test}\par
3. \findinstring{the}        {This is the\bfseries{} test}
   \findinstring{he\bfseries}{This is the\bfseries{} test}\par
4. \findinstring{a{bc}} {gf{vf{a{b c}g}gh}hn}
   \findinstring{a{b c}}{gf{vf{a{b c}g}gh}hn}\par
5. \findinstring{a\notmymac{b c}}{gf{vf{a\mymac{b c}g}gh}hn}
   \findinstring{a\mymac{b c}}   {gf{vf{a\mymac{b c}g}gh}hn}\par
6. \findinstring{\textit{Italic}}{this is an \textit{italic} test}
   \findinstring{\textit{italic}}{this is an \textit{italic} test}
\end{exampleA}

\endgroup

\subsubsection{Counting the significant digits in a number}

\begingroup

The problem of counting the significant digits in a number is an interesting
  one, in that leading and trailing zeros are not significant, but zeros
  in the middle of the number are significant. 
In a problem like this, the \macname{cytoks} token list is not used to
  carry a transformation of the input tokens, but only to carry a record
  of errant tokens from the input (as seen from the helper macro
  \macname{throwerr}).
If \macname{the\textbackslash cytoks} is anything other than empty when the
  macro completes, the macro will set the significant-digit counter 
  \texttt{sigd} to a value of -1 to signify an error condition.

\medskip
The macro expects its input to comprise digits in the 0--9 range, an [optional]
  single decimal point, and ignorable spaces.  Any other token will throw
  an error.

\medskip
The main work of the \macname{sigdigits} macro is to use the counters
  \texttt{sigd} to count significant digits and \texttt{trailingz} to
  tabulate the number of trailing-zeros with which to decrement
  \texttt{sigd}.

\begin{exampleC}[]{The \macname{sigdigits} macro for counting significant digits}
\newcounter{sigd}
\newcounter{trailingz}
\newcommand\sigdigits[1]{%
  \setcounter{sigd}{0}% SIGNIFICANT DIGIT COUNT
  \setcounter{trailingz}{0}% TRAILING ZERO COUNT
  \def\founddot{F}% MADE T WHEN DECIMAL HAS BEEN 1ST LOCATED
  \def\zerosig{F}% MADE T WHEN ZERO BECOMES SIGNIFICANT
  \tokcycle% CYCLE THROOUGH EACH TOKEN
    {\tctestifx{.##1}%
     {\if T\founddot\throwerr\fi\def\founddot{T}}% IF .
     {\tctestifnum{`##1>`/}%
      {\tctestifnum{`##1<`:}%
       {\tctestifnum{##1>0}%
        {\stepcounter{sigd}\def\zerosig{T}\setcounter{trailingz}{0}}% IF 1-9
        {\if T\zerosig\stepcounter{sigd}\fi\stepcounter{trailingz}}% IF 0
       }%
       {\throwerr}% IF ASCII > `9
      }%
      {\throwerr}% IF ASCII < `0
     }%
    }% APPLY ABOVE LOGIC FOR CHAR TOKENS
    {\throwerr}% IF BRACES
    {\throwerr}% IF CONTROL SEQUENCE
    {}% IGNORE SPACES
    {#1}% THE ARGUMENT
  \addtocounter{sigd}{-\thetrailingz}% SUBTRACT TRAILING ZEROS
  \expandafter\ifx\expandafter\relax\detokenize\expandafter{\the\cytoks}%
  \relax\else\setcounter{sigd}{-1}\fi% CHECK FOR INVALID INPUT
  \thesigd
}
\newcommand\throwerr{\addcytoks{##1}}% ADD INVALID TOKS TO \cytoks
\end{exampleC}

\begin{exampleA}[lefthand width=8.0cm]{Example use of the \macname{sigdigits}
  macro}
1) \sigdigits{12.3}\\
2) \sigdigits{0.123}\\
3) \sigdigits{1.2.3}\\
4) \sigdigits{1.3\today}\\
5) \sigdigits{321.345 678}\\
6) \sigdigits{000321.305}\\
7) \sigdigits{.000 300 345}\\
8) \sigdigits{0003x1.345}\\
9) \sigdigits{123000}\\
A) \sigdigits{123.000}\\
B) \sigdigits{0003;345}
\end{exampleA}

\endgroup


\subsection{Creating your own \TokCycle-based environments}

The \macname{tokcycleenvironment} macro allows users to define
  their own \TokCycle{} environments.  
The more recently introduced \macname{xtokcycleenvironment} macro extends 
  that capability to allow initialization and postprocessing when defining
  a named \TokCycle{} environment.
Here are some examples of both types.

\subsubsection{``Removing'' spaces, but still breakable/hyphenatable}

\begingroup

\begin{exampleC}[]{The \macname{spaceBgone} environment}
\tokcycleenvironment\spaceBgone
    {\addcytoks{##1}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{\hspace{.2pt plus .2pt minus .8pt}}}%
\end{exampleC}

\begin{exampleA}[lefthand width=6.0cm]{Example of using the \macname{spaceBgone}
  environment}
\spaceBgone
  Here we have a \textit{test} of 
  whether the spaces are removed.  
  We are choosing to use the 
  tokencycle environment.

  We are also testing the use of 
  paragraph breaks in the 
  environment.
\endspaceBgone
\end{exampleA}

\endgroup

\subsubsection{Remapping text}

\begin{minipage}[t]{\textwidth}
\begingroup

\begin{exampleC}[]{The \macname{remaptext} environment with
  supporting macros}
\tokcycleenvironment\remaptext
    {\addcytoks[x]{\tcremap{##1}}}
    {\processtoks{##1}}
    {\addcytoks{##1}}
    {\addcytoks{##1}}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
  \csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am   \tcmapto bf   \tcmapto cz   \tcmapto de   \tcmapto ey
\tcmapto fl   \tcmapto gx   \tcmapto hb   \tcmapto ic   \tcmapto jn
\tcmapto ki   \tcmapto lr   \tcmapto mh   \tcmapto nt   \tcmapto ok
\tcmapto ps   \tcmapto qa   \tcmapto ro   \tcmapto sq   \tcmapto tw
\tcmapto uj   \tcmapto vp   \tcmapto wd   \tcmapto xg   \tcmapto yu
\tcmapto zv
\end{exampleC}

\begin{exampleA}[lefthand width=8.5cm]{Demo of \macname{remaptext}}
\remaptext
What can't we \textit{accomplish} if we try?

Let us be of good spirit and put our minds to it!
\endremaptext
\end{exampleA}

Because \macname{tcremap} is expandable, the original text is totally absent 
  from the processed output:

\altdecytoks

\endgroup
\end{minipage}

\bigskip
\begingroup

This approach can be used, for example, to temporarily remap Latin into Greek in 
  math mode.
In the example given below, only several of the Latin letters are remapped
  for demonstration purposes, but others can be added.
Also, remember that one can use the \verb+|...|+ delimiters to escape back to
  Latin within the environment.

\begin{exampleC}[]{The \macname{greek} environment with
  supporting macros}
\tokcycleenvironment\greek%     NEW TOKCYCLE ENVIRONMENT:
{\addcytoks[4]{\tcremap{##1}}}% HOW TO TREAT CHARACTERS
{\processtoks{##1}}%            HOW TO TREAT GROUP CONTENTS
{\addcytoks{##1}}%              HOW TO TREAT MACROS
{\addcytoks{##1}}%              HOW TO TREAT SPACES
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
  \csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto a\alpha \tcmapto b\beta \tcmapto e\epsilon 
\tcmapto g\gamma \tcmapto p\pi
\end{exampleC}

\tcmapto a\alpha \tcmapto b\beta \tcmapto e\epsilon 
\tcmapto g\gamma \tcmapto p\pi

\begin{exampleA}[lefthand width=8.5cm]{Demo of \macname{greek}}
\[
  y = 
  \greek a^2b\frac{2e}{2 + p^|p| + g}\endgreek 
  + x
\]
\end{exampleA}

\endgroup

\subsubsection{Creating an ``extended'' \TokCycle{} environment}

\begingroup
The \macname{xtokcycleenvironment} macro extends the capability of
  \macname{tokcycleenvironment} by adding two arguments that correspond,
  respectively, to initialization code and ``close-out'' code that is run 
  after the token cycle is complete (but before it is typeset).
Let's say I wanted an environment that capitalizes instances of the letter
  `\texttt{t}', but I want it to tell me how many
  letter \texttt{t}'s are affected before typesetting the result.
Furthermore, I want it always to set the result in red and add two  
  exclamation points to the end of the environment output.

\begin{exampleC}[]{The \macname{capTive} extended-environment definition}
\newcounter{Tcount}
\xtokcycleenvironment\capTive% ENVIRONMENT NAME
{\tctestifx{t##1}{\stepcounter{Tcount}\addcytoks{T}}{\addcytoks{##1}}}% CHAR. DIRECTIVE
{\processtoks{##1}}% GROUP DIRECTIVE
{\addcytoks{##1}}% MACRO DIRECTIVE
{\addcytoks{##1}}% SPACE DIRECTIVE
{\setcounter{Tcount}{0}\color{red}\tcafterenv{\ Here}}% INITIALIZATION CODE
{\theTcount{} t's capitalized: \addcytoks{!!}}% CLOSE-OUT CODE
\end{exampleC}

\begin{exampleA}[lefthand width=7.9cm]{Demo of \macname{capTive}
  environment}
Before \capTive the cytoks \footnotesize token list\endcapTive After
\end{exampleA}

In the initialization code, the \texttt{Tcount} counter is reset and the color
  of what follows is made red.
It also makes use of the specialized \macname{tcafterenv} macro that can
  \textbf{only be used in \macname{xtokcycle\-environment} definitions}.
This macro indicates code to execute \textit{after} the \texttt{capTive}
  environment scope is exited.
This invocation typesets the word `Here'.
We see that in the output, it occurs at normal size and in black, because
  it is no longer subject to the color and fontsize that was left inside
  the environment.
The \macname{tcafterenv} macro is unique in that it is an appending, rather
  than a defining, macro.
Therefore, multiple invocations of it will append multiple code segments,
  each of which will be executed upon environment exit.
It may be invoked in any of the directives, as well as the initialization and
  close-out code segments.

\medskip
For the close-out code, because it is executed after the token cycle is 
  complete, but before it is typeset, the updated value of \texttt{Tcount}
  is available.
Anything typeset directly in the close-out code will occur before the
  \macname{cytoks} token-list output is typeset. 
Thus, the ``...t's capitalized.'' phrase is typeset before \macname{cytoks}
  itself, even as \macname{theTcount} is accurate.
The \macname{addcytoks} occurring in the close-out code is placed at the
  end of \macname{cytoks} and thus takes on the \macname{footnotesize}
  declaration that is still in force.
\endgroup

\begingroup
\medskip
Next, we consider a \TokCycle{} extended environment that counts words and
  letters.
We try to build some features into the code so that, for example, spaces
  and punctuation do not count as letters.
Further, we try to build the logic so that the following cases would
  count as two words, not one: ``here...now'', ``Case(number)''.

\medskip
We provide two modes of output: one that provides an ending summary, and
  the other that keeps a running count.
As with other \TokCycle{} environments and macros, portions can be
  exempted from the count (for example macro arguments) by using the 
  escape delimiters provided.

\begin{exampleC}[]{The \macname{countem} extended-environment definition
  and supporting macros}
\newcounter{wordcount}
\newcounter{lettercount}
\newif\ifinword
\newif\ifrunningcount
\newcommand\dumpword{%
  \addcytoks[1]{\accumword}%
  \tctestifcon\ifinword{\stepcounter{wordcount}%
    \ifrunningcount\addcytoks[x]{$^{\thewordcount,\thelettercount}$}\fi}{}%
  \inwordfalse\def\accumword{}}
\makeatletter
\newcommand\addletter[1]{%
  \tctestifcatnx A#1{\stepcounter{lettercount}\inwordtrue}{\dumpword}%
  \tc@defx\accumword{\accumword#1}}
\makeatother

\xtokcycleenvironment\countem
  {\addletter{##1}}
  {\processtoks{##1}\dumpword}
  {\dumpword\addcytoks{##1}}
  {\dumpword\addcytoks{##1}}
  {\def\accumword{}\setcounter{wordcount}{0}\setcounter{lettercount}{0}}
  {\dumpword\ifrunningcount\else\tcafterenv{\par(Wordcount=\thewordcount,
    Lettercount=\thelettercount)}\fi}
\end{exampleC}

\begin{exampleA}[lefthand width=5.6cm]{Demo of the two modes of 
  \macname{countem}}
\countem 
This is a test.

But...punctuation does not count.
\endcountem

\bigskip\runningcounttrue
\countem 
This is a test.

But...punctuation does not count.
\endcountem
\end{exampleA}

\endgroup

\subsection{Truncation of input}

\begingroup

\TokCycle{} has several facilities for truncating input, either
  to the end of the current group or to the end of the input stream.
The basic commands are \macname{truncategroup} and \macname{truncatecycle},
  which can be part of the Character, Macro, or Space directives (see the
  package documentation for truncation from the Group directive).

\medskip
These commands may be issued directly or they may be placed as
  one fork of a conditional test.
If the conditional test is the occurrence of a particular token in the
  input stream, special conditional shortcuts have been formulated:
  \macname{truncategroupiftokis} and \macname{truncatecycleiftokis}.
Both of these commands take two arguments: the first is the comparison
  token against which to compare the tokens of the input stream, and the
  second is the directive to follow for tokens that fail the 
  comparison test.
If the input stream token matches the comparitor, the relevant 
  truncation command is executed.

\medskip
The description below shows the truncations to a given input text,
  resulting from several different forms of truncating directives.
In the first case, the token cycle is to be truncated if a space
  is found in the input stream.
Comparing to the input text, we see that the space occurs after the
  word ``until'', and that is indeed where the \TokCycle{} output
  ceases.

\medskip
In the second case, the truncation is limited to the current group,
  rather than the remainder of the input stream.
Again, the space following ``until'' is found, and the words
  ``certain token'' are excised from the input stream until the
  end-of-group is located.
The cycle continues successfully with ``-is'' until the next space
  is found before ``located''.
Since this space is found in the top grouping level of the input,
  the remainder of the input stream is thereafter truncated.

\medskip
In the third case, the cycle is to be truncated in the event the
  letter `a' is found in the input stream.
Examining the input text, the first `a' is in the word ``certain'' and
  so the input is truncated after the string ``cert'' is digested.

\medskip
The final case, calls for group truncation if an `a' is found in the
  input stream.
As before, the first `a' is found after ``cert''.
However, in the group case, only the remaining group content, consisting
  of ``ain token'', is discarded.
At group end, the cycle successfully resumes until the `a' in
  ``located'' is reached.
Thus, ``-is loc'' is also added to the output stream.

\def\thetext{Continue-\textit{until certain token}-is located.}

\newcommand\rundemowith[1]{%
  {\footnotesize\ttfamily\altdetokenize{#1}}: 
  \resettokcycle#1\expandafter\tokencyclexpress\thetext\endtokencyclexpress
}

\begin{exampleB}[text only, righthand width=0.01cm,  colback=white]%
{Effects of various truncation directives}
{\footnotesize\ttfamily Input text}: \expandafter\altdetokenize\expandafter
 {\thetext}

\rundemowith{\Spacedirective{\truncatecycle}}

\rundemowith{\Spacedirective{\truncategroup}}

\rundemowith{\Characterdirective{\truncatecycleiftokis{a}{\addcytoks{#1}}}}

\rundemowith{\Characterdirective{\truncategroupiftokis{a}{\addcytoks{#1}}}}
\end{exampleB}

Note how the macro function, in this case \macname{textit}, is not
  disrupted by the truncation.
Even as tokens are truncated from the input stream, group closures
  are retained so that the final output is group-balanced.
For example, a detokenization of the final case reveals this to be
  the case:

\altdecytoks

\endgroup

\subsection{Advanced topics: implicit tokens and catcode changes}

\subsubsection{Trap Active Characters (catcode 13)}

%\begin{minipage}[t]{\textwidth}
\begingroup
Active characters in the \TokCycle{} input stream are processed in
  their original form.
Their active substitutions arising from \macname{def}s 
  only occur \textit{afterwards}, when
  the \TokCycle{} output is typeset.
They may be identified with the \macname{ifactivetok} test.
If \macname{let} to a character, they may be identified in the Character
  directive;
If \macname{let} to a control sequence or defined via \macname{def},
  they may be identified in the Macro directive.
For information on how to process active spaces, please refer to 
  section~\ref{s:spc}.
\let\svt T
\Characterdirective{\tctestifcon\ifactivetok
  {\addcytoks{\fbox{#1-chr}}}{\addcytoks{#1}}}
\Macrodirective{\tctestifcon\ifactivetok
  {\addcytoks{\fbox{#1-mac}}}{\addcytoks{#1}}}

\begin{exampleA}[lefthand width=7.8cm]{Processing active characters}
\resettokcycle
\tokencyclexpress 
1. This is a test!!\endtokencyclexpress

2. \catcode`!=\active
\def !{?}%
\tokencyclexpress 
This is a test!!\endtokencyclexpress

3. 
\Characterdirective{\tctestifcon\ifactivetok
 {\addcytoks{\fbox{#1-chr}}}{\addcytoks{#1}}}%
\Macrodirective{\tctestifcon\ifactivetok
 {\addcytoks{\fbox{#1-mac}}}{\addcytoks{#1}}}%
\tokencyclexpress 
This is a test!!\endtokencyclexpress

4. \catcode`T=\active
\let T+%
\tokencyclexpress 
This is a test!!\endtokencyclexpress

5. \detokenize\expandafter{\the\cytoks}
\end{exampleA}

%%%

\ddbend If the input stream is pre-\textit{expanded}, any active 
  substitutions that are expandable (i.e., those involving \macname{def} 
  as well as those \macname{let} to something expandable) 
  are made before reaching \TokCycle{} processing.  
\svt hey are, thus, no longer detected as active, unless 
  \macname{noexpand} is applied before the pre-expansion.
In this example, the `\texttt{!}' that is not \macname{noexpand}\kern.3pted
  is converted to a `\texttt{?}' prior to reaching \TokCycle{}
  processing (and thus, not detected as \macname{active}).  
Likewise, the `\texttt{T}' is pre-converted to a `\texttt{+}':

\catcode`!=\active
\def !{?}
\catcode`T=\active
\let T+
\begin{exampleA}[lefthand width=7.0cm]{Expanded input stream acts 
  upon active \macname{def}ed characters unless \macname{noexpand} is applied}
\expandedtokcyclexpress{This is a test!\noexpand!}
\the\cytoks\par
\detokenize\expandafter{\the\cytoks}
\end{exampleA}

However, pre-tokenization does not suffer this behavior:
\begin{exampleA}[lefthand width=7.8cm]{Pre-tokenized input stream
  does not affect active characters}
\def\tmp{This is a test!!}
\expandafter\tokcyclexpress\expandafter{\tmp}
\the\cytoks\par
\detokenize\expandafter{\the\cytoks}
\end{exampleA}

%%%
\endgroup
%\end{minipage}

\begingroup
\ddbend One aspect of \TeX{} to remember is that catcodes
  are assigned at tokenization; however, for active characters,
  the substitution assignment is evaluated only upon execution.
So, if a cat-13 token is placed into a \macname{def}, it will
  remain active even if the catcode of that character code is
  later changed.
But if the cat-13 active definition is changed prior to the 
  execution of the \macname{def}'ed token, the revised
  token assignment will apply.

\medskip
The following example demonstrates this concept, while showing,
  without changing the input in any way, that \TokCycle{} 
  can properly digest active and implicit grouping (cat-1,2) characters:

\begin{exampleA}[lefthand width=8cm]{Active and implicit grouping
  tokens digestible by \TokCycle}
\catcode`Y=13
\catcode`Z=13
\let Y{
\let Z}
\let\Y{
\let\Z}
\def\tmp{\textit YabcZ de\Y\itshape f\Zg}%

\def Y{\bgroup[NEW]}% APPLIES AT EXECUTION
\catcode`Y=11% DOES NOT AFFECT Y IN \tmp

\expandafter\tokcyclexpress\expandafter{\tmp}
\the\cytoks

\detokenize\expandafter{\the\cytoks}
\end{exampleA}
\endgroup

\subsubsection{Trap Catcode 6 (explicit \& implicit) tokens}

\begingroup

\let\myhash#

\medskip

Typically, cat-6 tokens (like \texttt{\#}) are used to designate the
  following digit (1-9) as a parameter.
Since they are unlikely to be used in that capacity inside a 
  \TokCycle{} input stream, the package behavior is to convert them
  into something cat-12 and set the if-condition \macname{catSIXtrue}.
In this manner, \macname{ifcatSIX} can be used inside the
  Character directive to convert cat-6 tokens into something of
  the user's choosing.

\medskip
As to this cat-12 conversion, explicit cat-6 characters are converted
  into the same character with cat-12.  
On the other hand, implicit cat-6 control sequences (e.g., \verb|\let\myhash#|) 
  are converted into a fixed-name macro, \macname{implicitsixtok}, 
  whose cat-12 substitution text is a \macname{string} of the original
  implicit-macro name.

\begingroup
\resettokcycle
\Characterdirective{\ifcatSIX
  \addcytoks{\fbox{#1}}
  \else\addcytoks{#1}\fi}

%\tokencyclexpress This# isQ \textit{a Q# test\myhash}!\endtokencyclexpress

\begin{exampleA}[lefthand width=6.5cm]{Treatment of cat-6 tokens}
\resettokcycle
\Characterdirective{\ifcatSIX
  \addcytoks{\fbox{#1}}
  \else\addcytoks{#1}\fi}
\let\myhash#
\tokcyclexpress{This# isQ 
  \textit{a Q# test\myhash}!}
\the\cytoks\bigskip\par
\detokenize\expandafter{\the\cytoks}
\end{exampleA}

%\tokcyclexpress{This# isQ \textit{a Q# t\,e\, s\,t\myhash}!}\the\cytoks{}
%with 1-character macros

%%%
%\expandedtokcyclexpress{This# isQ \noexpand\textit{a Q# test\myhash}!}\the\cytoks
%{} PRE-EXPANDED
%%%


%\tokencyclexpress This# isQ \textit{a Q# test}!\endtokencyclexpress

%\Characterdirective{\ifcatSIX\addcytoks{\fbox{#1}}\else\addcytoks{#1}\fi}

\begin{exampleA}[lefthand width=6.5cm]{Multiple explicit cat-6 tokens are not 
  a problem}
\catcode`Q=6
\tokcyclexpress{This# isQ 
  \textit{a Q# test\myhash}!}
\the\cytoks
\end{exampleA}

\catcode`Q=11
\hrulefill
\endgroup

\ddbend For what is, perhaps, a rare situation, one can even process input 
  streams that contain cat-6 macro parameters.
A package macro, \macname{whennotprocessingparameter\#1\{<}%
  \textit{directive when not a parameter}\verb|>}|, can be
  used inside of the Character directive to intercept parameters.
In this example, a macro is defined and then executed, subject to
  token replacements brought about by the expandable Character directive.

\begin{exampleA}[lefthand width=7cm]{Preserving parameters (e.g.
  \texttt{\#1, \#2}) in the \TokCycle{} input stream}
\Characterdirective{%
  \whennotprocessingparameter#1{%
    \addcytoks[x]{\vowelcap{#1}}}}
\tokcyclexpress{%
  \def\zQ#1#2{[one:#1](two:#2)}
  This is a \zQ big test.

  \renewcommand\zQ[2]{\ifx t#1[#1]\fi(#2)}
  This is a \zQ test.}
\the\cytoks
\end{exampleA}

\altdecytoks

\endgroup

\subsubsection{Trap implicit tokens in general}

\begingroup

Implicit control sequences (assigned via \macname{let} to characters) 
  were already mentioned in the context of cat-6.
However, implicit control sequences can be of any valid catcode (except
  for cat-0, which we instead call macros or primitives).
The condition \macname{ifimplicittok} is used to flag such
  tokens for special processing, as well as active tokens that
  are \macname{let} to anything unexpandable.

In the next example, implicit, cat-6 and implicit-cat-6 tokens may
  all be differentiated, shown here with a multiplicity of 
  \macname{fbox}es.

\fboxsep=1.5pt

\begin{exampleA}[lefthand width=8cm]{Implicit\ensuremath{{}={}}%
  single box\char44 \ cat-6\ensuremath{{}={}}double box\char44 \ %
  implicit-cat-6\ensuremath{{}={}}triple box}
\catcode`Q=\active \let QN
\let\littlet=t
\let\littlel=l
\let\svhash#
\Characterdirective{\ifimplicittok
  \ifcatSIX\addcytoks{\fbox{\fbox{\fbox{#1}}}}%
  \else\addcytoks{\fbox{#1}}\fi\else\ifcatSIX
  \addcytoks{\fbox{\fbox{#1}}}\else
  \addcytoks{#1}\fi\fi}

\tokencyclexpress We wi\littlel\littlel# 
  \textit{ make a \littlet est #} \littlet

This \textit{is a \textbf{big}} \littlet est.

Qext pa#agraph ending with implicit cat six 
  \svhash.\endtokencyclexpress
\end{exampleA}

In the following example, we use both control sequences and active characters
 in \macname{def} and \macname{let} capacities, to demonstrate how 
  \TokCycle{} digests things.
Implicit tokens (tokens \macname{let} to characters) are shown in a box,
  with both the token name and the implicit value (note that tokens 
  \macname{let} to macros and primitives are not considered implicit).
Active tokens processed through the character directive are followed with
  a \dag, whereas those processed through the macro directive are followed
  with a \ddag.


\begin{exampleA}[lefthand width=7.5cm]{Non-active vs{.} active \macname{def} 
  \& \macname{let}}
\Characterdirective{\ifimplicittok
  \addcytoks{\fbox{\detokenize{#1}:#1}}%
  \else\addcytoks{#1}\fi\ifactivetok
  \addcytoks{\rlap{\dag}}\fi\addcytoks{~\,}}
\Macrodirective{\ifimplicittok
  \addcytoks{\fbox{\detokenize{#1}}}%
  \else\addcytoks{#1}\fi\ifactivetok
  \addcytoks{\rlap{\ddag}}\fi
  \ifx\par#1\else\addcytoks{~\,}\fi}

\def\A{a}
\let\B i
\let\C\today
\let\D\relax
\def\E{\relax}
\catcode`V=13 \def V{a}
\catcode`W=13 \let Ww
\catcode`X=13 \let X\today
\catcode`Y=13 \let Y\relax
\catcode`Z=13 \def Z{\relax}
\tokcyclexpress{\A\B\C\D\E ab\par VWXYZab}
\the\cytoks
\end{exampleA}


\ddbend If the input stream is subject to pre-expansion, one
  will require \macname{noexpand} for macros where no pre-expansion
  is desired.\medskip

\ddbend If the input stream is provided pre-tokenized via 
  \macname{def}, \TeX{} convention requires cat-6 tokens to appear 
  in the input stream as duplicate, e.g. \texttt{\#\#}.


\subsubsection{Changing grouping tokens (catcodes 1,2)}

\begingroup

Changing grouping tokens (catcodes 1,2) may require something more,
  if the output stream is to be detokenized.  
In the following examples, pay attention to the
  detokenized grouping around the argument to \macname{fbox}.

\medskip
As we will see, the issues raised here only affect the situation
  when detokenization of the output stream is required.
\medskip

\begin{exampleA}[]{\TokCycle{} defaults grouping tokens to braces:}
\tokencycle
 {\addcytoks{(#1)}}
 {\processtoks{#1}}
 {\addcytoks{#1}}
 {\addcytoks{ }}
This \fbox{is a} test.
\endtokencycle\medskip

\detokenize\expandafter{\the\cytoks}
\end{exampleA}

One can make brackets cat-1,2, redefining bgroup/egroup to [ ].
However, while one can now use brackets in input stream, braces 
  will still appear in the detokenized \TokCycle{} output stream:

\begin{exampleA}[lefthand width=6.5cm]
{\TokCycle{} will not automatically change its grouping tokens}
\catcode`\[=1
\catcode`\]=2
\let\bgroup[
\let\egroup]
\tokencycle
 {\addcytoks{(#1)}}
 {\processtoks{#1}}
 {\addcytoks{#1}}
 {\addcytoks{ }}
This \fbox[is a] test.
\endtokencycle\medskip

\detokenize\expandafter{\the\cytoks}
\end{exampleA}

If it is necessary to reflect revised grouping tokens in the 
  output stream, the \macname{settcgrouping} macro is 
  to be used.

\begin{exampleA}[lefthand width=6.5cm]
{Redefine \TokCycle{} grouping tokens as angle
  brackets using \macname{settcGrouping}}
\catcode`\<=1
\catcode`\>=2
\catcode`\{=12
\catcode`\}=12
\let\bgroup<
\let\egroup>
\settcGrouping<<#1>>
\tokencycle
 <\addcytoks<(#1)>>
 <\processtoks<#1>>
 <\addcytoks<#1>>
 <\addcytoks< >>
This \fbox<is a> test.
\endtokencycle\medskip

\detokenize\expandafter<\the\cytoks>
\end{exampleA}

Angle brackets are now seen in the above detokenization.
Until subsequently changed, cat-1,2 angle brackets now appear in 
  detokenized \TokCycle{} groups, even if other cat-1,2 tokens were
  used in the input stream.
Bottom line: 
\begin{itemize}
\item adding, deleting, or changing catcode 1,2 explicit 
  grouping tokens, e.g., \{\}, (in conjunction with their 
  associated implicit  \macname{bgroup}\macname{egroup}) 
  tokens will not affect \TokCycle's ability to digest proper 
  grouping of the input stream, regardless of which tokens 
  are catcode 1,2 at the moment.

\item The grouping tokens used in \TokCycle's output 
  default to \verb|{}| braces (with cat-1,2), but can be 
  changed deliberately  using \macname{settcGrouping}.

\item The package, currently, has no way to reproduce in the
   output stream the actual grouping tokens that occur in the 
  input stream, but one should ask, for the particular
  application, if it really matters, as long as the the 
  proper catcodes-1,2 are preserved?

\end{itemize}

\endgroup

\subsubsection{Catcode 10 space tokens\label{s:spc}}

\begingroup

Here we demonstrate that \TokCycle{} can handle arbitrary redesignation
  of tokens to cat-10, as well as implicit space tokens (both implicit
  macro spaces and active-implicit character spaces).

\ddbend While it should seem natural, we note that implicit
  space tokens are directed to the Space directive rather than
  the Character directive.
However, \macname{ifimplicittok} may still be used to 
  differentiate an explicit space from an implicit one.


\ddbend
Also, if the implicit space is an active character, \macname{ifactivetok}
  is also set, for the user's benefit.
Likewise, \macname{ifactivechar} is also checked for active spaces, to see
  if the charcode of the active space is still, indeed, active.
While \verb|#1| may be used to represent the space in the space directive,
  a special technique is required to recover the detokenized name of the 
  active space character.
In particular, \verb|#1| will, for active spaces, contain a generic
  implicit space token, \macname{tc@sptoken}.
However, the name of the associated active character in the input stream
  will be defined as a cat-12 token in \macname{theactivespace}.

\medskip
Note in the following examples that cat-10 tokens do \textit{not}
  get under-dots.
The next three examples all use the same input, but with different
catcode settings for the space and the underscore.

\fboxsep=1pt

\begin{exampleA}[lefthand width=10cm]{space cat-10{,} underscore cat-12}
\catcode`\_=12 %
\catcode`\ =10 %

\tokencycle{\addcytoks{\underdot{#1}}}%
{\processtoks{#1}}%
{\addcytoks{#1}}%
{\addcytoks{#1}}%
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle
\end{exampleA}
%
\begin{exampleA}[lefthand width=10cm]{space cat-10{,} underscore cat-10}
\catcode`\_=10 %
\catcode`\ =10 %

\tokencycle{\addcytoks{\underdot{#1}}}%
{\processtoks{#1}}%
{\addcytoks{#1}}%
{\addcytoks{#1}}%
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle
\end{exampleA}

%

\begin{exampleA}[lefthand width=10cm]{space cat-12{,} underscore cat-10}
\catcode`\_=10 %
\catcode`\ =12 %

\tokencycle{\addcytoks{\underdot{#1}}}%
{\processtoks{#1}}%
{\addcytoks{#1}}%
{\addcytoks{#1}}%
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle
\end{exampleA}

The next two examples introduce implicit and active-implicit spaces.
The latter example also demonstrates detokenization of such spaces.

\medskip
Here is a useful expandable macro for detokenizing space tokens 
  in the context of \TokCycle{}.
It can process not only explicit cat-10 spaces, but also implicit and
  active-implicit spaces.
To add it to the \macname{cytoks} token list, make sure you use the 
  \verb|[x]| fully expanded option to \macname{addcytoks}.

%%%
\resettokcycle
\Characterdirective{\addcytoks{\underdot{#1}}}

\begin{exampleA}[lefthand width=10cm]{Implicit spaces also work}
\resettokcycle
\Characterdirective{\addcytoks{\underdot{#1}}}
\def\:{\let\z= } \: %
\catcode`\_=10 %
\catcode`\ =12 %

\tokencyclexpress
\fbox{a\z{}c d} b_g\itshape f\upshape
\endtokencyclexpress
\end{exampleA}

\begin{minipage}[t]{\textwidth}
\begin{exampleC}[]{The \macname{detokenizespacetok} macro for handling
  implicit and active-implicit spaces}
\newcommand\detokenizespacetok[1]{\tctestifcon{\ifimplicittok}%
  {\tctestifcon{\ifactivetok}{\theactivespace}{\string#1}}{\detokenize{#1}}}
\end{exampleC}

\begin{exampleA}[lefthand width=10cm]{Active Implicit spaces work{,} too}
\resettokcycle
\Characterdirective{\addcytoks{\underdot{#1}}}
\def\:{\let\z= } \: %
\catcode`Q=\active %
\def\:{\let Q= } \: %
\catcode`\_=10 %

\tokencyclexpress
xQx x_x\z{}x%
\endtokencyclexpress

\resettokcycle
\Spacedirective{\addcytoks{\textcolor{cyan}\bgroup}%
  \addcytoks[x]{(\detokenizespacetok{#1})}%
  \addcytoks{\egroup}}

\medskip
\tokencyclexpress
xQx x_x\z{}x%
\endtokencyclexpress
\end{exampleA}
\end{minipage}
\endgroup


\subsubsection{Changes to catcode 0}

\begingroup

\begin{exampleA}[]{Cat-0 changes are not a hindrance to \TokCycle}
\let\littlet=t
\catcode`\! 0 !catcode`!\ 12
!Characterdirective{!ifimplicittok
  !addcytoks{!fbox{#1}}!else!ifcatSIX
  !addcytoks{!fbox{!fbox{#1}}}
  !else!addcytoks{#1}!fi!fi}
!tokencyclexpress Here, {!scshape!bgroup 
  on !today!itshape{} we are !egroup 
  !littlet es!littlet ing} cat-0 
  changes{!bgroup}!egroup
!endtokencyclexpress!medskip

!detokenize!expandafter{!the!cytoks}
\end{exampleA}

\endgroup


\end{document}