1

First an MWE and its output:

\documentclass[a4paper, 10pt]{book}

\usepackage{hyperref}
\usepackage{imakeidx}
\makeindex

\newcommand{\AddIndexLetterToTOC}[1]{\textbf{#1}\phantomsection\addcontentsline{toc}{section}{#1}}
\usepackage{filecontents}
\begin{filecontents*}[overwrite]{\jobname.mst}
headings_flag 1 %
heading_prefix "\\AddIndexLetterToTOC{"
    heading_suffix "}\\nopagebreak\n"
delim_0 "\\dotfill"
delim_1 "\\dotfill"
delim_2 "\\dotfill"
\end{filecontents*}

\begin{document}

\tableofcontents

\chapter{Test chapter}

\index{And}
\index{Book}
\index{Summer}
\index{Latex}

\pagebreak

\addcontentsline{toc}{chapter}{Index}
\printindex

\end{document}

enter image description here enter image description here

As you see, the dot spaces in Contents and Index are different, so

(question 1) is there any way to make the dot space in Index be the same as that in Contents, and to keep the dot space in Contents be unchanged?

I know an answer with the help of the tocloft package which I don't want to use since it will change some other setting in my .tex files. Or

(question 2) is there any way to make the dot space in Contents be the same as that in Index, and to keep the dot space in Index be unchanged?

By the way, the definition of \@dottedtocline for Contents is as follows:

\ifnum #1>\c@tocdepth \else
\vskip \z@ \@plus .2\p@ 
{\leftskip #2\relax \rightskip 
\@tocrmarg \parfillskip -\rightskip \parindent #2\relax 
\@afterindenttrue \interlinepenalty \@M \leavevmode 
\@tempdima #3\relax 
\advance \leftskip \@tempdima \null \nobreak 
\hskip -\leftskip {#4}\nobreak
\leaders \hbox {$\m@th \mkern \@dotsep mu\hbox {.}\mkern \@dotsep mu$}
\hfill \nobreak 
\hb@xt@ \@pnumwidth {\hfil \normalfont \normalcolor #5}\par }
\fi

and the definition for \dotfill for Index is also as follows:

\def\dotfill{\leaders\hbox to.6em{\hss .\hss}\hskip\z@ plus 1fill\kern\z@}%

But I don't know how to get the desired result after reading them.

2 Answers 2

2

The TeX primitive which generates repeated dots is

\leaders <box or rule> <glue>

If the <box> is used (this is your case, the box includes the dot) then this box is repeated in the area where <glue> is stretched.

In your first snippet, \leaders is used as

\leaders \hbox {$\m@th \mkern \@dotsep mu\hbox {.}\mkern \@dotsep mu$} \hfill

The box is constructed here more complicated way than needed (unfortunately, this is very common in LaTeX's internal sources). The dot is put in \hbox in math mode and kerns around it is expressed in mu units (math units, 1/18 em). The \@dotsep is defined in book.cls as 4.5, so we have 4.5/18 = 0.25 em here. The same can be expressed without math mode as

\leaders \hbox{\kern.25em .\kern.25em} \hfill

The width of the dot vary depending on the font used. For example, if Computer Modern at 10pt is used, then the dot has its width 2.7777pt and 1em = 10pt. The width of our box is 7.7777pt and we can express the same by

\leaders \hbox to .7777em {\hss .\hss} \hfill

You can see that dots used in the Index is created by

\leaders \hbox to .6em {\hss .\hss} \hskip 0pt plus 1fill

Note that \hfill is primitive equivalent to \hskip 0pt plus 1fill. So there is no difference. The difference is in the width of the repated boxes.

We cannot compare the width of \hbox used in the TOC with the width used in the Index without knowing about the width of the dot, which is font dependent value. I suggest to use the same width in both cases, for example replace the \leaders in the TOC macro by

\leaders \hbox to .6em {\hss .\hss} \hfill

and you have the same dots (with narrower spaces) in both: TOC and Index.

Or replace the \leaders in both, in the Index macro and in the TOC macro by

\leaders \hbox to .77777em {\hss .\hss} \hfill

and you have the same dots (with wider spaces) in TOC and Index. As mentioned above, the original usage of \leaders in the TOC macro is, unfortunately, over-killed.


(The following is edited by the OP) So to get as what is asked in the first question, modify the definition of \dotfill as follows which is equivalent to @Jinwen 's old answer:

\makeatletter
\def\dotfill{\leaders\hbox to .7777em {\hss .\hss}\hfill\nobreak}%
\makeatother

and to get as what is asked in the second question, modify the definition of \@dottedtocline as follows in your preamble.

\makeatletter
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders \hbox to .6em {\hss .\hss}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5%
                        \kern-\p@\kern\p@}%
     \par}%
  \fi}
\makeatother
6
  • Could you show how to replace the \leaders in the TOC macro in details?
    – M. Logic
    May 23, 2022 at 5:55
  • Copy \def\@dottedtocline#1#2#3#4#5{...} from the latex.ltx file, modify it and save it to your macros. Of course, your macros are surrounded by \makeatletter and \makeatother in your macro file.
    – wipet
    May 23, 2022 at 6:13
  • I wrote it clearly in the comment.
    – wipet
    May 23, 2022 at 7:16
  • The name of latex.ltx changed? And where to find the file?
    – M. Logic
    May 23, 2022 at 7:42
  • In TeXlive, I see the latex.ltx in texmf-dist/tex/latex/base/. I supposed that you know about this file because you cited from it in your OP: "By the way, the definition of \@dottedtocline for Contents is as follows".
    – wipet
    May 23, 2022 at 7:46
1

If you wish to have the same dots style everywhere in your document, you can simply define your own version. For example, in my document class, I have (of course the color is a little different):

\titlerule*[.67em]{\textcolor{gray!55}{.}}

(I think this is a command from the packages titlesec or titletoc, hopefully these packages won't break your existed settings. Anyway, this serves just as an indication, you may define your own version without using any further packages)

or the plain form

\leaders \hbox to .67em {\hss \textcolor{gray!55}{.} \hss} \hfill

(This works surprisingly well as it produces a perfect horizontal alignment)

In the .mst file there is:

delim_0 "\\IndexDotfill " % Filler between section heading and page number
delim_1 "\\IndexDotfill " % Filler between subsection heading and page number

where \IndexDotfill is defined as

\newcommand*{\IndexDotfill}
  {
    \leaders \hbox to .67em {\hss \textcolor{main-text!15!paper}{.} \hss} \hfill
  }

And in the configuration of ToC, I have something like:

\titlecontents{chapter}
      [2em] % 0em (part) + 2em
      {\addvspace{.5pc}\normalfont}
      {\contentslabel{2em}}
      {\hspace*{-2em}}
      {\titlerule*[1em]{\textcolor{gray!55}{.}}\contentspage}

The result looks like:

enter image description here

enter image description here


Old answer

As already indicated by yourself in your question, if you redefine \dotfill to be

\def\dotfill{\leaders\hbox{$\m@th\mkern\@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill\nobreak}

Then you will have the same dots as in the ToC :)

\documentclass[a4paper, 10pt]{book}

\usepackage{hyperref}
\usepackage{imakeidx}
\makeindex

\newcommand{\AddIndexLetterToTOC}[1]{\textbf{#1}\phantomsection\addcontentsline{toc}{section}{#1}}
\makeatletter
\def\dotfill{\leaders\hbox{$\m@th\mkern\@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill\nobreak}%
\makeatother
\begin{filecontents*}[overwrite]{\jobname.mst}
headings_flag 1 %
heading_prefix "\\AddIndexLetterToTOC{"
    heading_suffix "}\\nopagebreak\n"
delim_0 "\\dotfill"
delim_1 "\\dotfill"
delim_2 "\\dotfill"
\end{filecontents*}

\begin{document}

\tableofcontents

\chapter{Test chapter}

\index{And}
\index{Book}
\index{Summer}
\index{Latex}

\pagebreak

\addcontentsline{toc}{chapter}{Index}
\printindex

\end{document}

enter image description here

enter image description here

4
  • It's quite strange that I've ever tried this way while it's not successful. Maybe I miss \makeatletter and \makeatother?
    – M. Logic
    May 23, 2022 at 2:15
  • Thanks very much! Since I could accept only one answer and @wipet 's answer shows two ways, I have to accept his answer, while vote your answer once.
    – M. Logic
    May 23, 2022 at 8:27
  • @M.Logic I just updated my answer, hopefully this would be helpful to you (and thanks to this opportunity, I found that the dots in my index are not properly aligned, working on it...)
    – Jinwen
    May 23, 2022 at 8:31
  • @M.Logic I owe you a huge thank you for your question. Now that I know about \leaders I rewrote the command for the dots fill in index and the dots are finally vertically aligned :)
    – Jinwen
    May 23, 2022 at 8:55

You must log in to answer this question.

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