2

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.

4
  • 1
    {\string\ttfamily\space #3} should work. Sep 12 at 8:20
  • @UlrikeFischer I could have sworn I tried that, but you're absolutely right. Thank you again.
    – cfr
    Sep 12 at 12:37
  • @UlrikeFischer Do I need \endgroup somewhere or does \@wrindex do that? I've copy-pasted this, but I don't really know what these commands do.
    – cfr
    Sep 12 at 12:54
  • 1
    the \endgroup is in the \@wrindex (in latex, I didn't check the various packages). Sep 12 at 13:18

0

You must log in to answer this question.

Browse other questions tagged .