summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/indextools/indextools.dtx
blob: b88e96682ef8f1fdbe7e38fb22b560f019fb95e0 (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
% \iffalse
%<*internal>
\begingroup
\input docstrip.tex
\keepsilent
\usedir{tex/latex/indextools}
\preamble
  ___________________________________________________________
  The indextools package for LaTeX
  Copyright (C) 2010-2013 Enrico Gregorio
  Copyright (C) 2014-* Maïeul Roquette
  All rights reserved

  License information appended

\endpreamble
\postamble

Copyright 2010--2013 Enrico Gregorio <Enrico dot Gregorio at univr dot it>

Copyright 2014 Maïeul Rouquette <maieul at maieul dot net>

Distributable under the LaTeX Project Public License,
version 1.3c or higher (your choice). The latest version of
this license is at: http://www.latex-project.org/lppl.txt

This work is "author-maintained"

This work consists of this file indextools.dtx, a README file
and the derived files indextools.sty and indextools.pdf.

\endpostamble
\askforoverwritefalse

\generate{\file{indextools.sty}{\from{indextools.dtx}{package}}}

\def\tmpa{plain}
\ifx\tmpa\fmtname\endgroup\expandafter\bye\fi
\endgroup
%</internal>
%
%<*driver>
\ProvidesFile{indextools.dtx}%
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
%<package>\ProvidesPackage{indextools}%
%<*package>
   [2015/11/22 v1.5.1 Package for typesetting indices in a synchronous mode]
%</package>
%<*driver>
\documentclass{ltxdoc}
\def\prog#1{\textsf{\slshape#1}}
\def\progpdflatex{\textsf{(\textsl{pdf})\textsl{latex}}}
\def\eTeX{\mbox{$\varepsilon$-\TeX}}
\def\Kvalue#1{\textsf{\textsl{#1}}}
\def\pack#1{\textsf{#1}}
\def\class#1{\textsf{\itshape#1}}
\def\opz#1{\texttt{#1}}
\usepackage{multicol}
\usepackage[colorlinks]{hyperref}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{textcomp,metalogo,booktabs}
\begin{document}
\frenchspacing
\makeatletter
\GetFileInfo{indextools.dtx}% dtx extension required
\title{The package \pack{indextools}\thanks{Version number \fileversion; last
 revision \filedate.}}
\author{Maïeul Rouquette\thanks{\texttt{maieul at maieul dot net}} \\ from Enrico Gregorio's work}
\date{}
\maketitle
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\smallskip
\RecordChanges
\DocInput{indextools.dtx}
\end{document}
%</driver>
% \fi
% \CheckSum{891}
%
% \changes{v1.0}{2010/07/10}{First public version}
% \changes{v1.0a}{2010/07/12}{Small bug correction}
% \changes{v1.1}{2012/05/09}{Fixed compatibility with \pack{memoir}}
% \changes{v1.1}{2012/05/09}{Modified interaction with Lua\TeX}
% \changes{v1.1a}{2012/09/07}{Fixed bug with possibly defined \cs{directlua}}
% \changes{v1.2}{2012/08/31}{made package compatible with showidx}
% \changes{v1.2}{2012/08/31}{made package compatible with fancyhdr}
% \changes{v1.2}{2012/08/31}{added index processing engine option truexindy}
% \changes{v1.2c}{2012/10/02}{Fixed regression}
% \changes{v1.2d}{2012/10/06}{Fixed bugs with index internal names}
% \changes{v1.2e}{2012/10/22}{Fixed bug with showidx simulated by memoir}
% \changes{v1.3}{2013/07/11}{Added internal so as to cooperate in a better
% way with eledmac (by Maïeul Rouquette)}
% \changes{v1.4}{2015/01/23}{Compatibility with bidi. Fork from imakeidx. New name indextools}
% \changes{v1.5}{2015/11/15}{New option `afterindex'}
% \changes{v1.5}{2015/11/15}{New option `innote'}
% \changes{v1.5}{2015/11/15}{New option `notenumber'}
% \changes{v1.5.1}{2015/11/22}{Fix bug with options `notenumber' and `innote' when using \cs{index} without optional argument}
%
% \begin{abstract}
% This package is a fork of the \pack{imakeidx} package. The original authors
% of \pack{imakeidx} were Claudio Beccari and Enrico Gregorio; the first author withdrew his availability as a maintainer in 2013. The second author declined
% some upgrade suggestions  to remove some incompatibilities with certain
% packages that are particularly important with critical editions dealing with
% languages that are being written from right to left.
% Therefore this fork, named \pack{indextools}, was created by Maïeul Rouquette,
% who acknowledges the work of the two preceding authors thanking them for the
% excellent package they created; nevertheless this fork was necessary in order
% to let other users benefit from its functionalities very useful in the field
% of humanities.
%
% We hope that one day, only one package will be provided.
% The differences with the \pack{imakedix} are:
% \begin{itemize}
% 	\item the name;
% 	\item the compatibility with package \pack{bidi};
%	\item this documentation.
% \end{itemize}
% This package replaces and extends the functionalities of \pack{imakeidx},
% therefore  it MUST not be loaded at the same time as \pack{imakeidx}. Some 
% controls are implemented to avoid these situations; if loading of
% \pack{indextools} is required after \pack{imakeidx} is already loaded,
% it does not get loaded; at the same time, if it gets loaded, it sets a
% suitable macro so that \pack{imakeidx} can't be loaded after
% \pack{indextools}.
%
% This package exploits the |\write18| facility of modern \TeX\ system
% distributions that allows to run system commands while typesetting a
% document written with the \LaTeX{} mark up.  By so doing, the index
% or indices, that are usually typeset at the very end of the
% document, are possibly split and sorted so as to include them in the
% document itself. This process has some minor limitations: it's
% impossible to start an index before all other pages have been
% ejected.
%
% For support, please open issues on:\\[1ex] 
% \makebox[\linewidth]{\url{https://github.com/maieul/indextools}}
% \end{abstract}
%
% \section{Introduction}
% It's been some years now that the typesetting engine of the \TeX\
% system is just \prog{pdftex}; the original Knuthian \prog{tex} is
% still corrected by D.\,E.~Knuth himself, but is frozen, according to
% his will; it is still distributed by every \TeX\ distribution, but
% in practice \prog{pdftex}, \prog{xetex} or \prog{luatex} are the most used 
% interpreters  and typesetting engines of most macro packages.
%
% This program \prog{pdftex} was originally born with the facility of
% producing either a pdf output file, as its name suggests, or a dvi
% file. Since then it has been enriched with many upgrades, also with
% regard to the evolution of the PDF language itself. It also
% incorporates the extensions of \eTeX\ and has the ability to open a
% shell so as to run system commands with their arguments. The same
% is true for \prog{xetex} and \prog{luatex}.
% 
% This facility, since the \TeX~Live 2010 distribution, is official,
% but is sort of restricted, in the sense that the \TeX\ system
% configuration file contains a list of ``safe'' system commands that
% can be run by \prog{pdftex}; presently the only program relevant for
% this package is \prog{makeindex}. This precaution is necessary in
% order to avoid running malicious code. Other programs can be run,
% though, but it's necessary to expressly tell \prog{pdftex} that it
% can do so; this authorisation is given by means of a suitable
% program option, as explained below.
%
% This package will exploit this facility in order to run a perl
% script that is capable of splitting a raw index file into different
% chunks and to run the \prog{makeindex} or \prog{xindy} \TeX\ system
% programs so as to sort and format the index entries according to a
% specified index style file. Once the shell is terminated, the
% typesetting program resumes its work and possibly prints the various
% formatted indices produced in previous steps. In this way the document
% indices are always synchronous with their document and no further
% typesetting runs are necessary.
%
% In order to reach this goal, it is necessary that at least the
% restricted |write18| facility is enabled; if the \TeX\ distribution
% in use does not enable this restricted feature by default, it
% is necessary to enable the typesetting engine to run such facility;
% depending on the distribution and the shell editor that is being
% used to work on a specific document, it is necessary to add
% \texttt{-\/-shell-escape} (or \texttt{-\/-enable-write18} for MiK\TeX)
% to the command with which the typesetting program is launched,
% possibly through the shell editor. This applies to all three
% \prog{pdftex}, \prog{xetex}, and \prog{luatex} typesetting engines.
%
% {\medskip\footnotesize If Lua\LaTeX{} is used and \prog{luatex} is
% version 0.42 to 0.66, it's impossible to distinguish whether the
% restricted shell escape is active or not, so the automatic procedure
% will be tried anyway, unless disabled with the \texttt{noautomatic}
% package option. With version 0.68 or later, the behaviour is the same
% as with the other engines.\par}
%
%
%\section{Package usage}
% This package is invoked as usual by means of a \cs{usepackage} command:
% \begin{flushleft}
% \cs{usepackage}\oarg{options}\texttt{\{indextools\}}
% \end{flushleft}
%
% The available \meta{options} consist in a comma separated list of
% the following options:
% \begin{description}\def\Item[#1]{\item[\normalfont\ttfamily#1]}
% \Item[makeindex] in order to use the \prog{makeindex} sorting and
% formatting engine; this option is the default and is mutually
% exclusive with the next option.
% \Item[xindy] in order to use the \prog{xindy} sorting and formatting
% engine; \texttt{texindy} is an alias for \texttt{xindy} and actually
% it's the script \prog{texindy} which is called by this package.
% Nevertheless if the real \prog{xindy} is desired, in order to avoid
% the settings made up by \prog{texindy}, so as to add \prog{xindy}
% the command line specific settings, it is possible to specify the
% option \texttt{truexindy}; the user is then responsible to set up
% the \prog{xindy} engine with the suitable command line options.
% \Item[noautomatic] disables the automatic splitting of the raw
% index files and running of the system programs; this option might
% be used to save time when one knows for sure that the index files
% are already OK and do not need to be refreshed. Actually the time
% spent in splitting, sorting and formatting is so short that this
% option might be useful only when very lengthy indices are being
% processed.
% \Item[nonewpage] inhibits the new page command to be issued when
% using an article type document class and multiple indices are being
% typeset. We don't see why someone would use multiple indices in an
% article (except possibly for package documentations, which usually
% provide a macro index and a list of changes).
% \Item[quiet] suppresses all messages about manual index processing.
% \Item[original] uses the class-provided \texttt{theindex}
% environment for typesetting the indices; it is implicitly set if the
% document class option \texttt{twocolumn} has been specified.
% \Item[afterindex] allows to use \cs{index} command after \cs{printindex}.
% If this case, the index will be printed only on the secund run.
% \Item[innote] adds a mark for content indexed in note. The standard mark is an italic `n' added after the page number. You can redefine the \cs{innnote} command to modify the standard mark.
% The default value is:\\
% \begin{verbatim}
% \providecommand{\innote}[1]{#1\textit{n}}
% \end{verbatim}
% \Item[notenumber] adds the note number for content indexed in note. The standard marks is a `n.' followed by the note number.
% You can redefine \cs{innnotenumber} to modify the standard mark.
% The default value is:\\
% \begin{verbatim}
% \providecommand{\innotenumber}[2]{#2n.~#1}
% \end{verbatim}
% \Item[splitindex] calls the \prog{splitindex} script by Markus Kohm,
% which is included in every \TeX~Live distribution since 2009. With
% this option all index entries, which are written in one raw index
% file, are successively split into all the requested index files;
% in this way there is virtually no limit on the number of indices
% that is possible to create for a particular document.
% \end{description}
%
% The last described option deserves an explanation. \LaTeX\ can
% write on a limited number of files during a run, and some of these
% \emph{output streams} are already reserved (among these: aux files,
% toc files, lof files, lot files, plus several other ones). When more than
% one index is produced, there's the risk to run off the number of
% writable files, because normally \pack{indextools} reserves an output
% stream for each index.  So the \texttt{splitindex} option comes to
% rescue: with it only \emph{one} raw index file is written out. At the
% first \cs{printindex} command, the program \prog{splitindex} is
% called; it splits the large index file into as many parts as the
% number of requested indices; after this, \prog{makeindex} or
% \prog{xindy} can do their job. In this way only one output stream is
% needed during the \LaTeX\ run.
%
% When should you apply this option, then? With one index it's
% useless, you should begin to consider it for two or more indices and
% definitely use it if you get the error message
% \begin{verbatim}
% ! No room for a new \write
% \end{verbatim}
% Apart from this case, with or without it, the results are the
% same. See section~\ref{sec:wrong} to see what files are written
% during the \LaTeX\ run with or without the option.
%
% \section{Specific package commands}
% As it is customary when just one index is produced, the standard
% \LaTeX\ facilities, i.e.\ the commands \cs{makeindex}, \cs{index},
% and \cs{printindex} must be used. This package redefines them so as
% to produce multiple indices and defines some other commands. The first three
% of the following commands may be used only in the preamble.
% \begin{description}\def\Item[#1]{\item[\normalfont\ttfamily\cs{#1}]}
% \Item[makeindex] with the syntax:\\[1ex]
% \cs{makeindex}\oarg{key-values}\\[1ex]
% where \meta{key-values} is a comma separated list of key-value
% assignments of the form: \texttt{key=value}; the available
% keys are the following:
% \begin{description}\def\Item[#1]{\item[\normalfont\sffamily\textsl{#1}]}
% \Item[name] is the symbolic name for an index; if this key is not
% specified, it defaults to the value of the \cs{jobname} control
% sequence, in other words the name of the current main \texttt{.tex}
% file, i.e., the file that |\input|s and/or |\include|s all the files
% of the complete document. This symbolic name is necessary only when
% doing multiple indices and is used with the \cs{index}
% command to point to the right index.\\*
% Example: \texttt{name=nameidx}
% \Item[title] is the title that is typeset at the beginning of the
% specific index; if not specified, the \cs{indexname} value is
% used.\\* Example: \texttt{title=Index of names}.
% \Item[program] is the name of the system program that is used to
% sort and format an index; valid choices are \prog{makeindex},
% \prog{xindy}, or \prog{texindy}, plus \prog{truexindy}. If not
% specified the program specified among the package options is used.
% If no option is specified, \prog{makeindex} is used. In order to
% use \prog{xindy}, it's necessary to call \prog{pdflatex} with
% the shell escape command line option.\\* Example: \texttt{program=xindy}.
% \Item[options] is the list of options to be passed to the sorting
% and formatting program; this list is a balanced text of program
% options, separated with the syntax required by the sorting and
% formatting program. For example, in order to use a different
% \prog{makeindex} sorting and formatting style \pack{mystyle.ist} and
% avoiding any message in the screen output, write \texttt{options=-s
% mystyle}.
% \Item[noautomatic] is a boolean key that defaults to \texttt{false};
% you can set it to \texttt{true} by simply listing its key in the
% key-value list, without necessarily specifying the \texttt{=true}
% part. If specified the index sorting program won't be called during
% the typesetting run for this particular index.
% \Item[intoc] is a boolean variable that defaults to \texttt{false};
% if you want to set it \texttt{true} you must simply list this key in
% the key-value list, with no need of specifying the \texttt{=true}
% part. By setting this key to \texttt{true} an entry for this
% particular index is put in the table of contents.
% \Item[columns] accepts an integer representing the number of columns
% in the index; this is silently ignored if the \texttt{original} or
% the \texttt{twocolumn} options are set; the number can even
% be~$1$.\\* Example: \texttt{columns=3}
% \Item[columnsep] accepts a dimension representing the separation
% between index columns; the default is $35$\,pt as in the standard
% classes.\\* Example: \texttt{columnsep=15pt}
% \Item[columnseprule] is a boolean option; if it is set, a rule will appear
% between the index columns.
% \end{description}
% \Item[indexsetup] with the syntax:\\[1ex]
% \cs{indexsetup}\marg{key-values}\\[1ex]
% where again \meta{key-values} is a comma separated list of \texttt{key=value}
% assignments; the available keys are:
% \begin{description}\def\Item[#1]{\item[\normalfont\sffamily\textsl{#1}]}
% \Item[level] whose acceptable values are sectioning commands such as
% \cs{chapter} or \cs{chapter*}. Actually any command with an argument
% will do and will receive the index title as its argument. The
% default is \cs{chapter*} or, if the class doesn't provide chapters,
% \cs{section*}. If you specify |\chapter| so as to override the default
% starred command |\chapter*|, the index title goes directly to the table
% of contents; in this case do not specify the \texttt{intoc} option.
% ^^A
% \Item[toclevel] whose acceptable value is a sectioning command
% \emph{name} such as \texttt{section} to indicate the level at which
% we want the indices appear in the table of contents.
% ^^A
% \Item[noclearpage] is a boolean option; when set, no \cs{clearpage}
% will be issued between indices. You might want to set it in order to
% have a `chapter of indices'; in this case you are responsible for
% setting the right value of the above keys. For example
%\begin{verbatim}
% \indexsetup{level=\section*,toclevel=section,noclearpage}
% ...
% \chapter*{Indices}
% \printindex
% \printindex[names]
% \printindex[objects]
%\end{verbatim}
% See more on this subject in section~\ref{sec:hints}
% ^^A
% \Item[firstpagestyle] whose acceptable value is a page style, default
% \texttt{plain}. You might want to set it to \texttt{empty} or some
% other page style defined by the class or by yourself. This keyword
% is disabled when the package \pack{fancyhdr} is loaded; any definition
% or choice of page styles must be done before typesetting the indices. 
% ^^A
% \Item[headers] which takes two values: the left and right marks. You
% might want to use this for disabling automatic uppercasing, by
% saying, for example, |headers={\indexname}{\indexname}|; notice that
% these values should always be a pair of balanced braced texts.
% Don't use these keys if you use \pack{fancyhdr}.
% ^^A
% \Item[othercode] whose acceptable value is arbitrary \TeX\ code that
% will be executed at the beginning of index entries typesetting. For
% example you might want to change here the setting of \cs{parskip}.
% \end{description}
% ^^A
% \Item[splitindexoptions] must have as its argument the command line
% option to \prog{splitindex}; this might be necessary on some
% systems. The default is |-m ""|, because we want it only for
% splitting the large index file into its components which are later
% processed by this package. See the \prog{splitindex} documentation
% for other options.
% ^^A
% \Item[index] with the syntax:\\[1ex]
% \cs{index}\oarg{name}\marg{entry}\\[1ex]
% inserts \meta{entry} in the raw index file named \meta{name}; if only
% one raw index file is being built in order to split it in different index
% files by means of  \prog{splitindex}, the \meta{entry} is listed in the
% specific slit index file with name \meta{name}; if no name is specified,
% this \meta{entry} is added to the default index with name
% \cs{jobname}. The \meta{entry} should be written according to the
% particular syntax of the sorting and formatting program.
% ^^A
% \Item[indexprologue] with the syntax:\\*[1ex]
% \cs{indexprologue}\oarg{spacing}\marg{text}\\[1ex]
% is used to define some \meta{text} to go between the index header and the
% entries; the \meta{spacing} should be a vertical space command such
% as |\vspace{36pt}| (default is \cs{bigskip}), controlling the
% spacing between the prologue and the index proper. The command
% affects only the next index produced by \cs{printindex} and is best
% placed just before this command. Please read ahead for further
% information on the use of this command.
% ^^A
% \Item[printindex] with the syntax:\\*[1ex]
% \cs{printindex}\oarg{name}\\[1ex]
% is used to typeset the particular index named \meta{name}; if no
% optional argument is specified, the default index with name
% \cs{jobname.ind} is typeset.  Actually this command activates all
% the mechanism of closing the output to the raw index file, shelling
% out, possibly calling the \prog{splitindex} script in order to
% divide the single raw file generated by the typesetting engine into
% distinct raw files according to the default or specified
% \meta{name}s for each index, calling the sorting and
% formatting program on each of these split raw files (unless
% inhibited by a \Kvalue{noautomatic} option; in which case a warning
% is issued in order to remember the typesetter that this particular
% index has not been processed), producing the sorted and formatted
% \texttt{.ind} files, and eventually inputs and typesets these
% formatted files. \ Deep breath.
% \end{description}
%
% Let's see an example. The sequence of commands
%\begin{verbatim}
% ...
% \usepackage{indextools}
% ...
% \makeindex[title=Concept index]
% \makeindex[name=persons,title=Index of names,columns=3]
% ...
% \begin{document}
% ...
% ...relativity\index{relativity}...
% ...
% ... Einstein\index[persons]{Einstein, Albert}...
% ...
% And this is the end of the story.
%
% \printindex
%
% \indexprologue{\small In this index you'll find only
%   famous people's names}
% \printindex[persons]
% \end{document}
%\end{verbatim}
% will produce two indices. Entries for either index must be typed as
% shown above.  The prologue will be printed (full text width) only
% in the ``Index of names'', which will be typeset in three columns.
%
% When the \texttt{original} option is set, maybe implicitly because
% of \texttt{twocolumn}, \cs{indexsetup} and the keys
% \Kvalue{columns}, \Kvalue{columnsep} and \Kvalue{columnseprule} for
% \cs{makeindex} have no effect. Please read more on this matter further~on.
%
% \section{If something goes wrong}\label{sec:wrong}
% Since \pack{indextools} relies on good cooperation between package
% options and command line options for the \LaTeX\ run, in some cases
% it may happen that the indices are not correctly built or built at
% all.
%
% If you use only \prog{makeindex} and \TeX~Live 2010 or later, then
% you shouldn't need anything special, since \prog{makeindex} is among
% the safe programs allowed to be called during a \LaTeX\ run, be it
% \prog{latex}, \prog{pdflatex}, \prog{xelatex}, or \prog{lualatex}.
%
% When the options \texttt{splitindex}, \texttt{xindy},
% \texttt{texindy} or \texttt{truexindy} are specified (globally or
% locally), the \LaTeX\ run should be called with the unrestricted
% \texttt{-\/-shell-escape} (which is \texttt{-\/-enable-write18} for
% MiK\TeX) typesetting program  option or the \texttt{noautomatic}
% option should be specified when loading \pack{indextools}.
% 
% Let's look at a couple of examples. In both we suppose that the
% document \texttt{mybook.tex} defines two indices through
%\begin{verbatim}
% \makeindex[...]
% \makeindex[name=secondary,...]
%\end{verbatim}
% where |...| denotes possible options excluding \texttt{name}.
%
% First of all we examine the case when \pack{indextools} is called
% \emph{without} \texttt{splitindex}. Two files called
% \texttt{mybook.idx} and \texttt{secondary.idx} will be written
% during the \LaTeX\ run. At the corresponding \cs{printindex}
% command, \prog{makeindex} will act on each of them producing the files
% \texttt{mybook.ind}, \texttt{mybook.ilg}, \texttt{secondary.ind} and
% \texttt{secondary.ilg}. The \texttt{.ind} files contain the relevant
% \texttt{theindex} environments with alphabetised entries, while in
% the \texttt{.ilg} files \prog{makeindex} will write its log. You can
% check in \texttt{mybook.log} whether the \prog{makeindex} run has
% been executed by searching for a line
%\begin{verbatim}
% runsystem(makeindex <...>)...executed
%\end{verbatim}
% where |<...>| stands for the rest of the command line in the
% particular case. If this line is not present, then \prog{makeindex}
% has not been called; this happens when you didn't specify the shell
% escape command line option for the \LaTeX\ run or the restricted
% shell escape is not active; also, of course, if you set the
% \texttt{noautomatic} option for the index.
%
% {\tolerance=9999
% When using \prog{splitindex}, the situation is different. During the
% \LaTeX\ run, only a large index file called \texttt{mybook.idx} file
% gets written; the first \cs{printindex} command will call
% \prog{splitindex} (unrestricted shell escape \emph{must} be active),
% which will produce the two partial index files \texttt{mybook-mybook.idx}
% and \texttt{mybook-secondary.idx}. These two files will be processed by
% \prog{makeindex} producing the four files \texttt{mybook-mybook.ind},
% \texttt{mybook-mybook.ilg}, \texttt{mybook-secondary.ind} and
% \texttt{mybook-secondary.ilg}.
% The line
%\begin{verbatim}
% runsystem(splitindex <...>)...executed
%\end{verbatim}
% in \texttt{mybook.log} will tell that the splitting has been done
% (see later on if this doesn't seem true). In table~\ref{tab:files} you
% can see what files are produced when the first two lines are in the
% preamble.\par}
%
% \begin{table}[tp]
% \centering\begingroup\ttfamily
% \begin{tabular}{rll}
% \toprule
% \multicolumn{3}{c}{\ttfamily\cs{makeindex}\hphantom{[name=secondary]}}\\
% \multicolumn{3}{c}{\ttfamily\cs{makeindex}[name=secondary]}\\
% \midrule
% \addlinespace
% &\normalfont without \texttt{splitindex} &
% \normalfont with \texttt{splitindex} \\
% \cmidrule(lr){2-2} \cmidrule(lr){3-3}
% \addlinespace
% \normalfont(at \cs{begin}\texttt{\{document\}})\\
% \addlinespace
% &mybook.idx & mybook.idx \\
% &secondary.idx  \\
% \addlinespace
% \normalfont(at \cs{printindex})\\
% \addlinespace
% &mybook.ind    & mybook-mybook.idx \\
% &mybook.ilg    &  mybook-secondary.idx\\
% &secondary.ind & mybook-mybook.ind\\
% &secondary.ilg & mybook-mybook.ilg\\
% &              & mybook-secondary.ind \\
% &              & mybook-secondary.ilg \\
% \bottomrule
% \end{tabular}\endgroup
% \caption{Files written during a \LaTeX\ run}\label{tab:files}
% \end{table}
%
% Everything is the same when using \prog{texindy} for alphabetising,
% except that, by default, it doesn't write \texttt{.ilg} files. If
% you want them, add |options=-t |\meta{name}|.ilg| to the relevant
% \cs{makeindex} command, in our example it should be
%\begin{verbatim}
% \makeindex[...,options=-t mybook.ilg]
% \makeindex[name=secondary,...,options=-t secondary.ilg]
%\end{verbatim}
% The name of the \texttt{.ilg} file \emph{must} be specified.
% Remember, though, that \prog{xindy} \texttt{.ilg} files may turn out
% to be very large.
%
% When something different from expected appears to take place, check
% also the time stamps of the produced files; if they are older than
% \texttt{mybook.log}, it means that they have not been written in the
% last run. The most common case is that you forgot to activate the shell
% escape feature (which is not necessary with \TeX~Live 2010 or later,
% provided you use only \prog{makeindex}).
%
% Another cause of malfunction might be a wrong option passed to
% \prog{makeindex}, \prog{texindy} or \prog{splitindex}. For example,
% if you specify a style option for \prog{makeindex} such as
% |options=-s mystyle.ist| and the style file is missing or its name
% is mistyped, the run of \prog{makeindex} will result in
% \texttt{mybook.log}, but it will be aborted and the \TeX\ program
% has no control over this process. In this case the \texttt{.ilg} and
% \texttt{.ind} files will not be produced and you can spot the
% problem by checking the time stamps. On some systems a message such as
%\begin{verbatim}
% Index file mystyle.ist not found
% Usage: makeindex [-ilqrcgLT] [-s sty] [-o ind] [-t log] [-p num]
%\end{verbatim}
% may appear on the screen, but often this window gets closed before
% you realize you have a problem. The time stamp is the best clue to
% detect such problems.
%
% Shell hackers may be able to redirect the \texttt{stderr} stream to
% a file, but this requires skills that can't be explained here,
% because they require tens of different tricks, depending on what
% method is used to start a \LaTeX\ run. From the command line,
% assuming \prog{bash}, it would be something like
%\begin{verbatim}
% pdflatex --shell-escape mybook.tex 2>latex-errors
%\end{verbatim}
% If shell hackers know a way to access the exit status of the called
% program, we'd be glad to implement a supplementary check.
%
% \section{Hints}\label{sec:hints}
% Actually this package reaches two goals: (\textit{a})~it typesets
% the indices of a specific document in just one run, and
% (\textit{b})~it lets the author\slash typesetter produce
% documents with multiple indices.
%
% \subsection{Conflicts}
%
% \subsubsection{Two columm typesetting and index prologue}
% As it has been already mentioned, it is possible to use the command
% |\indexprologue| to write some text before the index proper gets typeset;
% an optional space may be used in place of the default one-line spacing
% between the index title, the prologue and the index body.
% 
% This facility relies on a particular feature of the |multicols|
% environment, that \pack{indextools} uses to instruct the typesetting
% program to typeset the index with a specified number of balanced columns.
% The choice of |multicols| has been made because it balances the
% columns in the last page; the declaration |\twocolum| does not
% perform the same way, and, if used, it makes |\printindex| typeset
% the index in two-column mode with an unbalanced last column.
%
% In the previous sections it has been clearly stated that any
% configuration of the way \pack{indextools} typesets the indices
% is bypassed if the option \opz{original} has been specified
% either explicitly or implicitly. It is implicitly specified
% if the option \opz{twocolumn} is specified in the class
% declaration statement.
%
% Why? Because if two column typesetting is desired for the whole
% document, it is not clear if the index has to be typeset in one
% column within each column of the document, or if it should be
% typeset in two column mode after a |\onecolumn| command is being
% issued; the results are not the same: with the former method the
% columns remain unbalanced, while the latter has balanced columns.
% Furthermore the |\onecolum| command forces a page break; without
% the |\onecolumn| command if the index is treated as a  chapter,
% there is a page break, while if it is configured to be typeset
% as a section there is no forced page break. With this plethora
% of combinations we decided to avoid any configuration of the
% index typesetting and left the decision to the user. This
% requires the user to practice some ingenuity in order to obtain
% what he expects.
%
% First of all the user shall not specify the \opz{twocolumn}
% option to the class. Secondly the user asks for the use of
% \pack{indextools} and sets up the single or multiple |\makindex|
% commands. thirdly he loads all other packages required for his
% document; possibly he uses also \pack{geometry} in order to
% specify a specific page layout. He shall specify the |\twocolumn|
% declaration after the above has been completed, in any case
% after the \pack{indextools} package has been loaded and the single
% or multiple |\makeindex| commands are configured.
%
% A good example might be this one:
%\begin{verbatim}
%\documentclass[a4paper,11pt]{book}
%\usepackage{indextools}
%\indexsetup{level=\section*,toclevel=section,noclearpage}
%\makeindex[title=Index of places,columns=1]
%\twocolumn
%\usepackage[T1]{fontenc}
%\usepackage{lmodern}
%\usepackage[italian,english]{babel}
%\usepackage[utf8]{inputenc}
%\usepackage[a4paper,margin={1in,1in},binding=3mm]{geometry}
%\usepackage[english]{varioref}
%\usepackage[hang]{caption}
%\begin{document}
%...
%\indexprologue{This index lists all the residences where Lady Esther
%lived during the time span described in the book.}
%\printindex
%\end{document}
%\end{verbatim}
% In this way the index is typeset as a ``section'', not as a
% ``chapter'', in one column mode within the two column document
% style; the indicated prologue is typeset between the index
% title ``Index of places'' and the start of the index proper.
%
% On the opposite the following code:
%\begin{verbatim}
%\documentclass[a4paper,11pt]{book}
%\usepackage{indextools}
%\makeindex[title=Index of places,columns=2]
%\twocolumn
%\usepackage[T1]{fontenc}
%\usepackage{lmodern}
%\usepackage[italian,english]{babel}
%\usepackage[utf8]{inputenc}
%\usepackage[a4paper,margin={1in,1in},binding=3mm]{geometry}
%\usepackage[english]{varioref}
%\usepackage[hang]{caption}
%\begin{document}
%...
%\onecolumn
%\indexprologue{This index lists all the residences where Lady Esther
%lived during the time span described in the book.}
%\printindex
%\end{document}
%\end{verbatim}
% produces an index typeset as a ``chapter'', starting on a new page;
% it is typeset in two balanced columns. The prologue is typeset where it should.
% The ingenious reader can experiment mixing the various settings
% used in these two examples in order to find out what benefits or
% disadvantages one can obtain with settings that are not physically
% impossible, but that may be aesthetically conflicting with one another.

% \subsubsection{Class \class{memoir}}
%
% The first public version of this package was not compatible with the
% \pack{memoir} class. Since version 1.1 it is; however, one has to
% keep in mind that all index processing is done with the methods of
% the present package, and \emph{not} with \pack{memoir}'s; however
% the syntax used is the same and there should be no problem. There is
% an interaction between \pack{memoir} and \pack{showidx} that required
% special attention. See below about using \pack{showidx}.
%
% \subsubsection{Package \pack{showidx}} 
%
% Up to version 1.1 this package did not allow to use it together with
% the \pack{showidx} package; now it is, provided that \pack{showidx}
% is loaded \emph{before} \pack{indextools}. See below more information
% on using \pack{showidx}.
%
% \subsubsection{Package \pack{fancyhdr}}
% 
% When using package \pack{fancyhdr} some inconveniences did show up;
% now we believe we have detected the causes and we implemented the
% necessary corrections\footnote{Thanks to Maïeul Rouquette who, before
% creating this fork, spotted the problems and suggested some patches.}.
%
% \subsubsection{Package \pack{combine}}
%
% Apparently there might be some conflicts with package \pack{combine},
% because this package redefines the contents of |\jobname|; we tried
% to control this behaviour, and made the necessary patches, but it is still
% necessary to load this package \pack{indextools} \emph{before} package
% \pack{combine}.
%
%
% \subsection{Package \pack{bidi}}
% A strange conflict was discovered between packages \pack{imakeidx} and
% \pack{bidi}, in order to let \pack{hyperref} redefine some
% internal macros so as to have index hyperlinks properly executed.
% This implies that the ``index'' package be loaded before \pack{hyperref};
% on the opposite \pack{bidi} (for bidirectional writing, needed for
% typesetting Arabic and Hebrew scripts, among other ones) \emph{must}
% be loaded after \pack{hyperref} and redefines some hyper commands
% that have influence on the indexing package; therefore after loading
% \pack{bidi} the previous redefinitions are lost. A solution was found
% by modifying the definition of the environment |theindex| and delaying
% its definition to the execution of |\begin{document}|. This is the main
% reason of this fork so that \pack{indextools} may be loaded, as most
% packages normally do, before \pack{hyperref}.
%
%\subsection{Index layout customisation}
% If you redefine yourself the \texttt{theindex} environment, please
% remember not to number the chapter or section that introduces the
% index if you ask for the \Kvalue{intoc} option; either use the
% commands \cs{chapter*} or the \cs{section*} respectively and the
% \Kvalue{intoc} option or don't use this option and redefine your
% \texttt{theindex} environment with numbered chapter or section
% commands, that will put the index titles directly into the table of
% contents. See below the effect of the |\backmatter| declaration.
% You may use the \pack{idxlayout} package by Thomas Titz,
% which offers many functions for index typesetting customisation and
% is compatible with our package; remember to load \pack{idxlayout}
% after \pack{indextools}. This package has a similar function to our
% \cs{indexprologue}, called \cs{setindexprenote}; however
% \pack{idxlayout} doesn't reset the index prologue, which must be
% declared anew or disabled with \cs{noindexprenote} before the next
% \cs{printindex} command. In any case take into serious consideration
% what is being said hereafter about customisation.
%
% \subsection{Index page customisation}
% The same, more or less, holds true if you customise your headings;
% \pack{indextools} can deal with standard settings, but it generally
% cannot deal with personal styles and customisations. This is why
% if you load \pack{fancyhdr}, some of the |\indexsetup| settings may be
% disabled.

% When you make any kind of customisation, remember that there are
% several classes, or personal settings, or features that may render your
% customisation very difficult to handle. Typically:
%\begin{itemize}
% \item There are classes where the normal usable highest sectioning
% command, disregarding |\part|, is not |\chapter|, but |\section|; for
% example classes \class{article}, \class{scrartcl}.
%\item Sectioning commands come in two varieties: starred and unstarred;
% the former ones are not numbered and do not produce any entry in the
% headings and in the table of contents; the latter ones may  behave
% differently according to the next item.
% \item There are some classes (for example \class{book},
% \class{scrbook}, \class{menoir},\dots) that have the special
% ``sectioning'' declarations |\frontmatter|, |\mainmatter|, and
% |\backmatter| which modify how the unstarred sectioning commands behave for
% what concerns the heading and the table of contents entries.
%
% For what concerns indices, these are generally typeset at the document
% end. This means that the |\backmatter| declaration may be in force;
% in this case unstarred sectioning commands are not numbered but they
% produce normal headings and table of contents entries.
%\item Heading entries are used by the output routine paging algorithm
% in such a way as to extract the left or the right part from suitable
% ``marks''; for the even (left) pages the required information is
% extracted from the |\topmark| or |\firstmark|, while for the odd (right)
% pages they are extracted from the |\botmark|. Generally speaking,
% the even page heading refers to |\leftmark| and odd page headings
% refer to |\rightmark|; these are the two commands that extract the
% correct part from the suitable marks. But when you use the
% \Kvalue{headers} option value, you specify two brace balanced strings that
% are loaded through |\markboth| as the left and right part of the
% current mark.
% \item The contents of the marks may be very varied; they are generally
% made up of two brace balanced strings, which in turn may contain other
% brace balanced strings that may be used in different ways by the selected page
% style and by the page style definitions of the macros |\chaptermarks|,
% |\sectionmarks|, and |\subsectionmarks| that may interfere with personal
% customisations.
% \item Remember also that things change in an obvious way when one side
% typesetting is chosen; all pages are treated as if they were odd ones;
% therefore any customisation must take into account also this aspect.
%\end{itemize}
%
% The above list has not been written down for discouraging customisations
% of any kind: simply it recalls what must be taken care of in order to
% create one's customisation in a proper way.
%
% The above list explains why we disabled the commands tied to the
% |\indexsetup| keys \Kvalue{headings} and \Kvalue{firstpagestyle} when
% package \pack{fancydr} is being used; you may even set up these keys,
% but if the \pack{fancyhdr} package has been loaded, we disable their
% action; this implies that any fancy customisation must be done before
% starting to print any index.
%
% The \Kvalue{intoc} option must also be used with care, as well as the starred
% or unstarred sectioning commands for the \Kvalue{level} and
% \Kvalue{toclevel} option values. They must be chosen according to what the
% possible |\backmatter| declaration sets up, in order to avoid double
% entries in the table of contents; the |\backmatter| settings may also
% influence the way heading information is being used, and this makes it
% even more stringent to set up any index page style customisation
% before starting to print any index.
%
% Therefore if by chance you get double entries into the table of contents,
% eliminate the \Kvalue{intoc} option from your calls; your class,
% packages, and settings are already taking care of it. 

% The package \pack{tocbibind} should be loaded with the \Kvalue{noindex}
% option, otherwise it would interfere with our redefinition of
% \texttt{theindex}.
%
% If you redefine your \texttt{theindex} environment by means of other
% packages, pay attention that these redefine a real \texttt{theindex}
% environment with this very name; if they create an environment with
% a different name, \pack{indextools} can't take care of the indices
% production (in particular the \TeX\ system program \prog{makeindex}
% creates a sorted and formatted \texttt{.ind} file that refers
% explicitly to the \texttt{theindex} environment), and it can't take
% care of the table of contents entry and of the position of the hyper
% link anchors needed to navigate your document by means of hyper
% links.
%
% \subsection{Index location customisation}
%
% Some packages might want to use the facilities of \emph{indextools}
% to offer customised index commands, where the entry location is not
% expressed by a page number, but by an other reference value (for
% example: line number, entry number, etc.).
%
% These packages may use the low-level command \cs{indtl@wrindexentry}
% which takes three arguments:
% \begin{enumerate}
% 	\item The index name.
% 	\item The entry.
% 	\item The location number.
% \end{enumerate}
% For example \verb+\indtl@wrindexentry{names}{Charles}{26}+ adds to
% the index \verb+names+ the value \verb+Charles+ with the location
% reference \verb+26+.
%
% In simpler words, the new command \cs{indtl@wrindexentry} is of interest
% mainly to package and class authors; it is not to be used by the
% ``normal'' user, who, on the opposite, if interested in referencing
% the index entries on some location counter different from the page one,
% is urged to refer to the classes and packages that implement this
% facility; \pack{eledmac} should be one of these packages. Matter of
% fact this addition to the \pack{indextools} package was contributed
% by Maïeul Rouquette, the author and maintainer of \pack{eledmac}.
%
% \subsection{Using the \textsf{showidx} package}
% If you want to make use of the \pack{showidx} facilities, remember
% to load that package \emph{before} \pack{indextools}; remember also
% to disable or comment out the call to \pack{showidx} when you
% typeset the final version of your document. This constraint is due
% to the fact that \pack{showidx} redefines several internal commands,
% some of which have to receive the \pack{indextools} definition in order
% to perform as described in this documentation.

% At the same time if the \pack{memoir} class is being used, remember that
% this class simulates the \pack{showidx} package and has its own commands
% to enable or disable the printing of the index entries into the margin
% of the document; the default setting is with the |\hideindexmarks|
% command in force; but if the user wants to write his index entries in
% the margin he has to issue the declaration |\showindexmarks| just after
% begin document, in any case before the part of the source file(s) he
% wants to be marked with the index entries in the margin.
%
% \subsection{Index List sectioning customisation}
% Use freely the options and the key values in order to reach the
% desired results, but you are advised to prepare in advance the
% styles for composing the various indices in a proper way; for
% example, if you use a titled style for the index, where the index
% sections are distinguished with a bold face title or alphabetic
% letter, you have to set up a \texttt{.ist} file, such as
% \texttt{myindexstyle.ist}, made up like this:
%\begin{verbatim}
%	headings_flag 1
%	heading_prefix   "\\par\\penalty-50\\textbf{"
%	heading_suffix   "}\\\\\*\~\\\\\*"
%	symhead_positive "Symbols"
%	symhead_negative "symbols"
%	numhead_positive "Numbers"
%	numhead_negative "numbers"
%	delim_0 ",\~"
%\end{verbatim}
% where the numeric and non alphabetic entries have different
% titles. But, say, you are making also an index where the entries are
% file names, and for some names only the extension is entered; the
% extensions start with a dot, so the sorting program will sort these
% names at the beginning of the sorted index file, but you won't like
% to have  a title such as ``Symbols''; you probably prefer to have a title
% such as ``Extensions''; therefore you have to prepare a different
% index style file, such as this one:
%\begin{verbatim}
%	headings_flag 1
%	heading_prefix   "\\par\\penalty-50\\textbf{"
%	heading_suffix   "}\\\\\*\~\\\\\*"
%	symhead_positive "Extensions"
%	symhead_negative "extensions"
%	numhead_positive "Numbers"
%	numhead_negative "numbers"
%	delim_0 ",\~"
%\end{verbatim}
%
% This done, besides requiring the use of this package, you have to
% declare the \cs{makeindex} command with the necessary options; pay a
% particular attention to the options that involve the index symbolic
% name, the index title, the index style, the fact that the index
% titles shall appear in the table of contents, and if you are
% preparing an e-book, you probably would like to hyper link both the
% page numbers and the index titles to the proper locations.
% The typesetting program will do everything for you but be careful
% not to confuse it with illogical index entries.
%
% Especially with multiple indices it is important that you are
% consistent in putting the right information in the right index and
% with a consistent mark-up. Define yourself appropriate macros so
% that, for example, personal names are consistently typeset, say, in
% caps and small caps and are entered into a specific index; you may
% even create one command to typeset the name in the document and
% replicate the same name in the index.
%
% Of course there is no program that can decide at your place what and
% where to index each piece of information; this is a task for
% humans. Soooooo\dots
% \begin{center} HAPPY \TeX{}ING!\end{center}
%
% \StopEventually{\PrintChanges}
% \section{Implementation}
% The heading to the file is in common with the documentation file,
% and has already been taken care of. But we require the
% \pack{xkeyval} package, in order to handle the key-value lists.
%
% Notice that in order to create a specific name space so as to avoid
% possible conflicts with other packages, all the commands defined in
% this package are prefixed with the string \texttt{indtl@}.
%\iffalse
%<*package>
%\fi
%    \begin{macrocode}
\RequirePackage{xkeyval}
%    \end{macrocode}
%
% We define the various options and their defaults. After
% \cs{ProcessOptions}, we set anyway the \texttt{original} option if
% the document class has been given the \texttt{twocolumn} option,
% which is incompatible with \pack{multicol}. We define also an
% internal alias for |\immediate\write18|, a rudimentary check for the
% typesetting engine and a macro for modifying the command line call
% to \prog{splitindex}. The \pack{xpatch} is requested; at the moment
% none if its features are used, but it calls \pack{etoolbox}, whose
% facilities are used here; while future extensions will benefit from
% the extended patched commands, that strongly enrich the performances
% of the main package \pack{etoolbox}.
% \changes{v1.1a}{2012/09/07}{Fixed bug with possibly defined
% \cs{directlua}; now we leave the check to \pack{ifluatex}; using
% also \pack{ifxetex} for symmetry.}
%    \begin{macrocode}
\DeclareOption{xindy}{\def\indtl@progdefault{texindy}}
\DeclareOption{texindy}{\def\indtl@progdefault{texindy}}
\DeclareOption{truexindy}{\def\indtl@progdefault{truexindy}}
\DeclareOption{makeindex}{\def\indtl@progdefault{makeindex}}
\newif\ifindtl@disableautomatic
\DeclareOption{noautomatic}{\indtl@disableautomatictrue}
\newif\ifindtl@nonewpage
\DeclareOption{nonewpage}{%
  \indtl@nonewpagetrue\indtl@disableautomatictrue
}
\newif\ifindtl@splitindex
\DeclareOption{splitindex}{\indtl@splitindextrue}
\newif\ifindtl@original
\DeclareOption{original}{\indtl@originaltrue}
\DeclareOption{quiet}{\AtEndOfPackage{%
  \let\indtl@finalmessage\@gobble
  \let\indtl@splitindexmessage\relax}}
\newif\ifindtl@afterindex
\DeclareOption{afterindex}{\indtl@afterindextrue} 
 
\newif\ifindtl@innote
\DeclareOption{innote}{\indtl@innotetrue} 

\newif\ifindtl@notenumber
\DeclareOption{notenumber}{\indtl@notenumbertrue} 

\ExecuteOptions{makeindex}
\ProcessOptions\relax

\if@twocolumn\indtl@originaltrue\fi
\def\indtl@exec{\immediate\write18}
\def\indtl@engine{(pdf)latex}
\RequirePackage{ifxetex,ifluatex}
\ifxetex\def\indtl@engine{xelatex}\fi
\ifluatex % luatex doesn't have \(pdf)shellescape
 \def\indtl@engine{lualatex}
  \ifnum\luatexversion<68
    \chardef\indtl@shellescape\@ne % no way to know the value
  \else
    \RequirePackage{pdftexcmds} % provides \pdf@shellescape
    \chardef\indtl@shellescape\pdf@shellescape
  \fi
  \let\indtl@exec\pdf@system
\fi
\RequirePackage{xpatch}
\edef\indtl@splitindexoptions{-m \string"\string"}
\def\splitindexoptions#1{\g@addto@macro\indtl@splitindexoptions{ #1}}
\@onlypreamble\splitindexoptions
%    \end{macrocode}
%
% \subsection{Incompatibilities and emulation}
%
% While experimenting we found out that some classes or packages are
% either incompatible with this one, or must be faked in order to
% pretend they have been loaded.
%
% There is a serious incompatibility with the \pack{memoir} class.  In
% facts \pack{memoir} puts all index entries in the main \texttt{.aux}
% file and extracts them to the various raw index files at
% |\end{document}| time. This means that no raw index file output
% stream has been defined, and therefore this package can't close it;
% moreover it can't typeset the indices before |\end{document}|
% because they are not yet available. Therefore if \pack{memoir} is
% the active class, we will hijack its index mechanism replacing it
% with ours.
%
% On the opposite we pretend that package \pack{makeidx} or package
% \pack{multind} have been loaded, so that \pack{hyperref} can play
% with their commands, that are substantially the same as those used
% here. By so doing those packages are inhibited from being loaded
% after this one.
% We do the same with the forked original \pack{imakeidx} package
% so that if \pack{imakeidx} is loaded before, \pack{indextools} is
% not loaded, but an error message is issued; otherwise we fake the
% fact that \pack{imakeidx} has already been loaded so that it does
% not get reloaded.
%    \begin{macrocode}
\@namedef{ver@makeidx.sty}{3000/12/31}
\@ifpackageloaded{multind}
  {\PackageError{indextools}{Incompatible package `multind' loaded}
    {This package is incompatible with multind, don't load both.%
     \MessageBreak\@ehc}}
  {\@namedef{ver@multind.sty}{3000/12/31}}

\@ifpackageloaded{imakeidx}
  {\PackageError{indextools}{Incompatible package `imakeidx' loaded}
    {This package is incompatible with imakeidx, don't load both.%
     \MessageBreak\@ehc}}
  {\@namedef{ver@imakeidx.sty}{3000/12/31}}
%    \end{macrocode}
% At the same time we redefine some commands defined by \pack{makeidx}
% and we define the default English names for the \cs{see} and
% \cs{seealso} commands. We use \cs{providecommand} so that, if
% \pack{makeidx} has already been loaded, we do not redefine things
% that have already been defined.
%    \begin{macrocode}
\providecommand*\see[2]{\emph{\seename} #1}
\providecommand*\seealso[2]{\emph{\alsoname} #1}
\providecommand*\seename{see}
\providecommand*\alsoname{see also}
%    \end{macrocode}
%
% \subsection{\cs{makeindex}}
%
% From here on, some commands are duplicated; this depends on the
% fact that the behavior must be different when using \prog{splitindex} or
% not. The memory occupied by the useless commands will be cleared at the 
% end of package.
%    \begin{macrocode}
\providecommand*\makeindex{} % to use \renewcommand safely
\renewcommand{\makeindex}[1][]{\indtl@makeindex{#1}}
% \@onlypreamble\makeindex % Already in latex.ltx
%    \end{macrocode}
% This package implementation of \cs{makeindex} sets default values
% for the keys, then evaluates its argument (which is the optional
% argument to \cs{makeindex}) and calls two other macros. After that
% we have to reset the defaults.
%    \begin{macrocode}
\def\indtl@makeindex#1{%
  \edef\indtl@name{\jobname}%
  \edef\indtl@jobname{\jobname}%
  \def\indtl@title{\indexname}%
  \edef\indtl@program{\indtl@progdefault}%
  \let\indtl@options\space
  \KV@indtl@noautomaticfalse\KV@indtl@intocfalse
  \setkeys{indtl}{#1}%
  \ifindtl@splitindex\KV@indtl@noautomaticfalse\fi
  \indtl@build\indtl@name
  \indtl@startidx\indtl@name
  \indtl@resetdefaults
}
%    \end{macrocode}
%
% Here are the keys. As usual, the \texttt{indtl@} prefix is used to
% distinguish anything that is being defined in this package, even the
% keys.
%    \begin{macrocode}
\define@key{indtl}{name}{\def\indtl@name{#1}}
\define@key{indtl}{title}{\def\indtl@title{#1}}
\define@choicekey{indtl}{program}[\indtl@val\indtl@nr]
  {makeindex,xindy,texindy,truexindy}{%
    \ifcase\indtl@nr\relax
      \def\indtl@program{makeindex}%
    \or
      \def\indtl@program{texindy}%
    \or
      \def\indtl@program{texindy}%
    \or
      \def\indtl@program{xindy}%
    \fi}
\define@key{indtl}{options}{\def\indtl@options{ #1 }}
\define@boolkey{indtl}{noautomatic}[true]{}
\define@boolkey{indtl}{intoc}[true]{}
\define@key{indtl}{columns}{\def\indtl@columns{#1}}
\define@key{indtl}{columnsep}{\def\indtl@columnsep{#1}}
\define@boolkey{indtl}{columnseprule}[true]{}
\def\indtl@resetdefaults{%
  \def\indtl@options{ }%
  \def\indtl@columns{2}\def\indtl@columnsep{35\p@}%
  \KV@indtl@columnseprulefalse
  \KV@indtl@intocfalse\KV@indtl@noautomaticfalse}
\indtl@resetdefaults
%    \end{macrocode}
%
% The control sequence \cs{indtl@build} defines a control sequence to
% hold the setup for an index to be used when the index is sorted and
% printed
%    \begin{macrocode}
\def\indtl@build#1{%
  \toks@{}%
  \indtl@dokey\indtl@title
  \indtl@dokey\indtl@program
  \indtl@dokey\indtl@options
  \indtl@dokey\indtl@columns
  \indtl@dokey\indtl@columnsep
  \ifKV@indtl@noautomatic
    \addto@hook\toks@{\KV@indtl@noautomatictrue}%
  \else
    \addto@hook\toks@{\KV@indtl@noautomaticfalse}%
  \fi
  \ifKV@indtl@intoc
    \addto@hook\toks@{\KV@indtl@intoctrue}%
  \else
    \addto@hook\toks@{\KV@indtl@intocfalse}%
  \fi
  \ifKV@indtl@columnseprule
    \addto@hook\toks@{\KV@indtl@columnsepruletrue}%
  \else
    \addto@hook\toks@{\KV@indtl@columnseprulefalse}%
  \fi
  \expandafter\edef\csname indtl@set@#1\endcsname{\the\toks@}%
}
%    \end{macrocode}
% Comand \cs{indtl@dokey} receives as argument the text of the values
% assigned to certain keys, and adds them to the options token list.
%    \begin{macrocode}
\def\indtl@dokey#1{%
  \expandafter\addto@hook\expandafter\toks@\expandafter{%
    \expandafter\def\expandafter#1\expandafter{#1}}}
%    \end{macrocode}
%
% Command \cs{indtl@startidx} defines the output stream(s); the macro
% with suffix \texttt{split} is used when \Kvalue{splitindex} is not
% enabled, the one with suffix \texttt{unique} is used otherwise.  In
% the case of many indices, the symbolic name for an index named
% `pippo' is \cs{pippo@idxfile} corresponding to the file
% \texttt{pippo.idx}. When \Kvalue{splitindex} is enabled, the only
% output stream is called \cs{@indexfile} as in standard \LaTeX,
% corresponding to \cs{jobname.idx}.
%    \begin{macrocode}
\def\indtl@startidxsplit#1{%
  \if@filesw
    \def\index{\@bsphack
      \@ifnextchar [{\@index}{\@index[\indtl@jobname]}}
    \expandafter\newwrite\csname #1@idxfile\endcsname
    \immediate\openout \csname #1@idxfile\endcsname #1.idx\relax
    \typeout{Writing index file #1.idx}%
  \fi}
%    \end{macrocode}
% We define a switch which is set to true when a \cs{makeindex} command is given:
% with \Kvalue{splitindex} we open only one stream.
%    \begin{macrocode}
\newif\ifindtl@startedidx
\def\indtl@startidxunique#1{%
  \if@filesw
    \ifindtl@startedidx\else
      \newwrite\@indexfile
      \immediate\openout\@indexfile\indtl@jobname.idx%
      \global\indtl@startedidxtrue
    \fi
    \def\index{\@bsphack
      \@ifnextchar [{\@index}{\@index[\indtl@jobname]}}
    \expandafter\let\csname #1@idxfile\endcsname\@empty
    \typeout{Started index file #1}%
 \fi}
%    \end{macrocode}
%
% \subsection{\cs{index}} 
%
% Provide a default definition for \cs{index}; when a \cs{makeindex}
% command is given and \LaTeX\ is writing on auxiliary files,
% \cs{index} will be redefined, as seen before. When index files are
% written, \cs{index} always calls \cs{@index}. Some code is borrowed
% from \pack{memoir.cls}, but heavily modified. We want \cs{@wrindex}
% to be defined with two arguments, so that \pack{hyperref} can hook
% into it just like it does with the similar commands defined by the
% old packages \pack{multind} and \pack{index}.
%    \begin{macrocode}
\renewcommand{\index}[2][]{\@bsphack\@esphack}
\def\@index[#1]{%
  \@ifundefined{#1@idxfile}%
  {\PackageWarning{indextools}{Undefined index file `#1'}%
   \begingroup
   \@sanitize
   \indtl@nowrindex}%
  {\edef\@idxfile{#1}%
   \begingroup
   \@sanitize
   \@wrindex\@idxfile}}
\def\indtl@nowrindex#1{\endgroup\@esphack}
%    \end{macrocode}
%
% Command \cs{@wrindex} must be duplicated; we have to call it the same as
% usual in order to support \pack{hyperref}. But the real name will be given
% at the end. We have to define a switch to allow the use of the
% \pack{showidx} facilities. We define also a helper macro so as to do the
% right thing so as to show the index file name to which a certain index
% entry is going to be written; the idea is to prefix the index entry with
% the actual name of the specific index, except in the case or the default
% index, where the name is set to |\jobname|. Since the control sequence is
% a primitive command, its value cannot be directly compared in the |\ifx|
% sense to the current macro represented by argument |#1|. Therefore we need
% a further helper control sequence |\indtl@jobname| that contains the value
% assigned to |\jobname|.
%
% We must also take care of the case where the user wants to print the index
% entries in the margin while working on the document. This implies testing
% for the package \pack{showidx} being already loaded;  but this is not
% sufficient, because the \pack{memoir} class simulates the \pack{showidx}
% package and the test would result to be true even if the user did not load
% that package, but uses the \pack{memoir} class.
%
% Therefore we use the same boolean used by \pack{memoir}, testing in advance
% so as not redefining it; then we use it to let the \pack{showidx} true or
% simulated macros do their job, but we also take care of resetting the switch
% default value to false at begin document time if the \pack{memoir} class is
% being used. 
%
%
%    \begin{macrocode}
\@ifundefined{showindexmarks}{\newif\ifshowindexmark}{}
\@ifpackageloaded{showidx}{\showindexmarktrue}{\showindexmarkfalse}
\newcommand\indtl@showidxentry[2]{%
  \ifshowindexmark
    \@showidx{\ifdefequal{\indtl@jobname}{#1}{}{[#1]\space}#2}%
  \fi}
\newcommand\indtl@wrindexentrysplit[3]{%
  \expandafter\protected@write\csname#1@idxfile\endcsname{}%
    {\string\indexentry{#2}{#3}}%
}
\newcommand\indtl@wrindexentryunique[3]{%
  \protected@write\@indexfile{}%
    {\string\indexentry[#1]{#2}{#3}}%
}
\def\indtl@wrindexsplit#1#2{%
	\indtl@wrindexentrysplit{#1}{#2}{\thepage}%
	\endgroup\indtl@showidxentry{#1}{#2}%
  \@esphack%
}
\def\indtl@wrindexunique#1#2{%
	\indtl@wrindexentryunique{#1}{#2}{\thepage}%
	\endgroup\indtl@showidxentry{#1}{#2}%
  \@esphack%
  }
%    \end{macrocode}
%
% \subsection{\cs{printindex}}
%
% Compilation of the indices is disabled if \texttt{-shell-escape} has
% not been given or the restricted mode is not active; in this case we
% emit a warning. \XeTeX{} has \cs{shellescape} instead of
% \cs{pdfshellescape}, so we take care of this (hoping that users or
% packages don't define a \cs{shellescape} command). In any case we
% define an internal version of this command. In the case of
% \prog{luatex} we can't emit the proper messages if \prog{luatex} is
% not version 0.68 or later. The conditional
% \cs{ifKV@indtl@noautomatic} is defined by \cs{define@boolkey} above.
%
%    \begin{macrocode}
\def\indtl@shellwarn{}
\ifdefined\indtl@shellescape % luatex
\else
  \@ifundefined{shellescape}
    {\let\indtl@shellescape\pdfshellescape} % pdftex
    {\let\indtl@shellescape\shellescape} % xetex
\fi
\ifnum\indtl@shellescape=\z@
  \let\KV@indtl@noautomaticfalse\KV@indtl@noautomatictrue
  \KV@indtl@noautomatictrue
  \def\indtl@shellwarn{\MessageBreak or call \indtl@engine\space with
    -shell-escape}
\fi
%    \end{macrocode}
% Do the same if \Kvalue{noautomatic} has been given as an option.
%    \begin{macrocode}
\ifindtl@disableautomatic
  \let\KV@indtl@noautomaticfalse\KV@indtl@noautomatictrue
  \KV@indtl@noautomatictrue
\fi
%    \end{macrocode}
%
% Now we set up the \texttt{theindex} environment. If the
% \texttt{original} option is set, we simply patch the class
% definition in order to call the macro that does the work related to
% the table of contents. Otherwise we define a new \texttt{theindex}
% environment, based on the standard, but using, if the number of
% columns is greater than one, the \texttt{multicols} environment.
% Users needing a different setup can use the \cs{indexsetup} command.
% Before doing so, though, we need to disable certain features if the
% package \pack{fancyhdr} is loaded sometimes before the
% |\begin{document| statement, be it before or after loading
% \pack{indextools}. If that package is loaded we have to patch the
% definition of the environment |theindex|, because its default
% commands interfere with those defined here that work perfectly
% when \pack{fancyhdr} is not loaded. This patching could be defined
% further on, but if it remains close to what has to be patched, it's
% easier to understand it's operation.
%
% We make this redefinition at the begining of the document, to
% prevent redefinition by other packages, like \pack{bidi}. This
% point is where \pack{indextools} and \pack{imakeidx} diverge.
%    \begin{macrocode}
\newif\ifindtl@fancyhdr
\AtBeginDocument{%
  \@ifpackageloaded{fancyhdr}{\indtl@fancyhdrtrue}{\indtl@fancyhdrfalse}%
    \ifindtl@original
     \expandafter\def\expandafter\theindex\expandafter{\expandafter
        \indtl@maybeaddtotoc\theindex}
    \else
      \renewenvironment{theindex}
        {\indtl@maybeaddtotoc
         \indtl@indexlevel{\indexname}%
         \ifindtl@fancyhdr\else%
           \indtl@indexheaders%
           \thispagestyle{\indtl@firstpagestyle}%
         \fi%
         \ifnum\indtl@columns>\@ne
           \columnsep \indtl@columnsep
           \ifx\indtl@idxprologue\relax
             \begin{multicols}{\indtl@columns}
           \else
             \begin{multicols}{\indtl@columns}[\indtl@idxprologue]
           \fi
         \else
           \indtl@idxprologue
         \fi
         \global\let\indtl@idxprologue\relax
         \parindent\z@
         \parskip\z@ \@plus .3\p@\relax
         \columnseprule \ifKV@indtl@columnseprule.4\p@\else\z@\fi
         \raggedright
         \let\item\@idxitem
         \indtl@othercode}
        {\ifnum\indtl@columns>\@ne\end{multicols}\fi
    }
\fi
}
\ifindtl@original
\else
  \global\let\indtl@idxprologue\relax
  \RequirePackage{multicol}
\fi
%    \end{macrocode}
% The command \cs{indexsetup} may be used to customize some aspects of
% index formatting.
%    \begin{macrocode}
\def\indtl@indexlevel{%
  \@ifundefined{chapter}{\section}{\chapter}*}
\define@key{indtlindex}{level}{\def\indtl@indexlevel{#1}}
\def\indtl@toclevel{%
  \@ifundefined{chapter}{section}{chapter}}
\define@key{indtlindex}{toclevel}{\def\indtl@toclevel{#1}}
\define@boolkey{indtlindex}{noclearpage}[true]{\let\indtl@clearpage\relax}
\def\indtl@indexheaders{%
  \@mkboth{\MakeUppercase\indexname}{\MakeUppercase\indexname}}
\define@key{indtlindex}{headers}{\def\indtl@indexheaders{\markboth#1}}
\def\indtl@firstpagestyle{plain}
\define@key{indtlindex}{firstpagestyle}{\def\indtl@firstpagestyle{#1}}
\let\indtl@othercode\relax
\define@key{indtlindex}{othercode}{\def\indtl@othercode{#1}}
\newcommand{\indexsetup}[1]{%
  \ifindtl@original\else\setkeys{indtlindex}{#1}\fi}
\@onlypreamble\indexsetup
%    \end{macrocode}
%
% The command \cs{indexprologue} sets the internal version which is
% always \cs{let} to \cs{relax} during |\begin{theindex}|.
%    \begin{macrocode}
\newcommand{\indexprologue}[2][\bigskip]{%
  \long\gdef\indtl@idxprologue{{#2\par}#1}}
%    \end{macrocode}
%
% Now we provide the relevant \cs{printindex} macros by transferring
% the real job to a secondary macro \cs{indtl@putindex} after due
% checks and messages.
%    \begin{macrocode}
\providecommand*{\printindex}{}
\renewcommand*{\printindex}[1][\indtl@jobname]{%
  \@ifundefined{#1@idxfile}{\indtl@error{#1}}{\indtl@putindex{#1}}}

\def\indtl@error#1{%
  \def\@tempa{#1}\def\@tempb{\indtl@jobname}%
  \ifx\@tempa\@tempb
    \let\indtl@optarg\@empty
  \else
    \def\indtl@optarg{[#1]}%
  \fi
  \PackageError{indextools}
    {Misplaced \protect\printindex\indtl@optarg}
    {You are not making this index, as no appropriate
     \protect\makeindex\MessageBreak
     command has been issued in the preamble.}}
%    \end{macrocode}
%
% We define a command to do a \cs{cleardoublepage} if the option
% \Kvalue{openright} is active (in classes where \Kvalue{twoside} is
% meaningful).  In case \cs{chapter} is defined but not
% \cs{if@openright}, we assume that the class wants ``open right''.
%    \begin{macrocode}
\def\indtl@clearpage{%
  \@ifundefined{chapter}
    {\clearpage} % article and similar classes
    {\@ifundefined{if@openright}
       {\cleardoublepage}
       {\if@openright
          \cleardoublepage
        \else
          \clearpage
       \fi}
    }}
%    \end{macrocode}
%
% We need a helper macro to do a check in order to avoid a loop and
% the hook where to insert the table of contents related stuff.
%    \begin{macrocode}
\def\indtl@check@indexname{\indexname}
\providecommand*\indtl@maybeaddtotoc{}
%    \end{macrocode}
% Two helper macros for preparing the final messages to the user.
%    \begin{macrocode}
\def\indtl@finalmessage#1{%
  \expandafter\edef\csname indtl@message#1\endcsname
    {\indtl@program\indtl@options#1.idx}
  \AtEndDocument{\PackageWarning{indextools}{%
    Remember to run \indtl@engine\space again after calling\MessageBreak
    `\@nameuse{indtl@message#1}'\indtl@shellwarn\@gobble}}}
\def\indtl@splitindexmessage{%
  \AtEndDocument{\PackageWarningNoLine{indextools}{%
    Remember to run \indtl@engine\space again after calling\MessageBreak
    `splitindex' and processing the indices\indtl@shellwarn}}}
%    \end{macrocode}
%
% Here is a helper macro for deciding whether to call the external
% utility or to issue a final message. In \cs{indtl@makeindexname} we
% put the name of the only program allowed by default
% (\prog{makeindex}). If the list is updated, we can supplement the
% list here, maybe defining a list macro; for now this is
% sufficient. The temporary switch \cs{if@tempswa} is set to true if
% automatic processing is possible, so that the main macro can take
% the appropriate action.
%    \begin{macrocode}
    \def\indtl@makeindexname{makeindex}
    \def\indtl@decide{%
      \@tempswafalse
      \ifindtl@splitindex % splitindex is not "safe"
      \ifnum\indtl@shellescape=\@ne\@tempswatrue\fi
      \else
      \ifx\indtl@program\indtl@makeindexname % nor is texindy
      \ifnum\indtl@shellescape=\tw@\@tempswatrue\fi
      \fi
      \ifnum\indtl@shellescape=\@ne\@tempswatrue\fi
      \fi
      \ifKV@indtl@noautomatic
      \@tempswafalse
    \fi}
%    \end{macrocode}
% We define a macro that close a specified index file and run in. 
%    \begin{macrocode}
\newcommand{\indtl@closeandrundidxsplit}[1]{%
  \immediate\closeout\csname #1@idxfile\endcsname
  \let\indtl@indexname\indexname % keep \indexname
  \@nameuse{indtl@set@#1}\indtl@decide
  \if@tempswa % we can call the external program
    \indtl@exec{\indtl@program\indtl@options#1.idx}%
  \else
    \indtl@finalmessage{#1}%
  \fi
}
%    \end{macrocode}
% We define a macro that close the general  index file and run in. 
%    \begin{macrocode}
\newcommand{\indtl@closeandrundidxunique}{%
  \immediate\closeout\@indexfile
  \indtl@exec{splitindex \indtl@splitindexoptions\space\indtl@jobname.idx}%
  \global\indtl@splitdonetrue
}
%    \end{macrocode}
% We now define the main macro that puts the specified index file into
% the document and possibly orders to add the index title to the
% table of contents. It is duplicated as usual. The argument |#1| is
% the specific symbolic name of the index.  In particular if the
% \Kvalue{intoc} option has been specified, the hook
% \cs{indtl@maybeaddtotoc} is defined in such a way that the relevant
% information is added to the \texttt{toc} file. The
% \cs{phantomsection} command is necessary when using \pack{hyperref};
% here it is hidden as argument to \cs{@nameuse}, so it is equivalent
% to \cs{relax} and does nothing if \pack{hyperref} has not been
% loaded.
%    \begin{macrocode}
\def\indtl@putindexsplit#1{%
  \ifindtl@nonewpage\else
    \indtl@clearpage
  \fi
  \ifindtl@afterindex%
    \AtEndDocument{%
      \indtl@clearpage%
      \indtl@closeandrundidxsplit{#1}%
      \indltl@afterindexMessage%
    }%
  \else%
    \indtl@closeandrundidxsplit{#1}%
  \fi
  \ifKV@indtl@intoc
    \def\indtl@maybeaddtotoc{\@nameuse{phantomsection}%
      \addcontentsline{toc}{\indtl@toclevel}{\indtl@title}}%
  \else
    \def\indtl@maybeaddtotoc{}%
  \fi
  \ifx\indtl@title\indtl@check@indexname\else
    \def\indexname{\indtl@title}%
  \fi
  \@input@{#1.ind}
  \let\indexname\indtl@indexname % restore \indexname
}

\newif\ifindtl@splitdone
\def\indtl@putindexunique#1{%
  \ifindtl@nonewpage\else
    \indtl@clearpage
  \fi
  \let\indtl@indexname\indexname % keep \indexname
  \@nameuse{indtl@set@#1}\indtl@decide
  \if@tempswa % we can call the external program
    \ifindtl@splitdone\else
      \ifindtl@afterindex
        \AtEndDocument{%
          \indtl@clearpage%
          \indtl@closeandrundidxunique%
          \indltl@afterindexMessage%
        }
      \else
        \indtl@closeandrundidxunique
      \fi
    \fi
  \else
    \ifindtl@splitdone\else
      \indtl@splitindexmessage\global\indtl@splitdonetrue
    \fi
  \fi
  \if@tempswa % we can call the external program
    \indtl@exec{\indtl@program\indtl@options\indtl@jobname-#1.idx}%
  \fi
  \ifKV@indtl@intoc
    \def\indtl@maybeaddtotoc{\@nameuse{phantomsection}%
      \addcontentsline{toc}{\indtl@toclevel}{\indtl@title}}%
  \else
    \def\indtl@maybeaddtotoc{}%
  \fi
  \ifx\indtl@title\indtl@check@indexname\else
    \def\indexname{\indtl@title}%
  \fi
  \@input@{\indtl@jobname-#1.ind}
  \let\indexname\indtl@indexname % restore \indexname
}
%    \end{macrocode}
% A warning message when using `afterindex' option.
%    \begin{macrocode}
\newcommand{\indltl@afterindexMessage}{%
 \PackageWarning{indextools}{You use the `afterindex' option.
 \MessageBreak That means you use \string\index\space commands after \string\printindex\space command.
 \MessageBreak Consequently, the index may be not ok at the first run.
 }%
}
%    \end{macrocode}
% \subsection{`innnote' and `notenumber' options}
% 
% If the `innote' option is enabled, we modify the standard index command when we are in a note, in order to insert the \verb+|innote+ in the \verb+.idx+ file.
%    \begin{macrocode}

\AtBeginDocument{%
    \ifindtl@innote%
      \RequirePackage{letltxmacro}
      \LetLtxMacro\orig@@index\index
      \pretocmd{\@footnotetext}{\let\index\nindex}{}{}
      \apptocmd{\@footnotetext}{\let\index\orig@@index}{}{}
      
    \fi%
%    \end{macrocode}
% The same is true if the `notnumber' option is enabled.
%    \begin{macrocode}
  \ifindtl@notenumber%
      \RequirePackage{letltxmacro}
      \LetLtxMacro\orig@@index\index
      \pretocmd{\@footnotetext}{\let\index\nnumberindex}{}{}
      \apptocmd{\@footnotetext}{\let\index\orig@@index}{}{}
  \fi%
%    \end{macrocode}
%    \begin{macrocode}
% And now, the command, defines in any case, but only at the begin of documents, because people can have defined them in the preamble.
  \newcommand\nnumberindex[2][\indtl@jobname]{\orig@@index[#1]{#2|innotenumber{\thefootnote}}}
  \newcommand\nindex[2][\indtl@jobname]{\orig@@index[#1]{#2|innote}}
  \providecommand{\innotenumber}[2]{#2n.~#1}
  \providecommand{\innote}[1]{#1\textit{n}}
}
%    \end{macrcocode}
% \subsection{Final decisions}
%
% At this point, we choose the meaning of the relevant commands,
% reclaiming the space occupied by the discarded ones
%    \begin{macrocode}
\ifindtl@splitindex
  \let\indtl@startidx\indtl@startidxunique
  \let\@wrindex\indtl@wrindexunique
  \let\indtl@putindex\indtl@putindexunique
  \let\indtl@wrindexentry\indtl@wrindexentryunique
  \let\indtl@startidxsplit\@undefined
  \let\indtl@wrindexsplit\@undefined
  \let\indtl@putindexsplit\@undefined
\else
  \let\indtl@startidx\indtl@startidxsplit
  \let\@wrindex\indtl@wrindexsplit
  \let\indtl@putindex\indtl@putindexsplit
  \let\indtl@wrindexentry\indtl@wrindexentrysplit
  \let\indtl@startidxunique\@undefined
  \let\indtl@wrindexunique\@undefined
  \let\indtl@putindexunique\@undefined
\fi
%    \end{macrocode}
% To end the code, we deal with \pack{memoir}:
%    \begin{macrocode}
\@ifclassloaded{memoir}{\let\@wrindexm@m\@wrindex
   \AtBeginDocument{\hideindexmarks}}{}
%    \end{macrocode}
%\iffalse
%</package>
%\fi
% The end.
% \Finale