0

I have a command \persName{key} that expands to the Name of a person represented by a key-string.

I would like to create a command \eindex that can have this command as argument, expanding it fully to the name of the person and then using this as argument to \sindex (from the splitidx-pacakge).

\NewDocumentCommand{\persName}{m}{%
    % Only an example. Is more complicated than that.
    John Doe (#1)%
}%

\NewDocumentCommand{\eindex}{o m}{%
    \edef\temp@a{#2}
    \IfValueTF{#1}{%
        \sindex[#1]{\temp@a}%
    }{%
        \sindex{\temp@a}%
    }%
}%

But this solution does not work as intended. If I use \eindex[names]{\persName{Marc}} it enters

\indexentry{\persName {Marc}}{1}

into the .idx file. While creating the index, this will get expanded to "John Doe (Marc)", but is ordered under "M" in the index, not "J".

I would like to have \eindex expand the mandatory argument prior to entering it in the .idx-file like this:

\indexentry{John Doe (Marc)}{1}

More complicated version of \persName:

I use LuaLaTeX and defined a Lua-function (in the external package "Persons") to gather the name of the person in question from an addressbook.

\define@cmdkey[pers]{pers}[cT@]{instance}[]{}%
\define@cmdkey[pers]{pers}[cT@]{organization}{}%
\NewDocumentCommand{\persName}{O{} m d() o}{%
    \presetkeys[pers]{pers}{instance=nil}{}%
    \setkeys[pers]{pers}{#1}%
    \begingroup
        \if@emphasize%
            \bfseries%
        \fi%
        \if@color%
            \color{\@pers@color}%
        \fi%
        \luaexec{
            local _case
            \csname IfValueTF\endcsname{#3}{%
                _case = tostring( '\luatexluaescapestring{#3}' )
            }{}%
            local _form
            \csname IfValueTF\endcsname{#4}{%
                _form = tostring( '\luatexluaescapestring{#4}' )
            }{}%
            local _key = tostring( '\luatexluaescapestring{#2}' )
            local _instance = tonumber( '\luatexluaescapestring{\cT@instance}' )
            tex.sprint( Persons.name( _key, { form=_form, case=_case, instance=_instance, } ) )
        }%
    \endgroup%
}%
  • 2
    When you do \edef\temp@a{\persName{Mark}} no expansion is actually performed, because \persName is protected against expansion in \edef. I'm afraid you need to show the “more complicated” working of \persName if you want more help. – egreg Mar 27 '18 at 23:01
  • 2
    That wouldn’t happen if you defined \persName with \DeclareExpandableDocumentCommand (or, for that matter, with \newcommand) instead of with \NewDocumentCommand; but this may have undesired side effects, so I wouldn’t recommend doing so unless you provide further context for your question. – GuM Mar 27 '18 at 23:11
  • I edited the question to add the "more complicated" version of \persName. Since the command has an optional argument at the end \DeclareExpandableDocumentCommand seems not to be the way to go. – leviathan Mar 28 '18 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.