1

I want to create an index for a multi-volume book. I tried this one: How to produce a combined index from multiple documents? There are some first results, but the sorting is wrong. Here is a short example:

First volume:

\documentclass{article}
\usepackage{makeidx}
\makeindex

\newcommand\docname[1]{#1}
\let\LATEXindex\index % save old definition to prevent recursion
\renewcommand\index[1]{\LATEXindex{#1|docname{I}}}

\begin{document}

\index{word1}word1
\index{word2}word2
\index{word3}word3
\index{word4}
\newpage
\setcounter{page}{3}
\index{word1}word1
\newpage
\setcounter{page}{4}
\index{word1}word1
\index{word2}word2
\newpage
\setcounter{page}{5}
\index{word1}word1
\index{word2}word2

\printindex
\end{document}

second volume:

\documentclass{article}
\usepackage{makeidx}
\makeindex

\newcommand\docname[1]{#1}
\let\LATEXindex\index % save old definition to prevent recursion
\renewcommand\index[1]{\LATEXindex{#1|docname{II}}}

\begin{document}

\index{word1}word1
\index{word3}word3
\index{word4}word4
\newpage
\setcounter{page}{3}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\newpage
\setcounter{page}{4}
\index{word1}word1
\index{word3}word3
\newpage
\setcounter{page}{5}
\index{word2}word2
\index{word4}word4

\printindex
\end{document}

index volume:

\documentclass{article}
\usepackage{makeidx}

\newcommand\docname[2]{#1: #2}

\begin{document}

\printindex
\end{document}

As written in the above link, the two .idx documents are combined to one, I do it by:

copy /b *.idx volume_ind.idx

After that, do makeindex and latex on volume_ind.idx. It works, but the two volumes are intermixed and the sorting is wrong. It looks like this:

enter image description here

Does anybody have an idea how to get the ordering of the index entries correct? Best would be something like this:

word1 I: 1, 3-4, II: 1, 3-4

Thank you a lot!!

1

I found an answer myself. Now only one document is needed and a perl script.

\documentclass{article}
\usepackage{makeidx}
\usepackage{xpatch}
\makeindex

\makeatletter
\patchcmd{\@wrindex}{\thepage}{\volumenr-\thepage}{}{}
\makeatother

\begin{document}
\newcommand{\volumenr}{I}

\index{word1}word1
\index{word2}word2
\index{word3}word3
\index{word4}
\newpage
\setcounter{page}{3}
\index{word1}word1
\newpage
\index{word1}word1
\index{word2}word2
\newpage
\index{word1}word1
\index{word2}word2

\renewcommand{\volumenr}{II}

\index{word1}word1
\index{word3}word3
\index{word4}word4
\newpage
\setcounter{page}{3}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\newpage
\index{word1}word1
\index{word3}word3
\newpage
\index{word2}word2
\index{word4}word4

\printindex
\end{document}

The perl script eliminates the redundant roman numbers in the *.ind file, so I keep only one "I-" or "II-" in a line:

#!/usr/bin/perl
use strict;

my $src = shift @ARGV;
my $des = shift @ARGV;

open(SRC, '<', $src) or die $!;
open(DES,'>',$des) or die $!;

while (my $row = <SRC>) {
  $row =~ s/II-/II: /;          # search first "II-" in each row, replace with "II: "
  $row =~ s/(?<!%)\II-//g;      # search all "II-" in each row, delete

  $row =~ s/I-/I: /;            # search first "I-" in each row, replace with "I: "
  $row =~ s/(?<!%)\I-//g;       # search all "I-" in each row, delete
  print DES "$row";
}

close(SRC);
close(DES);

I call the perl script after makeindex and before the second latex call.

My index now looks like this:

enter image description here

0
0

To do that I recommend glossaries-extra with bib2gls. For example, if you have this three documents:

MyDoc1.tex

\documentclass{article}

\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}

%number of columns
\renewcommand{\glsxtrbookindexcols}{2}

%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}

%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}

\setfileprefix{MyDoc2.pdf}{II-}
\setfileprefix{MyDoc3.pdf}{III-}

\renewcommand*\glsxtrdisplaysupploc[5]{%
  \setentrycounter[#1]{#2}%
  \usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}

%style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
    {\tablehead{}\tabletail{}%
     \begin{supertabular}{@{}lp{20cm}}}%
    {\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}


%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}

%loading supplemental files
\GlsXtrLoadResources[
 src=index,
 type=index,
 supplemental-locations={MyDoc2, MyDoc3},% you can have more than one!
 supplemental-category={supplemental},
 supplemental-selection={all}
]

%and you can define all kinds of glossaries
\GlsXtrLoadResources[
 src=symbols,
 type=symbols,
 supplemental-locations={MyDoc2, MyDoc3},
 supplemental-category={supplemental},
 supplemental-selection={all}
]

\begin{document}
A test \gls{ind1} and another time with different output \glsuseri{ind1}. Finally a constant \gls{symb1}.

\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}

MyDoc2.tex

\documentclass{article}

\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}

%number of columns
\renewcommand{\glsxtrbookindexcols}{2}

%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}

%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}

\setfileprefix{MyDoc1.pdf}{I-}
\setfileprefix{MyDoc3.pdf}{III-}

\renewcommand*\glsxtrdisplaysupploc[5]{%
  \setentrycounter[#1]{#2}%
  \usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}

 %style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
    {\tablehead{}\tabletail{}%
     \begin{supertabular}{@{}lp{20cm}}}%
    {\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}

%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}

%loading supplemental files
\GlsXtrLoadResources[
 src=index,
 type=index,
 supplemental-locations={MyDoc1, MyDoc3},% you can have more than one!
 supplemental-category={supplemental},
 supplemental-selection={all}
]

%and you can define all kinds of glossaries
\GlsXtrLoadResources[
 src=symbols,
 type=symbols,
 supplemental-locations={MyDoc1, MyDoc3},
 supplemental-category={supplemental},
 supplemental-selection={all}
]

\begin{document}
A test in this document \gls{ind2} and a constant \gls{symb1}.

\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}

and MyDoc3.tex

\documentclass{article}

\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}

%number of columns
\renewcommand{\glsxtrbookindexcols}{2}

%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}

%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}

\setfileprefix{MyDoc1.pdf}{II-}
\setfileprefix{MyDoc2.pdf}{III-}

\renewcommand*\glsxtrdisplaysupploc[5]{%
  \setentrycounter[#1]{#2}%
  \usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}

%style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
    {\tablehead{}\tabletail{}%
     \begin{supertabular}{@{}lp{20cm}}}%
    {\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}

%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}

%loading supplemental files
\GlsXtrLoadResources[
 src=index,
 type=index,
 supplemental-locations={MyDoc1, MyDoc2},% you can have more than one!
 supplemental-category={supplemental},
 supplemental-selection={all}
]

%and you can define all kinds of glossaries
\GlsXtrLoadResources[
 src=symbols,
 type=symbols,
 supplemental-locations={MyDoc1, MyDoc2},
 supplemental-category={supplemental},
 supplemental-selection={all}
]

\begin{document}
A test \gls{ind2} and \gls{symb2}.

\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}

You will also need files for index and symbols in this example index.bib

@index{ind1, %id
 name = {Something}, %as is going to appear in the index
 user1 = {some-thing} %if you want something different in the text
}

  @index{ind2,
     name = {another thing},
     parent = {ind1}
  }

And symbols.bib

@entry{symb1,
 name = {\ensuremath{c}},
 description = {an important constant},
 sort = {constant}
}

@entry{symb2,
 name = {\ensuremath{\binom{n}{k}}},
 description = {binomial coeficient},
 sort = {binomial}
}

All of them can have a sort key, but in the index case it is correct.

Then, compile with the following chain:

pdflatex MyDoc1
pdflatex MyDoc2
pdflatex MyDoc3
bib2gls MyDoc1
bib2gls MyDoc2
bib2gls MyDoc3 
pdflatex MyDoc1
pdflatex MyDoc2
pdflatex MyDoc3

and you will get the following in the different files

enter image description here

2
  • This is more a suggestion that an answer, but it is very log for a comment Jul 26 at 20:53
  • Thank you so much for your answer, Luis Turcio. I will look at your approach in detail. Very nice detail to get an index in every volume with fitting volume-numbers.
    – Heidrun
    Jul 27 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.