MakeIndex, Nomenclature, Glossaries and AcronymsProblem with "@" Symbol in '\newcommand' and Index

Information and discussion about MakeIndex - the tool to generate subject indices for LaTeX documents.
Post Reply
David Marcus
Posts: 1
Joined: Mon Jul 04, 2011 2:02 am

Problem with "@" Symbol in '\newcommand' and Index

Post by David Marcus »

The following code

Code: Select all

\documentclass{book}
\usepackage{makeidx}
\makeindex
\newcommand{\cmd}[1]{\textsf{\textsl{#1}}}
\newcommand{\CmdEntry}[1]{#1@\cmd{#1}}
\newcommand{\IndexCmd}[1]{\index{\CmdEntry{#1}}}
\begin{document}
Hello.
\index{one@\cmd{one}}
\IndexCmd{two}
\index{\CmdEntry{three}}
\printindex
\end{document}
produces the following index

three@three, 1
one, 1
two, 1

"one", "two", and the "three" after the @ are in the correct font. Why isn't the index entry for "three" correct? It seems I can use CmdEntry when defining other commands, but I can't use it as the argument to index in the body of the document.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Problem with "@" Symbol in '\newcommand' and Index

Post by cgnieder »

Looks to me like a question of in which order the macros are expanded.

I guess this would work (haven't tested it, though):

Code: Select all

\documentclass{book}
\usepackage{makeidx}
\makeindex
\newcommand{\cmd}[1]{\textsf{\textsl{#1}}}
\newcommand{\CmdEntry}[1]{#1@\cmd{#1}}
\newcommand{\IndexCmd}[1]{\index{\CmdEntry{#1}}}
\begin{document}
Hello.
\index{one@\cmd{one}}
\IndexCmd{two}
\expandafter\index\expandafter{\CmdEntry{three}}
\printindex
\end{document}
site moderator & package author
Post Reply