# Automatically add theorems to index and label them

In another question (Remove math from command parameter to get only the letters) this command was elaborated to have an optional argument for sorting purposes within the index

\documentclass{article}
\usepackage{makeidx}
\usepackage{xparse}
\NewDocumentCommand{\defined}{om}{%
{\bfseries #2}%
\IfNoValueTF{#1}
{\index{#2}\label{#2}}
{\index{#1@#2}\label{#1}}
}
\makeindex
\begin{document}
\defined{$G$-invariant}

\defined[G-invariant]{$G$-invariant}
\printindex
\end{document}


I now wonder if theres a way to have the same optional argument for an own theorem style, so something along that line.

\newtheoremstyle{blanksample} % name of the style to be used
{}             % measure of space to leave above the theorem
{}             % measure of space to leave below the theorem
{}             % name of font to use in the body of the theorem
{}             % measure of space to indent
{\bfseries}    % name of head font
{}             % punctuation between head and body
{\newline}     % space after theorem head
{\thmname{#1} \thmnumber{#2.} \thmnote{\normalfont(#3)} \defined{#1!#3}} % head


But like this I again have the problem with dollar signs or other stuff within #3 and therefore again would like to use an optional parameter and could hand this over to \defined.

Furthermore I actually would like to have the index link to the theorem number and not the page number.

Any ideas?

So I would like to use it somehow like this Using \theoremstyle{blanksample} anf for \newtheorem{definition}{Definition} I would like to use it somehow like this

\begin{definition}[G-invariant][$G$-invariant]
some definition
\end{definition}

-
Would you please add an example of intended usage? –  egreg Feb 17 '13 at 17:05
@egreg: I tried to explain it at the bottom of my question. –  heinzlein Feb 17 '13 at 17:09
First define your theorem style, then create a new environment with an optional argument that calls the theorem with or without the optional argument. –  Thruston Feb 17 '13 at 17:35

I'd use a two step approach: define an inner environment with \newtheorem and on top of it a new environment that checks for optional arguments, passing them to the inner environment and to \index:

\documentclass{article}
\usepackage{imakeidx} % instead of makeidx, so you don't need to run MakeIndex
\usepackage{xparse}
\usepackage{amsthm}

\NewDocumentCommand{\defined}{om}{%
{\bfseries #2}%
\IfNoValueTF{#1}
{\index{#2}\label{#2}}
{\index{#1@#2}\label{#1}}%
}

\theoremstyle{definition} % use any style you prefer
\newtheorem{xdefinition}{Definition} % note the "x"

\NewDocumentEnvironment{definition}{oo}
{\IfNoValueTF{#1}
% no optional argument to definition
{\begin{xdefinition}}
% at least one optional argument
{\IfNoValueTF{#2}
% only one optional argument
{\begin{xdefinition}[#1]\index{Definition!#1}\label{def-#1}}
% two optional arguments
{\begin{xdefinition}[#2]\index{Definition!#1@#2}\label{def-#1}}%
}%
}
{\end{xdefinition}}
\makeindex

\begin{document}
\defined[G-invariant]{$G$-invariant}

\begin{definition}[G-invariant][$G$-invariant]
Something
\end{definition}

\begin{definition}[group]
Something
\end{definition}

The concept of $G$-invariant subgroup has been defined in~\ref{def-G-invariant} on page~\pageref{def-G-invariant}.

\printindex
\end{document}


When only one optional argument is specified, that is used for the index entry and the label.

The implicit \label command will refer to the definition's number, but it's preceded by a def- prefix in order to avoid duplicate labels: in in the body of a definition environment you say \defined with the same term, you'd end up with two identical labels.

If you want to add the definition number to the index entry, change the block where definition is defined with

\NewDocumentEnvironment{definition}{oo}
{\IfNoValueTF{#1}
{\begin{xdefinition}}
{\IfNoValueTF{#2}
{\begin{xdefinition}[#1]\doindex{Definition!#1@#1}\label{def-#1}}
{\begin{xdefinition}[#2]\doindex{Definition!#1@#2}\label{def-#1}}%
}%
}
{\end{xdefinition}}
\ExplSyntaxOn
\NewDocumentCommand{\doindex}{m}
{
\use:x { \exp_not:N \index { \exp_not:n { #1~ } (\thexdefinition) } }
}
\ExplSyntaxOff


This will print in the index something like

Definition
G-invariant (1), 1

where the (1) refers to the definition number which is followed as usual by the page number.

-
Ok, thanks. I'll try this one. According the Number, the label thingy is fine. I wanted to actually have the definitions in the index refered to the definitions number as well.. but now actually I wonder if this is a good idea.. anyway: would there be a way for this? –  heinzlein Feb 17 '13 at 17:46
@heinzlein You won't have so many definitions in a single page, so the page number in the index should be sufficient to find the place. –  egreg Feb 17 '13 at 17:49
How to support the unnumbered theoreme environment \newtheorem*{xdefinition*}{Definition} as well? Do I need to copy that thing completely just with a star or is there another way? –  heinzlein Feb 17 '13 at 17:57
@heinzlein I'd go with \NewDocumentEnvironment{definition*} with the same scheme. After all, excluding comments, it's just nine lines of code. And, if you want to add the definition number to the index, this is probably the best, as you have to use a different command than \index for numbered definitions. –  egreg Feb 17 '13 at 18:02
What are the % for at the end of some lines? –  heinzlein Feb 17 '13 at 18:14