3

Feeding

\documentclass{article}
\pagestyle{empty}
\usepackage{makeidx}
\makeindex
\begin{document}%
\newlength{\myLen}\setlength{\myLen}{33em}%
\noindent 1st line. Plain \(B\), no index:\\
\hspace*{\myLen}\(\Sigma\to B\)\\
\noindent 4th line. Index before plain B:\\
\hspace*{\myLen}\(\Sigma\to \index{B}B\)\\
\noindent 7th line. Index after plain B:\\
\hspace*{\myLen}\(\Sigma\to B\index{B}\)
\printindex
\end{document}

to the standard pdflatex-makeindex loop or lualatex-makeindex loop produces

output

As you see, line 6 starts with a spurious horizontal space before 𝐵.

  1. Where does this space come from?

  2. How to hack \index to remove the spurious horizontal space if we wish to continue using \index before the indexed math item (e.g., because some package uses \index internally)?

A known workaround is putting \index after the indexed term; no question about it.

Similar issue: https://www.dickimaw-books.com/bugtracker.php?key=189 .

2 Answers 2

3

TL;DR

Reverse the order

\Sigma\to B\index{B}

as there won't be a line/page break between B and the index entry. Or add braces

\Sigma\to{\index{B}B}

What's the problem?

A minimal example showing the issue, to be compiled with pdftex (or also with tex to see that it's a problem with the basic tool):

\hsize=0pt
\parindent=0pt
\overfullrule=0pt % no distractions
\hfuzz=\maxdimen  % no warnings

\nopagenumbers
\tracingall

$A\to B$

$A\to\special{}B$

\bye

enter image description here

Here's the trace for the first paragraph:

...\hbox(0.0+0.0)x0.0
...\mathon
...\teni A
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\tensy !
...\penalty 500
...\glue(\rightskip) 0.0
..\penalty 300
..\glue(\baselineskip) 5.16669
..\hbox(6.83331+0.0)x0.0
...\teni B
...\kern0.50172
...\mathoff
...\penalty 10000
...\glue(\parfillskip) 0.0 plus 1.0fil
...\glue(\rightskip) 0.0

and for the second one

...\hbox(0.0+0.0)x0.0
...\mathon
...\teni A
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\tensy !
...\penalty 500
...\glue(\rightskip) 0.0
..\penalty 300
..\glue(\baselineskip) 5.16669
..\hbox(6.83331+0.0)x0.0
...\special{}
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\teni B
...\kern0.50172
...\mathoff
...\penalty 10000
...\glue(\parfillskip) 0.0 plus 1.0fil
...\glue(\rightskip) 0.0

You might use \write-1{} instead of \special and you'd get the same: the main ingredient for this recipe is any whatsit.

What happens? The whatsit is ignored as far as automatic computation of math spacing is concerned, so from A\to\special{}B you get

Ord Rel W Ord

(where W stands for ‘whatsit’) but spaces and penalties are inserted as

Ord \thickmuskip Rel \relpenalty W \thickmuskip Ord

where \relpenalty allows a line break before the whatsit, so it and the space go on the next line, rather than the expected

Ord \thickmuskip Rel \relpenalty \thickmuskip W Ord

The TeXbook is silent about treatment of whatsits in math lists; however the final result makes sense even if unexpected, because glue is attached to math atoms, not to whatsits.

Adding braces around the whatsit will turn it into an Ord atom and solve the problem. Including the following symbol in the braces will do as well and is better for the specific case.

However, adding the \index command after the symbol seems even better, because no line break is allowed there.

4
  • Thanks! How about \indexing relations, e.g., certain right arrows? We might need stuff such as, logically speaking, A\index{A} \index{myArrow}\to B\index{B} \index{myArrow}\to C\index{C} \index{myArrow}\to D\index{D}. Where should the various \index commands go to avoid spurious-space issues?
    – user224332
    Commented Nov 6, 2021 at 17:00
  • @GeekestGeek I’d say \mathrel{\index=}=}
    – egreg
    Commented Nov 6, 2021 at 18:32
  • Sorry, I don't get it. \mathrel{\index=}=} has one opening { but two closing }.
    – user224332
    Commented Nov 6, 2021 at 18:39
  • 1
    @GeekestGeek Sorry, typing on the phone is irksome: \mathrel{\index{=}=}.
    – egreg
    Commented Nov 6, 2021 at 20:59
2

you can put a brace group around the \index{...}

enter image description here

\documentclass{article}
\pagestyle{empty}

\usepackage{makeidx}
\makeindex
\begin{document}%
\newlength{\myLen}\setlength{\myLen}{33em}%
\noindent 1st line. Plain \(B\), no index:\\
\hspace*{\myLen}\(\Sigma\to B\)\\
\noindent 4th line. Index before plain B:\\
\hspace*{\myLen}\(\Sigma\to {\index{B}}B\)\\
\noindent 7th line. Index after plain B:\\
\hspace*{\myLen}\(\Sigma\to B\index{B}\)
\printindex
\end{document}

when used in this context the \write node ends up allowing a line break between the arrow and the \thickmuskip that follows it so you get a thickmuskip at the start of the line.

3
  • 1
    I don't think that's the cause, because \@bsphack does nothing in math mode. If I change \index{B} into \special{}, then the behavior is the same. It's the whatsit that ruins the business.
    – egreg
    Commented Nov 5, 2021 at 23:33
  • 1
    @egreg well yes it;s the whatsit in the end but I was thinking it''s moved the space but I guess not, I'll reword Commented Nov 5, 2021 at 23:35
  • 1
    Thanks! Putting braces around the whole thing, as in {\index{B}B}, would work too, wouldn't it? Or are there any adverse effects when compared to {\index{B}}B?
    – user224332
    Commented Nov 6, 2021 at 1:10

You must log in to answer this question.