TeX - LaTeX Asked on June 9, 2021
Consider the subsequent Index which is generated by the following code:
documentclass[12pt]{book}
usepackage{imakeidx}
makeindex
usepackage{idxlayout}
begin{document}
large
par Here are some words.index{Person, Key@{scshape{Person, Key}}! In the beginning {bf EVENT 1}}
newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! This is followed by {bf EVENT 2}}
newpage abc newpage abc newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Later, we have {bf EVENT 3}}
newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Continuing with {bf EVENT 4}}
newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! So follows {bf EVENT 5}}
newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Later comes {bf EVENT 6}}
newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Followed by {bf EVENT 7}}
newpage abc newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Then comes {bf EVENT 8}}
newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! Shortly thereafter Event 8 comes {bf EVENT 9}}
newpage abc
par More words.index{Person, Key@{scshape{Person, Key}}! The end: {bf EVENT 10}}
idxlayout{columns=1}
renewcommandindexname{color{RoyalRed}Index}
%renewcommandindexname{Index.}
printindex
end{document}
What I would like to do, is to simply arrange the sub-entries in chronological order so that the sub-entry containing EVENT 1 comes first, followed by the sub-entry containing EVENT 2, and so forth, all the way up to the one that contains EVENT 10. This way, also, the associated pages will appear in ascending order.
How may I accomplish this? Thank you.
You can use something like
index{Person, Key@textsc{Person, Key}!NOOP{<number>}<description>}
where <number>
is autogenerated and NOOP
is a macro that does nothing, but is recorded in the .idx
file. The number is padded to five digits with zeros, so it will be used for sorting.
Any “Person, Key” pair defines a counter that's incremented at each usage. Of course, we'll use a macro for automating the process.
documentclass[12pt]{book}
%usepackage{xparse} % not needed with LaTeX 2020-10-01 or later
usepackage{imakeidx}
makeindex[columns=1]
ExplSyntaxOn
NewDocumentCommand{NOOP}{m}{}
NewDocumentCommand{personevent}{mm}
{
mlchristians_personevent:nn { #1 } { #2 }
}
cs_new_protected:Nn mlchristians_personevent:nn
{
int_if_exist:cF { g_mlchristians_personevent_#1_int }
{
int_new:c { g_mlchristians_personevent_#1_int }
}
int_gincr:c { g_mlchristians_personevent_#1_int }
% emit the index entry
index{ #1 @ textsc{#1}! NOOP { __mlchristians_pad:n { #1 } } #2 }
}
cs_new:Nn __mlchristians_pad:n
{
prg_replicate:nn { 5 - tl_count:v { g_mlchristians_personevent_#1_int } } { 0 }
int_use:c { g_mlchristians_personevent_#1_int }
}
cs_generate_variant:Nn tl_count:n { v }
ExplSyntaxOff
begin{document}
Here are some words.personevent{Person, Key}{In the beginning textbf{EVENT 1}}
newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{This is followed by textbf{EVENT 2}}
newpage abc newpage abc newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{Later, we have textbf{EVENT 3}}
newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{Continuing with textbf{EVENT 4}}
newpage abc
More words.personevent{Person, Key}{So follows textbf{EVENT 5}}
newpage abc
More words.personevent{Person, Key}{Later comes textbf{EVENT 6}}
newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{Followed by textbf{EVENT 7}}
newpage abc newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{Then comes textbf{EVENT 8}}
newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc newpage abc
More words.personevent{Person, Key}{Shortly thereafter Event 8 comes textbf{EVENT 9}}
newpage abc
More words.personevent{Person, Key}{The end: textbf{EVENT 10}}
printindex
end{document}
Please, note that textsc{text}
is the correct syntax instead of {scshape{text}}
and that bf
has been deprecated for 25 years.
The .idx
file is
indexentry{Person, Key@textsc {Person, Key}!NOOP {00001}In the beginning textbf {EVENT 1}}{1}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00002}This is followed by textbf {EVENT 2}}{4}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00003}Later, we have textbf {EVENT 3}}{9}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00004}Continuing with textbf {EVENT 4}}{16}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00005}So follows textbf {EVENT 5}}{17}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00006}Later comes textbf {EVENT 6}}{18}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00007}Followed by textbf {EVENT 7}}{21}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00008}Then comes textbf {EVENT 8}}{25}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00009}Shortly thereafter Event 8 comes textbf {EVENT 9}}{32}
indexentry{Person, Key@textsc {Person, Key}!NOOP {00010}The end: textbf {EVENT 10}}{33}
The .ind
file is
begin{theindex}
item textsc {Person, Key}
subitem NOOP {00001}In the beginning textbf {EVENT 1}, 1
subitem NOOP {00002}This is followed by textbf {EVENT 2}, 4
subitem NOOP {00003}Later, we have textbf {EVENT 3}, 9
subitem NOOP {00004}Continuing with textbf {EVENT 4}, 16
subitem NOOP {00005}So follows textbf {EVENT 5}, 17
subitem NOOP {00006}Later comes textbf {EVENT 6}, 18
subitem NOOP {00007}Followed by textbf {EVENT 7}, 21
subitem NOOP {00008}Then comes textbf {EVENT 8}, 25
subitem NOOP {00009}Shortly thereafter Event 8 comes textbf {EVENT 9},
32
subitem NOOP {00010}The end: textbf {EVENT 10}, 33
end{theindex}
Correct answer by egreg on June 9, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP