1

I am writing a document with many indexed entries. I noted that this adds extra unwanted space in the text. Here is a MWE.

\documentclass{article}
\usepackage{amsthm}
\usepackage{imakeidx}
\makeindex
\newtheorem{Theorem}{Theorem}
\begin{document}
\begin{Theorem}
Here.
The statement.
\end{Theorem}
\begin{Theorem}
Here.
\index{1}
\index{2}
\index{3}
\index{4}
\index{5}
The statement.
\end{Theorem}
\end{document}

and the result is

I can solve the problem by adding a % after every \index{} , but I wonder if there is a better solution... (I have more than 2000 index entries)

1
  • 2
    index does not add space, you are adding space with the linebreak, so a % after Here. is the most natural fix Commented Oct 7, 2023 at 13:35

1 Answer 1

2

\index does not add space, you are adding space with the linebreak, so a % after Here. and the \index commands is the most natural fix, however you could insert an \unskip to remove a space before \index using a command hook as follows.

\documentclass{article}
\usepackage{amsthm}
\usepackage{imakeidx}
\makeindex
\AddToHook{cmd/index/before}{\ifhmode\unskip\fi}
\newtheorem{Theorem}{Theorem}
\begin{document}
\begin{Theorem}
Here.
The statement.
\end{Theorem}
\begin{Theorem}
Here.
\index{1}
\index{2}
\index{3}
\index{4}
\index{5}
The statement.
\end{Theorem}
\end{document}
3
  • @campa no the space hacks essentially move a single space before to a single space after but then don't stop subsequent ones building up. Commented Oct 7, 2023 at 14:49
  • @campa sure, but the latter case is the one the space hacks are designed for Commented Oct 7, 2023 at 15:50
  • Always adding \unskip before an index entry isn't always the safest approach. There are situations when it's more reliable to input the index term before the term in the text, particularly when it's unclear where a page may break, and it's desirable to avoid an off-by-one page number in the index. %, though a nuisance, is more flexible. Commented Oct 7, 2023 at 20:23

You must log in to answer this question.

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