I'm having a problem with the below code. It's the smallest I could make the example.
I'm working on a document with an index of persons, and have for this purpose created a command for each person. It has an optional parameter which overrides the "default name". So \JensHansen prints "Jens Hansen" and \JensHansen{Jens} prints "Jens" but both create entries in the index.
However, in the first case, there is no space after Hansen, which clashes when the next word is just a normal word as in the last lines below becoming "Jens Hansenliked to walk the country roads" or "A man told Jens Hansento leave"...
If I put a space in the command, it results in the quotes & apostrophes being preceded by a space. I've put an \underline in the command for legibility but it will be removed in the final version.
It likely has to do with the \Fallback command's optional argument but I'm not sure how...
\documentclass[a4paper]{memoir}
\usepackage{xparse}
\usepackage[makeindex]{imakeidx}
\makeindex
\DeclareDocumentCommand\Fallback{ m m }{\IfNoValueF{#1}{#1}\IfNoValueT{#1}{#2}}
% underline just for clarity
\DeclareDocumentCommand\person{ m g }{\underline{\index{#1}{\Fallback{#2}{#1}}}}
\DeclareDocumentCommand\JensHansen{ g }{\person{Jens Hansen}{#1}}
\begin{document}
There was a man named \JensHansen, who usually just went by \JensHansen{Jens}.
Sometimes, he even went by ``\JensHansen{Nysteds Skraek}!''
\JensHansen{Jens}' father was named Hans.
\JensHansen liked to walk the country roads. % space following Hansen is missing!
A man told \JensHansen to leave. % again!
\printindex
\end{document}
{..}for optional argument is a bit, hmm, I am looking for the word, perverse ? braces have such a deep role in the defaultTeXset-up... the usual convention is to use brackets[...]. – Jan 09 '16 at 12:19