1

First, let me show you something that works. It creates a link from an indexed term to its corresponding entry in the index, and vice-versa.

\newcommand{\myname}[2]{#1\renewcommand{\@currentlabel}{#2}}  % #1 is index entry; #2 is entry in the text

\begin{document}
\ref{314}\index{\myname{decision tree}{decision trees}\label{314}}
\end{document}

Now I want to automate this and write a macro called Index. I came up with this:

\newcommand{\myname}[2]{#1\renewcommand{\@currentlabel}{#2}}  % #1 is index entry; #2 is entry in the text
\newcommand{\Index}[2]{\index{\myname{#1}{#2}\label{#1}}}

\begin{document}
\Index{decision tree}{decision trees}
\end{document}

If I don't call the Index function in the main part, there is no error. But when I call it as in the above example, I get this error:

! Use of \@index doesn't match its definition.
\new@ifnextchar ...served@d = #1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...

I have very little experience with macros. I know that if I were to use my failing Index function everywhere, then run my Latex document through a Python script that expands the Index macro as it should, then compile the resulting expanded Latex source, it would work. So my macro does not expand the way I think it should. I am wondering what is wrong in my code.

Update: working example

Here is some stuff that works:

\documentclass[10pt]{article}
\usepackage{imakeidx}
\makeindex
\usepackage[nottoc]{tocbibind}
\usepackage[colorlinks = true, 
                   linkcolor = red]{hyperref}
\makeatletter
\newcommand{\myname}[2]{#1\renewcommand{\@currentlabel}{#2}}  
\newcommand{\Index}[2]{\index{\myname{#1}{#2}\label{#1}}}
\makeatother

\begin{document}
The technique presented here, called \ref{bvc}\index{\myname{decision tree!boosted tree}{boosted trees}\label{bvc}}, is used for ...
%The technique presented here, called \Index{decision tree!boosted tree}{boosted trees}, is used for ...
\printindex
\end{document}

Now if I replace the line inside the document body by the one commented out (my ultimate goal), that's when I get the compilation error. I suspect the implicitly nested \newcommand with a \renewcommand is the source of the problem.

Here is that output from the version that works (the one I posted above). On page 1:

enter image description here

Then on page 2 (the index page):

enter image description here

What I try to do is treat index entries as if they were equations, for referencing purposes. But the link to an equation is a counter. I want to avoid that and overwrite the counter by the target keyword. So it is equivalent to relabeling.

3
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – dexteritas
    Commented Sep 14, 2022 at 8:40
  • 1
    As always here please make sure your examples are complete. Document class is missing. Additionally you tagged this hyperref but none of your examples uses hyperref. Thirdly, an error does not appear until one adds \makeindex, again this is not in your example. You should probably start by explaining what your end goal is as it is not clear what the combination of \index and \myname and \label is suppose to be doing here.
    – daleif
    Commented Sep 14, 2022 at 8:40
  • I will work on that and remove the hyperref tag (it started with hyperref but after some improvements, I no longer need it in this case). Commented Sep 14, 2022 at 14:16

1 Answer 1

1

I'm not sure how pointing to the index is useful. Anyway…

\documentclass{article}
\usepackage{makeidx}
\usepackage{hyperref}

\makeindex

\newcounter{indexcount}
\NewDocumentCommand{\Index}{mm}{%
  % #1 is index entry; #2 is entry in the text
  \refstepcounter{indexcount}%
  \index{#1@#1\indexlabel{\theindexcount}}%
  \hyperref[index\theindexcount]{#2}%
}
\NewDocumentCommand{\indexlabel}{m}{\label{index#1}}

\begin{document}

Some text

\Index{decision tree}{decision trees}

\printindex

\end{document}
2
  • Turns out I actually need \hyperref, on my minimum working example that I will post shortly. The reason to point to the index is as follows: some topics, say 'decision trees', are discussed in multiple chapters on many different pages. A reader encountering 'decision trees' in chapter 3 can click on the indexed keyword, then land on the corresponding entry in the index, find that a sub-keyword listed under 'decision trees' is 'boosted tree' in the index, click on 'boosted tree' subentry in the index, and end up in the page(s) where I discuss 'boosted trees', say in chapter 7. Commented Sep 14, 2022 at 15:58
  • Thank you. This works, but not so well with subindex: try \Index{decision tree!boosted tree}{decision trees}. I also need to change the style, but it answers the original question (if you ignore subindex which I did not include initially). Thanks a lot! Commented Sep 14, 2022 at 18:55

You must log in to answer this question.

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