summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/texpower/texpower-addons.dtx
blob: 0177aea101f1c4d2f615104637be6570f3c7e20c (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
% \iffalse meta-comment
% --------------------------------------------------------------
% Part of the TeXPower bundle
% Copyright (C) 1999-2004 Stephan Lehmke
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% --------------------------------------------------------------
%
% texpower-addons.dtx,v 1.3 2005/03/28 22:36:05 hansfn Exp
%
% \fi
%
% \iffalse
%
%<*driver>
\documentclass{article}
\begin{document}
\typeout{***********************************************************}
\typeout{*}
\typeout{*\space\space\space\space\space\space\space Do NOT compile this dtx file!}
\typeout{*}
\typeout{***********************************************************}
\end{document}
%</driver>
%
% \fi
%
%<*automata>
%<<KEEPCOMMENTS
%=======================================================================================================================
% File: automata.sty
%
% Draw and animate `automata' as used in theoretical computer science, using PSTricks and TeXPower.
%
% This package is an addition to the TeXPower bundle, to be found at
% 
% http://texpower.sourceforge.net/
%
%=======================================================================================================================
% Author: Stephan Lehmke <Stephan.Lehmke@cs.uni-dortmund.de>
%
% v0.2  Oct 05, 2001 First version for the pre-alpha release of texpower.
%                    Only deterministic FA and Mealy Automata yet.
%

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage%
{automata}%
[2001/10/05 v0.2 Draw and animate automata.]


%=======================================================================================================================

\RequirePackage{ifthen}

\RequirePackage{relsize}

\RequirePackage{texpower}

\ifthenelse{\boolean{psspecialsallowed}}{\RequirePackage{tppstcol}}{}


%=======================================================================================================================
% Implementation of the functionality.

%-----------------------------------------------------------------------------------------------------------------------
% Some definitions of general interest.

\defineTPcolor{automatoncolor}{gray}{0.8}
\defineTPcolor{autoplatecolor}{gray}{0.95}
\defineTPcolor{currstatecolor}{rgb}{0.8,1,0.8}
\colorbetween{markstatecolor@TPATM}{pagecolor}{currstatecolor}
\replacecolor{cellcolor}{white}



\newlength{\tapepos@TPATM}

\newcounter{tapecells@TPATM}
\newcounter{inputlength@TPATM}
\newcounter{states@TPATM}
\newcounter{currstate@TPATM}
\newcounter{nextstate@TPATM}
\newcounter{currinputsymbol@TPATM}
\newcounter{currtapepos@TPATM}

\newtoks\tabrows@TPATM
\newtoks\tabdecl@TPATM

\newsavebox{\tabbox@TPATM}

\newboolean{stategraph@TPATM}

\newif\iffliplabel@TPATM

\newcommand{\formatsymbol}[1]{\texttt{#1}}


%-----------------------------------------------------------------------------------------------------------------------
% Helper macros.

% Make a fixed point number have exactly five decimal places, either by truncating or by filling up with trailing
% zeroes.
\newcommand{\filltofive@TPATM}[1]{\expandafter\@filltofive@TPATM#1000000\@nil#1}

\def\@filltofive@TPATM#1.#2#3#4#5#6#7\@nil#8{\def#8{#1.#2#3#4#5#6}}


% Count the number of items in a comma-separated list.
\newcommand{\countsymbols@TPATM}[2]{\setcounter{#2}{1}\@countsymbols@TPATM#1,\@nil{#2}}

\def\@countsymbols@TPATM#1,#2\@nil#3{\ifthenelse{\equal{#2}{}}{}{\stepcounter{#3}\@countsymbols@TPATM#2\@nil{#3}}}



% Get a symbol from a (macro containing a) comma-separated list.
\newcommand{\getsymbol@TPATM}[2]
{%
  \expandafter\@getsymbol@TPATM#1\@nil{#1}{#2}%
}

\def\@getsymbol@TPATM#1,#2\@nil#3#4{\gdef#3{#2}\@@getsymbol@TPATM#1\@nil{#4}}

\def\@@getsymbol@TPATM{\@ifnextchar({\@@@getsymbol@TPATM}{\@@@getsymbol@TPATM()}}

\def\@@@getsymbol@TPATM(#1)#2\@nil#3{\def\statepos@TPATM{#1}\def#3{#2}}


% Get a state from a (macro containing a) comma-separated list of states and check whether it's accepting.

\newif\ifacceptingstate@TPATM

\newcommand{\getstate@TPATM}[4]{\expandafter\@getstate@TPATM#1\@nil{#1}{#2}{#3}{#4}}

\def\@getstate@TPATM#1,#2\@nil#3#4#5#6{\gdef#3{#2}\@@getstate@TPATM#1\@nil{#4}{#5}{#6}}

\def\@@getstate@TPATM{\@ifnextchar({\@@@getstate@TPATM}{\@@@getstate@TPATM()}}

\def\@@@getstate@TPATM(#1)#2#3\@nil#4#5#6%
{%
  \def\statepos@TPATM{#1}%
  \def#6{#2#3}%
  \ifx\accept#2%
    \acceptingstate@TPATMtrue%
    \def#5{#4}%
   \else
    \acceptingstate@TPATMfalse%
    \let#5\empty
  \fi
  }

% Get a state transition from a (macro containing a) comma-separated list of states.
\newcommand{\getdeltapair@TPATM}[3]
{%
  \expandafter\@getdeltapair@TPATM#1\@nil{#1}{#2}{#3}%
}

\def\@getdeltapair@TPATM{\@ifnextchar[{\@@getdeltapair@TPATM}{\@@getdeltapair@TPATM[\@nil]}}

\def\@@getdeltapair@TPATM[#1]%
{%
  \def\optarg@TPATM{#1}%
  \@ifstar{\let\fliplabel@TPATM=\fliplabel@TPATMtrue\@@@getdeltapair@TPATM}
  {\let\fliplabel@TPATM=\fliplabel@TPATMfalse\@@@getdeltapair@TPATM}%
  }

\def\@@@getdeltapair@TPATM{\@ifnextchar({\@@@@getdeltapair@TPATM}{\@@@@getdeltapair@TPATM(.5)}}

\def\@@@@getdeltapair@TPATM(#1)%
{%
  \def\labelpos@TPATM{#1}%
  \@ifnextchar[{\@@@@@getdeltapair@TPATM}{\@@@@@getdeltapair@TPATM[\@nil]}%
  }

\def\@@@@@getdeltapair@TPATM[#1]#2->#3,#4\@nil#5#6#7{\def\labelopt@TPATM{#1}\protected@edef#6{#2}\protected@edef#7{#3}\gdef#5{#4}}

% Decompose a comma-separated pair (without parentheses) into its components.
\def\decomposestpos@TPATM#1,#2\@nil#3#4{\def#3{#1}\def#4{#2}}

% Remove trailing comma from a comma-separated list.
\def\gobblelastcomma@TPATM#1,\@nil#2{\gdef#2{#1}}

% A first step towards internationalization - german only at the moment.
\newcommand{\automatonname}{Automatenmodell}
\newcommand{\inputname}{Eingabe}
\newcommand{\inputtapename}{Eingabeband}
\newcommand{\outputname}{Ausgabe}
\newcommand{\outputtapename}{Ausgabeband}
\newcommand{\acceptname}{Akzeptieren}
\newcommand{\statename}{Zustand}
\newcommand{\statesname}{Zust"ande}
\newcommand{\statetransname}{Zustands"uberg"ange}
\newcommand{\nextstatename}{Folgezust.}

\newcommand{\statetransitiontablepreamble}{}

\newcommand{\transitionarrayparameters}
{%
  \renewcommand{\arraystretch}{.3}%
  \setlength{\extrarowheight}{16pt}%
  \setlength{\tabcolsep}{.5pt}%
}

\let\accept\empty
\let\comma\empty


%-----------------------------------------------------------------------------------------------------------------------
% Finite Automata.

% Generic macros for drawing deterministic finite automata. The difference between simple dfa and mealy automata is made
% by setting parameters.

\def\finalisetabrows@TPATM#1\\\@nil#2{#2={#1\end{tabular}}}%

\newcommand{\draw@outputtape@TPATM}[1]{}%
\newcommand{\getoutput@TPATM}[2]{}
\newcommand{\inittabrows@TPATM}[1]{}
\newcommand{\addtabrow@TPATM}[2]{}
\newcommand{\label@output@TPATM}{}%
\newcommand{\fix@nextstate@TPATM}{}
\newcommand{\mk@new@outputtape@TPATM}[1]{}%



\newcommand{\genericdfa@TPATM}[8]
{%
  \begin{pspicture}(1,1)
    % Draw automaton body.
    \psframe[fillstyle=solid,fillcolor=automatoncolor](0.05,0)(0.95,1)
    \rput[b]{90}(0.05,0.5){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\automatonname}}%
    % Draw input tape.
    \rput[l](0.07,0.93){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\inputtapename}}%
    \setlength{\tapepos@TPATM}{0.55pt-0.1pt*\real{#6}}%
    \setcounter{currinputsymbol@TPATM}{1*\real{#6}}%
    \mkfactor{\startx@TPATM}{\ratio{\tapepos@TPATM}{1pt}}%
    \setcounter{tapecells@TPATM}{6+\value{currinputsymbol@TPATM}}%
    \countsymbols@TPATM{#5}{inputlength@TPATM}%
    \ifthenelse{\value{tapecells@TPATM}>\value{inputlength@TPATM}}
    {\setcounter{tapecells@TPATM}{\value{inputlength@TPATM}}}
    {}%
    \multips(\startx@TPATM,0.88)(0.1,0){\value{tapecells@TPATM}}{\psframe[fillstyle=solid,fillcolor=cellcolor](0.1,0.1)}%
    \mkfactor{\startx@TPATM}{\ratio{\tapepos@TPATM+0.05pt}{1pt}}%
    \filltofive@TPATM{\startx@TPATM}%
    \protected@edef\tapecontents@TPATM{#5,}%
    \setcounter{currtapepos@TPATM}{0}%
    \let\inputtapesymbol@TPATM\relax
    \let\nextinputtapesymbol@TPATM\relax
    \multido{\n=\startx@TPATM+0.10000}{\value{tapecells@TPATM}}
    {%
      \getsymbol@TPATM\tapecontents@TPATM\inputtapesymbol@TPATM
      \ifnum\value{currtapepos@TPATM}=\value{currinputsymbol@TPATM}\relax
        \global\let\nextinputtapesymbol@TPATM\inputtapesymbol@TPATM
      \fi
      \stepcounter{currtapepos@TPATM}%
      \ifnum\value{currtapepos@TPATM}=\value{currinputsymbol@TPATM}\relax
        \global\let\inputtapesymbol@TPATM\inputtapesymbol@TPATM
      \fi
      \rput(\n,0.93){\formatsymbol\inputtapesymbol@TPATM}%
      }%
    \psframe[linewidth=2pt,linecolor=conceptcolor](0.45,0.88)(0.55,0.98)
    % Draw state transitions
    \countsymbols@TPATM{#1}{states@TPATM}%
    \mkfactor{\tapeadvancement@TPATM}{\real{#6}-\value{currinputsymbol@TPATM}pt}%
    \def\defdelta@TPATM{#3,}%
    \whiledo{\not\equal{\defdelta@TPATM}{}}
    {%
      \getdeltapair@TPATM\defdelta@TPATM\domainvalue@TPATM\rangevalue@TPATM
      \expandafter\def\csname delta(\domainvalue@TPATM)@TPATM\expandafter\endcsname\expandafter{\rangevalue@TPATM}%
      \expandafter\let\csname deltaopt(\domainvalue@TPATM)@TPATM\endcsname=\optarg@TPATM
      \expandafter\let\csname deltalabelpos(\domainvalue@TPATM)@TPATM\endcsname=\labelpos@TPATM
      \expandafter\let\csname deltalabelopt(\domainvalue@TPATM)@TPATM\endcsname=\labelopt@TPATM
      \expandafter\let\csname deltaflag(\domainvalue@TPATM)@TPATM\endcsname=\empty
      \expandafter\let\csname deltafliplabel(\domainvalue@TPATM)@TPATM\endcsname=\fliplabel@TPATM
      }%
    \ifthenelse{\boolean{stategraph@TPATM}}% Draw state transition diagram?
    {% Yes.
      \ifthenelse{\lengthtest{\tapeadvancement@TPATM pt=0pt}}
      {%
        \let\nextinputtapesymbol@TPATM\inputtapesymbol@TPATM
        \ifthenelse{\value{nextstate@TPATM}>-1}{\def\tapeadvancement@TPATM{1}}{}%
        }{}%
      \colorbetween[\tapeadvancement@TPATM]{activetranscolor@TPATM}{currstatecolor}{pagecolor}%
      \rput(0.5,0.5)
      {%
        \begingroup
          % Make `subpicture' containing the state transition diagram.
          \psset{xunit=.43\psxunit,yunit=.36\psyunit,labelsep=1pt}%
          \begin{pspicture}(0,0)
            \psframe[fillstyle=solid,fillcolor=cellcolor,dimen=inner](-1,-1)(1,1)
            \rput[lt](-1,1){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\statetransname}}%
            % First, the nodes representing states.
            \mkfactor{\statedistance@TPATM}{2/\value{states@TPATM}}%
            \mkfactor\startx@TPATM{-1+\statedistance@TPATM pt/2}%
            \def\states@TPATM{#1,}%
            \setcounter{currstate@TPATM}{0}%
            \multido{\n=\startx@TPATM+\statedistance@TPATM}{\value{states@TPATM}}
            {%
              \stepcounter{currstate@TPATM}%
              \getstate@TPATM{\states@TPATM}{,linewidth=2pt,linecolor=red}{\statetype@TPATM}{\state@TPATM}%
              \ifnum\value{currstate@TPATM}=#7\relax
                \ifacceptingstate@TPATM\global\acceptingstate@TPATMtrue\else\global\acceptingstate@TPATMfalse\fi
              \fi
              \edef\next
              {%
                \noexpand
                \cnode
                [%
                  fillstyle=solid,%
                  fillcolor=\ifnum\value{currstate@TPATM}=#7currstatecolor\else cellcolor\fi\statetype@TPATM
                  ]%
                (\ifx\statepos@TPATM\empty\n,0\else\statepos@TPATM\fi)%
                }%
              \next{.4}{{state\state@TPATM}}%
              \rput(\ifx\statepos@TPATM\empty\n,0\else\statepos@TPATM\fi){\let\comma,\state@TPATM}%
              \ifthenelse{\value{currstate@TPATM}=#4}
              {%
                \ifx\statepos@TPATM\empty
                  \mkfactor{\stxpos@TPATM}{\real{\n}-.2pt}%
                  \def\stypos@TPATM{0}%
                 \else
                  \expandafter\decomposestpos@TPATM\statepos@TPATM\@nil{\stxpos@TPATM}{\stypos@TPATM}%
                  \mkfactor{\stxpos@TPATM}{\real{\stxpos@TPATM}-.2pt}%
                \fi
                \pnode(\stxpos@TPATM,\stypos@TPATM){start}%
                \ncline{->}{start}{state\state@TPATM}%
                }{}%
              }%
            % Next, the node connections.
            \setcounter{currstate@TPATM}{0}%
            \def\states@TPATM{#1,}%
            \whiledo{\not\equal{\states@TPATM}{}}
            {%
              \stepcounter{currstate@TPATM}%
              \getsymbol@TPATM\states@TPATM\state@TPATM
              \protected@edef\state@TPATM{\state@TPATM}%
              \def\inputalphabet@TPATM{#2,}%
              \whiledo{\not\equal{\inputalphabet@TPATM}{}}
              {%
                \getsymbol@TPATM\inputalphabet@TPATM\inputsymbol@TPATM
                \getoutput@TPATM\state@TPATM\inputsymbol@TPATM
                \ifthenelse{\equal{\csname deltaflag(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname}{}}
                {%
                  \let\currentnstate@TPATM=\nstate@TPATM
                  \let\currentia@TPATM=\inputalphabet@TPATM
                  \inittabrows@TPATM\tabrows@TPATM
                  \def\arrowparams@TPATM{border=1pt}%
                  \begingroup
                    \def\inputalphabet@TPATM{#2,}%
                    \whiledo{\not\equal{\inputalphabet@TPATM}{}}
                    {%
                      \getsymbol@TPATM\inputalphabet@TPATM\inputsymbol@TPATM
                      \getoutput@TPATM\state@TPATM\inputsymbol@TPATM
                      \ifx\currentnstate@TPATM\nstate@TPATM
                        \expandafter\global\expandafter\let
                        \csname deltaflag(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname=\relax
                        \ifthenelse
                        {%
                          \value{currstate@TPATM}=#7%
                          \and\equal\inputsymbol@TPATM\nextinputtapesymbol@TPATM
                          \and\lengthtest{\tapeadvancement@TPATM pt>0pt}%
                          }
                        {%
                          \xdef\arrowparams@TPATM
                          {%
                            doubleline=true,doublecolor=activetranscolor@TPATM,doublesep=\tapeadvancement@TPATM pt,%
                            arrowsize=1pt 1.5,border=1pt%
                            }%
                          \def\@hlinput@TPATM{\noexpand\rowcolor{activetranscolor@TPATM}}%
                          }
                        {\let\@hlinput@TPATM=\empty}%
                        \addtabrow@TPATM\tabrows@TPATM{\inputsymbol@TPATM}%
                        \ifnum\value{currstate@TPATM}=#7\relax
                          \ifx\inputsymbol@TPATM\inputtapesymbol@TPATM
                            \fix@nextstate@TPATM%
                          \fi
                        \fi
                      \fi
                      }%
                    \expandafter\finalisetabrows@TPATM\the\tabrows@TPATM\@nil\tabrows@TPATM
                    \global\tabrows@TPATM=\tabrows@TPATM
                  \endgroup
                  \expandafter\gdef\expandafter\fullarrowparams@TPATM\expandafter{\expandafter[\arrowparams@TPATM]}%
                  \expandafter\gdef\expandafter\addarrowparams@TPATM\expandafter{\expandafter,\arrowparams@TPATM}%
                  \global\let\inputalphabet@TPATM=\currentia@TPATM
                  \transitionarrayparameters
                  \csname deltafliplabel(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname
                  \edef\mkcirclabel@TPATM
                  {%
                    \iffliplabel@TPATM\noexpand\aput\else\noexpand\bput\fi
                    \expandafter\ifx\csname deltalabelopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\@nnil
                     \else
                      [\csname deltalabelopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname]%
                    \fi
                    (\csname deltalabelpos(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname)%
                    }%
                  \edef\mklinelabel@TPATM
                  {%
                    \iffliplabel@TPATM\noexpand\bput\else\noexpand\aput\fi
                    \expandafter\ifx\csname deltalabelopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\@nnil
                     \else
                      [\csname deltalabelopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname]%
                    \fi
                    (\csname deltalabelpos(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname)%
                    }%
                  \expandafter
                  \ifx\csname deltaopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\@nnil
                    \ifx\state@TPATM\nstate@TPATM
                      \expandafter\nccircle\fullarrowparams@TPATM{->}{state\state@TPATM}{.35}%
                      \mkcirclabel@TPATM{\the\tabrows@TPATM}%
                     \else
                      \expandafter\ncline\fullarrowparams@TPATM{->}{state\state@TPATM}{state\nstate@TPATM}%
                      \mklinelabel@TPATM{\the\tabrows@TPATM}%
                    \fi
                   \else
                    \ifx\state@TPATM\nstate@TPATM
                      \edef\next
                      {%
                        \noexpand\nccircle
                        \expandafter
                        \ifx\csname deltaopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\empty
                          \fullarrowparams@TPATM
                         \else
                          [\csname deltaopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\addarrowparams@TPATM]%
                        \fi
                        }%
                      \next{->}{state\state@TPATM}{.35}%
                      \mkcirclabel@TPATM{\the\tabrows@TPATM}%
                     \else
                      \edef\next
                      {%
                        \noexpand\ncarc
                        \expandafter
                        \ifx\csname deltaopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\empty
                          \fullarrowparams@TPATM
                         \else
                          [\csname deltaopt(\state@TPATM;\inputsymbol@TPATM)@TPATM\endcsname\addarrowparams@TPATM]%
                        \fi
                        }%
                      \next{->}{state\state@TPATM}{state\nstate@TPATM}%
                      \mklinelabel@TPATM{\the\tabrows@TPATM}%
                    \fi
                  \fi
                  }%
                {}%
                }%
              }%
          \end{pspicture}%
        \endgroup
        }%
      }
    {% No; draw state transition table.
      % First, the list of states.
      \rput[l](0.07,0.81){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\statesname}}%
      \mkfactor\startx@TPATM
      {\ratio{\widthof{\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\statesname}}}{\psyunit}}%
      \mkfactor{\statewidth@TPATM}{0+(0.88pt-\startx@TPATM pt)/\value{states@TPATM}}%
      \ifthenelse{\lengthtest{\statewidth@TPATM pt>.1pt}}{\def\statewidth@TPATM{.1}}{}%
      \filltofive@TPATM{\startx@TPATM}%
      \filltofive@TPATM{\statewidth@TPATM}%
      \def\states@TPATM{#1,}%
      \setcounter{currstate@TPATM}{0}%
      \multido{\n=\startx@TPATM+\statewidth@TPATM}{\value{states@TPATM}}
      {%
        \stepcounter{currstate@TPATM}%
        \mkfactor{\endx@TPATM}{\real{\n}+\statewidth@TPATM pt}%
        \getstate@TPATM{\states@TPATM}{,linewidth=2pt,linecolor=red}{\statetype@TPATM}{\state@TPATM}%
        \ifnum\value{currstate@TPATM}=#7
          \ifacceptingstate@TPATM\global\acceptingstate@TPATMtrue\else\global\acceptingstate@TPATMfalse\fi
        \fi
        \edef\next
        {%
          \noexpand
          \psframe
          [%
            fillstyle=solid,%
            fillcolor=\ifnum\value{currstate@TPATM}=#7currstatecolor\else cellcolor\fi\statetype@TPATM
          ]%
          }%
        \next(\n,0.76)(\endx@TPATM,0.86)%
        \mkfactor{\charpos@TPATM}{\real{\n}+\statewidth@TPATM pt/2}%
        \rput(\charpos@TPATM,0.81){\state@TPATM}%
        \ifthenelse{\value{currstate@TPATM}=#4}{\psline{->}(\charpos@TPATM,0.74)(\charpos@TPATM,0.76)}{}%
        }%
      % Next, the state transition table itself.
      \psframe[fillstyle=solid,fillcolor=cellcolor,dimen=inner](0.07,0.14)(0.93,0.74)
      \rput[lt](0.07,0.74){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\smaller\statetransname}}%
      % Construct tabular declaration and first row.
      \def\inputalphabet@TPATM{#2,}%
      \tabrows@TPATM={}%
      \tabdecl@TPATM={}%
      \ifthenelse{\equal{\inputtapesymbol@TPATM}{\nextinputtapesymbol@TPATM}}
      {%
        \replacecolor{oldinputcolor@TPATM}{markstatecolor@TPATM}%
        \replacecolor{newinputcolor@TPATM}{markstatecolor@TPATM}%
        \replacecolor{oldoutputcolor@TPATM}{currstatecolor}%
        \replacecolor{newoutputcolor@TPATM}{currstatecolor}%
        }
      {%
        \colorbetween[\tapeadvancement@TPATM]{oldinputcolor@TPATM}{pagecolor}{markstatecolor@TPATM}%
        \colorbetween[\tapeadvancement@TPATM]{newinputcolor@TPATM}{markstatecolor@TPATM}{pagecolor}%
        \ifthenelse{\lengthtest{\tapeadvancement@TPATM pt>0.5pt}}
        {%
          \colorbetween[\tapeadvancement@TPATM]{newoutputcolor@TPATM}{currstatecolor}{pagecolor}%
          \replacecolor{oldoutputcolor@TPATM}{markstatecolor@TPATM}%
          }
        {%
          \colorbetween[\tapeadvancement@TPATM]{oldoutputcolor@TPATM}{pagecolor}{currstatecolor}%
          \replacecolor{newoutputcolor@TPATM}{markstatecolor@TPATM}%
          }%
        }%
      \whiledo{\not\equal{\inputalphabet@TPATM}{}}
      {%
        \getsymbol@TPATM\inputalphabet@TPATM\inputsymbol@TPATM
        \protected@edef\newtabrows@TPATM{\the\tabrows@TPATM&\noexpand\formatsymbol{\inputsymbol@TPATM}}%
        \expandafter\tabrows@TPATM\expandafter{\newtabrows@TPATM}%
        \edef\newtabdecl@TPATM
        {%
          \the\tabdecl@TPATM 
          \ifx\inputsymbol@TPATM\nextinputtapesymbol@TPATM>{\noexpand\columncolor{newinputcolor@TPATM}}\fi
          \ifx\inputsymbol@TPATM\inputtapesymbol@TPATM>{\noexpand\columncolor{oldinputcolor@TPATM}}\fi
          c%
          }%
        \expandafter\tabdecl@TPATM\expandafter{\newtabdecl@TPATM}%
        }%
      % Construct remaining rows.
      \setcounter{currstate@TPATM}{0}%
      \def\states@TPATM{#1,}%
      \whiledo{\not\equal{\states@TPATM}{}}
      {%
        \stepcounter{currstate@TPATM}%
        \getsymbol@TPATM\states@TPATM\state@TPATM
        \protected@edef\newtabrows@TPATM
        {%
          \the\tabrows@TPATM\noexpand\\%
          \ifnum\value{currstate@TPATM}=#7 \noexpand\rowcolor{markstatecolor@TPATM}\fi\state@TPATM
          }%
        \expandafter\tabrows@TPATM\expandafter{\newtabrows@TPATM}%
        \def\inputalphabet@TPATM{#2,}%
        \whiledo{\not\equal{\inputalphabet@TPATM}{}}
        {%
          \getsymbol@TPATM\inputalphabet@TPATM\inputsymbol@TPATM
          \protected@edef\newtabrows@TPATM
          {%
            \the\tabrows@TPATM&%
            \ifnum\value{currstate@TPATM}=#7
              \ifx\inputsymbol@TPATM\inputtapesymbol@TPATM
                \noexpand
                \multicolumn{1}{>{\noexpand\columncolor{oldoutputcolor@TPATM}}c}
                {\formatdfaoutput@TPATM\state@TPATM\inputsymbol@TPATM}%
               \else
                \ifx\inputsymbol@TPATM\nextinputtapesymbol@TPATM
                  \noexpand
                  \multicolumn{1}{>{\noexpand\columncolor{newoutputcolor@TPATM}}c}
                  {\formatdfaoutput@TPATM\state@TPATM\inputsymbol@TPATM}%
                 \else
                  \formatdfaoutput@TPATM\state@TPATM\inputsymbol@TPATM
                \fi
              \fi
             \else
              \formatdfaoutput@TPATM\state@TPATM\inputsymbol@TPATM
            \fi
            }%
          \expandafter\tabrows@TPATM\expandafter{\newtabrows@TPATM}%
          % Set succeeding state (and output symbol).
          \ifnum\value{currstate@TPATM}=#7\relax
            \ifx\inputsymbol@TPATM\inputtapesymbol@TPATM
              \getoutput@TPATM{\state@TPATM}{\inputsymbol@TPATM}%
              \fix@nextstate@TPATM
            \fi
          \fi
          }%
        }%
      % Save tabular into a box.
      \savebox{\tabbox@TPATM}
      {%
        \raisebox{\depth}
        {%
          \setlength{\fboxsep}{0.4pt}%
          \fbox
          {%
            \clipbox
            {%
              \edef\next{\noexpand\begin{tabular}[b]{c\the\tabdecl@TPATM}}%
                \statetransitiontablepreamble
                \next
                \the\tabrows@TPATM
              \end{tabular}%
              }%
            }%
          }%
        }%
      % Print state transition table, with some decoration.
      \rput(0.5,0.44)
      {%
        \raisebox{.5\tabcolsep}
        {\rotatebox{90}{\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\smaller \statename}}}%
        \parbox[b]{\wd\tabbox@TPATM}
        {%
          \offinterlineskip\raggedleft
          \psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\smaller \inputname}\kern\tabcolsep\\%
          \usebox{\tabbox@TPATM}%
          }%
        \raisebox{.5\tabcolsep}
        {%
          \rotatebox{90}
          {\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\smaller[2]\nextstatename}}%
          }%
        \label@output@TPATM
        }%
      }%
    % Draw output tape, if mealy automaton.
    \draw@outputtape@TPATM{#8}%
    % Calculate number of succeeding state.
    \setcounter{currstate@TPATM}{0}%
    \def\states@TPATM{#1,}%
    \whiledo{\not\equal{\states@TPATM}{}}
    {%
      \stepcounter{currstate@TPATM}%
      \getsymbol@TPATM\states@TPATM\state@TPATM
      \edef\state@TPATM{\state@TPATM}%
      \ifx\state@TPATM\nextstate@TPATM
        \setcounter{nextstate@TPATM}{\value{currstate@TPATM}}%
      \fi
      }%
    % Calculate new output tape.
    \mk@new@outputtape@TPATM{#8}%
  \end{pspicture}%
  \endgroup
  }


% \dfa{<width>}{<height>}{<states>}{<inputalphabet>}{<statetransitions>}
% {<initial state>}{<input tape>}{<tape position>}{<current state>}
% draws a deterministic finite automaton.
%    
% The parameters have the following meanings:
% <width>             Width of the automaton to be drawn.
% <height>            Height of the automaton to be drawn.
% <states>            Comma-separated list (symbolically) specifying the set of states.
% <inputalphabet>     Comma-separated list (symbolically) specifying the set of input symbols.
% <statetransitions>  Comma-separated list (symbolically) specifying the state transition function (also giving the
%                     output symbols associated with state transitions).
% <initial state>     Number of the initial state.
% <input tape>        Comma-separated list (symbolically) specifying the contents of the input tape.
% <tape position>     Fixed-point number (greater or equal 1) specifying the current position of the head reading input
%                     and output tape. The symbol currently read corresponds to the integer part of <tape position>.
% <current state>     Number of the current state.
%
% The standard version of \dfa displays a state transition table, i.e. for every combination of state and
% input symbol, the successor state is displayed in a tabular. The current state, current input
% symbol and next input symbol (if <tape position> is not integer) are indicated by colored bars.
%
% There is a starred version \mealyautomaton* which displays a state transition graph where states are displayed as
% circles and state transitions as arrows between circles. The `active' transition (leading from the current to the next
% state) is highlighted.
% 
% The <states> parameter looks like <state1>,<state2>,...,<staten>, where every single <state> has the following form:
% {(<x>,<y>)\accept <symbol>}
% where (<x>,<y>) and the special keyword \accept are optional and <symbol> is the symbolic name of the state.
% The optional parameter (<x>,<y>) is used only in the starred version \mealyautomaton*. If given, it specifies the
% position of the circle representing the state in the state transition graph, in PSTricks notation. The origin of the
% state transition graph is in the middle of the area reserved for it, stretching one unit in all directions, so that
% the corners of the area are at coordinates (-1,1), (1,1), (1,-1), (-1,-1). If not given in the starred version
% \mealyautomaton*, the position is calculated automatically in a `bead' style.
% The special keyword \accept flags this state as an accepting one.
% If the optional coordinates argument is not given, the outer curly braces may be left out, but otherwise leaving the
% curly braces out would disturb the parsing of the comma-separated list because of the comma between <x> and <y>.
%
% The <statetransitions> parameter looks like <trans1>,<trans2>,...,<transm>, where every single <trans> has the
% following form: 
% [<opt>]<state>;<input>-><state'>
% where [<opt>] is optional. <state> and <input> are the arguments of the state transition function, and <state'> is the
% successor state. 
% The optional parameter [<opt>] is used only in the starred version \mealyautomaton*. If given, it specifies additional
% optional parameters for the node connection command (\nccircle or \ncarc) drawing the arrow for this
% transition. \ncline is used for transitions from a state to itself. For transitions between states, \ncarc is used
% whenever the optional argument is given, otherwise \ncline is used. This is a seldom instance where giving an empty
% optional argument is different from giving no optional argument at all: Giving the optional argument [] switches
% between \ncline and \ncarc (useful if two transitions go `back and forth' between two states). 
%    

\newcommand{\initializedfa@TPATM}
{%
  \renewcommand{\draw@outputtape@TPATM}[1]
  {%
    \colorbetween{outercirclecolor@TPATM}{black}{automatoncolor}%
    \pscircle[fillstyle=solid,fillcolor=outercirclecolor@TPATM,linewidth=.5\pslinewidth](0.5,0.07){0.25}%
    \ifacceptingstate@TPATM    
      \replacecolor{acceptlampcolor@TPATM}{red}%
     \else
      \colorbetween[.1]{acceptlampcolor@TPATM}{red}{automatoncolor}%
    \fi
    \pscircle[fillstyle=solid,fillcolor=acceptlampcolor@TPATM,linewidth=.5\pslinewidth](0.5,0.07){0.15}%
    \rput[l](0.53,0.07){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\acceptname}}%
    }%
  \renewcommand{\getoutput@TPATM}[2]
  {%
    \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\nstate@TPATM
    \expandafter\expandafter\expandafter{\csname delta(##1;##2)@TPATM\endcsname}%
    }%
  \renewcommand{\inittabrows@TPATM}[1]{##1={\smaller\begin{tabular}{@{}r@{}}}}%
  \renewcommand{\addtabrow@TPATM}[2]
  {%
    \protected@edef\newtabrows@TPATM{\the##1\@hlinput@TPATM\formatsymbol{##2}\noexpand\\}%
    \expandafter##1\expandafter{\newtabrows@TPATM}%
    }%
  \def\formatdfaoutput@TPATM##1##2{\csname delta(##1;##2)@TPATM\endcsname}%
  \renewcommand{\label@output@TPATM}{}%
  \renewcommand{\fix@nextstate@TPATM}
  {%
    \global\let\nextstate@TPATM=\nstate@TPATM
    }%
  \renewcommand{\mk@new@outputtape@TPATM}[1]{}%
  }%

\newcommand{\dfa}
{%
  \initializedfa@TPATM
  \dfa@TPATM
  }

\newcommand{\dfa@TPATM}
{%
  \@ifstar
  {\setboolean{stategraph@TPATM}{true}\@dfa@TPATM}
  {\setboolean{stategraph@TPATM}{false}\@dfa@TPATM}%
  }

\newcommand{\@dfa@TPATM}[2]
{%
  \ifthenelse{\boolean{psspecialsallowed}}
  {%
    \begingroup
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
      \psset{xunit=\@tempdima,yunit=\@tempdimb,arrowinset=0}%
      \let\next=\@@dfa@TPATM
    }
  {%
    \rule{#1}{#2}%
    \let\next=\@@nodfa@TPATM
    }%
  \next
}

\newcommand{\@@nodfa@TPATM}[7]{}

\newcommand{\@@dfa@TPATM}[7]{\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{#6}{#7}{}}


% \simulatedfa{<width>}{<height>}{<states>}{<inputalphabet>}{<statetransitions>}
% {<initial state>}{<input tape>}{<number of steps>}{<tape move resolution>}
% simulates a deterministic finite automaton. 
%    
% The parameters <width>, <height>, <states>, <inputalphabet>, <statetransitions>, <initial state>, and <input tape>
% have the same meaning as for \dfa. 
% <number of steps> gives the number of state transitions to perform (in fact, the first state transition occurs after
% the first step, so <number of steps>=1 means no state transition.
% The simulation starts with both tapes at the beginning, in the initial state. After this, the simulation proceeds in
% the following steps: 
% 1) Change state.
% 2) Advance input tape and reiterate.
% The steps are realised with TeXPower's \step mechanism. This means the whole simulation will lead to the corresponding
% number of `subpages' each displaying the automaton in changing states.
% If <tape move resolution>=1, then step number 3 will just switch over the tape position. Otherwise, advancing the tape
% will take <tape move resolution> intermediate steps, which are automatically animated using hyperref's pdfpageduration
% mechanism, i.e. when displayed in acroread's full screen mode (with automatic advancing activated), a `smooth' tape
% movement will be seen.
% \simulatedfa comes in normal and starred versions, with the same semantics as for \dfa, i.e. the normal version
% displays a state transition table, the starred version displays a state transition graph. 

\newcommand{\simulatedfa}
{%
  \initializedfa@TPATM
  \@ifstar
  {\setboolean{stategraph@TPATM}{true}\@simulatedfa@TPATM}
  {\setboolean{stategraph@TPATM}{false}\@simulatedfa@TPATM}%
  }

\newcommand{\@simulatedfa@TPATM}[2]
{%
  \ifthenelse{\boolean{psspecialsallowed}}
  {%
    \begingroup
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
      \psset{xunit=\@tempdima,yunit=\@tempdimb,arrowinset=0}%
      \let\next=\@@simulatedfa@TPATM
    }
  {%
    \rule{#1}{#2}%
    \let\next=\@@nosimulatedfa@TPATM
    }%
  \next
}

\newcommand{\@@nosimulatedfa@TPATM}[7]{}

\newcounter{currentdfastate@TPATM}
\newcounter{currentsupstep@TPATM}
\newcounter{currentsubstep@TPATM}
\newboolean{beginautosim@TPATM}
\newcommand{\autosimduration}{0.04}
\releasecounter{currentdfastate@TPATM}\releasecounter{nextstate@TPATM}

\newcommand{\@@simulatedfa@TPATM}[7]
{%
  % Setup initial state.
  \setcounter{currentsupstep@TPATM}{0}%
  \setcounter{currentdfastate@TPATM}{#4}%
  \setcounter{nextstate@TPATM}{0}%
  \stepwise[\setcounter{currentdfastate@TPATM}{#4}\setcounter{nextstate@TPATM}{0}]
  {%
  \setboolean{beginautosim@TPATM}{true}%
  % Main loop for state transitions.
  \whiledo{\value{currentsupstep@TPATM}<#6}
  {%
    % At the beginning, the situation right before the state transition is displayed. This is effected by only
    % displaying the last step of the tape movement.
    \ifthenelse{\boolean{beginautosim@TPATM}}
    {\setboolean{beginautosim@TPATM}{false}\setcounter{currentsubstep@TPATM}{#7-1}}
    {\setcounter{currentsubstep@TPATM}{0}}%
    % Inner loop for tape movement.
    \whiledo{\value{currentsubstep@TPATM}<#7}
    {%
      \stepcounter{currentsubstep@TPATM}%
      % Display sequence of steps while the tape gradually moves.
      \mkfactor{\tapeposition@TPATM}{\value{currentsupstep@TPATM}+\arabic{currentsubstep@TPATM}pt/#7}%
      \ifthenelse{\value{currentsubstep@TPATM}=#7}
      {%
        \setcounter{tmpcnta@TP}{\value{currentsupstep@TPATM}+1}%
        \ifthenelse{\value{tmpcnta@TP}=#6}{\switch}{\switch[][\boolean{firstactivation}]}{}
        {%
          \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{}%
          \afterstep{\stopAdvancing}%
          \setcounter{currentdfastate@TPATM}{\value{nextstate@TPATM}}\setcounter{nextstate@TPATM}{-1}%
          }%
        }
      {%
        \switch[][\boolean{firstactivation}]{}
        {%
          \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{}%
          \afterstep{\pageDuration{\autosimduration}}%
          }%
        }%
      }%
    \stepcounter{currentsupstep@TPATM}%
    % After the tape is in the next (or initial) position, switch state and reiterate.
    \ifthenelse{\value{currentsupstep@TPATM}=#6}{}
    {%
      \switch[][\boolean{firstactivation}]{}%
      {%
        \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{}%
        }%
      }%
    }%
  }%
\endgroup
}


%-----------------------------------------------------------------------------------------------------------------------
% Finite Automata with output.

% \mealyautomaton{<width>}{<height>}{<states>}{<inputalphabet>}{<statetransitions>}
% {<initial state>}{<input tape>}{<tape position>}{<current state>}{<output tape>}
% draws a mealy automaton (deterministic finite automaton with output tape, output is triggered by state transitions).
%    
% The parameters have the same meanings as for \dfa, with one addition:
% <output tape>       Comma-separated list (symbolically) specifying the contents of the output tape.
%
% The standard version of \mealyautomaton displays a state transition table, i.e. for every combination of state and
% input symbol, the successor state and output symbol are displayed in a tabular. The current state, current input
% symbol and next input symbol (if <tape position> is not integer) are indicated by colored bars.
%
% There is a starred version \mealyautomaton* which displays a state transition graph where states are displayed as
% circles and state transitions as arrows between circles. The `active' transition (leading from the current to the next
% state) is highlighted.
% 
% The <states> parameter has the same structure as for \dfa.
%
% The <statetransitions> parameter looks like <trans1>,<trans2>,...,<transm>, where every single <trans> has the
% following form: 
% [<opt>]<state>;<input>-><state'>;<output>
% where [<opt>] is optional. <state> and <input> are the arguments of the state transition function, and <state'> and
% <output> are the successor state and output symbol, respectively.
% The optional parameter [<opt>] is used only in the starred version \mealyautomaton*. If given, it specifies additional
% optional parameters for the node connection command (\nccircle or \ncarc) drawing the arrow for this
% transition. \ncline is used for transitions from a state to itself. For transitions between states, \ncarc is used
% whenever the optional argument is given, otherwise \ncline is used. This is a seldom instance where giving an empty
% optional argument is different from giving no optional argument at all: Giving the optional argument [] switches
% between \ncline and \ncarc (useful if two transitions go `back and forth' between two states). 
%    

\newcommand{\initializema@TPATM}
{%
  \renewcommand{\draw@outputtape@TPATM}[1]
  {%
    \rput[l](0.07,0.07){\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\outputtapename}}%
    \mkfactor{\startx@TPATM}{\ratio{\tapepos@TPATM}{1pt}}%
    \setcounter{tapecells@TPATM}{6+\value{currinputsymbol@TPATM}}%
    \multips(\startx@TPATM,0.02)(0.1,0){\value{tapecells@TPATM}}{\psframe[fillstyle=solid,fillcolor=cellcolor](0.1,0.1)}%
    \mkfactor{\startx@TPATM}{\ratio{\tapepos@TPATM+0.05pt}{1pt}}%
    \filltofive@TPATM{\startx@TPATM}%
    \protected@edef\tapecontents@TPATM{##1,}%
    \setcounter{currtapepos@TPATM}{0}%
    \multido{\n=\startx@TPATM+0.10000}{\value{tapecells@TPATM}}
    {%
      \ifx\tapecontents@TPATM\empty
       \else
        \stepcounter{currtapepos@TPATM}%
        \getsymbol@TPATM\tapecontents@TPATM\outputtapesymbol@TPATM
        \ifnum\value{currtapepos@TPATM}=\value{currinputsymbol@TPATM}\relax
          \global\let\outputtapesymbol@TPATM\outputtapesymbol@TPATM
        \fi
        \rput(\n,0.07){\formatsymbol\outputtapesymbol@TPATM}%
      \fi
      }%
    \psframe[linewidth=2pt,linecolor=mathcolor](0.45,0.02)(0.55,0.12)
    }%
  \renewcommand{\getoutput@TPATM}[2]
  {\expandafter\expandafter\expandafter\@getoutput@TPATM\csname delta(##1;##2)@TPATM\endcsname\@nil}%
  \def\@getoutput@TPATM##1;##2\@nil{\def\nstate@TPATM{##1}\def\output@TPATM{##2}}%
  \renewcommand{\inittabrows@TPATM}[1]{##1={\smaller\begin{tabular}{rcl}}}%
  \renewcommand{\addtabrow@TPATM}[2]
  {%
    \protected@edef\newtabrows@TPATM
    {%
      \the##1%
      \@hlinput@TPATM\formatsymbol{##2}&\origmath{\vartriangleright}&\noexpand\formatsymbol{\output@TPATM}\noexpand\\%
      }%
    \expandafter##1\expandafter{\newtabrows@TPATM}%
    }%
  \def\formatdfaoutput@TPATM##1##2%
  {\expandafter\expandafter\expandafter\@formatdfaoutput@TPATM\csname delta(##1;##2)@TPATM\endcsname\@nil}%
  \def\@formatdfaoutput@TPATM##1;##2\@nil{##1;\formatsymbol{##2}}%
  \renewcommand{\label@output@TPATM}
  {%
    \raisebox{.5\tabcolsep}
    {\rotatebox{90}{\psframebox[linestyle=none,fillstyle=solid,fillcolor=autoplatecolor]{\smaller \outputname}}}%
    }%
  \renewcommand{\fix@nextstate@TPATM}
  {%
    \global\let\newoutputsymbol@TPATM=\output@TPATM
    \global\let\nextstate@TPATM=\nstate@TPATM
    }%
  \renewcommand{\mk@new@outputtape@TPATM}[1]%
  {%
    \setcounter{currtapepos@TPATM}{0}%
    \protected@edef\tapecontents@TPATM{##1,}%
    \let\newoutputtape@TPATM=\empty
    \whiledo{\not\equal{\tapecontents@TPATM}{}}
    {%
      \stepcounter{currtapepos@TPATM}%
      \getsymbol@TPATM\tapecontents@TPATM\outputsymbol@TPATM
      \ifnum\value{currtapepos@TPATM}=\value{currinputsymbol@TPATM}%
        \let\outputsymbol@TPATM=\newoutputsymbol@TPATM%
      \fi
      \protected@edef\newoutputtape@TPATM{\newoutputtape@TPATM\outputsymbol@TPATM,}%
      }%
    \expandafter\gobblelastcomma@TPATM\newoutputtape@TPATM\@nil\newoutputtape@TPATM
    }%
  }

\newcommand{\mealyautomaton}
{%
  \initializema@TPATM
  \mealyautomaton@TPATM
  }

\newcommand{\mealyautomaton@TPATM}
{%
  \@ifstar
  {\setboolean{stategraph@TPATM}{true}\@mealyautomaton@TPATM}
  {\setboolean{stategraph@TPATM}{false}\@mealyautomaton@TPATM}%
  }

\newcommand{\@mealyautomaton@TPATM}[2]
{%
  \ifthenelse{\boolean{psspecialsallowed}}
  {%
    \begingroup
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
      \psset{xunit=\@tempdima,yunit=\@tempdimb,arrowinset=0}%
      \let\next=\@@genericdfa@TPATM
    }
  {%
    \rule{#1}{#2}%
    \let\next=\@@nomealyautomaton@TPATM
    }%
  \next
}

\newcommand{\@@nomealyautomaton@TPATM}[8]{}


% \simulatema{<width>}{<height>}{<states>}{<inputalphabet>}{<statetransitions>}
% {<initial state>}{<input tape>}{<number of steps>}{<tape move resolution>}{<output tape>}
% simulates a mealy automaton (deterministic finite automaton with output tape, output is triggered by state
% transitions). 
%    
% The parameters <width>, <height>, <states>, <inputalphabet>, <statetransitions>, <initial state>, <input tape>,
% <output tape> have the same meaning as for \mealyautomaton.
% The parameters <number of steps> and <tape move resolution> have the same meaning as for \simulatedfa.
% The simulation starts with both tapes at the beginning, in the initial state and with the output tape in the state
% given by <output tape>. After this, the simulation proceeds in the following steps:
% 1) Write output symbol of the `active' transition to the output tape.
% 2) Change state.
% 3) Advance both tapes and reiterate.
% \simulatema comes in normal and starred versions, with the same semantics as for \mealyautomaton, i.e. the normal
% version displays a state transition table, the starred version displays a state transition graph.

\newcommand{\simulatema}
{%
  \initializema@TPATM
  \@ifstar
  {\setboolean{stategraph@TPATM}{true}\@simulatema@TPATM}
  {\setboolean{stategraph@TPATM}{false}\@simulatema@TPATM}%
  }

\newcommand{\@simulatema@TPATM}[2]
{%
  \ifthenelse{\boolean{psspecialsallowed}}
  {%
    \begingroup
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
      \psset{xunit=\@tempdima,yunit=\@tempdimb,arrowinset=0}%
      \let\next=\@@simulatema@TPATM
    }
  {%
    \rule{#1}{#2}%
    \let\next=\@@nosimulatema@TPATM
    }%
  \next
}

\newcommand{\@@nosimulatema@TPATM}[8]{}

\newcommand{\@@simulatema@TPATM}[8]
{%
  % Setup initial state.
  \setcounter{currentsupstep@TPATM}{0}%
  \setcounter{currentdfastate@TPATM}{#4}\gdef\currentoutputtape@TPATM{#8}%
  \stepwise[\setcounter{currentdfastate@TPATM}{#4}\gdef\currentoutputtape@TPATM{#8}]
  {%
  \setboolean{beginautosim@TPATM}{true}%
  % Main loop for state transitions.
  \whiledo{\value{currentsupstep@TPATM}<#6}
  {%
    % At the beginning, the situation right before the state transition is displayed. This is effected by only
    % displaying the last step of the tape movement.
    \ifthenelse{\boolean{beginautosim@TPATM}}
    {\setboolean{beginautosim@TPATM}{false}\setcounter{currentsubstep@TPATM}{#7-1}}
    {\setcounter{currentsubstep@TPATM}{0}}%
    % Inner loop for tape movement.
    \whiledo{\value{currentsubstep@TPATM}<#7}
    {%
      \stepcounter{currentsubstep@TPATM}%
      % Display sequence of steps while the tape gradually moves.
      \mkfactor{\tapeposition@TPATM}{\value{currentsupstep@TPATM}+\arabic{currentsubstep@TPATM}pt/#7}%
      \switch[][\boolean{firstactivation}]{}
      {%
        \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{\currentoutputtape@TPATM}%
        \ifthenelse{\value{currentsubstep@TPATM}=#7}
        {\afterstep{\stopAdvancing}\global\let\currentoutputtape@TPATM\newoutputtape@TPATM}%
        {\afterstep{\pageDuration{\autosimduration}}}%
        }%
      }%
    \stepcounter{currentsupstep@TPATM}%
    % After the tape is in the next (or initial) position, display new output symbol.
    \switch[][\boolean{firstactivation}]{}
    {%
      \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{\currentoutputtape@TPATM}%
      \setcounter{currentdfastate@TPATM}{\value{nextstate@TPATM}}\setcounter{nextstate@TPATM}{-1}%
      }%
    % Next, switch state and reiterate.
    \ifthenelse{\value{currentsupstep@TPATM}=#6}{\switch}{\switch[][\boolean{firstactivation}]}{}%
    {%
      \begingroup\genericdfa@TPATM{#1}{#2}{#3}{#4}{#5}{\tapeposition@TPATM}{\value{currentdfastate@TPATM}}{\currentoutputtape@TPATM}%
      }%
    }%
  }%
\endgroup
}

%KEEPCOMMENTS
%</automata>
%=======================================================================================================================
%<*fixseminar>
%<<KEEPCOMMENTS
%=======================================================================================================================
% File: fixseminar.sty
%
% Fix some problems in conjunction with seminar.
%
% This package is part of the TeXPower bundle, to be found at
% 
% http://texpower.sourceforge.net/
%
% If using hyperref, load this package _after_ hyperref.
%
%=======================================================================================================================
% Author: Stephan Lehmke <Stephan.Lehmke@cs.uni-dortmund.de>
%
% v0.0.1  Mar 28, 2000 First version for the pre-alpha release of texpower and hyperref v6.70a.
%
% v0.0.1a May 18, 2000 Added a \vss to a \vbox to get rid of an `overfull vbox' message.
%
% v0.0.2  Jun 02, 2000 Now handle seminar as well as powersem.
%
% v0.0.3  Sep 07, 2000 hyperref v6.70u and later don't have the \@hyperfixhead bug any more.
%
% v0.0.4  Nov 22, 2002 Now using ifpdf package if available.
%                      As hyperref v6.70u exists for some time now, the \@hyperfixhead hack is removed.
%                      Fixed a problem with \twoup (bug found and fix proposed by Laurent Mugnier).
%                      Added a preliminary fix for rotating slides with pdftex.
%
% v0.0.5  May 26, 2003 A small change to make fixseminar work better when hyperref is not loaded.


\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage%
{fixseminar}%
[2003/05/26 v0.0.5 Fix some problems of seminar.]


%=======================================================================================================================

\RequirePackage{ifthen}


%=======================================================================================================================
% Options and general setup.

\ProcessOptions


% The switch \ifpdf is to determine whether pdfLaTeX is being run and outputting pdf, using Heiko Oberdiek's faultproof
% pdf detector: 

\IfFileExists{ifpdf.sty}
{%
  \RequirePackage{ifpdf}%
  }
{%
  \@ifundefined{pdftrue}
  {%
  \expandafter\newif\csname ifpdf\endcsname
  \ifx\pdfoutput\undefined
   \else
    \ifx\pdfoutput\relax
     \else
      \ifcase\pdfoutput
       \else
        \pdftrue
       \fi
     \fi
   \fi
   }{}%
   }


%=======================================================================================================================
% Here come the fixes.

% We need to make sure that magnification, as used by seminar, is respected when setting \pdfpageheight and
% \pdfpagewidth. We strip the pt and add truept which respects magnification.


\ifthenelse{\boolean{pdf}}%          Are we outputting PDF?
{% Yes.
  \@ifclassloaded{seminar}%         seminar document class?
  {%
    \RequirePackage{graphics}%
    \def\leftsliderotation#1{\rotatebox{90}{#1}}%
    \def\rightsliderotation#1{\rotatebox{270}{#1}}%
    \sliderotation{left}%
  }{}%
  \AtBeginDocument%                   Then we need to set \pdfpagewidth and \pdfpageheight in a _true_ unit to make sure
  {%                                  the pdf page has the right dimensions even when magnifying (as seminar does).
    \setlength{\pdfhorigin}{1truein}% \pdfhorigin and \pdfvorigin also need to be set to true dimensions.
    \setlength{\pdfvorigin}{1truein}%
    \@ifclassloaded{seminar}%         seminar document class?
    {%
      \ifportrait%                    Portrait slides?
        \setlength{\pdfpageheight}{\strip@pt\paperheight truept}%
        \setlength{\pdfpagewidth}{\strip@pt\paperwidth truept}%
       \else%                         Landscape. Need to exchange height and width. 
        \renewcommand{\printlandscape}
        {%
          \setlength{\pdfpagewidth}{\strip@pt\paperheight truept}%
          \setlength{\pdfpageheight}{\strip@pt\paperwidth truept}%
        }%
      \fi
      }%
    {%
      \setlength{\pdfpageheight}{\strip@pt\paperheight truept}%
      \setlength{\pdfpagewidth}{\strip@pt\paperwidth truept}%
    }%
  }%
}
{% No. In this case we define \special@paper which will put an appropriate \special into the dvi file.
  \@ifclassloaded{seminar}
  {%
    \ifportrait
      \edef\special@paper{\the\paperwidth,\the\paperheight}%
     \else%                                                     Need to exchange height and width. 
      \renewcommand{\printlandscape}{\edef\special@paper{\the\paperheight,\the\paperwidth}}
    \fi
    \AtBeginDocument{\special{papersize=\special@paper}}%
    }%
  {%
    }%
  }%


% There is a bug in old hyperref versions to the effect that apparently, when using seminar there is a glitch in
% vertical spacing. Issue a warning if hyperref is too old.

\@ifpackageloaded{hyperref}%               Looking for \@hyperfixhead bug.
{%
  \@ifpackagelater{hyperref}{2000/09/07}%  Is the \@hyperfixhead bug present in hyperref?
  {}%                                      No. 
  {%                                       Yes. Issue a warning.
    \PackageWarning{fixseminar}
    {Package hyperref too old (older than 6.70u).\MessageBreak Expect problems with seminar}%
    }%
  }{}
%KEEPCOMMENTS
%</fixseminar>
%=======================================================================================================================
%<*tppstcol>
%<<KEEPCOMMENTS
%=======================================================================================================================
% File: tppstcol.sty
%
% Replacement for ``pstcol.sty'' with some quirks corrected.
% There is a small problem with pstcol.sty which makes it impossible
% to set something like "fillcolor=\linecolor" in pstricks (see LaTeX
% Bug graphics/3366). As this is absolutely indispensible for
% automata.sty, I had to make a modified version to be used until the
% bug is mended.
% The following code is identical with the original pstcol.sty, with
% some modifications to correct the bug. 
%
% This package is an addition to the TeXPower bundle, to be found at
% 
% http://texpower.sourceforge.net/
%
% Beware! As soon as the original bug in pstcol is corrected, this
% package will be deleted.
%
%=======================================================================================================================

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tppstcol}
          [2001/08/28 v0.2  PSTricks color compatibility (DPC; revised
          by STL)]
%%%%%%%%%%%%%%%%%%
\let\saved@gray\color@gray
\RequirePackage{pstricks}
\let\color@gray\saved@gray
%%%%%%%%%%%%%%%%
\DeclareOption*{\PassOptionsToPackage\CurrentOption{color}}
\ProcessOptions
\RequirePackage{color}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pst@getcolor#1#2{%
\@ifundefined{\string\color@#1}%
{\@pstrickserr{Color `#1' not defined}\@eha}%
 {\edef#2{#1}}}
\def\pst@usecolor#1%
{%
  \expandafter\expandafter\expandafter
  \c@lor@to@ps\csname\string\color @#1\endcsname\@@\space%
  }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{darkgray}{gray}{.25}
\definecolor{gray}{gray}{.5}
\definecolor{lightgray}{gray}{.75}
\definecolor{gradbegin}{rgb}{0,1,1}
\definecolor{gradend}{rgb}{0,.1,.95}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\psset@bordercolor{white}
\psset@doublecolor{white}
\psset@linecolor{black}
\psset@fillcolor{white}
\psset@hatchcolor{black}
\psset@gridcolor{black}
\psset@gridlabelcolor{black}
\psset@shadowcolor{darkgray}
\psset@subgridcolor{gray}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\newgray#1{%
  \definecolor{#1}{gray}}
\def\newrgbcolor#1#2{%
  \newrgbcolorx{#1}#2\@@}
\def\newrgbcolorx#1#2 #3 #4\@@{%
  \definecolor{#1}{rgb}{#2,#3,#4}}
\def\newcmykcolor#1#2{%
  \newcmykcolorx{#1}#2\@@}
\def\newcmykcolorx#1#2 #3 #4 #5\@@{%
  \definecolor{#1}{cmyk}{#2,#3,#4,#5}}
%KEEPCOMMENTS
%</tppstcol>
%=======================================================================================================================
%<*tpsem-a4>
%<<KEEPCOMMENTS
% Fixed for LaTeX2e (\paperwidth and \paperheight are dimensions) by
% Stephan Lehmke.
\setlength\paperwidth{210mm}
\setlength\paperheight{297mm}
\input sem-page.sty
\slidewidth 222mm
\slideheight 152mm
%KEEPCOMMENTS
%</tpsem-a4>
\endinput