10

I have defined \Index to mark a word or phrase for indexing and also preserve it in text:

\newcommand{\Index}[1]{#1\index{#1}}

How would I define a macro to invert the index entry? e.g. \NewIndex{Tom Jones} would put "Tom Jones" in the text and "Jones, Tom" in the index.

I tried \newcommand{\N}[2]{#1 #2\index{#2, #1}} but \N{Tom Jones}, produces "Tom Jones ," in the text and ",, Tom Jones" in the index.

If this is possible, is it also possible to provide for names with an arbitrary number of elements so that \NewIndex{Tom Jones} works as above but \NewIndex{Homer W. Simpson} puts "Homer W. Simpson" in the text and "Simpson, Homer W." in the index? (There are only a few special forms like "Homer Simpson Jr" in my text and I can deal with them manually.)

2
  • You mention "word or phrase". Are the above-listed all forms of treatment? What about James Earl Jones II?
    – Werner
    Feb 9 '14 at 18:51
  • Your solution works well and is generalizable to bigger names but requires transposing the names in the existing text. But modifying it slightly requires just dropping in two pairs of braces: \newcommand{\N}[2]{\index{#2, #1}#1~#2} Thanks! Feb 9 '14 at 19:31
6

The simplest solution is to define

\newcommand{\Index}[2]{\index{#1, #2}#2~#1}

and use

\Index{Jones}{Tom}

in your document. It will also be more easily searchable.

2

Here's a method that defines the NewIndex command precisely as you described; I have used the xstring package to do the heavy lifting.

It works by counting the number of spaces in #1 and if:

  • the number of spaces is just 1 (as in Tom Jones) then it splits #1 at the space character
  • the number of spaces is greater than 1 (as in Homer W. Simpson) then it splits #1 at the second space character.

Here's the complete code, including arara directives- assuming you save it as myfile.tex, you simply have to run:

arara myfile

to obtain a complete pdf.

% arara: pdflatex
% arara: makeindex
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{xstring}
\usepackage{makeidx}

\newcommand{\NewIndex}[1]{%
    % output the name in the text (as is)
    #1%
    % write the name to the index in format: last name, first name
    %
    % count the number of spaces in #1
    \StrCount{#1}{\space}[\numberofspaces]
    \ifnum\numberofspaces=1
        % Tom Jones (just one space)
        % find the position of the space character
        \StrPosition{#1}{\space}[\spaceCharacterPos]
    \else
        % Homer W. Simpson (more than one space)
        % find the position of the SECOND space character
        \StrPosition[2]{#1}{\space}[\spaceCharacterPos]
    \fi%
    % split #1 at the appropriate space character
    \StrSplit{#1}{\spaceCharacterPos}{\firstName}{\lastName}%
    \index{\lastName, \firstName}%
}

\makeindex
\begin{document}

\NewIndex{Tom Jones}

\NewIndex{Homer W. Simpson}

\clearpage
\printindex
\end{document}

You didn't mention with index package you're using, so I assumed makeidx; if it's different, you may have to change the arara directives.

0

I followed the simple solution (with #1 = Name and #2 = first name)

newcommand{\Index}[2]{\index{#1, #2}#2~#1}

which works fine, with one exception: It creates a space in the text after the name, as if the command looked like that:

newcommand{\Index}[2]{\index{#1, #2}#2~#1~}

What can I do to get rid of the space? It is especially annoying, when the name appears in the text before a point or a comma.

Your Answer

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

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