Consider the following MWE:
\documentclass{ltxdoc}
\makeatletter
\newcommand*\myindex[3]{%
\doc@providetarget
\index{#1:\levelchar#2\actualchar{\ttfamily#3}\doc@handleencap{usage}}%
}
\makeatother
\makeindex
\begin{document}
\DescribeEnv{myenv}\marg{argument}\par
\myindex{environments}{myenv}{myenv}
\end{document}
While \DescribeEnv
indexes myenv
with just a single space before myenv
, \myindex
adds two.
Contents of .idx
:
\indexentry{myenv={\ttfamily myenv} (env.)|hdclindex{2}{usage}}{1}
\indexentry{environments:>myenv={\ttfamily myenv}|hdclindex{2}{usage}}{1}
\indexentry{environments:>myenv={\ttfamily myenv}|hdclindex{3}{usage}}{1}
This results in twice as many index entries as there should be.
Thanks to Heiko Oberdiek's answer I can prevent this using
\documentclass{ltxdoc}
\makeatletter
\newcommand*\myindex[3]{% ateb Heiko Oberdiek https://tex.stackexchange.com/a/412521/
\doc@providetarget
\@bsphack
\begingroup
\@sanitize
\@wrindex{#1:\levelchar#2\actualchar{\string\ttfamily #3}\doc@handleencap{usage}}%
}
\makeatother
\makeindex
\begin{document}
\DescribeEnv{myenv}\marg{argument}\par
\myindex{environments}{myenv}{myenv}
\end{document}
which gives me
\indexentry{myenv={\ttfamily myenv} (env.)|hdclindex{2}{usage}}{1}
\indexentry{environments:>myenv={\ttfamily myenv}|hdclindex{2}{usage}}{1}
\indexentry{environments:>myenv={\ttfamilymyenv}|hdclindex{3}{usage}}{1}
but now I have no spaces and I cannot figure out how to keep exactly one. In case it matters, I'm actually doing this with expl3
syntax on, though I could avoid that if necessary.
{\string\ttfamily\space #3}
should work.\endgroup
somewhere or does\@wrindex
do that? I've copy-pasted this, but I don't really know what these commands do.