Index

In the last portion of a book or of a large document, we see an index. An index is quite helpful in finding a topic of one’s interest. Latex also provides us with features to create an index for ourselves. Look at the code written below.

Output:

Index and Glossary in LaTex
Index and Glossary in LaTex
Index and Glossary in LaTex

In the preamble, we must include the imakeidx package and \makeindex command. The package imakeidx gives a lot of customization options. Instead of imakeidx, we also use the makeidx package. However, the latter does not provide a lot of customization possibilities. In the absence of \makeindex command, an index will never work (try to comment on this command see the output).

The command \index{entry} will render the output on the .idx page (index) along with the page number on which the entry is passed to the parameter of the command. This .idx is generated only when we put \makeindex in the preamble. The \printindex command will render the index. The position of this command decides where the index will be rendered in our document. In the code, we have written it just above the \end{document}. Therefore, the index will be generated at the end of the document.

In most of the codes in LaTex, we will see the \printindex command is written at last because the index in a book, a document, is generally placed at the end.

To make entries and subentries with \index{} command, we use an exclamation mark (!) to separate entries, i.e., \index{main! sub}. Notice how lesser and firmament are getting render on the index page. LaTex supports three levels of entries, which are main, sub and subsub. Therefore, \index(main!sub!subsub) will print

Index and Glossary in LaTex

Customization in Index

Ranges and columns size in Index

Let us play with the index a bit more. The index display is based on the default settings. We can tweak these settings as per our needs. Along with the \makeindex command, we can give change the column size, the name of the index, etc.

In the above code, replace the command \makeindex with \makeindex[columns=3, title = Customized Index] and observe the output.

Suppose a topic is spread among 4 to 5 pages. Then to render the topic on the index page, it becomes more suitable to write the range of pages rather than pointing out each and every page. Suppose a topic, refraction of light, start from page 1 and ends on page 3. Then the code to index the topic will be

to render the output as following.

Index and Glossary in LaTex

We see that on the first-page \index{…|(}  is used. This marks the beginning of the range, and \index{…|)} marks the end of the range used on the third page.

Visual Presentation in Index

We can also control the presentation while indexing. The usual syntax to control the presentation is key@visualtype. Look at the examples given below.

Index and Glossary in LaTex

To typeset page number, the syntax is key|visualtype. So, the above code can be re-written as

to get the following output.

Styles Files

Like in HTML, we can create our own CSS (Cascading Style Sheet) file, in LaTex also a style file can be created and included in our main file. The extension of the style file will be .ist. This file contains some key-value pairs that are required to format the index page. Let us create our own style page and use it as our code.

In the above code, replace the \makeindex command with \makeindex[columns=3, title=Customized Index, options= -s customStyle.ist]. The options parameter in the command \makeindex is used to include a file. In the file customStyle.ist, add the following content:

To get the output as follows.

Index and Glossary in LaTex

The first line (headings_flag 1) is responsible for the group headers (A, G, M, S etc.). The second line (heading_prefix …..) does the formatting of the group headers. \Huge is the font size, and we are using a typewriter family. \textbf{“heading_suffix “} is responsible for the boldface of the group headers. Item_0 key is used for formatting the contents of the group headers.  delim_* acts as the delimiter between the key and the page number. The first delimiter delim_0 is set to \dotfill, which is responsible for dots between the key and the page number. _0 tells us that the delimiter is for the first level of entries in \index command (remember we have three levels of entries in the \index command). Similarly, delim_1 is for the second level entries, and delim_3 is for the third level entries. Notice, for the delim_1 value, is \hfill, and hence there will be blank spaces between the key and the page number (firmament       2).

Adding Index in the Table of Contents

Look at the code mentioned below.

The output of the above program will be in three pages.

On the first page:

Index and Glossary in LaTex

On the second page:

Index and Glossary in LaTex

On the third page:

Index and Glossary in LaTex

Notice, we have used intoc in the third parameter to put the index title in the table of contents (take it from the name, intoc = in the table of contents).

Glossary

A glossary a list of terms along with its definition arranged in alphabetical order. The list of terms is of a particular domain or field. A glossary is sometimes also called a brief dictionary.  Typesetting a glossary is somewhat similar to typesetting an index. Look at the code and try to establish an analogy with the codes of index typesetting.

The above code will render the following.

On the first page:

Index and Glossary in LaTex

On the second page:

Index and Glossary in LaTex

In the preamble, we must include the package glossaries and the command \makeglossaries. To make entries in the glossary, the command \newglossaryentry{entry-name} is used. To make the glossary function, each entry-name must be referenced in the document using \gls{} command. For rendering, the command \printglossaries is used. There are few variations of \gls{} command. Let us discuss a few of them.

\gls{cat} – It will print the term in lowercase (cat).

\Gls{cat} – It will print the first letter of the term in uppercase and the rest in lowercase (Cat).

\glspl{cat} – Similar to the command \gls{}. It will append the letter ‘s’ to give the plural form (cats).

\Glspl{cat} – Similar to \Gls{}. It will also render in plural form (Cats).

Acronyms in Glossary

Now, it is the time to explore into the world of glossary to learn about acronyms. An acronym is a word or set of letters used to elaborate a lengthy phrase. Let us learn how to write acronyms in our code.

Output:

Index and Glossary in LaTex
Index and Glossary in LaTex

The output will consist of two pages since we have used \pagebreak command.

Notice the use of option acronym when we include package glossaries.

The command \newacronym is used for each new acronym. We see three entries along with the \newacronym command. Let us discuss each of them by taking the reference of the above-written code.

The first one {hcf} is the label. The second one {HCF} is the acronym. Acronyms are usually written in block letters. The third and last one is the definition {Highest Common Factor} for which the acronym HCF is used. To do the reference work for the label, we have used the following commands:

\acrshort{ } – This commands simply prints the acronym. \acrshort{hcf} will display HCF.

\acrlong{} – This command prints the definition of the acronym. \acrlong{hcf} will print the definition, Highest Common Factor.

\acrfull{} – This command will print the definition as well as its corresponding acronym. \acrfull{hcf} will print Highest Common Factor (HCF).

\printglossary[type=\acronymtype] is used for rendering the glossary. The option [type=acronymtype] is telling that this glossary is of acronym type. This improves the readability of code. Because of this option, we see the title “Acronyms” on the glossary page. Feel free to remove it if one feels it is making the code verbose. Removing the option changes the title to “Glossary”.

Note1: To change the title of our own choice, use the title in the parameter of the command \printglossary, i.e. (\printglossary[title=”My own Acronyms”]).

Note2: To include the glossary in the table of contents, we should include option toc while including package glossaries as

\usepackage[toc]{glossaries}.

It is obvious that the command \tableofcontents should be used to render the table of contents.

Pin It on Pinterest

Share This