How to define index and multiple glossaries with the glossaries package?

Problem Statement

I would like to make an index with the courtesy of glossaries package. However, I received a message while using makeglossaries

Warning: File 'document.idx' is empty. Have you used any entries defined in glossary 'index'?

I have defined the list of index through the type of index. While the glossary lists are successful (the sourced question was after for per chapter basis, while I adapted for per part basis), I could not do this for the list of index.

MWE

 \documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
\usepackage[xindy,order=letter,nonumberlist,toc,nopostdot,nomain,nohypertypes={index}]{glossaries}
\renewcommand*{\glspostdescription}{}
\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I} % Note: No front zeros
% Code amdended from Ludovic C.@TeX.SE (http://tex.stackexchange.com/questions/123824/list-of-symbols-for-each-chapter)
\printglossary[type=glossary\arabic{part}]}
\newglossary[ilg]{index}{ind}{idx}{\indexname}
\newcommand{\newidx}[2]{\newglossaryentry{#1}[type=index,name=#1,description=\nopostdesc]}
\makeglossaries

\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \addgloss{complex number} \newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number.

\cleardoublepage
\printpartglossary

\printglossary[type=index]
\end{document}


The principle problem is with the definition of \newidx:

 \newcommand{\newidx}[2]{\newglossaryentry{#1}[type=index,name=#1,description=\nopostdesc]}


Firstly, this uses the wrong syntax for \newglossaryentry. The key=value list should be in braces {} not square brackets. The second problem is that \newidx is defined here to have two arguments but you're only using it with one argument.

The other issue here is that you're only defining entries but not indexing them. This means that every time you do \newidx{complex number}, you're just defining the entry (storing it for later use). So the correct definition should be:

\newcommand{\newidx}[1]{%
\newglossaryentry{#1}{type=index,name=#1,description=\nopostdesc}% define it
}


A similar problem occurs with your other commands like \addgloss and \addglossmath. Each label (the first argument of \newglossaryentry) should be unique, but you have a label clash with two different entries both wanting to use the label complex number. However because you're defining your entries in the document instead of the preamble the safeguard that prevents this clash has been disabled, so the complex number label always refers to the first entry defined with that label. This clash can be overcome by enforcing a prefix in the definitions of your custom commands. For example:

\newcommand{\addgloss}[2]{%
\newglossaryentry
{gls\arabic{part}:#1}% label
{type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand{\newidx}[1]{%
\newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%


The other problem is that \addgloss is defined to take two arguments, but you've used it with only one argument in:

\addgloss{complex number} \newidx{complex number}


This means that it's taking \newidx as the second argument. Since complex number has already been defined with \addgloss, this does nothing and you're left with just {complex number}, which is why it looks as though the \addgloss seems to be working when actually it isn't. It would in fact be better to define:

\newcommand{\addgloss}[2]{%
\newglossaryentry
{gls\arabic{part}:#1}% label
{type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}


Then use \refgloss to reference the entry. Like this:

\documentclass{scrbook}
\usepackage[xindy,order=letter,nonumberlist,toc,nopostdot,
nomain,nohypertypes={index},
]{glossaries}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I}
\newglossary[ilg]{index}{ind}{idx}{\indexname}

\printglossary[type=glossary\arabic{part}]}

\makeglossaries

\newglossaryentry
{gls\arabic{part}:#1}% label
{type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
\newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%

\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number.

\cleardoublepage
\printpartglossary

\printglossary[type=index]
\end{document}


The principle behind glossaries is that you first define your entries and then reference them in the document. (Think of it in terms of a bibliography where you store the data in a separate file and reference it in the document.) On the fly definitions within the document can cause problems, but these can be reduced by extending the glossaries package with glossaries-extra and use the docdef=restricted package option. For example:

\documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
\usepackage[xindy,order=letter,nonumberlist,
nomain,nohypertypes={index},
docdef=restricted
]{glossaries-extra}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I} % Note: No front zeros
\newglossary[ilg]{index}{ind}{idx}{\indexname}

\printglossary[type=glossary\arabic{part}]}

\makeglossaries

\newglossaryentry
{gls\arabic{part}:#1}% label
{type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
\ifglsentryexists{idx:#1}%
{%
\newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
}%

\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number.

\cleardoublepage
\printpartglossary

\printglossary[type=index]
\end{document}


Edit: There's no need for \cleardoublepage here as \printglossary (which is used in the definition of \printpartglossary) already issues a clear double page since it starts with \chapter* when used with the scrbook class. Also, the \glsaddall within \printpartglossary will index all defined entries at that point (including those defined in previous parts). It's redundant to keep doing this for every part of the book, and it may cause some odd behaviour.

Incidentally, you might want to switch on the number list for the index, as there doesn't seem much point in having an index without it. You might also want to change to a more appropriate style for the index (e.g mcolindex) and suppress the bold font for the index entries.

Amended MWE:

\documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
\usepackage[xindy,order=letter,
nomain,nohypertypes={index},
docdef=restricted
]{glossaries-extra}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I}
\newglossary[ilg]{index}{ind}{idx}{\indexname}

% Just suppress numberlists for the "part" glossaries
\newcommand{\printpartglossary}{%
\printglossary[nonumberlist,type=glossary\arabic{part}]}

\makeglossaries

\newglossaryentry
{gls\arabic{part}:#1}% label
{type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
\ifglsentryexists{idx:#1}%
{%
\newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
}%

\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number.

\printpartglossary

\renewcommand*{\glstreenamefmt}[1]{#1}% remove bold font in mcolindex style
\printglossary[type=index,style=mcolindex]
\end{document}

• Thank you, Dr Talbot. I remember that you are the author of these packages, while your answer is very comprehensive and useful. – SHY.John Jul 23 '16 at 13:15
• However, I have an exam on Monday. While this is an important exam and I really want to spend time one reading your answer, may I reply again at Monday afternoon (at your time)? – SHY.John Jul 23 '16 at 13:19
• @SHY.John Yes, the site should notify me if you ping me in a comment. Good luck with your exam. – Nicola Talbot Jul 23 '16 at 13:21
• I end up detour from my study to read your answer. But I caught a cold so I slept straight away after I gave a tick mark. Now I am feeling better. – SHY.John Jul 24 '16 at 4:27
• After a while of using this code, the code produces two extra pages before the glossary/ index and an extra entry referred to this last page. I tested the code provided and the MWE could replicate the same issue. – SHY.John Jul 29 '16 at 3:43