#StackBounty: #memoir #indexing preindexhook with math changing parens to spacing commands

Bounty: 50

In a book-length document with memoir, I’m getting the following peculiar output from this code for the beginning of the index:

renewcommand{indexname}{Index}
renewcommand{preindexhook}{%
emph{%
For $d$emph{-ball},  $d$emph{-bounded set}, $d$emph{-open set},  $(d,d')$emph{-continuous map}, 
etc., see the corresponding entry emph{ball}, emph{bounded set}, emph{open set}, emph{continuous map}, etc.%
vskiponelineskip}%
}
printindexcleardoublepage%

parens in preindexhook arg changed to dims?

The surprise is not only that the left- and right-parentheses from $(d,d')$emph{-continuous map} have disappeared, but also that they have been replaced by -.04em and -.15em, respectively.

The peculiar output occurs no matter whether I use pdflatex or xelatex (which, as the example below indicates, determines which fonts are used); thus the error occurs with both newtxtext/newtxmath and TeX Gyre Termes/Tex Gyre Termes Math.

Environment of the actual book:

I’m using MacTeX/TeXLive 2020, with all current updates applied.

Typically, I just run "Typeset" on the root source file topology.tex with engine pdflatexmk inside TeXShop.app.
The effect should be the same as successively running:

pdflatex topology
bibtex topology
pdflatex topology
pdflatex topology
makeindex topology.nlo -s topology.ist -o topology.nls
makeindex names.idx -s names.ist -o names.ind
makeindex -s topology.ist topology
pdflatex topology
pdflatex topology

Here is the beginning of the .ind file for the book:

begin{theindex}
begingroup
  medskiphfilllargesffamilybfseriesifthenelse{boolean{colorsecs}}{color{myseccolor}}{} Ahfillendgroupmedskipnopagebreak

item idxmark{absolute-value function}, hyperpage{17}, 
        hyperpage{323}
item idxmark{abstract quotient map}, hyperpage{419}

Question: How can that be fixed?

Failed attempts to fix:

The following attempts failed to correct the issue:

  • protecting each parenthesis
  • usepackage{ragged2e) and changing renewcommand{preindexhook}{... to renewcommand{preindexhook}{RaggedRight...
  • replacing ( and ), respectively, with lparens and rparens (from mathtools)
  • deleting , from $(d,,d')$

Simple example?

I’m unable to reproduce that output in any simple example short of the entire 600+-page book!

Below is such an example, where the output is OK, but with all the relevant code (I hope) and probably some totally irrelevant code (but included "just in case"):

% book.tex

RequirePackage{filecontents}
begin{filecontents}{book.ist}
% book.ist -  MakeIndex style file
% output main entry <entry> as: item idxmark{<entry>}, 
item_0  "n\item \idxmark{"
delim_0 "}, "
% not forgetting the subitem case
item_x1 "} n \subitem "
headings_flag 1
heading_prefix "{\medskip\hfill\large\textsf{\textbf{"
heading_suffix "}}\hfill}\medskip\nopagebreakn"
end{filecontents}

documentclass{memoir}

RequirePackage[T2A,T1]{fontenc}
RequirePackage{amsmath} % avoid dddot clash
RequirePackage{mathtools} % load now to avoid unicode-math clash
RequirePackage{amsthm} % load now to avoid openbox clash
usepackage{iftex}

ifTUTeX
  usepackage{fontspec}
  usepackage{polyglossia}
  usepackage{unicode-math}
  setdefaultlanguage[variant=american]{english}
  setotherlanguages{french,german,polish,russian}
  setmainfont{TeX Gyre Termes}[Scale=1.0]
  defaultfontfeatures{Scale=MatchLowercase, Ligatures=TeX}
  setmathfont{TeX Gyre Termes Math}
  newfontfamily{cyrillicfont}{PT Serif}
  newcommand{textsi}[1]{textsc{#1}}
  newfontfamily{xcheckmarkfont}{LibertinusSerif-Regular.otf}
else
  usepackage[russian,french,ngerman,polish,main=english]{babel}
  babeltags{russian=russian,french=french,german=ngerman,polish=polish}
  RequirePackage{substitutefont}   % to allow non-default Cyrillic
  substitutefont{T2A}{rmdefault}{Tempora-TLF} % for Cyrillic
  RequirePackage[theoremfont]{newtxtext}
  RequirePackage[vvarbb]{newtxmath}%
fi  

% Additional font series macros
% light series
DeclareRobustCommandltseries
  {not@math@alphabetltseriesrelax
   fontseriesltdefaultselectfont}
%
newcommand{ltdefault}{l}
%
DeclareTextFontCommand{textlt}{ltseries}
% heavy(bold) series
DeclareRobustCommandhbseries
  {not@math@alphabethbseriesrelax
   fontserieshbdefaultselectfont}
newcommand{hbdefault}{hb}
DeclareTextFontCommand{texthb}{hbseries}

usepackage{microtype}
usepackage{ragged2e}

%% PAGE DIMENSIONS
setstocksize{11in}{8.5in}
setpagecc{55.25pc}{38pc}{*}
settypeblocksize{45pc}{28pc}{*}
setlrmargins{*}{*}{*}
setulmargins{*}{*}{*}
checkandfixthelayout

%% PENALTIES
% set clubpenalty, etc. to distinctive values for use
 % in tracing page breaks.
 clubpenalty=9996
 widowpenalty=9999
 brokenpenalty=4991
 predisplaypenalty=10000
 postdisplaypenalty=1549
 displaywidowpenalty=1602

nouppercaseheads
makepagestyle{mypage} 
makeheadrule{mypage}{textwidth}{normalrulethickness}
makeevenhead{mypage}{bfseriessffamilythepage}{}{bfseriessffamilyleftmark}
makeoddhead{mypage}{bfseriessffamilyrightmark}{}{bfseriessffamilythepage} 
addtopsmarks{mypage}{}{%
  % insert "Chapterspace" & "Sectionspace" if desired as 4th argument:
  createmark{chapter}{left}{shownumber}{}{enspace} 
  createmark{section}{right}{shownumber}{}{enspace}
}
makepsmarks{mypage}{%
  createmark{chapter}{both}{shownumber}{}{enspace} % use both not left
  createmark{section}{right}{shownumber}{}{enspace}
  createplainmark{toc}{both}{contentsname}
    createplainmark{shorttableofconents}{both}{shorttocname}
      createplainmark{longtableofcontents}{both}{longtocname}
  createplainmark{lof}{both}{listfigurename}
  createplainmark{lot}{both}{listtablename}
  createplainmark{bib}{both}{bibname}
  createplainmark{index}{both}{indexname}
  createplainmark{glossary}{both}{glossaryname}
  createplainmark{nomenclature}{both}{nomname}
}
providecommandgobbleone[1]{}
newcommand{idxmark}[1]{{letgobbleonerelaxmarkboth{sffamilybfseries#1}{sffamilybfseries#1}}#1}
makepagestyle{index}
makeheadrule{index}{textwidth}{normalrulethickness}
% Fix for babel error by daleif (Lars Madsen}
% https://tex.stackexchange.com/a/501483/13492
makeevenhead{index}%
{letgobbleonerelaxsffamilybfseriesrightmark}%
{sffamilybfseriesMakeTextUppercase{indexname}}%
{letgobbleonerelaxsffamilybfseriesleftmark}
makeoddhead{index}%
{letgobbleonerelaxsffamilybfseriesrightmark}%
{sffamilybfseriesMakeTextUppercase{indexname}}%
{letgobbleonerelaxsffamilybfseriesleftmark}
makeevenfoot{index}{}{sffamilythepage}{}
makeoddfoot{index}{}{sffamilythepage}{}  

makechapterstyle{mychapter}{%
  renewcommand{chapnamefont}{sffamilybfseries}
  renewcommand{chapnumfont}{fontsize{58}{64}selectfontsffamilybfseries}
  renewcommand{chaptitlefont}{Hugesffamilybfseries}
  %
  settoheight{midchapskip}{chapnumfont 1}
  setlength{midchapskip}{0pt}
  setlength{afterchapskip}{48pt}
  %
  renewcommand{printchaptername}{%
  chapnamefontcenteringMakeUppercase{@chapapp}par}
  %
  renewcommand*{printchapternum}{%
  vskip -8ptcentering chapnumfontthechapter} % or omit the vskip?
  %
  renewcommand{afterchapternum}{%
  parnobreakvskip midchapskip%
  centeringrule{0.825textwidth}{normalrulethickness}%
  newline}
  renewcommand{printchaptertitle}[1]{centeringchaptitlefont ##1}
}
chapterstyle{mychapter}

usepackage{blindtext}

makeindex

begin{document}

mainmatter

pagestyle{mypage}

Blindtext[1]

Alpacaindex{abc}
Zebraindex{zombie}% to fill up index page...
    index{a}index{aa}index{aaa}index{aaaa}index{aaaaa}index{aaaaaa}index{aaaaaaa}index{aaaaaaaa}
    index{b}index{bb}index{bbb}index{bbbb}index{bbbbb}index{bbbbbb}index{bbbbbbb}index{bbbbbbbb}
    index{c}index{cc}index{ccc}index{cccc}index{ccccc}index{cccccc}index{ccccccc}index{cccccccc}
    index{d}index{dd}index{ddd}index{dddd}index{ddddd}index{dddddd}index{ddddddd}index{dddddddd}
    index{e}index{ee}index{eee}index{eeee}index{eeeee}index{eeeeee}index{eeeeeee}index{eeeeeeee}
    index{f}index{ff}index{fff}index{ffff}index{fffff}index{ffffff}index{fffffff}index{ffffffff}
    index{g}index{gg}index{ggg}
    index{abcdefghijklmnopqustuvwxyzabcdef}
    index{zbcdefghijklmnopqustuvwxyzabcdef}


backmatter

pagestyle{index}

setlength{columnsep}{1em}
renewcommand{indexname}{Index}
renewcommand{preindexhook}{%
emph{%
For $d$emph{-ball}, $d$emph{-bounded set}, $d$emph{-open set},  $(d,,d')$emph{-continuous map}, 
etc., see the corresponding entry emph{ball}, emph{bounded set}, emph{open set}, emph{continuous map}, etc.%
vskiponelineskip}%
}
printindexcleardoublepage%

end{document}

Added:

I also tried in the "simple example" above replacing the preamble by the actual preamble of my book-length document (actually, an inputted .tex file which contains a bunch of usepackage commands for the several files constituting my preamble — a total of 16 files with some 120K). But still that does not cause the peculiar problem!

Possible work-around:

The following work-around seems to cure the problem. But I’m unhappy about the way that I had to fake the pair of parentheses, which really ought to be inside the inline math!

renewcommand{preindexhook}{%
emph{%
For $d$emph{-ball},  $d$emph{-bounded set}, $d$emph{-open set}, emph{(}$d,d'$emph{)hspace{2pt}-continuous map}, 
etc., 
see the corresponding entry emph{ball}, emph{bounded set}, emph{open set}, emph{continuous map}, etc.%
vskiponelineskip}%
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.