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
|
% \iffalse meta-comment
% rubikrotation.dtx
%
% Authors: RWD Nickalls (dick@nickalls.org)
% and Apostolos Syropoulos (asyropoulos@yahoo.com)
% Copyright 2014 RWD Nickalls + A Syropoulos
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this licence is in
%
% http://www.latex-project.org/lppl.txt
%
%<*readme>
%
% The rubikrotation package provides a collection of LaTeX commands and macros
% for the typesetting of Rubik cube configurations and rotation
% sequences using the TikZ graphic language.
%
% Please report errors or suggestions for improvement to
%
% Dick Nickalls or Apostolos Syropoulos
%
% This package requires the basic TikZ package to be loaded already
%</readme>
%
%<*driver>
\listfiles
\documentclass{ltxdoc}
\IfFileExists{rubikrotation.sty}{\usepackage{rubikrotation}}{%
\GenericWarning{rubikrotation.dtx}{Package file rubikrotation.sty not found.
Documentation will be messed up!^^J
(Generate rubikrotation.sty by (La)TeXing rubikrotation.ins, and then^^J
process rubikrotation.dtx again)^^J}\stop
}%
\usepackage{ifpdf}
\usepackage{url} %% for references
\usepackage{graphicx} %% for the two pdf figs
%%\OnlyDescription
\EnableCrossrefs
\CodelineIndex
\CodelineNumbered
\RecordChanges
\setcounter{StandardModuleDepth}{1}
\begin{document}
\DocInput{rubikrotation.dtx}
\PrintChanges
\PrintIndex
\end{document}
%</driver>
% \fi
%
%
%
% \CheckSum{302}
%
%%% \CharacterTable
%% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%% Digits \0\1\2\3\4\5\6\7\8\9
%% Exclamation \! Double quote \" Hash (number) \#
%% Dollar \$ Percent \% Ampersand \&
%% Acute accent \' Left paren \( Right paren \)
%% Asterisk \* Plus \+ Comma \,
%% Minus \- Point \. Solidus \/
%% Colon \: Semicolon \; Less than \<
%% Equals \= Greater than \> Question mark \?
%% Commercial at \@ Left bracket \[ Backslash \\
%% Right bracket \] Circumflex \^ Underscore \_
%% Grave accent \` Left brace \{ Vertical bar \|
%% Right brace \} Tilde \~}
%
%
%
% \title{The \textsf{rubikrotation} package}
%
% \author{
% RWD Nickalls (dick@nickalls.org) \\
% A Syropoulos (asyropoulos@yahoo.com)
% }
% \date{This file describes version \RRfileversion, last revised \RRfiledate}
% \maketitle
%
% \begin{abstract}
% The \rubikrotation\ package is an extension for the
% \textsf{rubikcube} package. It provides the \cmd{\RubikRotation} command which
% processes on-the-fly a sequence of Rubik rotation moves (using the Perl script
% \texttt{rubikrotation.pl}) and returns the new Rubik cube state. It implements
% some basic checking of the Rubik state, and also offers a command for displaying
% any errors (\cmd{\ShowRubikErrors}).
% This package requires access to the \TeX\ \texttt{write18} facility by using the
% \texttt{--shell-escape} commandline switch.
% The \textsf{rubikrotation} package has been road-tested on a Microsoft
% platform (with MikTeX and Strawberry Perl), on a Linux platform ({\TeX}Live and Mandriva),
% and on OpenIndiana (a Solaris platform).
% \end{abstract}
%
%
% \bigskip
%
% \tableofcontents
%
% \section{Introduction}
%
% The \rubikrotation\ package is a dynamic extension to the \textsc{rubikcube} package.
% It consists of a style option (\texttt{rubikrotation.sty}) and a
% Perl script (\texttt{rubikrotation.pl}).
%
% The primary role of the \rubikrotation\ package is to implement on-the-fly Rubik
% rotation sequences using the \cmd{\RubikRotation} command. Consequently, this
% package requires the use of the \texttt{--shell-escape} switch to allow
% commandline control of the Perl script, which is really the `engine' of this package.
% The \textsf{rubikrotation} package has been road-tested on a Microsoft
% platform (with MikTeX and Strawberry Perl\,\footnote{`Strawberry Perl'
% (\texttt{http://strawberryperl.com}) is a free Perl
% environment for MS Windows, designed to be as close as possible to the Perl
% environment of Unix/Linux systems.})
% and on a Linux platform ({\TeX}Live and Mandriva).
%
% The following commands are made available by \texttt{rubikrotation.sty}.
% \begin{quote}
% \begin{verbatim}
% \RubikRotation{}
% \SaveRubikState
% \CheckRubikState
% \ShowRubikErrors
%\end{verbatim}
% \end{quote}
% Note that if the Perl script is not located in the local working directory, then
% some care is needed regarding placing it where your system can both find it and
% also run it. In this case, the setting up of a simple one or two-line configuration
% file may be useful or even necessary, depending on your system
% (see section on `installation' below).
%
%
% \section{Requirements}
%
% The \rubikrotation\ package requires the TikZ and the \textsc{rubikcube} packages.
%
%
% \section{Installation}
%
% \subsection{Generating the files}
%
% Place the file \texttt{rubikrotation.zip} into a temporary directory, and unzip it.
% This will generate the following files:
% \begin{verbatim}
% rubikrotation.ins
% rubikrotation.dtx
% rubikrotation.sty
% rubikrotation.pl
% rubikrotation.PDF
% example-rot1.tex
% example-rot1.PDF
% example-rot2.tex
% example-rot2.PDF
%\end{verbatim}
% The style option \texttt{rubikrotation.sty} is generated by running (pdf)\LaTeX\ on
% the file \texttt{rubikrotation.ins} as follows:
%\begin{verbatim}
% pdflatex rubikrotation.ins
%\end{verbatim}
% The documentation file (\texttt{rubikrotation.pdf}) was generated using the following
% steps:
% \begin{verbatim}
% pdflatex --shell-escape rubikrotation.dtx
% pdflatex --shell-escape rubikrotation.dtx
% makeindex -s gind.ist rubikrotation
% makeindex -s gglo.ist -o rubikrotation.gls rubikrotation.glo
% pdflatex --shell-escape rubikrotation.dtx
%\end{verbatim}
%
%
% \subsection{Placing the files}
% \label{sec:placingfiles}
%
% Place the files either in the local working directory, or where your system
% will find them. For a Linux system with a standard \TeX\ Directory Structure (TDS), then:
%
%\medskip
%{\noindent}*.sty $\rightarrow$
% \texttt{/usr/local/texlive/texmf-local/tex/latex/local/rubikrotation/}
%{\newline}*.cfg $\rightarrow$
% \texttt{/usr/local/texlive/texmf-local/tex/latex/local/rubikrotation/}
%{\newline}*.pdf $\rightarrow$ \texttt{/usr/local/texlive/texmf-local/doc/rubikrotation/}
%{\newline}*.pl $\rightarrow$ \texttt{/usr/local/bin/}
%
% \medskip
% {\noindent}Finally, (depending on your system) update the \TeX\ file database using the
% \texttt{texhash} command.
%
% \subsection{Configuration file}
% \label{sec:configfile}
%
% A plane text configuration file with the name \texttt{rubikrotation.cfg} (if one exists)
% will be read by the system. Such a file allows the user to
% adjust (a)~the filename of the Perl script (\texttt{rubikrotation.pl})
% and (b)~the commandline code used by \texttt{rubikrotation.sty} for calling the
% Perl script. This sort of fine-tuning is sometimes convenient, and even necessary
% (depending on your system), for locating and running the Perl script.
% For example, on some systems it maybe preferable to use a different \textsc{path},
% file name and/or a different commandline code to call the script.
%
%
% \DescribeMacro{\rubikperlname}
% \DescribeMacro{\rubikperlcmd}
% The configuration file is essentially a convenient software vehicle for feeding some
% additional \LaTeX\ code to the style option, and hence allow the contents of some
% commands to be adjusted and/or fine-tuned.
% For the \rubikrotation\ package there are two particular commands we may wish to adjust.
% The first is that defining the filename of the Perl script, namely
% \cmd{\rubikperlname}. The second is that defining the commandline call,
% namely \cmd{\rubikperlcmd}.
% The default definitions for these (detailed in Section~\ref{sec:usefulcommands})
% are as follows:
%\begin{verbatim}
% \newcommand{\rubikperlname}{rubikrotation.pl}
% \newcommand{\rubikperlcmd}{perl \rubikperlname}
%\end{verbatim}
%
%
% For example, we might wish to test out a slightly modified Perl script, say
% with the name \texttt{rubikrotationV3.pl}. In this case we simply create,
% in the local working directory, a plane text configuration file
% (called \texttt{rubikrotation.cfg}) which includes the following line:
%\begin{verbatim}
%\renewcommand{\rubikperlname}{rubikrotationV3.pl}
%\end{verbatim}
%
% Alternatively, say, if the Perl script is being installed on a Linux platform, then
% it would be standard to install it in the directory \texttt{/usr/local/bin}.
% A convenient approach in this case, therefore, would be to indicate this new \textsc{path}
% by including the following line in the configuration file:
%\begin{verbatim}
%\renewcommand{\rubikperlname}{/usr/local/bin/rubikrotation.pl}
%\end{verbatim}
% and, since this would then be an essentially permanent feature, we would then
% place the configuration file with the style option in the
% \texttt{texmf-local/...} directory as described in section~\ref{sec:placingfiles}
% (placing files). Note that in this case, we have \textit{not} made the Perl script
% `executable', since the default commandline code is effectively
% \texttt{perl \rubikperlname}, and this works just as it is.
%
% However, if the Perl script \textit{is} made `executable', then
% a different commandline code will have to be used instead.
% For example, suppose the Perl script is made executable and renamed to just
% \texttt{rubikrotation} (ie with no filename extension), then we now have to
% omit the `perl' from the default commandline. Consequently, we now need to make
% two command changes, which we implement by including the following
% two lines in the configuration file:
%\begin{verbatim}
%\renewcommand{\rubikperlname}{/usr/local/bin/rubikrotation}
%\renewcommand{\rubikperlcmd}{\rubikperlname}
%\end{verbatim}
%
%
% \section{Usage}
%
% Load the packages \texttt{rubikcube.sty}
% and \texttt{rubikrotation.sty} in the \TeX\ file \textit{after} loading
% the TikZ package, as follows:
% \begin{verbatim}
% \usepackage{tikz}
% \usepackage{rubikcube,rubikrotation}
%\end{verbatim}
% and run (pdf)\LaTeX\ using the \texttt{--shell-escape} commandline switch
% (see following section).
%
%
% \subsection{Enabling the \TeX\ `shell' facility}
%
% In order to enable the \TeX\ `write18' facility (so it can run the Perl script)
% we will need to invoke (pdf)\LaTeX\ using the \texttt{--shell-escape} switch; say, as follows.
% \begin{verbatim}
% pdflatex --shell-escape filename.tex
%\end{verbatim}
% In practice, it is probably most convenient to run this via a
% bash/batch file---something like the following, both of which
% automatically show the graphic output:
% \begin{verbatim}
% pdflatex --shell-escape filename.tex
% xpdf filename.pdf
%\end{verbatim}
% or
% \begin{verbatim}
% latex --shell-escape filename.tex
% dvips filename.dvi
% gv filename.ps
%\end{verbatim}
%
%
% \subsection{Test files}
%
% Two example tex files (which demonstrate the use of the package commands) are
% included in the package, namely:
% \begin{verbatim}
% example-rot1.tex (shows 5 worked examples)
% example-rot2.tex (is a `test' file for experimenting with different commands)
%\end{verbatim}
% These need to be run using \texttt{--shell-escape} switch; for example:
% \begin{verbatim}
% pdflatex --shell-escape example-rot1.tex
%\end{verbatim}
% If the files give unexpected results, check-out the log file to see if the system
% has had any difficulties finding files etc.
%
%
%
% \section{Commands}
%
% The \textit{only} commands which \textit{must} be used inside a TikZ picture
% environment are the \cmd{\Draw...} commands (these are all provided by
% the \textsc{rubikcube} package), although most commands can be placed
% inside a TikZ environment if necessary. However, using commands outside the environment
% generally offers maximum flexibility, since the effects of commands used inside
% a TikZ picture environment are `local' to that picture environment, and are not
% therefore accessable outside the environment.
%
% The only command which must \textit{not}
% be inside a TikZ environment is the \cmd{\ShowRubikErrors} command
% (see the notes on this command below).
%
%
% \subsection{\cmd{\RubikRotation} command}
%
% \DescribeMacro{\RubikRotation}
% The \cmd{\RubikRotation}\marg{comma separated sequence} command processes
% a comma separated sequence of rotations, and returns the final state.
% For example, if we wanted to see the effect of the rotations
% \textbf{R, R, L, U, D} on a solved Rubik cube, we could use the
% following commands.
% \begin{verbatim}
% \begin{tikzpicture}[scale=0.7]
% \RubikCubeSolved
% \RubikRotation{R2,L,U,D}
% \DrawRubikCubeRU
% \end{tikzpicture}
%\end{verbatim}
% The \cmd{\RubikRotation} command results in \LaTeX\ first writing
% the current Rubik state to a text file (\texttt{rubikstate.dat}),
% and then calling the Perl program \texttt{rubikrotation.pl}. The Perl
% program then reads the current rubik state from the (\texttt{rubikstate.dat})
% file, performs all the rotations, and then writes the new rubik state
% (and any error messages) to the file \texttt{rubikstateNEW.dat},
% which is then input on-the-fly by the \LaTeX\ file and used to generate
% some graphic image of the cube.
%
% Note that the \cmd{\RubikRotation} command can be either inside
% or before the Tikz picture environment. In fact only the `\cmd{\Draw...}'
% commands (from the \textsc{rubikcube} package) actually need to be inside
% a TikZ picture environment. Consequently this makes for great flexibility.
%
% \subsubsection{Arguments prefixed with a *}
%
% If any of the comma separated arguments (strings) is prefixed with a * it is
% not processed as a rotation. This feature therefore allows a string argument
% to be used as a label, which can be very useful.
% For example, we can use the * feature to label the following
% sequence as generating the so-called
% `sixspot' configuration (described by Reid):
% \begin{verbatim}
% \RubikRotation{*sixspot,U,Dp,R,Lp,F,Bp,U,Dp}
%\end{verbatim}
% Alternatively, and probably more conveniently, we could simply use
% the name `sixspot' to define a new command, as follows (which therefore allows
% one to store lots of different rotation sequences by name alone):
% \begin{verbatim}
% \newcommand{\sixspot}{U,Dp,R,Lp,F,Bp,U,Dp}
%\end{verbatim}
% With this `newcommand' we are now able to generate the graphic (sixspot cube)
% much more easily using the following code:
%
% \medskip
%
% \begin{minipage}{6cm}
% \begin{verbatim}
% \begin{tikzpicture}[scale=0.7]
% \RubikCubeSolved
% \RubikRotation{\sixspot}
% \DrawRubikCubeRU
% \end{tikzpicture}
%\end{verbatim}
% \end{minipage}
% \begin{minipage}{3.4cm}
% \centering
% \ifpdf
% \includegraphics[height=3cm]{Rubikrot_doc_figA.pdf}
% \else
% \fi
% \end{minipage}
%
%
% \medskip
%
% In practice, it is quite useful to go one step further and include
% the \texttt{*} label feature as well in the newcommand, as follows:
% \begin{verbatim}
% \newcommand{\sixspot}{*sixspot,U,Dp,R,Lp,F,Bp,U,Dp}
%\end{verbatim}
% since this has the great advantage of making the particular
% rotation sequence identified by the label-name visible in the log file.
% For example, the following command, which uses the rotations \textbf{x2}
% and \textbf{y} to initially rotate the `solved' cube before applying
% the `sixspot' sequence of rotations,
% \begin{verbatim}
% \RubikRotation{x2,y,\sixspot}
%\end{verbatim}
% will then be represented in the log file as
% \begin{verbatim}
% ...command=rotation,x2,y,*sixspot,U,Dp,R,Lp,F,Bp,U,Dp
% ...arguments passed to `rotation' sub = x2 y *sixspot U Dp R Lp F Bp U Dp (n= 11)
% ...rotation x OK (= rrR + rrSr + rrLp)
% ...rotation x OK (= rrR + rrSr + rrLp)
% ...rotation y OK (= rrU + rrSu + rrDp)
% ...*sixspot is a label OK
% ...rotation U OK
% ...rotation Dp OK
% ...rotation R OK
% ...rotation Lp OK
% ...rotation F OK
% ...rotation Bp OK
% ...rotation U OK
% ...rotation Dp OK
%\end{verbatim}
% In this way, several named rotation sequences can be easily distinguished
% in the log file from adjacent rotation commands.
%
%
% \subsubsection{Random rotations}
%
% The \cmd{\RubikRotation} command can also be used to scramble the
% cube using a random sequence of rotations. If the first argument
% is the lowercase word `random' \textsc{and} the second argument
% is an integer $n$, ($1\leq n \leq 200$), then a random sequence
% of $n$ rotations will be performed; otherwise a default value
% of 50 is used (for example, if the second argument is not an integer).
% If $n > 200$ then the value $n=200$ will be used.
%
% For example, the following commands will scramble a solved cube using a
% sequence of 120 random rotations, and display the state in the form of
% a semi-flat cube.
%
% \medskip
%
% \begin{minipage}{5.5cm}
%\begin{verbatim}
% \RubikCubeSolved%
% \RubikRotation{random,120}%
% \begin{tikzpicture}[scale=0.5]
% \DrawRubikCubeFlat
% \end{tikzpicture}
%\end{verbatim}
% \end{minipage}
% \begin{minipage}{3.4cm}
% \centering
% \ifpdf
% \includegraphics[height=4cm]{Rubikrot_doc_figB.pdf}
% \else
% \fi
% \end{minipage}
%
% Note that in this particular example (above), only
% the \cmd{\Draw..} command is inside the TikZ picture environment---a useful
% method when more than one figure is being drawn. Note also, that when such
% commands are outside a TikZ picture environment, they should have a
% trailing \% to stop additional white space being included.
%
% \medskip
%
% The procedure is that all the possible rotations are first allocated
% a different number (integer) and collected into an array. Then a
% sequence of $n$ randomised numbers is generated and mapped to the array
% to generate the associated sequence of random rotations.
% The sequence used is detailed in the \texttt{.log} file.
%
%
% \subsection{\cmd{\SaveRubikState}}
%
% \DescribeMacro{\SaveRubikState}
% The command \cmd{\SaveRubikState}\marg{filename} saves the state (configuration)
% of the Rubik cube to the file \marg{filename} in the standard \cmd{\RubikFace...}
% format. Consequently such a file can then be input at a later stage so it can be
% drawn or processed in the usual way (inside the TikZ picture environment).
%
% For example, the following commands would save the so-called `sixspot' configuration
% (generated by the rotations \textbf{U, Dp, R, Lp, F, Bp, U, Dp})
% to the file \verb!sixspot.tex!.
%\begin{verbatim}
% \RubikCubeSolved%
% \RubikRotation{*sixspot,U,Dp,R,Lp,F,Bp,U,Dp}%
% \SaveRubikState{sixspot.tex}%
%\end{verbatim}
% The form of the file \verb!sixspot.tex! will then be as follows---the
% filename (commented out) is automatically written to the top of the
% file for convenience.
%\begin{verbatim}
% % filename: sixspot.tex
% \RubikFaceUp{O}{O}{O}{O}{W}{O}{O}{O}{O}%
% \RubikFaceDown{R}{R}{R}{R}{Y}{R}{R}{R}{R}%
% \RubikFaceLeft{Y}{Y}{Y}{Y}{B}{Y}{Y}{Y}{Y}%
% \RubikFaceRight{W}{W}{W}{W}{G}{W}{W}{W}{W}%
% \RubikFaceFront{G}{G}{G}{G}{O}{G}{G}{G}{G}%
% \RubikFaceBack{B}{B}{B}{B}{R}{B}{B}{B}{B}%
%\end{verbatim}
% We can therefore access and draw this configuration later, when required,
% simply by inputting the file as follows:
%\begin{verbatim}
%\begin{tikzpicture}
% \input{sixspot.tex}
% \DrawRubikCubeFlat
%\end{tikzpicture}
%\end{verbatim}
%
%
% \subsection{\cmd{\CheckRubikState} command}
%
% \DescribeMacro{\CheckRubikState}
% Since it is easy to inadvertently define an invalid Rubik cube
% (eg enter an invalid number of, say, yellow cubies), this command
% checks the current colour state of all the cubies
% of a 3x3x3 Rubik cube, and shows the number of cubies of each colour.
% An ERROR: code is issued if the number of cubies having a given colour exceeds 6.
% The results are written to the the \texttt{.log} file, and displayed under
% the graphic if the \cmd{\ShowRubikErrors} command is used.
%
% One can check the current Rubik state (for errors) by issuing the command
%\begin{verbatim}
% \CheckRubikState%
%\end{verbatim}
% Note that such a check is implemented automatically with each \cmd{\RubikRotation} command.
%
%
%
%
% \subsection{\cmd{\ShowRubikErrors} command}
%
% \DescribeMacro{\ShowRubikErrors}
%
% Any errors which arise can be made visible using the
% command \cmd{\ShowRubikErrors}.
% This command places a copy of the `error' file (\texttt{rubikstateERRORS.dat})
% underneath the image so you can see any errors if there are any---all this
% detail can also be found in the \texttt{.log} file.
%
% Consequently, this command must be placed \textit{after} a TikZ picture
% environment---it cannot be used inside a TikZ environment. In fact this command
% is probably best placed at the end of the document (if there are several such
% environments), where it will reveal all rotation errors generated while
% processing the document.
%
%
% \section{Files generated}
%
%
% Whenever the \cmd{\RubikRotation} or \cmd{\CheckRubikState} commands are used,
% three small temporary plain text files for holding data are generated as
% follows (they are refreshed with each \LaTeX\ run, and are not actively deleted).
% \begin{itemize}
% \item \LaTeX\ writes Rubik state data to the file \texttt{rubikstate.dat}.
%
% \item Perl reads the file \texttt{rubikstate.dat} and then writes
% the new rubik state to the file \texttt{rubikstateNEW.dat}.
%
% \item Perl also writes error data to the file \texttt{rubikstateERRORS.dat}.
% A copy of this file is displayed under the image when the command
% \cmd{\ShowRubikErrors} is used after (outside) the TikZ picture environment.
%
% \end{itemize}
%
%
% \section{General overview}
%
% When \LaTeX\ loads the \rubikrotation\ package the following steps are implemented.
% \begin{itemize}
%
%
% \item[1.] A check is made to see if \texttt{fancyvrb.sty} is loaded: if not
% then this package is loaded if it is available (this package is required
% for inputting the file \texttt{rubikstateERRORS.dat}).
%
% \item[2.] A check is made to see if a configuration file (\texttt{rubikrotation.cfg})
% exists: if so then this file is input.
%
% \item[3.] The text file \texttt{rubikstateNEW.dat} is overwritten (if it exists):
% otherwise the file is created (this prevents an `old' file being used by \LaTeX).
%
% \item[4.] The plain text file \texttt{rubikstateERRORS.dat} is created.
% This file collects error messages generated by the Perl script.
%
% \end{itemize}
%
%
% When a \cmd{\RubikRotation} command is processed it first writes the current
% colour configuration of each face (the `rubik state') to the temporary file
% \texttt{rubikstate.dat} (to be read by the Perl script \texttt{rubikrotation.pl}).
% The \cmd{\RubikRotation} command also appends the keyword `
% \texttt{checkrubik}' as well as a copy of the string of Rubik rotations.
% It then calls the Perl script \texttt{rubikrotation.pl}.
%
% For example, if we use the command \cmd{\RubikCubeSolved}
% followed by the command \verb!\RubikRotation{U,D,L,R}! then the associated
% \texttt{rubikstate.dat} file would be as follows:
% \begin{verbatim}
% % filename: rubikstate.dat
% up,W,W,W,W,W,W,W,W,W
% down,Y,Y,Y,Y,Y,Y,Y,Y,Y
% left,B,B,B,B,B,B,B,B,B
% right,G,G,G,G,G,G,G,G,G
% front,O,O,O,O,O,O,O,O,O
% back,R,R,R,R,R,R,R,R,R
% checkstate
% rotation,U,D,L,R
%\end{verbatim}
% Alternatively, if the \cmd{\RubikRotation} command was
% \verb!\RubikRotation{random, 45}! then the last line written to the file
% would be the string `\texttt{rotation,random,45}'
% A \cmd{\CheckRubikState} command triggers the same sequence of events except
% no `rotation' line is written.
%
% The action of the Perl program is controlled by the keywords (first argument of
% each line) associated with each line of the file \texttt{rubikstate.dat}.
% When control passes to Perl, the Perl program starts by loading the
% rubikstate (prompted by the keywords up, down,
% left, right, front, back). Next the program performs
% some basic checks (prompted by the key word `checkstate'), and then it processes
% the sequence of Rubik rotations (prompted by the keyword `rotation').
% If, instead, the second argument of the `rotation' string is the keyword `random', and
% provided this is followed by a valid integer, say $n$, then the Perl program performs
% a sequence of $n$ random rotations.
% Finally, the Perl program writes the final `rubikstate' to the text file
% \texttt{rubikstateNEW.dat}. All error messages are written to the
% text file \texttt{rubikstateERRORS.dat} and also to the \LaTeX\ log file.
%
% Control then reverts to \LaTeX\ which then inputs the file
% \texttt{rubikstateNEW.dat}. If there are more \cmd{\RubikRotation} commands
% then this cycle repeats accordingly. Eventually a `\texttt{Draw...}' command
% of some form is reached and the final rubikstate is drawn in a TikZ picture
% environment.
%
% If the TikZ picture environment is followed by a \cmd{\ShowRubikErrors} command,
% then a `verbatim' copy of the \texttt{rubikstateERRORS.dat} file is displayed
% immediately under the graphic. Once the graphic is error-free, then the
% \cmd{\ShowRubikErrors} can be removed or commented out.
%
%
%
% ^^A ==================================================
% \StopEventually{\PrintIndex}
%
%
%
% \section{The code (\texttt{rubikrotation.sty})}
%
% \subsection{Package heading}
%
% \begin{macrocode}
%<*rubikrotation>
\def\RRfileversion{2.0}%
\def\RRfiledate{2014/01/20}%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{rubikrotation}[\RRfiledate\space (v\RRfileversion)]
% \end{macrocode}
%
%
% {\noindent}The package requires rubikcube.sty. Do not automatically
% load rubikcube.sty since this makes it difficult to errorcheck new
% versions (for the moment at least).
% \begin{macrocode}
\@ifpackageloaded{rubikcube}{}{%
\typeout{---rubikrotation requires the rubikcube package.}%
}%
% \end{macrocode}
%
%
%
% {\noindent}The \rubikrotation\ package requires access to the fancyvrb package
% for the \cmd{\VerbatimInput\{\}} command which we use for inputting and displaying
% the error file.
% \begin{macrocode}
\@ifpackageloaded{fancyvrb}{}{%
\typeout{---rubikrotation requires the fancyvrb package%
for VerbatimInput{} command.}%
\RequirePackage{fancyvrb}}
% \end{macrocode}
% \medskip
%
%
%\subsection{Some useful commands}
% \label{sec:usefulcommands}
% \begin{macro}{\rubikrotation}
% First we create a suitable logo
% \begin{macrocode}
\newcommand{\rubikrotation}{\textsf{rubikrotation}}
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@comment}
% \begin{macro}{\@commentone}
% We require access to the percent character so we can write comments
% in files, including the log file.
% We first define percentchar for the write statement
% (From Abrahams PW, Berry K and Hargreaves KA (1990), ``TeX for the Impatient'', p~292;
% available from: CTAN.../info/impatient/)
% \begin{macrocode}
{\catcode`\%=12 \global\def\rubikpercentchar{%}}%
\newcommand{\@comment}{\rubikpercentchar\rubikpercentchar\space}%
\newcommand{\@commentone}{\rubikpercentchar}%
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@print}
%% We need a simple print command for writing comments to a file.
% \begin{macrocode}
\newcommand{\@print}[1]{\immediate\write\outfile{#1}}
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\rubikperlname}
% This holds the name of the Perl-5 script . It is used later to check
% whether the Perl script exists or not. A plain text configuration file
% \verb!rubikrotation.cfg! can be used to change the default name of the
% Perl script using a renewcommand.
% \begin{macrocode}
\newcommand{\rubikperlname}{rubikrotation.pl}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\rubikperlcmd}
% This holds the commandline code for calling the the Perl script.
% A plain text configuration file \verb!rubikrotation.cfg! can be used to
% change the default commandline code using a renewcommand.
% \begin{macrocode}
\newcommand{\rubikperlcmd}{perl \rubikperlname}
% \end{macrocode}
% \end{macro}
%
%
%\subsection{Configuration file}
% If a config file exists (rubikrotation.cfg) then input it here,
% ie \textit{after} defining the \cmd{\rubikperlname} and \cmd{\rubikperlcmd}
% commands and \textit{before} creating the \texttt{rubikstateERRORS.dat} file.
% \begin{macrocode}
\typeout{---checking for config file (rubikrotation.cfg)...}
\IfFileExists{rubikrotation.cfg}{%
\input{rubikrotation.cfg}%
}{\typeout{---no config file available}%
}%
% \end{macrocode}
%
%
% \subsection{Clean file rubikstateNEW.dat}
%
% {\noindent}We need to clean any existing (old) rubikstateNEW.dat file,
% since if the TeX shell switch is accidentally not used then Perl will
% not be CALLed and hence this file will not be renewed
% (ie an `old' image may be used).
% \begin{macrocode}
\typeout{---cleaning file rubikstateNEW.dat}%
\newwrite\outfile%
\immediate\openout\outfile=rubikstateNEW.dat%
\@print{\@comment rubikstateNEW.dat (by TeX)}%
\immediate\closeout\outfile%
% \end{macrocode}
%
%
% \subsection{rubikstateERRORS.dat}
%
% {\noindent}The \rubikrotation\ package requires the Perl program.
% We first open the file \texttt{rubikstateERRORS.dat} which is used
% by the Perl program (\texttt{rubikrotation.pl}) for writing its error
% messages to. This file is accessed and displayed by the command
% \cmd{\ShowRubikErrors}.
%
% \textsc{important note}: this file is created fresh each time LaTeX is run
% and hence the Perl program only appends data to it during the \LaTeX\ run,
% so this file just grows until either it is destroyed or recreated---a useful
% feature to keep since the file accumulates all error messages as the
% .tex file is processed.
% We can't make the Perl program create the file since the Perl program is only
% CALLed if we use a \cmd{\RubikRotation} or \cmd{\CheckRubikState}
% command (which we may not!)---so it has to be created (opened) here.
%
% The following code first opens the file, and then checks to see if
% the Perl program (\cmd{\rubikperlname}) exists;
% if the Perl prog does exist then all is OK,
% otherwise we write an error message to the file.
% \begin{macrocode}
\typeout{---creating file rubikstateERRORS.dat}%
\newwrite\outfile%
\immediate\openout\outfile=rubikstateERRORS.dat%
\@print{\@comment rubikstateERRORS.dat}%
\typeout{---checking for Perl script \rubikperlname...}
\IfFileExists{\rubikperlname}{%
\typeout{---\rubikperlname\space exists OK}%
}{\typeout{** ERROR: cannot find Perl program \rubikperlname}%
\@print{\@comment ** ERROR: cannot find Perl program \rubikperlname}}%
\immediate\closeout\outfile%
% \end{macrocode}
%
%
% \subsection{Setting up a newwrite and file-access for new files}
%
% Having set up all the primary files, we now need to set up a newwrite for
% all subsequent files openings (eg for rubikstate.dat and saving
% to arbitrary filenames by the \cmd{\SaveRubikState} command). Otherwise, we
% can easily exceed the LaTeX limit of 15. From here-on \TeX\ will use openout7
% when opening and writing to files. We will implement new openings using
% the command \verb!\@openstatefile! (see below).
% \begin{macrocode}
\typeout{---setting up newwrite for rubikrotation to use...}%
\newwrite\outfile%
% \end{macrocode}
%
%
% \begin{macro}{\@openstatefile}
% \begin{macro}{\@closestatefile}
% We also need commands for easy file opening and and closing
% for new instances of the file \texttt{rubikstate.dat} etc.
% Note that for this we are therefore using the same outfile number as set up
% by the \verb!\newwrite...! above.
% \begin{macrocode}
\newcommand{\@openstatefile}{\immediate\openout\outfile=rubikstate.dat}
\newcommand{\@closestatefile}{\immediate\closeout\outfile}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Saving the Rubik state}
%
% \begin{macro}{\@printrubikstate}
% This command writes the Rubik configuration (state) to
% the file \texttt{rubikstate.dat}, and is used by the \cmd{\RubikRotation} command.
% The file \texttt{rubikstate.dat} is read by the Perl program, and represents
% the state on which the new \cmd{\RubikRotation} command acts.
% Note that we append the key-word \texttt{checkstate} to the end of the file
% in order to trigger the Perl program to implement its \texttt{checkstate} subroutine.
% \begin{macrocode}
\newcommand{\@printrubikstate}{%
\@print{up,\Ult,\Umt,\Urt,\Ulm,\Umm,\Urm,\Ulb,\Umb,\Urb}%
\@print{down,\Dlt,\Dmt,\Drt,\Dlm,\Dmm,\Drm,\Dlb,\Dmb,\Drb}%
\@print{left,\Llt,\Lmt,\Lrt,\Llm,\Lmm,\Lrm,\Llb,\Lmb,\Lrb}%
\@print{right,\Rlt,\Rmt,\Rrt,\Rlm,\Rmm,\Rrm,\Rlb,\Rmb,\Rrb}%
\@print{front,\Flt,\Fmt,\Frt,\Flm,\Fmm,\Frm,\Flb,\Fmb,\Frb}%
\@print{back,\Blt,\Bmt,\Brt,\Blm,\Bmm,\Brm,\Blb,\Bmb,\Brb}%
\@print{checkstate}%
}
% \end{macrocode}
% \end{macro}
%
%
% \subsection{RubikRotation command}
%
% \begin{macro}{\RubikRotation}
% The \cmd{\RubikRotation}\marg{comma separated sequence}
% command (a)~writes the current Rubik state to the file
% \texttt{rubikstate.dat}, and then (b)~CALLs the Perl program.
% It also writes comments to the data file and also to the log file.
% \begin{macrocode}
\newcommand{\RubikRotation}[1]{\IfFileExists{\rubikperlname}{%
\typeout{---NEW rotation command------------------}%
\typeout{---command = RubikRotation{#1}}%
\typeout{---Perl script \rubikperlname\space exists OK}%
\typeout{---writing current Rubik state to file rubikstate.dat}%
\@openstatefile% open data file
\@print{\@comment filename: rubikstate.dat}%
\@printrubikstate%
\@print{rotation,#1}%
\@closestatefile% close data file
\typeout{---running Perl script}%
\immediate\write18{\rubikperlcmd}%
\typeout{---inputting NEW datafile (from Perl)}%
\input{rubikstateNEW.dat}%
\typeout{-----------------------------------------}%
}{\typeout{** ERROR: \rubikperlname\space does not exist}%
}}
% \end{macrocode}
% \end{macro}
%
%
% \subsection{ShowRubikErrors command}
%
% \begin{macro}{\ShowRubikErrors}
% This command inputs the file \texttt{rubikstateERRORS.dat}.
% \begin{macrocode}
\newcommand{\ShowRubikErrors}{%
\typeout{---ShowRubikErrors: inputting file rubikstateERRORS.dat}%
\VerbatimInput{rubikstateERRORS.dat}}
% \end{macrocode}
% \end{macro}
%
%
% \subsection{CheckRubikState command}
%
% \begin{macro}{\CheckRubikState}
% This command triggers the Perl program to implement
% some simple error checking of the Rubik configuration (state).
% This command (a)~writes the current Rubik state to the file
% \texttt{rubikstate.dat}, and then (b)~CALLs the Perl program.
% It also writes comments to the data file and also to the log file..
% \begin{macrocode}
\newcommand{\CheckRubikState}{\IfFileExists{\rubikperlname}{%
\typeout{---NEW check command------------------}%
\typeout{---command = CheckRubikState}%
\typeout{---Perl script \rubikperlname\space exists OK}%
\typeout{---writing current Rubik state to file rubikstate.dat}%
\@openstatefile% opens data file
\@print{\@comment filename: rubikstate.dat}%
\@printrubikstate%
\@closestatefile% close data file
\typeout{---running Perl script}%
\immediate\write18{\rubikperlcmd}%
\typeout{---inputting NEW datafile (from Perl)}%
\input{rubikstateNEW.dat}%
\typeout{-----------------------------------------}%
}{\typeout{** ERROR: \rubikperlname\space does not exist}%
}}
% \end{macrocode}
% \end{macro}
%
%
%
% \subsection{SaveRubikState macro}
%
% \begin{macro}{\SaveRubikState}
% The command \cmd{\SaveRubikState}\marg{filename} saves the Rubik state
% to a file. Note that in order to actually write a LaTeX
% command to a file without a trailing space one must use the
% \cmd{\string} command (see the book ``TeX by Topic'', p~238).
% Note that this macro uses the two internal commands \cmd{\Rubik@comment}
% and \cmd{\Rubik@print}. \#1 is the output filename.
% We use several \cmd{\typeout} commands to write to the log file.
% \begin{macrocode}
\newcommand{\SaveRubikState}[1]{%
\typeout{---NEW save command------------------}%
\typeout{---command = SaveRubikState{#1}}%
\typeout{---saving Rubik state data to file #1}%
\immediate\openout\outfile=#1%
\@print{\@comment filename: #1\@commentone}%
\@print{\string\RubikFaceUp%
{\Ult}{\Umt}{\Urt}{\Ulm}{\Umm}{\Urm}{\Ulb}{\Umb}{\Urb}\@commentone}%
\@print{\string\RubikFaceDown%
{\Dlt}{\Dmt}{\Drt}{\Dlm}{\Dmm}{\Drm}{\Dlb}{\Dmb}{\Drb}\@commentone}%
\@print{\string\RubikFaceLeft%
{\Llt}{\Lmt}{\Lrt}{\Llm}{\Lmm}{\Lrm}{\Llb}{\Lmb}{\Lrb}\@commentone}%
\@print{\string\RubikFaceRight%
{\Rlt}{\Rmt}{\Rrt}{\Rlm}{\Rmm}{\Rrm}{\Rlb}{\Rmb}{\Rrb}\@commentone}%
\@print{\string\RubikFaceFront%
{\Flt}{\Fmt}{\Frt}{\Flm}{\Fmm}{\Frm}{\Flb}{\Fmb}{\Frb}\@commentone}%
\@print{\string\RubikFaceBack%
{\Blt}{\Bmt}{\Brt}{\Blm}{\Bmm}{\Brm}{\Blb}{\Bmb}{\Brb}\@commentone}%
\immediate\closeout\outfile%
\typeout{-----------------------------------------}%
}%
% \end{macrocode}
% \end{macro}
%
%
% --------------------------
% End of this package
% --------------------------
% \begin{macrocode}
%</rubikrotation>
% \end{macrocode}
%
%
%
%
% \Finale
%
\endinput
|