summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/doctools/doctools.dtx
blob: c9b355b96e9b76bc81921f43c9a24948f6abd885 (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
% \iffalse meta-comment
% !TeX spellcheck = en-US
%   
% File:      doctools.sty
% Version:   2014/06/27 v0.1
% Author:    Matthias Pospiech
% Email:     <matthias@pospiech.eu>
%
% Copyright (C) 2012 by Matthias Pospiech (matthias@pospiech.eu)
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Matthias Pospiech.
%
% This work consists of the files doctools.dtx and doctools.ins
% and the derived filebase doctools.sty.
%
% \fi
% \iffalse
%<*driver>
\ProvidesFile{doctools.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{doctools}
%<*package>
   [2014/06/27 v0.1 commands and packages for documenting LaTeX Code]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{hypdoc}
\RequirePackage[%
   loadHyperref=true,%
   createIndexEntries=false,%
   applyLayout=true%
]{doctools} % 
\usepackage{lmodern}
%
%%% Configures doc.sty and its (outdated) index generation, which does
%   not work together with any modern package and has no hyperref support
%   and thus no links. Furthermore it conflicts with the index macros
%   of doctools.
%
\EnableCrossrefs  % (default) Every new macro name used within a macrocode or
                  % macrocode∗ environment will produce an index entry.
% \DisableCrossrefs % turn off this feature
%
% If an index is created is determined by the use of the following
% declarations in the driver file preamble; if neither is used, no index is 
% produced.
\PageIndex     % all index entries refer to their page number
% \CodelineIndex % index entries produced by \DescribeMacro and \DescribeEnv
               %  refer to page number but those produced by the macro
               %  environment refer to the code lines, 
               % which will be numbered automatically.
% \CodelineNumbered % no index is created, but the code lines are numbered

\RecordChanges
\listfiles
%
\begin{document}
  \DocInput{doctools.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
%
% \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         \~}
%
% \CheckSum{0}
%
% \changes{v0.1}{2012/12/01}{initial version (converted to dtx file)}
%
% \DoNotIndex{\newcommand,\newenvironment,\@bsphack,\@empty,\@esphack,\addtocounter,\arraystretch,\bs,\color,\colorlet,\else,\end,\expandafter,\fi,\hyperref,\ifcsdef,\ifstrempty,\ifx,\index,\label,\langle,\let,\linewidth,\lstinputlisting,\makeatletter,\makeatother,\NeedsTeXFormat,\newcounter,\noindent,\normalfont,\option,\par,\parindent,\parskip,\phantomsection,\PrintDescribeEnv,\PrintDescribeMacro,\PrintEnvName,\PrintMacroName,\ProcessKeyvalOptions,\providecommand,\ProvidesPackage,\rangle,\refstepcounter,\renewcommand,\RequirePackage,\setcounter,\setlength,\slshape,\small,\string,\strut,\textbackslash,\textsc,\texttt,\ttfamily,\urlstyle,\usepackage,\xspace}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{doctools.dtx}
% \title{The \textsf{doctools} package}
% \author{Matthias Pospiech \\ \url{matthias@pospiech.eu}}
% \date{0.1~from 2014/06/27}
%
% \maketitle
% \tableofcontents
% ^^A ===================================================================
% \section{Documentation}
% This package is a collections of tools for the documentation of \latex code
% either in a normal document or within a dtx file.
% \StopEventually{}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Options}
% This package has three options 
% \begin{Optionlist}
%  \option{applyLayout}    &  true/false (default: false)  \\
%  \option{loadHyperref}   &  true/false (default: false)  \\
%  \option{createIndexEntries}   &  true/false (default: true)  \\
% \end{Optionlist}
%
% The option \option{applyLayout} should only be used in dtx files for which
% the layout of this package shall be applied. For normal documents setting
% this option to true could disturb the layout.
%
% The selection of the other two options (\option{loadHyperref},
% \option{createIndexEntries}) should follow the following principles, which
% are mostly related to the fact that doc.sty does not support package 
% \package{hyperref} (prohibits display of index entries) and requires 
% the package \package{hypdoc} instead.
% 
% Package \package{doctools} is typically loaded before most other packages 
% and in the case of a dtx file possible the only package which 
% is necessary to load.
%
% \subsubsection{Selection of options inside a dtx file}
% In a dtx file index entries are created by the package \package{doc.sty}
% which have a format the requires the use of the format file 
% \texttt{gind.ist} for makeindex:
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[]
makeindex.exe -s gind.ist filename.idx
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% The index entries by \package{doc.sty} are created if \cs{PageIndex} or
% \cs{CodelineIndex} are defined except if \cs{CodelineNumbered} is defined.
% With the last definition no index is created, but the code lines are still
% numbered. In this case \package{hyperref} has no
% influence on the index creation since the index is never created: 
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleBase]
\CodelineNumbered
\usepackage[
  loadHyperref=true,%
  createIndexEntries=false% (has no influence)
]{doctools} % 
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% If in contrast the index entries of \package{doc.sty} are activated using \cs{PageIndex} or \cs{CodelineIndex}, then \package{hyperref} should not be loaded because it prohibits the display of the index entries created by \package{doc.sty}. Furthermore the index entries of \package{doctools} should be disabled because they use a different format and would be displayed wrong. 
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleBase]
\PageIndex
\usepackage[
  loadHyperref=false,% (would prohibit doc.sty index entries)
  createIndexEntries=false% (would be displayed wrong)
]{doctools} % 
\usepackage{hypdoc}
\end{lstlisting}
% \iffalse
%</example>
% \fi
% If the index entries of \package{doc.sty} are supposed to be displayed with hyperlinks the package \package{hypdoc} must be loaded instead\footnote{This issue is explained very well in this thread: \url{http://tex.stackexchange.com/questions/87670/disable-index-creation-in-dtx-file}}. If the option \option{loadHyperref} is used all styles are applied although \package{hyperref} is not loaded, because it is already loaded by the package \package{hypdoc}. Take care of the loading order: \package{hypdoc} must be loaded before \package{doctools}.
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleBase]
\PageIndex
\usepackage{hypdoc} % load before doctools !
\usepackage[
  loadHyperref=true,% apply hyperref styles
  createIndexEntries=false% (would be displayed wrong)
]{doctools} % 
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% However, if only the index entries of \package{doctools.sty} are supposed to be displayed in the index, this can be realized by loading \package{hyperref}. In this case the format file for \texttt{makeindex} must not be specified.
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleBase]
\PageIndex % creates index, but entries are not displayed
\usepackage[
  loadHyperref=true,% (prohibit doc.sty index entries)
  createIndexEntries=true% 
]{doctools} % 
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \subsubsection{Selection of options in a normal \latex file}
% In a normal \latex file that does not load \package{doc.sty} the package options may be selected according to the needs of the user. If both index entries shall be created and \package{hyperref} can be safely loaded one could load the package with these options:
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleBase]
\usepackage[
  loadHyperref=true,% 
  createIndexEntries=true% 
]{doctools} % 
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Commands provided by doc.sty and ltxdoc}
% For the complete reference refer to \url{doc.pdf} and \url{ltxdoc.pdf}. Here % only the most relevant commands for the code in the documentation part are
% described.
%
% \DescribeMacro{\DescribeMacro} \marg{macro}  \AfterLastParam
% Prints out the macro name in the margin and
% adds the command to the index. It primary usage is to indicate the part of
% the documentation where the usage of this macro is described.
% 
% \DescribeMacro{\DescribeEnv} \marg{environment}  \AfterLastParam
% Is used analogues to \cs{DescribeMacro}.
%
% \DescribeMacro{\marg} \marg{argument}  \AfterLastParam
% Mandatory argument, printed as \marg{argument}
%
% \DescribeMacro{\oarg} \marg{argument}  \AfterLastParam
% Optional argument, printed as \oarg{argument}
%
% \DescribeMacro{\meta} Both \cs{marg} and \cs{oarg} use \cs{meta}
% to print out the argument as \meta{argument}.
%
% If \package{ltxdoc} is not used both \cs{marg} and \cs{oarg} are instead 
% defined by \package{doctools}.
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Commands provided by doctools.sty}
% 
% \DescribeMacro{\bs} Shortcut for \cs{textbackslash}.
%
% \DescribeMacro{\command} \marg{cmd} \AfterLastParam
% Prints out the argument \cs{command}\arg{foo} as \cs{foo}.
%
% \DescribeMacro{\cs} \marg{cmd} \AfterLastParam
% Shortcut for \cs{command}. Also defined by \ltxclass{ltxdoc}.
%
% \DescribeMacro{\arg} \marg{cmd} \AfterLastParam
% Prints out an argument in curled brackets without the use of 
% angle brackets as in \cs{marg} or \cs{oarg}. 
% Thus prints \cs{arg}\arg{foo} as \arg{foo}.
%
% \DescribeMacro{\environment}  \marg{environment} \AfterLastParam
% Prints out an environment name as \environment{environment}.
%
% \DescribeMacro{\env} \marg{environment} \AfterLastParam
% Shortcut for \cs{environment}
%
% \DescribeMacro{\package} \marg{package} \AfterLastParam
% Prints out a package name as \package{package}.
%
% \DescribeMacro{\ltxclass} \marg{LaTeX documentclass} \AfterLastParam
% Prints out a class name as \ltxclass{class}.
%
% \DescribeMacro{\option} \marg{option} \AfterLastParam
% Prints out an option as \option{option}.
%
% \DescribeMacro{\parameter} \marg{parameter} \AfterLastParam
% Prints out an option as \parameter{parameter}.
%
% \DescribeMacro{\person} \marg{person} \AfterLastParam
%    Print out name of a person, for example for the acknowledge
%    of their help during the development of some code.
%    \Example{\normalfont This package was written by 
%    \person{Matthias Pospiech}.}
%
% \DescribeMacro{\AfterLastParam}
%    Used in conjuction with \cs{DescribeMacro} and the printout of arguments
%    using \cs{marg} or \cs{oarg}. \cs{AfterLastParam} is inserted at the end
%    to begin a new line.
%    \Example{\cs{DescribeMacro}\arg{foo}\cs{marg}\arg{argument}
%    \cs{AfterLastParam}}
%
% \DescribeMacro{\Default}
%    \cs{Default} is used to print out the default value of a command.
%
% \DescribeMacro{\Example}
%    \cs{Example} is used to print out an example for the usage of a command. %    See documentation of \cs{AfterLastParam} for an example.
%
% \DescribeMacro{\latex}  New lower case command for printing out \latex.
%
% \DescribeEnv{Optionlist} Table to print out all possible options 
% of a command. See the following code for an example.
%
% \iffalse
%<*example>
% \fi
\begin{latexcode}
\begin{Optionlist}
 top     &  placement is on top  \\
 bottom  &  placement is at the bottom\\
\end{Optionlist}
\end{latexcode}
% \iffalse
%</example>
% \fi
% Output:
% \begin{Optionlist}
%  top     &  placement is on top  \\
%  bottom  &  placement is at the bottom\\
% \end{Optionlist}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Printing of \latex code}
%
% This package provides two \package{listings} styles for the printing of 
% \latex code: \textsl{lstDemoStyleLaTeXCode} and
% \textsl{lstDocStyleLaTeXCode}. The first is meant for printing of code
% examples, the latter only to be used inside a dtx file for the documentation
% of the package code.
%
% For convenience these style are provided with the following environments.
%
% \DescribeEnv{latexcode} Listings environment for code examples. The output of this style is shown in the example below. The lines are not numbered, so that the code can be directly copied from the PDF document.
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDemoStyleLaTeXCode]
\begin{latexcode}
% comment
Example Code \ldots
\end{latexcode}
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% In the case of a dtx file this must be wrapped in special dtx code as shown % in the following code. 
%
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDemoStyleLaTeXCode]
% \iffalse
%<*example>
% \fi
\begin{latexcode}
Example Code \ldots
\end{latexcode}
% \iffalse
%</example>
% \fi
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \DescribeEnv{macrocode} is in contrast an environment for the code in a dtx file, which is used for the creation of a latex package or class. In a dtx file therefore the special comment and indentation of the environment must be taken care of. The following example is displayed with this style.
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDocStyleLaTeXCode]
%    \begin{macrocode}
% definition of foobar
\newcommand{\foobar}{foo-bar}
%    \end{macrocode}
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \DescribeMacro{\printCodeFromFile} 
% \oarg{first line} \marg{last line} \marg{file name} \AfterLastParam
% This command is very helpful to display parts of a file sequentially.
% If the first line is not given the last line value is incremented by one 
% and used as the first line.
% \Example{\cs{printCodeFromFile}[3]\arg{6}\arg{LaTeXTemplate.tex}}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Label Link system for files and their usage}
% The system explained in this section allows to adds a hyperlink to sections
% where a file is discussed and adds the usage page to the index. The code
% is very close to the label/ref system as shown in the following example:
%
% \iffalse
%<*example>
% \fi
\begin{latexcode}
\section{about links in pdf}    
The code is in \file{preamble/hyperref.tex}
\dots
\section{preamble/hyperref.tex}
\labelfile{preamble/hyperref.tex}
\end{latexcode}
% \iffalse
%</example>
% \fi
%
% \DescribeMacro{\labelfile} \marg{filepath/filename.tex} \AfterLastParam
% Creates a label that is used by \cs{file} for linking to the 
% occurrence of \cs{labelfile}. 
% Furthermore an index entry is created with a new sub-index for every path.
% In the case of \cs{labelfile}\arg{preamble/hyperref.tex} this would look
% like:
% \iffalse
%<*example>
% \fi
\begin{lstlisting}
Files
   preamble
      hyperref.tex
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \DescribeMacro{\file} \marg{filepath/filename.tex} \AfterLastParam
% Prints out the filename in typewriter font and links to the usage if an
% equivalent \cs{labelfile} is given.
%
% ^^A =====================================================================
% \section{Implementation}
%
% \iffalse
%<*doctools.sty>
% \fi
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{doctools}[2012/12/01 v0.1 commands and packages for documenting LaTeX Code]
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Define keys}
%
%    \begin{macrocode}
%%% === Define Keys ===================================
\RequirePackage{kvoptions-patch}
\RequirePackage{kvoptions}  % options
\RequirePackage{pdftexcmds} % string comparison
\SetupKeyvalOptions{family=doctools,prefix=doctools@}
%    \end{macrocode}
% Define default option for style key: \emph{stacked}
%    \begin{macrocode}
\DeclareBoolOption[false]{loadHyperref}
\DeclareBoolOption[true]{createIndexEntries}
\DeclareBoolOption[false]{applyLayout}
\ProcessKeyvalOptions{doctools}
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Preamble}
% \subsubsection{Packages}
% Most package are loaded at the beginning of the document to avoid clashes
% with other packages and make a correct loading order possible. Since the
% packages are only available at the end of the preamble, all commands
% defined by this package are also only available at the end of the preamble.
%    \begin{macrocode}
%%% ---- Packages ----
%%% Programming
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{kvsetkeys}
%%% Font packages
\ifdoctools@applyLayout
\usepackage{cmap} 
\usepackage[T1]{fontenc} % T1 Schrift Encoding
%\usepackage{lmodern} % Font not loaded, because this can lead
%                     % to incompatibilities with other math fonts
\usepackage{textcomp}
\fi % end \ifdoctools@applyLayout
%%% listings (must be loaded before \AtBeginDocument)
\@ifpackageloaded{listings}{}{\RequirePackage{listings}}
%%% load all further packages and all commands 
%%% at the beginning of the document and thus
%%% after all other packages 
\PassOptionsToPackage{table}{xcolor}
\AtBeginDocument{%
%%
%%% Additional packages
\usepackage{xspace}
\@ifpackageloaded{xcolor}{}
 {\usepackage{xcolor}}
%%% listings
\colorlet{doc@stringcolor}{green!40!black!100}
\colorlet{doc@commentcolor}{green!50!black!100}
\colorlet{doc@numbercolor}{white!50!black!100}
\definecolor{doc@keywordcolor}{rgb}{0,0.35,1.0}
\colorlet{doc@demo@backcolor}{white}
\definecolor{doc@rulecolor}{rgb}{0.5,0.5,0.5}
%
\lstdefinestyle{lstStyleDefault}{
%%% appearance
   ,basicstyle=\small\ttfamily % Standardschrift
%%%  Space and placement
   ,floatplacement=tbp    % is used as float place specifier
   ,aboveskip=\medskipamount % define the space above and 
   ,belowskip=\medskipamount % below displayed listings.
   ,lineskip=0pt          % specifies additional space between lines in listings.
   ,boxpos=c              % c,b,t
%%% The printed range
   ,showlines=false       % prints empty lines at the end of listings
%%% characters
   ,extendedchars=true   % allows or prohibits extended characters 
                         % in listings, that means (national)
                         % characters of codes 128-255. 
   ,upquote=true         % determines printing of quotes
   ,tabsize=2,           % chars of tab
   ,showtabs=false       % do not show tabs
   ,showspaces=false     % do not show spaces
   ,showstringspaces=false % do not show blank spaces in string
%%% Line numbers
   ,numbers=none         % left, right, none
%%% Captions
   ,numberbychapter=true %
   ,captionpos=b         % t,b
   ,abovecaptionskip=\smallskipamount % the vertical space respectively above 
   ,belowcaptionskip=\smallskipamount % or below each caption
%%% Margins and line shape
   ,linewidth=\linewidth % defines the base line width for listings.  
   ,xleftmargin=0pt      % extra margins
   ,xrightmargin=0pt     %
   ,resetmargins=false   % indention from list environments like enumerate 
                         % or itemize is reset, i.e. not used.
   ,breaklines=true      % line breaking of long lines.
   ,breakatwhitespace=false % allows line breaks only at white space.
   ,breakindent=0pt     % is the indention of the second, third, ...  
                         % line of broken lines.
   ,breakautoindent=true % apply intendation
   ,columns=flexible     %
   ,keepspaces=true      %
}

\lstset{style=lstStyleDefault}

\lstdefinestyle{lstDocStyleBase}{
%%% base style
   ,style=lstStyleDefault
%%% appearance
   ,commentstyle=\slshape
%%% Line numbers
   ,numbers=left         % left, right, none
   ,stepnumber=1         % seperation between numbers
   ,numberfirstline=false % number first line always
   ,numberstyle=\tiny\color{doc@numbercolor}    % style of numbers
   ,numbersep=5pt        % distance to text
   ,numberblanklines=true %   
%%% language
   ,language = [LaTeX]TeX
%%% commands
   % LaTeX programming
   ,moretexcs={setlength,usepackage,newcommand,renewcommand,providecommand,RequirePackage,SelectInputMappings,ifpdftex,ifpdfoutput,AtBeginEnvironment,ProvidesPackage},
   % other commands
   ,moretexcs={maketitle,text,includegraphics,chapter,section,subsection,
     subsubsection,paragraph,textmu,enquote,blockquote,ding,mathds,ifcsdef,Bra,Ket,Braket,subcaption,lettrine,mdfsetup,captionof,listoffigures,listoftables,tableofcontents,appendix}
   % tables
   ,moretexcs={newcolumntype,rowfont,taburowcolors,rowcolor,rowcolors,bottomrule,
     toprule,midrule,}
   % hyperref
   ,moretexcs={hypersetup}
   % glossaries
   ,moretexcs={gls,printglossary,glsadd,newglossaryentry,newacronym}
   % Koma
   ,moretexcs={mainmatter,frontmatter,geometry,KOMAoptions,setkomafont,addtokomafont}
   % SI, unit
   ,moretexcs={si,SI,sisetup,unit,unitfrac,micro}
   % biblatex package
   ,moretexcs={newblock,ExecuteBibliographyOptions,addbibresource}
   % math packages
   ,moretexcs={operatorname,frac,sfrac,dfrac,DeclareMathOperator,mathcal,underset}
   % demo package
   ,moretexcs={democodefile,package,cs,command,env,DemoError,PrintDemo}  
   % tablestyles
   ,moretexcs={theadstart,tbody,tsubheadstart,tsubhead,tend}
   % code section package
   ,moretexcs={DefineCodeSection,SetCodeSection,BeginCodeSection,
     EndCodeSection}
   % template tools package
   ,moretexcs={IfDefined,IfUndefined,IfElseDefined,IfElseUndefined,IfMultDefined,IfNotDraft,IfNotDraftElse,IfDraft,IfPackageLoaded,IfElsePackageLoaded,IfPackageNotLoaded,IfPackagesLoaded,IfPackagesNotLoaded,ExecuteAfterPackage,ExecuteBeforePackage,IfTikzLibraryLoaded,IfColumntypeDefined,IfColumntypesDefined,IfColorDefined,IfColorsDefined,IfMathVersionDefined,SetTemplateDefinition,UseDefinition,IfFileExists,iflanguage}
   % tablestyles
   ,moretexcs={setuptablefontsize,tablefontsize,setuptablestyle,tablestyle,  setuptablecolor,tablecolor,disablealternatecolors,   tablealtcolored,tbegin,tbody,tend,thead, theadstart,tsubheadstart,tsubhead,theadrow,tsubheadrow,resettablestyle,theadend,tsubheadend,tableitemize,PreserveBackslash}
   % todonotes
   ,moretexcs={todo,missingfigure}
   % listings
   ,moretexcs={lstloadlanguages,lstdefinestyle,lstset}
   % index
   ,moretexcs={indexsetup}
   % glossaries
   ,moretexcs={newglossarystyle,glossarystyle,deftranslation,newglossary}
   % tikz
   ,moretexcs={usetikzlibrary}
   % color
   ,moretexcs={definecolor,colorlet}
   % caption
   ,moretexcs={captionsetup,DeclareCaptionStyle}
   % floatrow
   ,moretexcs={floatsetup}
   % doc.sty
   ,moretexcs={EnableCrossrefs,DisableCrossrefs,PageIndex,CodelineIndex,CodelineNumbered}
   % refereces
   ,moretexcs={cref,Cref,vref,eqnref,figref,tabref,secref,chapref}     
}

\lstdefinestyle{lstDemoStyleLaTeXCode}{ %
%%% base style
   ,style=lstDocStyleBase
%%% Line numbers
   ,numbers=none         % left, right, none
%%% colors
   ,stringstyle=\color{doc@stringcolor}
   ,keywordstyle=\color{doc@keywordcolor}
   ,commentstyle=\color{doc@commentcolor}
   ,backgroundcolor=\color{doc@demo@backcolor}
   ,rulecolor=\color{doc@rulecolor}
%%% frame 
   ,frame=single         % none, leftline, topline, bottomline, lines
                         % single, shadowbox
   ,framesep=3pt 
   ,rulesep=2pt          % control the space between frame and listing 
                         % and between double rules.
   ,framerule=0.4pt      % controls the width of the rules.
}

\colorlet{doc@code@backcolor}{gray!5}
\colorlet{doc@code@keywordcolor}{black}
\colorlet{doc@code@commentcolor}{black!60}

\lstdefinestyle{lstDocStyleLaTeXCode}{%
%%% base style
   ,style=lstDocStyleBase
%%% colors
   ,stringstyle=\color{doc@stringcolor}
   ,keywordstyle=\color{doc@code@keywordcolor} % 
   ,commentstyle=\color{doc@code@commentcolor}
   ,backgroundcolor=\color{doc@code@backcolor}
   ,rulecolor=\color{doc@rulecolor}
%%% frame 
   ,frame=none         % none, leftline, topline, bottomline, lines
                         % single, shadowbox
   ,framesep=3pt 
   ,rulesep=2pt          % control the space between frame and listing 
                         % and between double rules.
   ,framerule=0.4pt      % controls the width of the rules.
%%% numbers
   ,firstnumber=last
}

\lstloadlanguages{[LaTeX]TeX}
%%% hyperref
\ifdoctools@loadHyperref
\makeatletter
\@ifpackageloaded{hyperref}{}
   % load hyperref only if package 
   % hypdoc is not loaded, which
   % loads hyperref itself
   {\usepackage[
     ,backref=page%
     ,pagebackref=false%
     ,hyperindex=true%
     ,hyperfootnotes=false%
     ,bookmarks=true%
     ,pdfpagelabels=true%
   ]{hyperref}}   
\makeatother
%
\usepackage[]{bookmark}
%
\definecolor{pdfanchorcolor}{named}{black}
\definecolor{pdfmenucolor}{named}{red}
\definecolor{pdfruncolor}{named}{cyan}
\definecolor{pdfurlcolor}{rgb}{0,0,0.6}
\definecolor{pdffilecolor}{rgb}{0.7,0,0}
\definecolor{pdflinkcolor}{rgb}{0,0,0.6}
\definecolor{pdfcitecolor}{rgb}{0,0,0.6}
%
\hypersetup{
   ,draft=false, % all hypertext options are turned off
   ,final=true   % all hypertext options are turned on
   ,debug=false  % extra diagnostic messages are printed in the log file
   ,hypertexnames=true % use guessable names for links
   ,naturalnames=false % use LATEX-computed names for links
   ,setpagesize=true   % sets page size by special driver commands
   ,raiselinks=true    % forces commands to reflect the real height of the link 
   ,breaklinks=true    % Allows link text to break across lines
   ,pageanchor=true    % Determines whether every page is given an implicit
   ,plainpages=false   % Forces page anchors to be named by the arabic
   ,linktocpage=true   % make page number, not text, be link on TOC, LOF and LOT
   ,colorlinks=true    % Colors the text of links and anchors.
   ,linkcolor  =pdflinkcolor   % Color for normal internal links.
   ,anchorcolor=pdfanchorcolor % Color for anchor text.
   ,citecolor  =pdfcitecolor   % Color for bibliographical citations in text.
   ,filecolor  =pdffilecolor   % Color for URLs which open local files.
   ,menucolor  =pdfmenucolor   % Color for Acrobat menu items.
   ,runcolor   =pdfruncolor    % Color for run links (launch annotations).
   ,urlcolor   =pdfurlcolor    % color magenta Color for linked URLs.
   ,bookmarksopen=true     % If Acrobat bookmarks are requested, show them
   ,bookmarksopenlevel=2   % level (\maxdimen) to which bookmarks are open
   ,bookmarksnumbered=true %
   ,bookmarkstype=toc      %
   ,pdfpagemode=UseOutlines %
   ,pdfstartpage=1         % Determines on which page the PDF file is opened.
   ,pdfstartview=FitH      % Set the startup page view
   ,pdfremotestartview=Fit % Set the startup page view of remote PDF files
   ,pdfcenterwindow=false  %
   ,pdffitwindow=false     % resize document window to fit document size
   ,pdfnewwindow=false     % make links that open another PDF file 
   ,pdfdisplaydoctitle=true  % display document title instead of file name 
} % end: hypersetup
%
\fi % end \ifdoctools@loadHyperref
%    \end{macrocode}
%
% \subsubsection{color setup}
%
%    \begin{macrocode}
%%% color setup
% table colors 
\ifcsdef{colorlet}{%
\colorlet{tablebodycolor}{white!100}
\colorlet{tablerowcolor}{gray!10}
\colorlet{tableheadcolor}{gray!25}
}{}
%    \end{macrocode}
%
% \subsubsection{Document layout / variables}
%
%    \begin{macrocode}
%%% Set document layout / variables
\ifdoctools@applyLayout%
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{0.5\baselineskip}
  \setcounter{secnumdepth}{2}
  \setcounter{tocdepth}{2}
\fi % end \ifdoctools@applyLayout%
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Internal Variables}
%
%    \begin{macrocode}
%%% ---- Internal Variables ---- 
%% Font for Index Headings
\newcommand{\doctools@indexHeadFont}[1]{\textsc{#1}}
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Commands}
%
% \begin{macro}{\bs}
% Shortcut for \cs{textbackslash}.
%    \begin{macrocode}
%%% ---- Commands ---- 
%% \bs
\newcommand{\bs}{\textbackslash}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\command}
% Prints the argument with backslash in typewriter font. 
% This is meant to be used for \LaTeX{} commands.
% Additionally the command is added to the index.
%    \begin{macrocode}
%% \command
\newcommand{\command}[1]{%
\texttt{\textbackslash{}#1}\relax%
\ifdoctools@createIndexEntries%
  \index{\doctools@indexHeadFont{Command}!\textbackslash{}#1}%
\fi%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cs}
% Shortcut for \cs{command}
%    \begin{macrocode}
%% \cs (shortcut for \command)
%% \cs might be defined by ltxdoc, therefore it needs to be deleted
%% before it can be redefined.
\ifcsdef{cs}{\csundef{cs}}{}%
\let\cs\command%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\environment}
% Formats an environment in typewriter font and adds it to the index. 
%    \begin{macrocode}
%% \environment
\ifcsdef{environment}{\csundef{environment}}{}
\newcommand{\environment}[1]{%
\texttt{#1}%
\ifdoctools@createIndexEntries
  \index{\doctools@indexHeadFont{Environment}!#1}%
\fi
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\env}
% Shortcut for \cs{environment}
%    \begin{macrocode}
%% \env
\newcommand{\env}[1]{\environment{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\package}
% Formats an package in typewriter font and adds it to the index. 
%    \begin{macrocode}
%% \package
\newcommand{\package}[1]{%
\texttt{#1}%
\ifdoctools@createIndexEntries
\index{\doctools@indexHeadFont{Package}!#1}%
\fi
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ltxclass}
% Formats a \latex class in typewriter font and adds it to the index. 
%    \begin{macrocode}
%% \ltxclass
\newcommand{\ltxclass}[1]{%
\texttt{#1}%
\ifdoctools@createIndexEntries
\index{\doctools@indexHeadFont{Class}!#1}%
\fi
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marg}
% Formats a mandatory argument of a command. If \cs{meta} is defined this is reused.
%    \begin{macrocode}
%% \marg
\ifcsdef{marg}{}{% if not defined
\ifcsdef{meta}%
  {\newcommand\marg[1]{\texttt{\{}\meta{#1}\texttt{\}}}}%
  {\newcommand\marg[1]{%
\texttt{\{}%
$\langle${\normalfont\slshape#1}$\rangle$%
\texttt{\}}}}%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\oarg}
% Formats a optional argument of a command. If \cs{meta} is defined this is reused.
%    \begin{macrocode}
%% \oarg
\ifcsdef{oarg}{}{% if not defined
\ifcsdef{meta}%
  {\newcommand\oarg[1]{\texttt{[}\meta{#1}\texttt{]}}}%
  {\newcommand\oarg[1]{%
\texttt{[}%
$\langle${\normalfont\slshape#1}$\rangle$%
\texttt{]}}}%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\arg}
% Formats an argument of a command without extra angle brackets 
% in curled brackets in monospaced font.
%    \begin{macrocode}
%% \arg
\ifcsdef{arg}{%
  \csundef{arg}%
  \newcommand\arg[1]{\{\texttt{#1}\}}%
}{}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\option}
% Formats an option in typewriter font and adds it to the index. 
%    \begin{macrocode}
%% \option
\newcommand{\option}[1]{%
\texttt{#1}%
\ifdoctools@createIndexEntries%
\index{option!#1}%
\fi
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\parameter}
% Formats a parameter in typewriter font. 
%    \begin{macrocode}
%% \parameter
\newcommand{\parameter}[1]{%
\texttt{#1}%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\person}
%    Print out name of a person, for example for the acknowledge
%    of their help during the development of some code.
%    \begin{macrocode}
%% \person
\newcommand{\person}[1]{\textsc{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AfterLastParam}
%    Used in conjuction with \cs{DescribeMacro} and the printout of arguments
%    using \cs{marg} or \cs{oarg}. \cs{AfterLastParam} is inserted at the end
%    to begin a new line.
%    \begin{macrocode}
%% \AfterLastParam
\newcommand{\AfterLastParam}{\par\noindent}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Default}
%    \cs{Default} is used to print out the default value of a command.
%    \begin{macrocode}
%% \Default
\newcommand{\Default}[1]{\par Default: \texttt{#1} \par}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Example}
%    \cs{Example} is used to print out an example for the usage of a command.
%    \begin{macrocode}
%% \Example
\newcommand{\Example}[1]{\par Example: \texttt{#1} \par}
%    \end{macrocode}
% \end{macro}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Option list}
%
% \begin{macro}{Optionlist}
%    Environment
%    \begin{macrocode}
\newenvironment{Optionlist}{%
\begin{flushleft}%
\vspace{-2\parskip}
%%  Style  changes
\small\renewcommand{\arraystretch}{1.4}%
%%  table
\begin{tabular} {>{\ttfamily}l<{\normalfont}p{0.7\textwidth}}%
}{% end of environment
\end{tabular}%
\end{flushleft}%
}%
%    \end{macrocode}
% \end{macro}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{\LaTeX{} engine names}
%
% \begin{macro}{\latex}
%    New lower case command for printing out \LaTeX{}
%    \begin{macrocode}
%%% ---- engine names ----
%% \latex
% ensure that space is added after \latex
\newcommand{\latex}{\LaTeX\xspace}
%    \end{macrocode}
% \end{macro}
%
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{doc.sty modifications}
% The following commands from doc.sty are changed by
% introducing colors to the commands
%    \begin{macrocode}
%%% ---- doc.sty modifications ----
% define color for Macro and Environment names
\colorlet{doctools@ColorCodeNames}{blue!50!black}
%%% Overwrite font for \meta
\def\meta@font@select{\normalfont\slshape} % original: \itshape
%
\ifcsdef{PrintMacroName}
   {\def\PrintMacroName#1{\strut \MacroFont %
   \color{doctools@ColorCodeNames}\string #1\ }}{}
%
\newcounter{MacroName} % hyperref uses \theH<counter>
\providecommand*{\theHMacroName}{\theMacroName}
%
%% create label 
%   \renewcommand*{\theHMacroName}{#1}%
%   \ifcsdef{phantomsection}{\phantomsection}{}%
%   \@bsphack%
%   \refstepcounter{MacroName}%
%   \label{doc:desc:#1}%   
\ifcsdef{PrintDescribeMacro}
   {\def\PrintDescribeMacro#1{%
   \strut \MacroFont %
   \color{doctools@ColorCodeNames} \string #1\ }}{}
\ifcsdef{PrintDescribeEnv}
   {\def\PrintDescribeEnv#1{\strut \MacroFont %
   \color{doctools@ColorCodeNames} #1\ }}{}
\ifcsdef{PrintEnvName}
   {\def\PrintEnvName#1{\strut \MacroFont %
   \color{doctools@ColorCodeNames} #1\ }}{}
%
%%% disable the index preamble if index entries are generated by this package
\ifdoctools@createIndexEntries
\ifcsdef{index@prologue}
     {\def\index@prologue{\section*{Index}\markboth{Index}{Index}}}
     {}
\fi
%     
%    \end{macrocode}
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Listings environments}
%
%    \begin{macrocode}
%%% ---- listings environments for LaTeX code
%%% Overwriting the environment macrocode for printing the code in a dtx file.
\ifcsdef{macrocode}{\csundef{macrocode}}{}
\lstnewenvironment{macrocode}{\lstset{style=lstDocStyleLaTeXCode}}{}
%    \end{macrocode}
%
%    \begin{macrocode}
%%% environment code examples.
\lstnewenvironment{latexcode}{\lstset{style=lstDemoStyleLaTeXCode}}{}
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{Print LaTeX code from external file using listings}
%
%    \begin{macrocode} 
% -------------------------------------------------
% \printCodeFromFile
% -------------------------------------------------
\newcounter{lstFirstLine}
\newcounter{lstLastLine}
\setcounter{lstLastLine}{0}
\setcounter{lstFirstLine}{0}
%
\newcommand{\printCodeFromFile}[3][]{%
\ifstrempty{#1}{}{%
  \setcounter{lstFirstLine}{#1}%
}%
\setcounter{lstLastLine}{#2}%
%
\lstinputlisting[%
  firstnumber=\thelstFirstLine,%
  firstline=\thelstFirstLine,%
  lastline=\thelstLastLine,%
  nolol=true,
  style=lstDemoStyleLaTeXCode]%
  {#3}%
%
%% set counter to lastLine + 1
\setcounter{lstFirstLine}{\thelstLastLine}
\addtocounter{lstFirstLine}{1}
}
%    \end{macrocode}
%
% ^^A ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \subsection{References and links to files}
%
%    \begin{macrocode}
% -------------------------------------------------
% \file and \labelfile
% -------------------------------------------------
% Code copied from http://tex.stackexchange.com/
%  questions/65639/how-to-create-my-on-ref-label-system/
% with later modifications.
% Thanks to Heiko Oberdiek for providing this answer !
% -------------------------------------------------
%% Command for printing the filename
\ifcsdef{urlstyle}{}{\usepackage{url}}
\DeclareUrlCommand{\PrintFileName}{\urlstyle{tt}}
%%
\newcounter{file}
%% hyperref uses \theH<counter>
\providecommand*{\theHfile}{\thefile}
%% code from tex.stackexchange with the 
%% the help from Heiko Oberdiek.
\newcommand*{\labelfile}[1]{%
  %% convert all "/" to "/," (comma list) and save in \IndexFileA
  \StrSubstitute{#1}{/}{/,}[\IndexFileA]%
  %% define \IndexFileB as empty (used for the output string)
  \let\IndexFileB\@empty
  %% parse and convert \IndexFileA using \@AddFileEntry
  \expandafter
  \comma@parse@normalized\expandafter{\IndexFileA}\@AddFileEntry
  %% create label and print to index
  \@bsphack
  \renewcommand*{\theHfile}{#1}%
  \refstepcounter{file}%
  \ifcsdef{phantomsection}{\phantomsection}{}
  \label{file:#1}%
  \ifdoctools@createIndexEntries
  \index{\textsc{Files}!\IndexFileB}%
  \fi
  \@esphack  
}
%% add entries of comma list to the index.
\newcommand*{\@AddFileEntry}[1]{%
  \ifx\IndexFileB\@empty
    %% add first entry to index with lexEntry@{printEntry}
    \def\IndexFileB{#1@#1}%
  \else
    \expandafter\def\expandafter\IndexFileB\expandafter{%
    %% add following entries to index with 
    %% previousEntries!lexEntry@{printEntry}
\IndexFileB!%
#1@#1%
    }%
  \fi
}
%% Print out filename and create a link if the label exists.
\newcommand*{\file}[1]{%
  \ifcsdef{hyperref}%
    {\hyperref[file:#1]{\PrintFileName{#1}}}%
    {\PrintFileName{#1}}%
}% 
%%
} % end of \AtBeginDocument 

%    \end{macrocode}
%
%
%
% \iffalse
%</doctools.sty>
% \fi
% \clearpage
% \Finale
\endinput