3

I want to put the notes \begin{defi}[<note>] to the index as default, but not in some cases.

So I would like to use a shifter like \noindex, that means
\begin{defi}[<note> \noindex].

I tried a newif

\newif\ifToIndex
\ToIndextrue % Default

But this does not work. What have I to do?

% arara: pdflatex
% arara: makeindex
% arara: pdflatex

\documentclass{scrartcl}
\usepackage{amsthm}
\usepackage{thmtools}

\makeatletter
\newif\ifToIndex
\ToIndextrue % Default

\declaretheoremstyle[
notebraces={}{},
headpunct={\normalfont.\hspace{1.5em}\NOTE},
%notefont=\normalfont,
headformat={\NAME\,%
\NUMBER\,{\let\thmt@space\@empty{\normalfont(\NOTE)}%
 \ifToIndex\index{\NOTE}\fi}% <-------!!!!  
},
%preheadhook=\vspace{\baselineskip}\begin{leftbar}, postfoothook=\end{leftbar}, % optional 2/2
]{mystyle}
\makeatother

\declaretheorem[title=Definition, within=section, style=mystyle]{defi}

\usepackage{makeidx}
\makeindex
\usepackage{lipsum} % Dummy-Text
\begin{document}
\section{Definitions in the First Section}
\noindent Here comes a definition. 
\begin{defi}[Definition to Index]
\lipsum[66]
\end{defi}

% Does not work
%\begin{defi}[Definition not to Index \ToIndexfalse]
%\lipsum[66]
%\end{defi}

\printindex
\end{document}

1 Answer 1

2

You should be aware that \NOTE doesn't just contain the note text, but several other tokens.

I propose a different approach. The environment is defined to use an inner theorem-like environment; thus it can examine the optional argument; if it starts with a *, the entry is not indexed.

I have also fixed your definition of the theorem style.

\documentclass{scrartcl}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xparse}
\usepackage{makeidx}
\usepackage{lipsum} % Dummy-Text

\makeindex

\makeatletter
\newcommand{\emptythmtspace}{\let\thmt@space\@empty}
\makeatother

\declaretheoremstyle[
  notebraces={(}{)},
  headpunct={\normalfont.\hspace{1.5em}},
  notefont=\normalfont,
  headformat=\NAME\ \NUMBER\NOTE,
]{mystyle}

\declaretheorem[title=Definition, within=section, style=mystyle]{defiinner}

\ExplSyntaxOn
\NewDocumentEnvironment{defi}{o}
 {
  \IfNoValueTF{#1}
   {
    \defiinner
   }
   {
    \str_if_eq:eeTF { \tl_head:n { #1 } } { * }
     {
      \defiinner[\tl_tail:n { #1 }]
     }
     {
      \defiinner[#1]\index{#1}
     }
   }
 }
 {\enddefiinner}

\ExplSyntaxOff

\begin{document}
\section{Definitions in the First Section}

\begin{defi}[Definition to Index]
\lipsum[66]
\end{defi}

\begin{defi}[*Definition not to Index]
\lipsum[66]
\end{defi}

\begin{defi}
\lipsum[66]
\end{defi}

\printindex
\end{document}

enter image description here

The produced .idx file contains only

\indexentry{Definition to Index}{1}
4
  • Ah thx. I realized your method an think it is easier to create \begin{defi} vs. \begin{defi*}. So I built \declaretheorem[title=Definition, sibling=section, style=mystyle]{defiinner} \NewDocumentEnvironment {defi}{o} {\IfNoValueTF{#1}{\defiinner} {\defiinner[#1]\index{#1}}} {} \NewDocumentEnvironment {defi*}{o} {\IfNoValueTF{#1}{\defiinner} {\defiinner[#1]}} {} I hope this is correct.
    – cis
    Apr 13, 2020 at 9:06
  • @cis It could be made slightly more efficient, but it's correct.
    – egreg
    Apr 13, 2020 at 9:08
  • Maybe more efficiency is needed, because it will not play together with preheadhook=\begin{leftbar}, postfoothook=\end{leftbar}, from thmbox.sty
    – cis
    Apr 13, 2020 at 9:15
  • @cis thmbox doesn't play well with headformat. And the output you get is really ugly.
    – egreg
    Apr 13, 2020 at 9:19

You must log in to answer this question.

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