3

I want to use index together with acro: Whenever I use an acronym, I want it automatically in the index. However, not the abbreviation but the long name!

What I did: I created a new command:

\newcommand{\acidx}[1]{\ac{#1}\index{\acl*{#1}}}

This works. However, latex does not expand the acl macro before writing the ind file! Mixing index and acidx` the contents of the idx file is:

\begin{theindex}
  \item \texttt  {\acl *{ABC}}, \hyperpage{2}
  \item \texttt  {\acl *{DEF}}, \hyperpage{1, 2}
  \indexspace
  \item consetetur, \hyperpage{1}
\end{theindex}

It can be seen that the elements from acidx are grouped together. I assume because \acl was not expanded. If it had, it would be properly sorted.

To alleviate the issue, I tried

\newcommand{\acidx}[1]{\ac{#1}\index{#1@\acl*{#1}}}

However, this is not quite clean because (to my understanding), the index is now sorted according to the abbreviation rather than the full name, e.g. for PHP it would be sorted according to PHP rather than "Hypertext Preprocessor". Furthermore, when doing this, every entry is separated by \indexspace which is also not correct.

Is there a way to force index to expand the macro before writing the ind file?

Is there another way to fix the problem?

I would really like to avoid using a different index processor like xindy!

  • Could you add a small example of a document to play with? – egreg May 1 '15 at 19:35
4

Package acronym

The internal expandable version of \acl is \AC@acl. Then \index inside the macro \acidx will write the expanded long version of the acronym:

\documentclass{article}
\usepackage{acronym}
\usepackage{makeidx}
\makeindex

\makeatletter
\newcommand*{\acidx}[1]{%
  \ac{#1}\index{\AC@acl{#1}}%
}
\makeatother

\acrodef{ABC}{alphabet}
\acrodef{DEF}{definition}

\begin{document}

The \acidx{ABC} contains all\index{all} letters. The \acidx{DEF}
is written with the help of the \acidx{ABC}.

\printindex

\end{document}

Page 1

And the index:

Page 2

The .idx file:

\indexentry{alphabet}{1}
\indexentry{all}{1}
\indexentry{definition}{1}
\indexentry{alphabet}{1}

Package acro

The same with package acro, which is based on LaTeX3. \prop_get:NnNF gets the property \l__acro_long_prop from the data list for acronym id #1 and stores the result in \l__acro_long_tl, see the definition of \acro_get:n). The result token list variable \l__acro_long_tl can then be used in \index:

\documentclass{article}
\usepackage{acro}
\usepackage{makeidx}
\makeindex

\ExplSyntaxOn
\newcommand*{\acidx}[1]{%
  \ac{#1}%
  \prop_get:NnNF \l__acro_long_prop {#1} \l__acro_long_tl {}
  \index{\l__acro_long_tl}%
}
\ExplSyntaxOff

\DeclareAcronym{ABC}{short=ABC, long=alphabet}
\DeclareAcronym{DEF}{short=DEF, long=definition}

\begin{document}

The \acidx{ABC} contains all\index{all} letters. The \acidx{DEF}
is written with the help of the \acidx{ABC}.

\printindex

\end{document}
  • The OP is using acro, not acronym. – egreg May 1 '15 at 19:27
  • @egreg Thanks for the hint. I have now also added a version for package acro. – Heiko Oberdiek May 1 '15 at 19:52
  • While the acro version works, you're using “private“ macros (with __), which is a “don't do it” according to the guidelines. This should be a feature request to the package author. – egreg May 1 '15 at 19:53
  • I am using acronym (not acro) but great explanation which is very helpful. I adjusted the title. – divB May 1 '15 at 20:45

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.