7
\documentclass{article}

\usepackage{makeidx}
\makeindex

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

\begin{document}

\cleardoublepage
This shows \key{1.1},\key{1.2},\key{1.3},\key{1.4},\key{2}.
% \key{1.1,1.2,1.3,1.4,2}

\cleardoublepage
This shows \key{1.1},\key{1.2},\key{2.3},\key{1.4},\key{2}.
% \key{1.1,1.2,2.3,1.4,2}

\cleardoublepage
This shows \key{1.1},\key{3.2},\key{5.3},\key{A},\key{C}.
\cleardoublepage
This shows \key{1.1},\key{E},\key{A},\key{D},\key{3.2}.

\printindex

\end{document}

Instead of using seperate \key{}s, is there a way to use ONE \key{}, but still being able to use it for indexing?

For example:

Thanks!

8
  • 1
    Is this just some example or why do you want to index 1.1 etc? Do you want to index the last counter (value) that was stepped? I don't understand this question so far. If you use the same \key over and over again, which information should go to \index? And consider the \index{foo!bar} subentries. Or do you want to use a CSV-list?
    – user31729
    Jul 5, 2015 at 16:58
  • the way \key is defined here will result in raggedy spacing in the output, since the spaces (separated by \index{...}) are no longer consecutive, and thus not compressed by tex..also, it's possible that the page number assigned to the index term may be one greater than appropriate, in the case where the typeset #1 finishes off the last line of a page, forcing the \index to go to the next page. Jul 5, 2015 at 17:10
  • Defining a macro that uses a comma-seperated list could be one way. Implementation of such a thing I leave to others. Jul 5, 2015 at 17:11
  • @ChristianHupfer This is exactly what I want to index and is not an example. It's part of a big problem I hope to achieve. So for example, if I use \key{1.1,1.2,1.3,1.4}, I want this to be the same as \key{1.1},\key{1.2},\key{1.3},\key{1.4}. There is no need for subentries. Jul 5, 2015 at 17:16
  • @barbarabeeton For my actual problem, I will NOT be considering page numbers. Instead, I would want to use, say for example, the section number like a reference. Jul 5, 2015 at 17:17

3 Answers 3

7

This is a quick-and-dirty way which both displays the index key and adds it to the Index, using \forcsvlist command for the first arg of \key command, which is a CSV - list

Longer keys with white space must be put in {...}, as well as keys which have a , inside.

The \dispkey is this CVS-list processing (helper) macro.

\documentclass{article}


\usepackage{etoolbox}
\usepackage{makeidx}
\makeindex

\newcommand{\dispkey}[1]{%
#1 \index{#1}%
}

\newcommand{\key}[1]{%
  \forcsvlist{dispkey}{#1}%
}

\usepackage{blindtext}

\begin{document}
\blindtext[5]

\key{This,that,{and this one}}
\blindtext[2]
\key{1.1,1.2,1.3}
\printindex
\end{document}
10
  • 3
    I suppose you're being faithful to the code of the question, but I usually find it better to do something like \newcommand{\dispkey}[1]{\index{#1}#1}. At at least you don't want to put an explicit space between #1 and \index{#1}.
    – jon
    Jul 5, 2015 at 18:48
  • 1
    @jon: Keeping the original style was my only intention to use it the #1 \index{#1} way. I wouldn't use the whole stuff this way however. ;-) Thanks for your comment
    – user31729
    Jul 5, 2015 at 18:51
  • @jon The "explicit space" was there because it reads more natural to me, so I would not forget to put an extra or miss any delimiters. Is there any danger of this "explicit space"? Jul 5, 2015 at 18:55
  • 1
    @ChenStatsYu -- Yes there is: try putting a punctuation mark after \key{<whatever>}. @Christian -- Indeed. It was, I suppose, more of a general warning. (I did +1 already.)
    – jon
    Jul 5, 2015 at 18:58
  • 1
    @ChristianHupfer Shorter, you mean. ;-)
    – egreg
    Jul 5, 2015 at 21:20
6

A very short implementation with expl3:

\documentclass{article}

\usepackage{xparse}
\usepackage{makeidx}
\makeindex

\ExplSyntaxOn
\NewDocumentCommand{\key}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    ##1\index{##1}~
   }
   \unskip
 }
\ExplSyntaxOff

\begin{document}

\key{This,that,{and this one}}

\key{1.1,1.2,1.3}

\printindex

\end{document}

enter image description here

If you want to preserve the commas between the keys in the text, then a slightly more complex approach is needed. We store the keys in a sequence, then extract the first item to be treated specially (no comma before it) and then map the remaining items prefixing them with a comma-space. The mapping will result in nothing if there was just one item.

\documentclass{article}

\usepackage{xparse}
\usepackage{makeidx}
\makeindex

\ExplSyntaxOn
\NewDocumentCommand{\key}{m}
 {
  \chen_key:n { #1 }
 }

\seq_new:N \l_chen_keys_seq
\tl_new:N \l_chen_first_tl

\cs_new_protected:Npn \chen_key:n #1
 {
  \seq_set_split:Nnn \l_chen_keys_seq { , } { #1 }
  \seq_pop_left:NN \l_chen_keys_seq \l_chen_first_tl
  \chen_print_and_index:Vn \l_chen_first_tl {}
  \seq_map_inline:Nn \l_chen_keys_seq
   {
    \chen_print_and_index:nn { ##1 } { ,~ }
   }
 }
\cs_new_protected:Npn \chen_print_and_index:nn #1 #2
 {
  #2 #1 \index{#1}
 }
\cs_generate_variant:Nn \chen_print_and_index:nn { V }
\ExplSyntaxOff

\begin{document}

\key{This,that,{and this one}}

\key{1.1,1.2,1.3}

\printindex

\end{document}

Here's the first page:

enter image description here

5
  • Like @Christian's answer this loses the commas between the keys...
    – cgnieder
    Jul 5, 2015 at 22:31
  • Does \unskip undo the horizontal space?
    – cfr
    Jul 6, 2015 at 1:41
  • The version which preserves the commas is not, however, 'shorter', is it?!
    – cfr
    Jul 6, 2015 at 2:05
  • @cfr It's not really much longer. It automatically trims spaces around entries; it is easy to set up a different delimiter; the \index command is fed with the actual entry and not with \next; there is no juggling in the meaning of \my@join (there even is no similar object). I could continue. ;-)
    – egreg
    Jul 6, 2015 at 6:52
  • @egreg I was referring to your claim that it was shorter than Christian Hupfer's solution. Mine is longer. (Also, I'm only teasing: not only did I vote your answer up because it is excellent. I also voted your comment saying it would be shorter up because I liked it.) Still like to know about \unskip, though. (I thought this had to do with vertical space.)
    – cfr
    Jul 6, 2015 at 11:37
2

A simple solution with no extra packages and no expl3 syntax. (I'm not saying these are advantages. But they are features of my answer, whether good or bad.)

This solution is short, inserts a comma and a space between consecutive keys, and does not insert such things before the first or after the last key in the list. If the list consists of a single key, just the key is printed.

\documentclass{article}
\usepackage{makeidx}
\makeindex
\makeatletter
\newcommand\key[1]{%
  \def\my@keys{#1}%
  \let\my@join\relax
  \@for \xx:=\my@keys \do {%
    \index{\xx}\my@join\xx\gdef\my@join{, }%
  }%
}
\makeatother
\begin{document}
Some keys: \key{1.1,1.2,1.3,1.4,2} are shown here.

\key{1.1,1.2,2.3,1.4,2} are some more keys.

\key{1.1}, which is a nice key, comes before the extremely horrid key \key{3.2}.

Keys \key{5.3,A} and \key{C} are other keys.

Keys \key{1.1,E,A,D,3.2} show that something.

\printindex

\end{document}

simple key management

indexed keys

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .