0

This question already has an answer here:

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}

marked as duplicate by Joseph Wright Jan 9 '16 at 8:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

This is, I presume, simply the basic TeX fact that control words \foo should be keyed \foo{} bar or \foo\ bar for the space after \foo not to be removed by TeX tokenization parser.

Look perhaps the xspace package. Or at least read its documentation as I presume it explains the underlying facts (which should be in any basic document introducing to LaTeX).

  • Using an empty {} will cause the commands to render nothing (because I'm now providing an empty replacement for the default name). So I guess I'll have to write the commands differently, but how? – meide Jan 9 '16 at 11:11
  • glad it helped. As per your first comment, using {..} for optional argument is a bit, hmm, I am looking for the word, perverse ? braces have such a deep role in the default TeX set-up... the usual convention is to use brackets [...]. – user4686 Jan 9 '16 at 12:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.