2

I'm creating an index using the makeidx package, and would like to change the default behavior:

key, number

to something like:

key ..................................................................................................... number

where "number is on the right border of the page, and the points could be spaces or points.

Currently I'm using a code similar to:

\documentclass[a4paper, 11pt]{article}
\usepackage{makeidx}
\usepackage[columns=1]{idxlayout}
\makeindex

% more packages...

\begin{document}

Cover page

% index
\clearpage
\renewcommand\indexname{Table of Contents}
\printindex

\clearpage
Introduction
\index{Introduction}

\clearpage
Document body

\end{document}

The included files have call to index as usual: eg. \index{1. Introduction}. The file is compiled with makeindex and pdflatex.

But I have no idea on how to make improvements or this, nor I have found similar questions posted on this topic. Any help is appreciated.

3
  • 1
    Welcome to TeX.SE! As it stands, you may not get much attention as your code can't simply be copied and pasted into a file for testing since the included files are missing. You may find it simpler to just put a single \index{test} in your document and omit the \include instances. Alternatively, you can use the testidx package, which is designed for testing index styles: \documentclass{article}\usepackage{makeidx}\usepackage{testidx} \makeindex \begin{document} \testidx \printindex \end{document}. That makes it easier to test and propose a solution. Dec 11, 2018 at 16:49
  • Are you using \index to simply create a table of contents? Is there any particular reason why you're not just using \tableofcontents? Dec 11, 2018 at 21:44
  • I guess it's been a mixture of a translation problem and lack of experience with latex. I'm just reading this website and seems that \tableofcontents provides the desired behavior. I'll try it, and if you write it in the form of an answer I'll accept it ;) Thank you! Dec 12, 2018 at 9:04

1 Answer 1

2

In this particular case, it's actually a table of contents, which can be simply be produced with \tableofcontents:

\documentclass{article}

\begin{document}
\tableofcontents

\section{Introduction}
This is an introductory section.

\subsection{Sample Subsection}
This is a sample sub-section.

\end{document}

This doesn't require any external tools, just two instances of LaTeX (pdflatex, xelatex or lualatex, as appropriate).

image of document

Here's a slightly longer example:

\documentclass{book}

\title{Sample Document}
\author{Ann Other}
\begin{document}
\maketitle

\frontmatter
\tableofcontents

\chapter{Preface}
This is the preface.

\mainmatter

\part{Sample Part}

\chapter{Introduction}
This is an introductory chapter.

\section{Sample section}
This is a sample section.

\subsection{Sample Subsection}
This is a sample sub-section.

\appendix
\chapter{Sample Appendix}
This is a sample appendix.

\chapter{Another Sample Appendix}
This is another sample appendix.

\end{document}

The table of contents now looks like:

image of document

This deals with all the numbering automatically. (\frontmatter switches to lower case Roman numerals for the page numbers and also suppresses the numbering of the section commands. \mainmatter switches back.)

If you were to use \index, makeindex would order alphabetically. For example:

\documentclass{book}

\usepackage{makeidx}

\makeindex

\title{Sample Document}
\author{Ann Other}

\begin{document}
\maketitle

\frontmatter
\printindex

\chapter{Preface}\index{Preface}
This is the preface.

\mainmatter

\part{Sample Part}\index{I Sample Part}

\chapter{Introduction}\index{1 Introduction}
This is an introductory chapter.

\section{Sample section}\index{1.1 Sample section}
This is a sample section.

\subsection{Sample Subsection}\index{1.1.2 Sample subsection}
This is a sample sub-section.

\appendix
\chapter{Sample Appendix}\index{A Sample Appendix}
This is a sample appendix.

\chapter{Another Sample Appendix}\index{B Another Sample Appendix}
This is another sample appendix.

\end{document}

This produces:

image of index

To answer your actual question of changing the separator between the indexed item and the corresponding number, you need to create a style file that changes the appropriate delimiters:

\documentclass{article}

\usepackage{makeidx}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.ist}
delim_0 "\\dotfill"
delim_1 "\\dotfill"
delim_2 "\\dotfill"
\end{filecontents*}

\makeindex

\begin{document}
Test\index{test}\index{test!subitem}\index{test!subitem!subsubitem}.

\printindex
\end{document}

The call to makeindex now needs to include -s with the .ist file created by the filecontents environment. For example, if the document is called myDoc.tex then:

pdflatex myDoc
makeindex -s myDoc.ist myDoc
pdflatex myDoc

image of index

The index defaults to 2 column format, which is why the dots don't span the entire page width. You may prefer to use imakeidx instead of makeidx to make it easier to customize.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .