I am trying to markup certain words in my text and automatically commit them to the index, with the option to modify the index entry. The \newcommand
approach was not satisfying, as I did not manage to set the default value of the first argument to be the second.
With \NewDocumentCommand
I managed to set the options properly, but it introduced an unwanted space when followed by punctuation. Can anyone explain, why this occurs and how to fix it?
Here is a minimal working example:
\documentclass{article}
\usepackage{xparse}
\newcommand{\neuold}[2][]{{\sffamily\textbf{ #2}}\index{#1 #2}}
\NewDocumentCommand{\neu}{m O{#1}}
{
\IfNoValueTF{#2}
{{\sffamily\textbf{#1}}}
{\sffamily\textbf{ #1}\index{ #2}}
}
\begin{document}
blabla \neu{test}, blabla \neuold{test}, blabla
\end{document}
\NewDocumentCommand
. It should be…O#1}}{%… \index{ #2}}%
– David Purton Oct 4 '20 at 13:05