5

I want to define a command that will emphasis the keywords and add it to the index at the same time. This can be archived by

\newcommand{\iemph}[1]{\emph{#1}\index{#1}}

But the problem is that, I want also to use the variant of index: \index{bundle!Vector bundle} which means the vector bundle should be indexed under the index bundle, I try to accomplish it as

\newcommand{\iemph}[2][]{\emph{#2}\index{#1!#2}}

That means, I set the first value default be empty, but this not give me the desired results (it is not indexed at all) when I suppress the first argument:

 \iemph{vector bundle}

Although it works perfectly in the full form

 \iemph{vector}{Vector bundle} 

or

 \iemph[vector]{vector bundle}

So is there any solution to this problem without define two command? If we can define something like:

\newcommand{\iemph}[2][]{\emph{#2}\if{#1 is not empty}{\index{#1!#2}}else{\index{#2}}}

UPDATE

Maybe the simple solution to the original problem--although the answer privided below is also useful in someother similar situation--is define the command as

\newcommand{\iemph}[2][]{\emph{#2}\index{#1#2}}

and use it as

\iemph{vector bundle}
\iemph[vector!]{vector bundle}

It should be noted that in this case

\iemph{vector!}{vector bundle}

behaves weird.

  • 1
    I assume you mean parameter, not paramentor – user31729 Nov 2 '15 at 8:12
6

With \NewDocumentCommand from the xparse package, it's quite easy to ask whether the optional argument is given or not:

\NewDocumentCommand{\iemph}{om}{%
  \emph{#2}%
  \IfValueTF{#1}{%
    \index{#1!#2}%
  }{%
    \index{#2}%
  }%
}

Here o means optional argument and with \IfValueTF{#1}{true}{false} it's possible to do the relevant branching.

Another possibility: Use \ifblank{}{}{} from the etoolbox package, as shown in \iemphother

\documentclass{article}

\usepackage{etoolbox}    
\usepackage{makeidx}
\usepackage{xparse}


\newcommand{\iemphother}[2][]{%
  \emph{#2}%
  \ifblank{#1}{%
    \index{#2}%
  }{%
  \index{#1!#2}%
  }%
}



\NewDocumentCommand{\iemph}{om}{%
  \emph{#2}%
  \IfValueTF{#1}{%
    \index{#1!#2}%
  }{%
    \index{#2}%
  }%
}

\makeindex

\begin{document}
\iemph[vector]{vector bundle}

\iemph{vector bundle}



\printindex

\end{document}
  • 2
    One could do \newcommand{\iemph}[2][\@gobble]{\emph{#2}\index{#1!#2}}, but it's better to avoid trickery when more robust methods are available. – egreg Nov 2 '15 at 8:40
  • @egreg: I am not really into \@gobble so far (you won't be surprised, I assume ;-)), so I leave trickery to you ;-) – user31729 Nov 2 '15 at 8:44
  • @egreg could you explain the trick? I have just learnt that \@gobble throw away its argument, but how this could work here? – user19832 Nov 5 '15 at 7:42
  • @user19832 \index performs one level expansion, so it expands \@gobble if it's the first token in the argument; this expansion gobbles the !. – egreg Nov 5 '15 at 7:46
  • 1
    @user19832 I said: “it's better to avoid trickery when more robust methods are available”. Since a more robust method is available, why trying trickery? – egreg Nov 5 '15 at 8:42

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.