summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/standalone/standalone.dtx
blob: 41496f58f5fc2b4a4df35ade4a7ffaec5b9313eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
% \iffalse meta-comment
%
% Copyright (c) 2010 by Martin Scharrer <martin@scharrer-online.de>
% -----------------------------------------------------------------
%
% 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 license is in
%
%   http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Martin Scharrer.
%
% This work consists of the files standalone.dtx, standalone.ins
% and the derived files standalone.cls, standalone.sty, standalone.tex and
% standalone.cfg.
%
% $Id: standalone.dtx 2194 2011-02-28 19:26:29Z martin $
% \fi
%
% \iffalse
%<cls|sty|cfg|tex>% $Id: standalone.dtx 2194 2011-02-28 19:26:29Z martin $
%<cls|sty>\NeedsTeXFormat{LaTeX2e}
%<cls>\ProvidesClass{standalone}   [2010/02/28 v0.4 Class to compile TeX sub-files standalone]
%<sty>\ProvidesPackage{standalone} [2010/02/28 v0.4 Package to include TeX sub-files with preambles]
%<tex>\ProvidesFile{standalone.tex}[2010/02/28 v0.4 Provides if-switch to show if file is compiled standalone]%
%<cfg>\ProvidesFile{standalone.cfg}[2010/02/28 v0.4 Default configuration file for 'standalone' class]%
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{standalone.dtx}[2011/02/28 v0.4 DTX File for 'standalone' cls/sty/tex/cfg files]
\documentclass{scrartcl}
\usepackage{ydoc}
\usepackage{ifpdf}
\usepackage{listings}
\usepackage{hyperref}

\def\optpar#1{\marginpar{\raggedright\opt{#1}}}

\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \newpage\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{1298}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v0.1}{2010/03/21}{First released version}
% \changes{v0.2}{2010/03/22}{Added support for beamer style settings in the config file using \cs{ifstandalonebeamer}. Updated documentation.}
% \changes{v0.3}{2010/03/26}{Added package options to automatically copy sub-preamble code to the main preamble.}
% \changes{v0.3a}{2010/03/27}{Fixed bug related to line endings in \texttt{.sta} file. Listed sub-files now have \texttt{.tex} as default extension.}
% \changes{v0.4}{2011/02/28}{Added option 'border' for easy adjustments of the preview border.}
%
% \GetFileInfo{\jobname.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment,\def,\edef,\xdef,\gdef,\DeclareRobustCommand}
% \DoNotIndex{\expandafter,\if,\else,\fi,\ifnum,\ifx,\let,\global,\long}
% \DoNotIndex{\newcounter,\newcount,\message,\meaning,\noexpand,\relax,\value}
% \DoNotIndex{\setcounter,\addtocounter,\advance,\afterassignment,\AtEndOfPackage}
% \DoNotIndex{\ProvidesPackage,\providecommand,\RequirePackage,\empty,\begin,\end}
% \DoNotIndex{\begingroup,\bgroup,\egroup,\endgroup,\csname,\endcsname,\@tempa,\@tempb}
% \DoNotIndex{\ignorespaces,\lccode,\sffamily,\@gobble,\@ifundefined,\@for,\or}
% \DoNotIndex{\@firstoftwo,\@ifnextchar,\@namedef,\@nameuse,\@secondoftwo}
% \DoNotIndex{\@temptokena,\toks@,\BODY,\do,\g@addto@macro,\lowercase,\uppercase,\the}
% \DoNotIndex{\aftergroup,\CurrentOption,\DeclareBoolOption,\DeclareDefaultOption,\DeclareStringOption,\DeclareVoidOption}
% \DoNotIndex{\endinput,\endpreview,\input,\LoadClass,\newif,\PassOptionsToPackage,\preview,\ProcessKeyvalOptions}
% \DoNotIndex{\renewcommand,\renewenvironment,\setbeamertemplate,\SetupKeyvalOptions,\space,\unskip,\usepackage}
% \DoNotIndex{\unexpanded,\write,\next,\jobname,\hbox}
%
% \ifpdf
% \hypersetup{%
%   pdfauthor   = {Martin Scharrer <martin@scharrer-online.de>},
%   pdftitle    = {The standalone class and package},
%   pdfsubject  = {Documentation of LaTeX class and package 'standalone'},
%   pdfkeywords = {standalone, LaTeX, TeX}
% }%
% \fi
% \clearpage
% \null
% \vspace*{-2em}
% \begin{center}
%   {\LARGE\sffamily The \emph{standalone} Class and Package\\[\medskipamount]}
%   {\large Martin Scharrer \\[\medskipamount]\normalsize
%   \url{martin@scharrer-online.de}\\[.8ex]
%   \url{http://www.ctan.org/pkg/standalone/}\\[\bigskipamount]}
%   {\large Version \fileversion\ -- \filedate}\\
% \end{center}
% \vspace{1.2em}%
%
% \section{Introduction}
% Larger \LaTeX{} documents can be split into multiple \TeX\ files which are then included in a main document with \cs{include}
% for e.g.\ chapter files or \cs{input} for e.g.\ \TeX-coded pictures. Keeping pictures in their own sub-files improves readability
% of the main file and simplifies the sharing of them between different documents. However, during the, sometimes lengthly,
% drawing/coding process it has benefits to be able to compile the pictures on their own. The compile process is much quicker and
% the resulting document only holds the picture which avoids constant page turning and zooming.
%
% While it is possible to write a small `main' file for each picture file, this method is a little cumbersome and clutters
% the directories with a lot of extra files. A second method is to place the `main' components, i.e. a preamble, directly into the
% picture files and make the main document ignore this code sections.
%
% The package |standalone| can be used in the main document to skip all extra preambles in included files. The main file must load all packages
% and settings required by the sub-files. Several package options are provided to collect the preambles of the sub-files automatically and execute them from the main file.
%
% A |standalone| class is also provided to minimise the extra preamble code needed in this files.
% It's usage is optional, but simplifies and standardises how picture files are compiled standalone.
% The class uses by default the |preview| package to create an output file which only contains the picture with no extra
% margins, page numbers or anything else. A configuration file |standalone.cfg| read by the class allows the user to adjust settings and macros easily
% on a per directory base.
%
% \subsection*{Similar Packages}
% The |docmute| package by T.M.\ Trzeciak is written for the same basic task as the |standalone| package. However, no sub-preamble processing other than the removal is support. It also
% doesn't provide a special class or configuration file.
%
% \section{Usage}
% \subsection{Quick instructions}
% Load the |standalone| \emph{package} very early in the main document. Also all packages needed by all the sub-files must be loaded by the main document.
% Include your picture or other sub-files using |\input| or a similar macro as normal.
% In the sub-files use the |standalone| \emph{class} with a normal \cs{documentclass} and load all packages needed for the particular
% file. Finally wrap the actual content of the sub-file in a |document| environment.
%
% When the sub-file is compiled on its own the |\documentclass| and |document| environment will be active as normal. The main file, however,
% will skip everything from the |\documentclass| till the |\begin{document}|. The (now fake) |document| environment is redefined to be a simple
% TeX-group. Any code after the |\end{document}| will be ignored. The real |document| environment of the main file will be unaffected and will work as normal.
%
% Instead of transferring the packages required by each sub-file manually to the main document preamble, this task can be automatised using
% the options listed in section~\ref{sec:pkgoptions}
%
% \subsection{Class Options}
% The |standalone| class will load a real document class.\optpar{class} By default this is |article|. The document class normally has not much
% influence on sub-files like pictures, especially when the \pkg{preview} package is active. However, the used class can be adjusted by the user
% with the |class=|\meta{class name} option.
%
% The boolean \opt{preview}\optpar{preview} option can be used to disable the use of the \pkg{review} package. The default is |preview=true|. The package is not loaded if |preview=false| is set.
% The preview border can be set using the \opt{border}\optpar{border} option. This border will be added to the file content.
% The usage is either \MacroArgs'border='{<left>~<lower>~<right>~<upper>}, \MacroArgs'border='{<left/right>~<lower/upper>} or \MacroArgs'border='<all sides>.
% The default unit is |bp| (big points) which is the unit used by the PostScript and PDF formats. If |preview=false| is set this option will be ignored.
%
% A special \opt{beamer}\optpar{beamer} option is provided to handle beamer frames and overlays correctly. See section~\ref{sec:beamer} for more information.
%
% All other used options are passed to the loaded class.
%
% \subsection{Package Options}\label{sec:pkgoptions}
% The |standalone| package removes all sub-file preambles (``sub-preambles'') by default when loaded.
% However, if the package is loaded with the \opt{subpreambles}\optpar{subpreambles} options, all sub-preambles are stored in an auxiliary file with the name `\meta{main tex file name}|.sta|' (for \emph{sta}ndalone).
% This file is then loaded or processed at the beginning of the next \LaTeX\ run (i.e.\ at the place in the preamble where the |standalone| package is loaded).
% The way how the \opt{subpreambles} option works can be controlled by the options \opt{sort}, \opt{print} and \opt{comments}/\opt{nocomments}.
% Please note that the \opt{sort} and \opt{print} options require of course the \opt{subpreambles} option and will enable it if not already done so.
%
% With only the \opt{subpreambles} option set, the sub-preambles are simple read and executed unchanged. This includes the risk of option clashes if one package is loaded with different
% options inside the sub-preambles and/or the main preamble. This is avoided by the \opt{sort}\optpar{sort} option, which accumulates all packages loaded by all sub-files together with their options.
% The options are then marked to be loaded by the package using \LaTeX{}s \cs{PassOptionsToPackage} macro. The packages are loaded at the end of the preamble using the \cs{AtBeginDocument} hook.
% This allows the user to load the same packages with own options in the main file, after the |subversion| package is loaded, without any option clashes.
%
% While the \opt{sort} option is giving already good results, problems with the order of packages can still occur. Some packages provide, redefine or patch the same macros, so that they must be loaded in
% the correct order to give the desired result. Potential additional code in the sub-preambles, required for some sub-figures but maybe incompatible with others, complicates the situation further.
% If such issues occur they can hardly be handled in an automatic way. Instead the sub-preambles must be carefully merged into the main preamble.
% The option \opt{print}\optpar{print} was created to simplify this otherwise cumbersome task.
% It concatenated all sub-preambles into a single file named `\meta{main tex file name}|.stp|' (for \emph{st}andalone, \emph{p}rint). Each preamble is commented with its original file name.
% Please note that |.sta| file mentioned above, while quite similar, holds additional macros and might not be easily user readable or editable.
% After the file was generated it can be easily pasted into the main file preamble using a text editor.
%
% When the \opt{print} option is enabled the normal |.sta| file is not generated or loaded. Because this will cause most likely some errors related to packages not loaded, all sub-file bodies will be skipped.
% A warning is printed for each sub-file to remind the user about this fact. The \opt{print} option is only indented to by used when required to get a list of sub-preambles. After including this list in the main file the option
% must be removed to compile the main file normally.
%
% Finally if both the \opt{print} and \opt{sort}\optpar{print}\optpar{sort} options are enabled, a `sorted' list of sub-preambles is printed into the |.stp| file. In this `sorted print' mode all |\usepackage| macros (and similar macros like
% |\usepgflibrary|, |\usetikzlibrary| and |\usetikztiminglibrary| from the |pgf|, |tikz| and |tikz-timing| packages, respectively) are removed from the rest of the sub-preamble code.
% A list of packages (and libraries) without duplicates is printed at the begin of the |.stp| file (using |\usepackage|, of course). Every option provided by any sub-file for a package is added, again without duplicates.
% If specific package date was requested in a sub-file it is also added. If multiple dates are requested for one package, the most recent (i.e.\ the ``highest one'', not the last processed) is used.
% After this list(s) the rest of the sub-preamble code is printed with the above macros removed. This mode frees the user from the need to remove duplicates and collect package options manually.
%
% Please note that all |\usepackage| and similar macros inside braces |{ }| will not be seen by |standalone|s sort macro and therefore are not extracted or handled in any special form mentioned above.
% This can be exploited to load certain packages only in |standalone| mode but not in the main document.
% Unfortunately, macros inside |\ifstandalone|\ldots|\fi| are seen and extracted while not wanted inside the main file.
% The macro \cs{onlyifstandalone}\marg{code} (see below) was created because of this two reasons. Its argument braces hide the content from the scanner.
% It is then also completely removed from the printed sub-preamble code.
%
% The complementary options \opt{comments}/\opt{nocomments}\optpar{comments}\optpar{nocomments} select if the |.stp| file should also include the comments of the sub-preambles.
% Comments are included by default in the non-sorting print mode (\opt{print} without \opt{sort} option), but can cause `wrong' results during the `sorting' process and are therefore removed by default in this mode.
% The reason for this can be explained as follows.
% In order to transfer the comments from the sub-files to the |.stp| file \TeX\ must be instructed to handle them as normal input and not discard them.
% However, in this case the scanning algorithm which removes |\usepackage| and friends can not distinguish
% between `active' macros and macros which are commented out. All above mentioned macro inside comments will then be processed as when there where `active'.
% The user might favour the information provided by the comments over this small risk and enable them using the \opt{comments} option.
%
% \subsection{Dependencies}
% The |standalone| class requires the |kvoptions| package (Oberdiek bundle) and the |preview| package. Both should be available in a standard \LaTeX\ installation.
% The |beamer| option of course requires the |beamer| bundle to be installed.
% The |standalone| package does not require other packages, but can take advantage from the |fink| package (\emph{Fi}le \emph{N}ame \emph{K}eeper),
% to access the filenames of the sub-files. For this the |fink| package must be loaded before |standalone|. Without it a file name macro defined by \LaTeX\ itself
% is used instead which should do also fine, but is reset at every |\input| macro. Placing this macro before |\documentclass| without marking
% it with |\csname standaloneignore\endcsname| will then lead to wrong file names inside the |.sta| and |.stp| files.
%
% \subsection{Environments and Macros}
%
% \DescribeEnv[<sub-file content>]{standalone}
% The \env{standalone} environment is automatically wrapped around the content of each sub-file when compiled standalone.
% By default it only contains a \env{preview} environment as long the class is not called with the |preview=false| option.
% It can be redefined in the configuration file if required.
% When compiled as part of a main document the \env{standalone} environment does nothing (apart of being a \TeX\ group).
%
% \begin{DescribeEnv}{standaloneframe}
%   !\color{optional}\colorlet{meta}{metaoptional}!
%   <<overlay specification>>'[<'<default overlay spec>'>]'
%   !\\\hspace*{7em}!
%   !\color{optional}\colorlet{meta}{metaoptional}!
%   [<options>]{<optional frame title>}{<optional frame subtitle>}
%   \relax\MacroArgs<code with beamer overlays>
% \end{DescribeEnv}
% The \env{standaloneframe} environment must be used in sub-file holding beamer overlay code.
% It is only defined when the class is called with the |beamer| option and acts as a replacement
% of the \env{frame} environment of beamer when compiled standalone.
% All optional arguments of \env{frame} are supported but most might not be useful for normal sub-files.
% When compiled as part of a main document it does nothing except of gobbling its arguments.
%
% \bigskip
% \DescribeMacro{\ifstandalone}
% Both the class and the package provide the if-switch \cs{ifstandalone}, which can be used to only include code if the file is
% compiled standalone. The switch is set to \cs{iftrue} by the class and to \cs{iffalse} by the package.
%
% The additional file |standalone.tex| also defines this switch by checking if \cs{documentclass} was already used. It can be included
% with |\input{standalone}| and is intended for specialised files which do not use the |standalone| class.
%
% \DescribeMacro{\ifstandalonebeamer}
% Both the class and the package provide the if-switch \cs{ifstandalonebeamer}, which can be used to only include code if the file is
% compiled standalone with the |beamer| class option set.
% The switch is set to \cs{iftrue} by the class when loaded with the |beamer| option and always to \cs{iffalse} by the package.
%
% \DescribeMacro{\onlyifstandalone}{<code>}
% This is the macro version of the |\ifstandalone| if-switch. It executes \meta{code} only in |standalone| mode. As mentioned in section~\ref{sec:pkgoptions}
% it can also be used to hide |\usepackage| and similar macros from the extraction scanner of the |sort| option. The macro and its argument is not printed into the |.stp| file.
%
% \DescribeMacro{\standaloneignore}
% In rare cases some code must be placed before the |\documentclass| of a sub-file (e.g.\ |\PassOptionsToPackage|).
% Because the main document will only skip code between |\documentclass| and |\begin{document}| this code will be executed by it.
% In order to avoid this the macro \cs{standaloneignore} can be used at the very beginning of a sub-file to skip over this code.
% However it must be written as |\csname standaloneignore\endcsname| to avoid a `Undefined control sequence' error when compiled standalone.
% After all the class is not loaded at this point, therefore no |standalone| macros are yet defined.
% The |\csname|\ldots|\endcsname| construct will simple make it equal to |\relax| in this case.
%
% Please note that all code before |\documentclass| is not processed by any of the \opt{subpreamble} options but always simply removed.
% This macro was inspired by the similar macro |\docmute| of the |docmute| package.
%
% ^^A\newpage
% \subsection{Usage Examples}
%
% \def\lstlistingname{Example}
%
% \par\noindent
% \begin{minipage}{\textwidth}
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Use of \emph{standalone} package.}]
%   % Main file
%   % Real document class:
%   \documentclass{article}
%
%   % Use the 'standalone' package:
%   \usepackage{standalone}
%
%   % Load all packages needed for all sub-files:
%   \usepackage{tikz}
%
%   % Inside the real 'document' environment
%   % read the sub-file with '\input'
%   \begin{document}
%   % ...
%   \begin{figure}
%     \input{subfile}
%     \caption{A subfile}
%   \end{figure}
%   % ...
%   \end{document}
% \end{lstlisting}
% \end{minipage}
%
% \par\noindent
% \begin{minipage}{\textwidth}
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Use of \emph{standalone} class.}]
%   % A sub-file (e.g. picture) using the 'standalone' class:
%   % Use 'standalone' as document class:
%   \documentclass{standalone}
%
%   % Load packages needed for this TeX file:
%   \usepackage{tikz}
%
%   % Surround TeX code with 'document' environment as usually:
%   \begin{document}
%   % Add your TeX code, e.g. a picture:
%   \begin{tikzpicture}
%     \draw (0,0) rectangle (2,1) node [midway] {Example};
%   \end{tikzpicture}
%   \end{document}
% \end{lstlisting}
% \end{minipage}
%
% \par\noindent
% \begin{minipage}{\textwidth}
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Effective code if compiled standalone.}]
%   \documentclass{article}
%
%   \newenvironment{standalone}{\begin{preview}}{\end{preview}}
%   \input{standalone.cfg}
%   % which by defaults loads:
%   % \PassOptionsToPackage{active,tightpage}{preview}
%   \usepackage{preview}
%
%   \usepackage{tikz}
%
%   \begin{document}
%   \begin{standalone}
%   \begin{tikzpicture}
%     \draw (0,0) rectangle (2,1) node [midway] {Example};
%   \end{tikzpicture}
%   \end{standalone}
%   \end{document}
% \end{lstlisting}
% \end{minipage}
%
% \par\noindent
% \begin{minipage}{\textwidth}
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Effective code if included in a main document.}]
%   \begingroup
%   \begin{tikzpicture}
%     \draw (0,0) rectangle (2,1) node [midway] {Example};
%   \end{tikzpicture}
%   \endgroup
%   \endinput
% \end{lstlisting}
% \end{minipage}
%
%
%
% \subsection{Support for Beamer Presentations}\label{sec:beamer}
% Presentation can be written in \LaTeX\ using the |beamer| class. Each presentation frame is wrapped in a |frame| environment. Overlay effects
% can be added using special macros. This effects result in multiple pages per frame. Pictures with such overlay effects can not be
% compiled standalone using the normal settings. Instead the |standalone| class must load the |beamer| class and wrap the content also in
% a |frame| environment while skipping the |preview| environment. To activate this settings load the |standalone| class with the |beamer| option.
% Because the |frame| environment is quite special (it normally collects all it's content and calls the \cs{frame}) and must also support
% verbatim content it is not easily possible to redefined the |document| environment to include |frame|. Also |frame| accepts options which |document| doesn't.
% Therefore a second environment called |standaloneframe| is used in the beamer picture files. It will be equal to |frame| in standalone mode, but
% without effect otherwise.
%
% \DescribeMacro{\ifstandalonebeamer}
% This if switch is only true if the class is compiled with the |beamer| option. The package sets it to false.
% It can be used to place beamer specific options in the configuration files, which should be skipped for non-beamer standalone files.
%
% \par\bigskip\noindent
% \begin{minipage}{\textwidth}\noindent
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Use of \texttt{standalone} class with \texttt{beamer} option.}]
%   % Use of 'standalone' class with a beamer overlay:
%   \documentclass[beamer]{standalone}
%   % Load packages needed for this TeX file:
%   \usepackage{tikz}
%
%   % Surround TeX code with 'document' environment as usually:
%   \begin{document}
%   \begin{standaloneframe}[options] % e.g. 'fragile'
%    % Add your TeX code:
%     \only<1>{ One }%
%     \only<2>{ Two }%
%   \end{standaloneframe}
%   \end{document}
% \end{lstlisting}
% \end{minipage}
%
% \par\bigskip\noindent
% \begin{minipage}{\textwidth}\noindent
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Effective beamer code if compiled standalone.}]
%   \documentclass{beamer}
%   \input{standalone.cfg}
%
%   \usepackage{tikz}
%
%   \begin{document}
%   \begin{frame}[your options]
%     \only<1>{ One }%
%     \only<2>{ Two }%
%   \end{frame}
%   \end{document}
% \end{lstlisting}
% \end{minipage}
%
% \par\bigskip\noindent
% \begin{minipage}{\textwidth}\noindent
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Effective code if included in a beamer presentation.}]
%   \begingroup
%     \only<1>{ One }%
%     \only<2>{ Two }%
%   \endgroup
%   \endinput
% \end{lstlisting}
% \end{minipage}
%
% \subsection{\texttt{standalone.tex}}
% \noindent
% \begin{minipage}{\textwidth}\noindent
% \begin{lstlisting}[language={[latex]tex},gobble=4,frame=lines,caption={Usage of 'standalone.tex'.}]
%   \input{standalone} % use before any '\documentclass'
%   \ifstandalone
%     % Used only if compiled standalone
%   \fi
% \end{lstlisting}
% \end{minipage}
%
% \subsection{Usage with \texttt{svn-multi} keywords}\label{sec:svnmulti}
% If the version control package \texttt{svn-multi} is used, the keyword macros (|\svnid| or |\svnidlong|)
% need to be placed after the |\begin{document}| to be taken into account by the main document.
% The \texttt{svn-multi} package must be loaded by the sub-file preamble to avoid compile errors in standalone mode.
% Alternative, if the keywords are not required in this mode, they can be surrounded by |\ifstandalone\else|\dots|\fi|.
%
% \StopEventually{}
% \clearpage
%
% \section{Implementation}
%
% \subsection{The Package File}
% \iffalse
%<*sty>
% \fi
% The package file is to be loaded by a main document which includes |standalone| sub-files.
% It is also loaded by the |standalone| class to share code. The class then redefines certain macros.
%
% \subsubsection{If-Switches}
%
% \begin{macro}{\ifstandalone}
% Declare |standalone| if-switch and set it to false. The class will set it to true.
% The |\csname| trickery is used to avoid issues if the switch was already defined.
%    \begin{macrocode}
\expandafter\newif\csname ifstandalone\endcsname
\standalonefalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifstandalonebeamer}
% \changes{v0.2}{2010/03/23}{New macro}
% Make sure that |standalonebeamer| if-switch is defined and set it to false.
% If the class was loaded beforehand with the |beamer| option it is already defined as true.
% The |\csname| trickery is used to avoid issues if the switch was already defined.
%    \begin{macrocode}
\@ifundefined{ifstandalonebeamer}{%
\expandafter\newif\csname ifstandalonebeamer\endcsname
\standalonebeamerfalse
}{}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\onlyifstandalone}
% \changes{v0.3}{2010/03/26}{New macro}
% Macro version of |\ifstandalone|. The |{ }| around the argument protects the content from the package etc. scanners.
% Only defined if not already defined by the class, in the case of a |standalone| file included other |standalone| files.
%    \begin{macrocode}
\@ifundefined{onlyifstandalone}
  {\let\onlyifstandalone\@gobble}
  {}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ifsa@subpreambles}
% \begin{macro}{\ifsa@sortsubpreambles}
% \begin{macro}{\ifsa@printsubpreambles}
% The if-switches for the options.
%    \begin{macrocode}
\newif\ifsa@subpreambles
\newif\ifsa@sortsubpreambles
\newif\ifsa@printsubpreambles
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Options}
%    \begin{macrocode}
\DeclareOption{subpreambles}{%
  \sa@subpreamblestrue
}
\DeclareOption{sort}{%
  \sa@subpreamblestrue
  \sa@sortsubpreamblestrue
}
\DeclareOption{print}{%
  \sa@subpreamblestrue
  \sa@printsubpreamblestrue
}
\DeclareOption{comments}{%
  \def\sa@percent{\@makeother\%}%
}
\DeclareOption{nocomments}{%
  \def\sa@percent{}%
}
\DeclareOption{mode=none}{%
    \let\sa@mode\relax
}
\DeclareOption{mode=pdf|tex}{%
    \def\sa@mode{0}%
}
\DeclareOption{mode=tex}{%
    \def\sa@mode{1}%
}
\DeclareOption{mode=pdf}{%
    \def\sa@mode{2}%
}
\DeclareOption{mode=build}{%
    \def\sa@mode{3}%
}
\DeclareOption{mode=buildnew}{%
    \def\sa@mode{4}%
}
\ProcessOptions*\relax
%    \end{macrocode}
%
% In non-sorted print mode comments are preserved by default.
%    \begin{macrocode}
\ifsa@printsubpreambles
  \ifsa@sortsubpreambles\else
    \@ifundefined{sa@percent}{%
      \def\sa@percent{\@makeother\%}%
    }{}%
  \fi
\fi
%    \end{macrocode}
%
% \begin{macro}{\sa@filepath}
% File name macro. If the |fink| package is loaded the macros \cs{finkdir} (with leading `|./|' removed) and \cs{finkpath} is used, otherwise the \LaTeX\ macro
% \cs{@filef@und} (with trailing space removed and with '.tex' added if it has no file extension).
% The latter causes issues if |\input| etc. was used before |\documentclass| in sub-files.
%    \begin{macrocode}
\@ifundefined{finkpath}{%
  \def\sa@rmspace#1 \empty{#1}%
  \def\sa@chkext#1.#2 \empty#3{%
    \ifx\empty#3\empty
      \sa@rmspace#1\empty.#2%
    \else
      #1.#2%
      \expandafter\sa@rmrest
    \fi
  }%
  \def\sa@rmrest tex \empty{}%
  \def\sa@filepath{\expandafter\sa@chkext\@filef@und\empty.tex \empty\empty}%
}{%
  \def\sa@filepath{\expandafter\expandafter\expandafter\sa@rmdotslash\expandafter\finkdir\finkfile\empty./\empty}%
  \def\sa@rmdotslash#1./#2\empty{%
    \ifx\empty#1\empty
      \sa@@rmdotslash#2%
    \else
      \ifx\empty#2\empty
        #1%
      \else
        \sa@@rmdotslash#1./#2%
      \fi
    \fi
  }
  \def\sa@@rmdotslash#1./\empty{#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Processing of Sub-Preambles}
%
%    \begin{macrocode}
\ifsa@subpreambles
%    \end{macrocode}
%
% \begin{macro}{\sa@out}
% Write handle.
%    \begin{macrocode}
\newwrite\sa@out
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@write}
% Helper macro for file output.
%    \begin{macrocode}
\def\sa@write{\immediate\write\sa@out}%
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\ifsa@printsubpreambles
%    \end{macrocode}
%
% \begin{macro}{\sa@removeonlyifstandalone}
% Scans for |\onlyifstandalone| and removes it argument.
%    \begin{macrocode}
\long\def\sa@removeonlyifstandalone#1\onlyifstandalone{%
  \g@addto@macro\sa@preamble{#1}%
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\expandafter\sa@gobbleeol\expandafter\sa@removeonlyifstandalone\expandafter^^J\@gobble}%
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\fi
%    \end{macrocode}
%
%
% \subsubsection{Sorting of package options}
%
% Macros only needed for this mode are defined inside the |\if...|
% to save memory otherwise.
%
%    \begin{macrocode}
\ifsa@sortsubpreambles
%    \end{macrocode}
%
% \begin{macro}{\sa@usepackagewithoutoptions}
% Simply calls the original |\usepackage| while skipping the optional
% argument with potential package options.
%    \begin{macrocode}
\newcommand{\sa@usepackagewithoutoptions}[2][]{%
  \sa@orig@usepackage{#2}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@endmarker}
% Unique end marker. Will not be expanded.
%    \begin{macrocode}
\def\sa@endmarker{%
  \@gobble{sa@endmarker}%
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\ifsa@printsubpreambles
%    \end{macrocode}
%
%
% In sorted print mode all collected package etc. information is printed into the output file, followed
% by the reduced sub-preambles.
%    \begin{macrocode}
\AtEndDocument{%
  \sa@write{\@percentchar\space Packages required by sub-files:}%
  \expandafter\@for\expandafter\pkg\expandafter:\expandafter=\sa@collpkgs\do{%
    \ifx\pkg\empty\else
      \sa@write{%
        \string\usepackage%
        \expandafter\ifx\csname sa@pkgopts@\pkg\endcsname\empty\else%
          [\csname sa@pkgopts@\pkg\endcsname]%
        \fi
        {\pkg}%
        \expandafter\ifx\csname sa@pkgdate@\pkg\endcsname\relax\else%
          [\csname sa@pkgdate@\pkg\endcsname]%
        \fi
        }%
    \fi
  }%
  \ifx\sa@collpgflibs\empty\else
  \sa@write{^^J\@percentchar\space PGF libraries required by sub-files:}%
  \expandafter\@for\expandafter\lib\expandafter:\expandafter=\sa@collpgflibs\do{%
    \ifx\lib\empty\else
      \sa@write{\string\usepgflibrary{\lib}}%
    \fi
  }%
  \fi
  \ifx\sa@colltikzlibs\empty\else
  \sa@write{^^J\@percentchar\space TikZ libraries required by sub-files:}%
  \expandafter\@for\expandafter\lib\expandafter:\expandafter=\sa@colltikzlibs\do{%
    \ifx\lib\empty\else
      \sa@write{\string\usetikzlibrary{\lib}}%
    \fi
  }%
  \fi
  \ifx\sa@colltikztiminglibs\empty\else
  \sa@write{^^J\@percentchar\space TikZ-Timing libraries required by sub-files:}%
  \expandafter\@for\expandafter\lib\expandafter:\expandafter=\sa@colltikztiminglibs\do{%
    \ifx\lib\empty\else
      \sa@write{%
        \string\usetikztiminglibrary%
        \expandafter\ifx\csname sa@tikztimingopts@\lib\endcsname\empty\else%
          [\csname sa@tikztimingopts@\lib\endcsname]%
        \fi
        {\lib}%
        \expandafter\ifx\csname sa@tikztimingdate@\lib\endcsname\relax\else%
          [\csname sa@tikztimingdate@\lib\endcsname]%
        \fi
        }%
    \fi
  }%
  \fi
  \sa@write{\expandafter\unexpanded\expandafter{\sa@preamble}}%
  \message{^^JPackage 'standalone' INFO: See file '\jobname.stp' for list of sub-preambles.^^J}%
  \immediate\closeout\sa@out
}
%    \end{macrocode}
%
% \begin{macro}{\sa@removepackages}
% Scans for |\usepackage|.
%    \begin{macrocode}
\long\def\sa@removepackages#1\usepackage{%
  \sa@removepgflibs#1\usepgflibrary\sa@endmarker
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\sa@sortpackages}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@removepgflibs}
% Scans for |\usepgflibrary|.
%    \begin{macrocode}
\long\def\sa@removepgflibs#1\usepgflibrary{%
  \sa@removetikzlibs#1\usetikzlibrary\sa@endmarker
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\sa@sortpgflibs}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@removetikzlibs}
% Scans for |\usetikzlibrary|.
%    \begin{macrocode}
\long\def\sa@removetikzlibs#1\usetikzlibrary{%
  \sa@removetikztiminglibs#1\usetikztiminglibrary\sa@endmarker
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\sa@sorttikzlibs}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@removetikztiminglibs}
% Scans for |\usetikztiminglibrary|.
%    \begin{macrocode}
\long\def\sa@removetikztiminglibs#1\usetikztiminglibrary{%
  \sa@removeonlyifstandalone#1\onlyifstandalone\sa@endmarker
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\sa@sorttikztiminglibs}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@sortpackage}
% Reads \cs{usepackage} arguments and stores them away. A list of all packages is compiled. Every package is only
% added once and has also a list of options used, also only saved once.
% If package dates are requested then the highest one is stored. Trailing newlines are removed.
%    \begin{macrocode}
\def\sa@collpkgs{}%
\newcommand\sa@sortpackages[2][]{%
  \@ifnextchar[%]
    {\sa@@sortpackages{#1}{#2}}%
    {\sa@@sortpackages{#1}{#2}[]}%
}
\def\sa@@sortpackages#1#2[#3]{%
  \@for\pkg:=#2\do {%
    \@ifundefined{sa@pkgopts@\pkg}%
      {%
        \expandafter\g@addto@macro\expandafter\sa@collpkgs\expandafter{\expandafter,\pkg}%
        \global\@namedef{sa@pkgopts@\pkg}{#1}%
        \global\@namedef{sa@pkgopt@\pkg @}{}%
        \ifx\relax#1\relax\else
          \@for\opt:=#1\do{\global\@namedef{sa@pkgopt@\pkg @\opt}{}}%
        \fi
      }%
      {%
        \ifx\relax#1\relax\else
          \@for\opt:=#1\do{%
            \@ifundefined{sa@pkgopt@\pkg @\opt}%
              {%
                \expandafter\g@addto@macro\csname sa@pkgopts@\pkg\expandafter\endcsname\expandafter{\expandafter,\opt}%
                \global\@namedef{sa@pkgopt@\pkg @\opt}{}%
              }{}%
          }%
        \fi
      }%
    \ifx\relax#3\relax\else
    \@ifundefined{sa@pkgdate@\pkg}%
      {\global\@namedef{sa@pkgdate@\pkg}{#3}}%
      {%
        \ifnum\expandafter\expandafter
         \expandafter\sa@@getdate\csname sa@pkgdate@\pkg\endcsname//00\relax<\sa@@getdate#3//00\relax
          \global\@namedef{sa@pkgdate@\pkg}{#3}%
        \fi
      }%
    \fi
  }%
  \sa@gobbleeol\sa@removepackages^^J%
}
\def\sa@@getdate#1/#2/#3#4#5\relax{#1#2#3#4}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@sortpgflibs}
% Reads the \cs{usepgflibrary} argument and stores it away.
% Trailing newlines are removed.
%    \begin{macrocode}
\def\sa@collpgflibs{}%
\def\sa@sortpgflibs#1{%
  \@for\lib:=#1\do {%
    \@ifundefined{sa@pgflib@\lib}%
      {%
        \expandafter\g@addto@macro\expandafter\sa@collpgflibs\expandafter{\expandafter,\lib}%
        \global\@namedef{sa@pgflib@\lib}{}%
      }%
      {}%
  }%
  \sa@gobbleeol\sa@removepgflibs^^J%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@sorttikzlibs}
% Reads the \cs{usetikzlibrary} argument and stores it away.
% Trailing newlines are removed.
%    \begin{macrocode}
\def\sa@colltikzlibs{}%
\def\sa@sorttikzlibs#1{%
  \@for\lib:=#1\do {%
    \@ifundefined{sa@tikzlib@\lib}%
      {%
        \expandafter\g@addto@macro\expandafter\sa@colltikzlibs\expandafter{\expandafter,\lib}%
        \global\@namedef{sa@tikzlib@\lib}{}%
      }%
      {}%
  }%
  \sa@gobbleeol\sa@removetikzlibs^^J%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@sorttikztiminglibs}
% Reads \cs{usetikztiminglibrary} arguments and stores them away.
% Trailing newlines are removed.
%    \begin{macrocode}
\def\sa@colltikztiminglibs{}%
\newcommand\sa@sorttikztiminglibs[2][]{%
  \@ifnextchar[%]
    {\sa@@sorttikztiminglibs{#1}{#2}}%
    {\sa@@sorttikztiminglibs{#1}{#2}[]}%
}
\def\sa@@sorttikztiminglibs#1#2[#3]{%
  \@for\lib:=#2\do {%
    \@ifundefined{sa@tikztimingopts@\lib}%
      {%
        \expandafter\g@addto@macro\expandafter\sa@colltikztiminglibs\expandafter{\expandafter,\lib}%
        \global\@namedef{sa@tikztimingopts@\lib}{#1}%
        \global\@namedef{sa@tikztimingopt@\lib @}{}%
        \ifx\relax#1\relax\else
          \@for\opt:=#1\do{\global\@namedef{sa@tikztimingopt@\lib @\opt}{}}%
        \fi
      }%
      {%
        \ifx\relax#1\relax\else
          \@for\opt:=#1\do{%
            \@ifundefined{sa@tikztimingopt@\lib @\opt}%
              {%
                \expandafter\g@addto@macro\csname sa@tikztimingopts@\lib\expandafter\endcsname\expandafter{\expandafter,\opt}%
                \global\@namedef{sa@tikztimingopt@\lib @\opt}{}%
              }{}%
          }%
        \fi
      }%
    \ifx\relax#3\relax\else
    \@ifundefined{sa@tikztimingdate@\lib}%
      {\global\@namedef{sa@tikztimingdate@\lib}{#3}}%
      {%
        \begingroup
        \edef\@tempa{{\csname sa@tikztimingdate@\lib\endcsname}{#3}}%
        \expandafter\sa@getlargerdate\@tempa
        \expandafter\xdef\csname sa@tikztimingdate@\lib\endcsname{\sa@thedate}%
        \endgroup
      }%
    \fi
  }%
  \sa@gobbleeol\sa@removetikztiminglibs^^J%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\sa@gobbleopt}
% Gobbles an optional argument and a potential line endings and then executes the command given by |#1|.
%    \begin{macrocode}
\def\sa@gobbleopt#1[#2]{%
  \@ifnextchar^^J%
    {\sa@gobbleeol{#1}}{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\else
%    \end{macrocode}
%
% \begin{macro}{\sa@scanpackages}
% Scans for |\usepackage|.
%    \begin{macrocode}
\def\sa@scanpackages#1\usepackage{%
  \@ifnextchar\sa@endmarker
    {\@gobble}%
    {\sa@collectpackage}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@collectpackage}
% Reads \cs{usepackage} arguments (ignores optional date) and stores it away.
% The options are later passed to the package to avoid option clashes.
%    \begin{macrocode}
\newcommand\sa@collectpackage[2][]{%
  \ifx\relax#1\relax\else
    \g@addto@macro\sa@collopts{\PassOptionsToPackage{#1}{#2}}%
  \fi
  \sa@scanpackages
}
\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@collopts}
% Accumulator for collected options. Is executed and cleared at the end of this package.
%    \begin{macrocode}
\def\sa@collopts{}
\AtEndOfPackage{\sa@collopts\let\sa@collopts\relax}
%    \end{macrocode}
% \end{macro}
%
% End of |\ifsa@sortsubpreambles|.
%    \begin{macrocode}
\fi
%    \end{macrocode}
%
% \begin{environment}{standalonepreambles}
% This environment simply adds a group and sets the endline character to a printed newline and the argument character
% |#| as a normal character. The first suppresses |\par|'s in the stored sub-preambles while preserving newlines. The latter
% is required to permit macro arguments in the preambles. Otherwise a |#| is doubled to |##| causing compile errors when the
% sub-preambles are used.
% The |.sta| file is closed after this environment.
%    \begin{macrocode}
\def\standalonepreambles{%
  \begingroup
  \endlinechar=\m@ne
  \@makeother\#%
}
\def\endstandalonepreambles{%
  \endgroup
  \endinput
}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{subpreambles}
% This environment rereads the sub-preambles from the |.sta| files and stores it globally under the name
% ``\cs{prevsubpreamble@}\meta{file name}''. If sorting is enabled the sub-preambles are also scanned for
% loaded packages.
%    \begin{macrocode}
\long\gdef\subpreamble#1#2\endsubpreamble{%
  \expandafter\gdef\csname prevsubpreamble@#1\endcsname{#2}%
  \ifsa@sortsubpreambles
    \sa@scanpackages#2\usepackage\sa@endmarker
  \fi
}
\def\endsubpreamble{}%
%    \end{macrocode}
% \end{environment}
%
% If in |print| mode open the |.stp| file.
%    \begin{macrocode}
\ifsa@printsubpreambles
  \immediate\openout\sa@out=\jobname.stp\relax
\else
%    \end{macrocode}
% otherwise:
%
% Process |.sta| file from last run. All changes must be made by own macros which define the value globally.
% Therefore the input is wrapped in a group. Some spaces or special line endings could process typeset content,
% which causes errors inside the preamble. To be on the save side the input `content' is stored in a temp box.
%    \begin{macrocode}
\begingroup
  \setbox\@tempboxa\hbox{%
  \InputIfFileExists{\jobname.sta}{}{\PackageInfo{standalone}{STA file not found!}{}{}}%
  }%
\endgroup
%    \end{macrocode}

% \begin{macro}{\AtBeginDocument}
% At begin of the document the |.sta| file is read again. This time the sub-preamble macros are executed as normal.
% The |standalone| macros are defined to be without effect. If `sorting' is enabled \cs{usepackage} is temporarily
% redefined to ignore any given options, which where already passed (\cs{PassOptionsToPackage}) beforehand.
%    \begin{macrocode}
\AtBeginDocument{%
  \let\subpreamble\@gobble
  \let\endsubpreamble\relax
  \let\standalonepreambles\relax
  \let\endstandalonepreambles\relax
  \ifsa@sortsubpreambles
    \let\sa@orig@usepackage\usepackage
    \let\usepackage\sa@usepackagewithoutoptions
  \fi
  \InputIfFileExists{\jobname.sta}{}{}%
  \ifsa@sortsubpreambles
    \let\usepackage\sa@orig@usepackage
  \fi
  \immediate\openout\sa@out=\jobname.sta\relax
  \immediate\write\sa@out{\string\standalonepreambles}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AtEndDocument}
% At end of the document write end macro to and close |.sta| file.
%    \begin{macrocode}
\AtEndDocument{%
  \sa@write{\string\endstandalonepreambles}%
  \immediate\closeout\sa@out
}
%    \end{macrocode}
% \end{macro}
%
% End of |\ifsa@printsubpreambles|.
%    \begin{macrocode}
\fi
%    \end{macrocode}
%
% End of |\ifsa@subpreambles|.
%    \begin{macrocode}
\fi
%    \end{macrocode}
%
%
% \subsubsection{Skipping of Sub-Preambles in Main Mode}
%
% This macros make the main document skip all preambles in sub-files.
%
% \begin{macro}{\sa@gobbleeol}
% Gobbles all following line endings (i.e.\ empty lines) and then executes the command given by |#1|.
% Because |\@ifnextchar| ignores spaces this also removes lines with only spaces.
%    \begin{macrocode}
\def\sa@gobbleeol#1^^J{%
  \@ifnextchar^^J%
    {\sa@gobbleeol{#1}}{#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@gobbleline}
% Gobbles the rest of the input line. This is required when skipping the body of a file to also skip
% everything on the same line after |\begin{document}|.
%    \begin{macrocode}
\def\sa@gobbleline#1^^J{}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\standaloneignore}
% \changes{v0.3}{2010/03/26}{New macro}
% This macro must only be used in a sub-file before a |\documentclass|. It gobbles everything up to this macro
% and then executes the |standalone| definition of it shown further below.
% It should be written as |\csname standaloneignore\endcsname| to ignore errors in standalone mode.
% The second definition allows the user to also write |\csname standaloneignore \endcsname| (note the extra space)
% without errors.
%    \begin{macrocode}
\long\def\standaloneignore#1\documentclass{%
  \sa@documentclass
}
\@namedef{standaloneignore\space}{\standaloneignore}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@documentclass}
% The |standalone| definition of \cs{documentclass}. If the sub-preambles are to be processed then the
% starting content is written into the output file etc., but only for the first time this sub-file is included.
% Some input related settings are set-up (line endings, macro argument and comments).
% Finally \cs{sa@gobble} is called to process the preamble.
%    \begin{macrocode}
\newcommand{\sa@documentclass}[2][]{%
  \let\document\sa@document
  \begingroup
  \ifsa@subpreambles
    \@ifundefined{sa@written@\sa@filepath}%
      {%
        \ifsa@printsubpreambles
          \ifsa@sortsubpreambles
            \begingroup
              \edef\@tempa{^^J\@percentchar\space Preamble from file '\sa@filepath'^^J}%
              \expandafter\g@addto@macro\expandafter\sa@preamble\expandafter{\@tempa}%
            \endgroup
          \else
            \sa@write{^^J\@percentchar\space Preamble from file '\sa@filepath'}%
          \fi
        \else
          \sa@write{\string\subpreamble{\sa@filepath}}%
        \fi
      }{}%
    \global\@namedef{subpreamble@\sa@filepath}{}%
    \ifsa@printsubpreambles
      \endlinechar=`\^^J%
    \else
      \endlinechar=\m@ne
    \fi
    \@makeother\#%
    \@nameuse{sa@percent}%
  \fi
  \def\sa@gobbleto{document}%
  \sa@gobbleeol\sa@gobble^^J%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@gobble}
% Gobbles everything to the next |\begin|, then checks if it was a |\begin{document}|.
% If sub-preamble extraction is activated it accumulates the skipped content in macros
% named ``\cs{subpreamble@}\meta{file name}''. Every sub-file is remembered and its preamble is only saved once.
% In |print| mode the file body is ignored and a appropriate warning is printed,
% otherwise the current and previous sub-preamble of the current processed file are compared. If different the file body is also
% ignored to avoid errors due to possible newly required but not loaded packages. The user is warned again about this
% and is asked to rerun \LaTeX{}.
%    \begin{macrocode}
\def\sa@preamble{}%
\long\def\sa@gobble#1\begin#2{%
  \def\@tempa{#2}%
  \ifx\@tempa\sa@gobbleto
    \ifsa@subpreambles
      \expandafter\g@addto@macro\csname subpreamble@\sa@filepath\endcsname{#1}%
      \@ifundefined{sa@written@\sa@filepath}%
        {%
          \ifsa@printsubpreambles
            \ifsa@sortsubpreambles
              \sa@removepackages#1\usepackage\sa@endmarker
            \else
              \begingroup
              \let\sa@preamble\empty
              \sa@removeonlyifstandalone#1\onlyifstandalone\sa@endmarker
              \expandafter\sa@write\expandafter{\expandafter\unexpanded\expandafter{\sa@preamble}}%
              \endgroup
            \fi
          \else
            \sa@write{\unexpanded{#1}}%
            \sa@write{\string\endsubpreamble}%
          \fi
        }{}%
      \global\@namedef{sa@written@\sa@filepath}{}%
      \ifsa@printsubpreambles
        \def\next{%
          \endgroup
          \PackageWarning{standalone}{Running 'standalone' package in sub-preamble print mode. All body content of file `\sa@filepath' is ignored!}{}{}%
          \hbox to 1pt{\vbox to 1pt{}}%
          \endinput
          %\sa@gobbleline
        }%
      \else
      \expandafter
      \ifx
      \csname prevsubpreamble@\sa@filepath \expandafter\endcsname
      \csname     subpreamble@\sa@filepath \endcsname
        \def\next{\expandafter\endgroup\expandafter\begin\expandafter{\sa@gobbleto}}%
      \else
        %\expandafter\show\csname prevsubpreamble@\sa@filepath \endcsname
        %\expandafter\show\csname     subpreamble@\sa@filepath \endcsname
        \def\next{%
          \endgroup
          \PackageWarning{standalone}{Sub-preamble of file '\sa@filepath' has changed. Content will be ignored. Please rerun LaTeX!}{}{}%
          \immediate\write\@mainaux{%
            \@percentchar\space standalone package info: Rerun LaTeX!
          }%
          \hbox to 1pt{\vbox to 1pt{}}%
          \endinput
          %\sa@gobbleline
        }%
      \fi
      \fi
    \else
      \def\next{\expandafter\endgroup\expandafter\begin\expandafter{\sa@gobbleto}}%
    \fi
  \else
    \ifsa@subpreambles
      \expandafter\g@addto@macro\csname subpreamble@\sa@filepath\endcsname{#1\begin{#2}}%
      \@ifundefined{sa@written@\sa@filepath}%
        {\sa@write{\unexpanded{#1\begin{#2}}}}{}%
    \fi
    \def\next{\sa@gobble}%
  \fi
  \next
}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{standalone}
% Provide an empty definition of the |standalone| environment. The class is defining it with the code required in |standalone| mode.
%    \begin{macrocode}
\@ifundefined{standalone}
  {\newenvironment{standalone}[1][]{}{}}
  {}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{standalone}
% Provide an `empty' definition of the |standaloneframe| environment.
% It only gobbles all arguments: |<...>[<...>][...]{...}{...}|. Please note that the last two |{ }| arguments are also optional.
% The class is defining it with the code required in |standalone| mode.
%    \begin{macrocode}
\@ifundefined{standaloneframe}
  {\@ifundefined{beamer@newenv}
    {\newenvironment{standaloneframe}[1][]{%
      \@ifnextchar[%]
        {\sa@framegobbleopt}{\sa@framegobbleargs}}{}%
    }
    {\newenvironment<>{standaloneframe}[1][]{%
      \@ifnextchar[%]
        {\sa@framegobbleopt}{\sa@framegobbleargs}}{}%
    }
   \def\sa@framegobbleopt[#1]{\sa@framegobbleargs}
   \def\sa@framegobbleargs{%
     \@ifnextchar\bgroup
       {\sa@framegobbleargs@}%
       {}%
   }
   \def\sa@framegobbleargs@#1{%
    \@ifnextchar\bgroup
      {\@gobble}%
      {}%
   }
  }
  {}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\sa@orig@document}
% \begin{macro}{\sa@orig@enddocument}
% Store original |document| environment.
%    \begin{macrocode}
\let\sa@orig@document\document
\let\sa@orig@enddocument\enddocument
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\document}
% Redefine the |\begin{document}| of the main file to redefine \cs{documentclass}.
% This can not be done using \cs{AtBeginDocument} because the original redefines
% \cs{documentclass} itself after executing the hook.
%    \begin{macrocode}
\def\document{%
  \sa@orig@document
  \let\documentclass\sa@documentclass
  \ignorespaces
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@document}
% This is the |\begin{document}| of the sub files. It does nothing except of
% redefining |\end{document}| and calling our own |atbegindocument| hook.
%    \begin{macrocode}
\def\sa@document{%
  \let\enddocument\sa@enddocument
  \sa@atbegindocument
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@enddocument}
% This is the |\end{document}| of the sub files. It does nothing except of
% calling our own |atenddocument| hook and then the `after end document' handler.
%    \begin{macrocode}
\def\sa@enddocument{%
  \sa@atenddocument
  \aftergroup\sa@@enddocument
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@@enddocument}
% This is a `after end document' handler for the sub-files. It restores macros and
% ends the input of the file.
%    \begin{macrocode}
\def\sa@@enddocument{%
  %\let\document\sa@orig@document
  \let\enddocument\sa@orig@enddocument
  \endinput
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@atbegindocument}
% This hook simply ignores all spaces after |\begin{document}| in the sub files.
%    \begin{macrocode}
\def\sa@atbegindocument{%
  \ignorespaces
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sa@atenddocument}
% This hook simply ignores the last skip (normally the spaces) before |\end{document}| in the sub files.
%    \begin{macrocode}
\def\sa@atenddocument{%
  \unskip
}%
%    \end{macrocode}
% \end{macro}
%
%
% \iffalse
%</sty>
% \fi
%
% \subsection{The Class File}
% \iffalse
%<*cls>
% \fi
%
% \subsubsection{If-Switches}
%
% \begin{macro}{\ifstandalone}
% This if-switch is defined by both the class and package. This class sets
% it to true while the package (loaded by the main document) sets it to false.
%    \begin{macrocode}
\newif\ifstandalone
\standalonetrue
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifstandalonebeamer}
% This if-switch is defined by both the class and package. This class sets
% it to true only if the |beamer| option was given. The package (loaded by the main document) sets it always to false.
%    \begin{macrocode}
\newif\ifstandalonebeamer
\standalonebeamerfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\onlyifstandalone}
% Macro version of |\ifstandalone|. The |{ }| around the argument protects the content from the package etc. scanners.
%    \begin{macrocode}
\let\onlyifstandalone\@firstofone
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Options}
%
%    \begin{macrocode}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{prefix=sa@}
%    \end{macrocode}
%
% Use of |preview| package is optional but enabled by default.
% This defines the |\ifsa@preview| switch.
%    \begin{macrocode}
\DeclareBoolOption[true]{preview}
%    \end{macrocode}
%
% To set the preview border:
%    \begin{macrocode}
\DeclareStringOption{border}
\let\sa@border\relax
%    \end{macrocode}
%
% Enable beamer support.
%    \begin{macrocode}
\DeclareVoidOption{beamer}{%
  \def\sa@class{beamer}%
  \sa@previewfalse
  \standalonebeamertrue
}
%    \end{macrocode}
%
% Option to set underlying class. Default is |article|.
%    \begin{macrocode}
\DeclareStringOption[article]{class}
%    \end{macrocode}
%
% The rest of the options are accumulated and set as the official class options
% for the real class loaded afterwards. This avoids the passing of any |standalone| class options
% to the underlying class in any way. The |beamer| class for example has an option called
% `|class|' in a similar way the |standalone| class does, which would cause problems if not filtered out.
%    \begin{macrocode}
\def\sa@classoptionslist{}
\DeclareDefaultOption{%
  \xdef\sa@classoptionslist{\sa@classoptionslist,\CurrentOption}%
}
\ProcessKeyvalOptions*\relax
\let\@classoptionslist\sa@classoptionslist
%    \end{macrocode}
%
% Loads the class given by the |class| option with the rest of the options.
%    \begin{macrocode}
\begingroup
\xdef\@tempa{[\sa@classoptionslist]{\sa@class}}
\expandafter
\endgroup
\expandafter\LoadClass\@tempa
%    \end{macrocode}
%
% \begin{environment}{standalone}
% The \env{standalone} environment is defined by default to be without effect.
% The \cs{endstandalone} macro is set to |\relax|, so a redefinition with
% \cs{renewenvironment} in the configuration file can be detected later.
%    \begin{macrocode}
\let\standalone\empty
\let\endstandalone\relax
%    \end{macrocode}
% \end{environment}
%
% Loads configuration file.
%    \begin{macrocode}
\input{standalone.cfg}
%    \end{macrocode}
%
% \subsubsection{Preview Code}
% The \env{standalone} environment is redefined to use the |preview| environment as
% long it was not redefined in the configuration file.
%    \begin{macrocode}
\ifsa@preview
  \@ifundefined{endstandalone}{%
    \renewenvironment{standalone}
      {\preview   }
      {\endpreview}
  }{}%
  \RequirePackage{preview}
%    \end{macrocode}
% Read the preview border values. The default unit is |bp| (PostScript points).
% Allowed are one, two or four values.
%    \begin{macrocode}
  \ifx\sa@border\relax\else
    \begingroup
    \def\rem@bp#1bp\relax#2\@nnil{#1}%
    \def\default@bp#1{\expandafter\rem@bp\the\dimexpr#1 bp\relax bp\relax\@nnil}%
    \def\sa@readborder#1 #2 #3 #4 {%
       \ifx\\#2#3#4\\%
         \@defaultunits\global\PreviewBorder=\dimexpr#1 bp\relax\@nnil%
       \else
       \ifx\\#4\\%
        \xdef\PreviewBbAdjust{-\default@bp{#1} -\default@bp{#2} \default@bp{#1} \default@bp{#2}}%
       \else
        \xdef\PreviewBbAdjust{-\default@bp{#1} -\default@bp{#2} \default@bp{#3} \default@bp{#4}}%
       \fi\fi
    }%
    \@firstofone{\expandafter\sa@readborder\sa@border} {} {} {} {} \relax
    \endgroup
  \fi
\fi
%    \end{macrocode}
%
% \subsubsection{Beamer Frame Environment}
%
%    \begin{macrocode}
\ifstandalonebeamer
%    \end{macrocode}
% \begin{environment}{standaloneframe}
% Front-end for the beamer |frame| environment. Parses all arguments
% the same way and calls it with an added option.
%    \begin{macrocode}
\newenvironment{standaloneframe}{%
  \@ifnextchar<%
    {\@standaloneframe}%
    {\@@standaloneframe{}}%
}{\end{frame}}%
\def\@standaloneframe<#1>{%
    \@@standaloneframe{<#1>}%
}
\def\@@standaloneframe#1{%
  \@ifnextchar[%]
    {\@@@standaloneframe{#1}}%
    {\@@@standaloneframe{#1}[]}%
}%
\def\@@@standaloneframe#1[{%
  \@ifnextchar<%
    {\@@@@standaloneframe{#1}[}%
    {\@@@@@@standaloneframe{#1}[}%
}%
\def\@@@@standaloneframe#1[#2]{%
  \@ifnextchar[%]
    {\@@@@@standaloneframe{#1}{#2}}%
    {\begin{frame}#1[#2][environment=standaloneframe]}%
}%
\def\@@@@@standaloneframe#1#2[#3]{%
    \begin{frame}#1[#2][environment=standaloneframe,#3]%
}%
\def\@@@@@@standaloneframe#1[#2]{%
    \begin{frame}#1[environment=standaloneframe,#2]%
}%
%    \end{macrocode}
% \end{environment}
%    \begin{macrocode}
\fi
%    \end{macrocode}
%
% \subsubsection{Document Environment in Sub-Files}
%
% \begin{macro}{\sa@cls@orig@document}
% \begin{macro}{\sa@cls@orig@enddocument}
% Store original |document| environment.
%    \begin{macrocode}
\let\sa@cls@orig@document\document
\let\sa@cls@orig@enddocument\enddocument
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{environment}{document}
% Adds own `after begin document' and `before end document` hooks.
%    \begin{macrocode}
\def\document{%
  \sa@cls@orig@document
  \let\documentclass\sa@documentclass  % TODO: really required?
  \sa@cls@afterbegindocument
}
\def\enddocument{%
  \sa@cls@beforeenddocument
  \sa@cls@orig@enddocument
}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\sa@cls@afterbegindocument}
% \begin{macro}{\sa@cls@beforeenddocument}
% Hooks which add the \env{standalone} environment. Surrounding spaces
% are removed. This hooks are used (instead of calling the content directly in the above macros)
% to add the possibility to fine tune this later, e.g.\ in the configuration file.
%    \begin{macrocode}
\def\sa@cls@afterbegindocument{\expandafter\standalone\ignorespaces}
\def\sa@cls@beforeenddocument{\unskip\endstandalone}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \iffalse
%</cls>
% \fi
%
% \subsection{Simple TeX File}
% \iffalse
%<*tex>
% \fi
% \begin{macro}{\ifstandalone}
% Provides \cs{ifstandalone} switch which is |\iftrue| if the normal |\documentclass|
% was not yet executed (and subsequently redefined to be |\@twoclasseserror|).
%    \begin{macrocode}
\expandafter\ifx\csname ifstandalone\endcsname\relax
\expandafter\newif\csname ifstandalone\endcsname
\expandafter\ifx\csname @twoclasseserror\endcsname\documentclass
\else
  \standalonetrue
\fi
\fi
%    \end{macrocode}
% \end{macro}
% \iffalse
%</tex>
% \fi
%
% \iffalse
%<*sty>
% \fi
%    \begin{macrocode}
\expandafter\ifx\csname sa@mode\endcsname\relax\else
%    \end{macrocode}
% \begin{macro}{\includestandalone}
%    \begin{macrocode}
\RequirePackage{gincltex}
\RequirePackage{ifpdf}
\ifpdf
    \def\sa@graphicext{.pdf}
\else
    \def\sa@graphicext{.eps}
\fi

\def\sa@comparegranpic#1{%
    \begingroup
    \let\next\empty
    \expandafter\sa@comparegranpic@\pdffilemoddate{#1\sa@graphicext}\relax
    \let\sa@picdate\sa@date
    \let\sa@pictime\sa@time
    \expandafter\sa@comparegranpic@\pdffilemoddate{#1.tex}\relax
    \ifnum\sa@date>\sa@picdate\relax
        \def\next{\sa@buildgraphic{#1}}%
    \else
        \ifnum\sa@date=\sa@picdate\relax
            \ifnum\sa@time>\sa@pictime\relax
                \def\next{\sa@buildgraphic{#1}}%
            \fi
        \fi
    \fi
    \expandafter
    \endgroup
    \next
}

\begingroup
\@makeother\D
\@makeother:
\global\@namedef{sa@comparegranpic@}D:#1#2#3#4#5#6#7#8#9\relax{%
    \def\sa@date{#1#2#3#4#5#6#7#8}%
    \sa@comparegranpic@@#9\relax
}
\endgroup

\def\sa@comparegranpic@@#1#2#3#4#5#6#7\relax{%
    \def\sa@time{#1#2#3#4#5#6}%
    \def\sa@tz{#7}%
}

\def\sa@compilecmd{\ifpdf pdf\fi latex --interaction=batchmode \image}%

\def\sa@buildgraphic#1{%
    \ifeof18
        \PackageError{standalone}{Shell escape needed to create graphic!}{}%
    \else
        \begingroup
        \def\image{#1}%
        \immediate\write18{\sa@compilecmd}%
        \endgroup
    \fi
}

\newcommand*\includestandalone[2][]{%
    \begingroup
    \edef\@tempa{{#2\sa@graphicext}}%
    \expandafter\includestandalone@\@tempa{#2}{#1}%
    \endgroup
}

%% 0 = PDF if exists, TEX otherwise
%% 1 = force TEX
%% 2 = force PDF
%% 3 = build PDF if not exists
%% 4 = build PDF if older than TEX
\def\includestandalone@#1#2#3{%
    \ifcase\sa@mode
    \relax% 0
        \IfFileExists{#1}%
            {\includegraphics[#3]{#1}}%
            {\includegraphics[#3]{#2.tex}}%
    \or% 1
        \includegraphics[#3]{#2.tex}%
    \or% 2
        \includegraphics[#3]{#1}%
    \or% 3
        \IfFileExists{#1}%
            {\includegraphics[#3]{#1}}%
            {\sa@buildgraphic{#2}%
             \IfFileExists{#1}%
                {\includegraphics[#3]{#1}}%
                {\PackageWarning{standalone}%
                 {Graphic '#1' could not be build.^^J%
                  Shell escape activated?}%
                 \includegraphics[#3]{#2.tex}%
                }%
            }%
    \else% 4
        \IfFileExists{#1}%
            {\sa@comparegranpic{#2}}%
            {\sa@buildgraphic{#2}%
             \IfFileExists{#1}%
                {\includegraphics[#3]{#1}}%
                {\PackageWarning{standalone}%
                    {Graphic '#1' could not be build.^^J%
                    Shell escape activated?}%
                    \includegraphics[#3]{#2.tex}%
                }%
            }%
    \fi
}
%    \end{macrocode}
%
% \end{macro}
%    \begin{macrocode}
\fi
%    \end{macrocode}
% \iffalse
%</sty>
% \fi
%
% \subsection{Config File}
% Default content of the configuration file. Users can override this by placing an own
% |standalone.cfg| file somewhere where \TeX\ can find it (user |texmf| directory or local directory).
% This user file can load the default config file using
% using |\InputIfFileExists{standalone/standalone.cfg}{}{}|.
% Be default only the |preview| package option are set and
% the navigation symbols of beamer standalones are disabled.
% \iffalse
%<*cfg>
% \fi
%    \begin{macrocode}
\PassOptionsToPackage{active,tightpage}{preview}%

\ifstandalonebeamer
  \setbeamertemplate{navigation symbols}{}%
\fi
%    \end{macrocode}
% \iffalse
%</cfg>
% \fi
%
% \iffalse
%<cfg>% vim: ft=tex
% \fi
% \Finale
% \endinput