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:
Then on page 2 (the index page):
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.
\documentclass{...}
and ending with\end{document}
.hyperref
but none of your examples useshyperref
. 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.