1

I'm trying to set an optional counter in the \newtheorem command based on some input given to a .sty file. Something like

\DeclareOption{empty}{
    \edef\counter{} 
}

\DeclareOption{section}{ \edef\counter{\detokenize{section}} }

\ProcessOptions\relax

\theoremstyle{definition} \newtheorem{definition}{Definition}[\counter] \providecommand*{\definitionautorefname}{Definition}

The amsthm package accepts section or chapter in \counter. I, instead, would like to not set a counter by setting \counter to some specific value. I've tried \empty,0 and {} as above.

Of course, I could use a bunch of if's for each \newtheorem and simply set \newtheorem{definition}{Definition} when I don't want to associate any counter. I would like to avoid that painful option, however.

Thanks in advance.

user40276
  • 279
  • You wrote, "The amsthm package accepts section or chapter in \counter." That's somewhat misleading, as any valid counter variable can be used for "\counter. Do please clarify what you mean by "trying to set an optional counter in the \newtheorem command based on some input given to a .sty file". E.g., why does it have to be a .sty file? – Mico May 01 '22 at 23:30
  • @Mico I would like to make package with two possible parameters, section and empty that sets \newtheorem{definition}{Definition}[section] or \newtheorem{definition}{Definition} depending on the chosen input in the main document using it. The point is that I have a bunch of \newtheorem{definition}{Definition} in an .sty file that I'm writing, so I wouldn't like to use a bunch of if's. I would like, then to known whether there's some valid counter in the amsthm package such that "\newtheorem{definition}{Definition}["valid counter"]= \newtheorem{definition}{Definition}". – user40276 May 02 '22 at 01:46
  • @Mico By the way, \newtheorem{definition}{Definition}[] seems to have the same effect of \newtheorem{definition}{Definition}. Still, I don't know how to define a new command \counter such that \newtheorem{definition}{Definition}[\counter] has the same effect of \newtheorem{definition}{Definition}[]. So I would like to define something like "let \counter be the empty command". – user40276 May 02 '22 at 01:51
  • Not tested! You might try creating \newcounter{xthm} and then \newtheorem{xdefn}{Definition}[xthm] and assign an appropriate value to the xthm counter. (May work, maybe not, but seems worth trying, since it's known that the equation counter can be used in this way.) – barbara beeton May 02 '22 at 03:07
  • @barbarabeeton Thanks for the suggestion, but how do I set the value of xthm to something empty? If I just define \newcounter{xthm} and set \newtheorem{definition}{Definition}[xthm] inside the sty file, I get "Definition 0.1 "from \begin{definition} inside the main tex document, whereas I would like "Definition 1" instead. Any ideas? – user40276 May 02 '22 at 04:13
  • Does the value of the xthm counter really have to be "empty" (or "undefined")? Would it suffice for the value of this counter to be equal to 0 in order for the typeset representation of the definition counter to be "1" instead of "0.1"? Please advise. – Mico May 02 '22 at 04:33
  • @Mico Do you mean \setcounter{xthm}{0} in the sty file? If so, in the main tex file begin{definition} appears later as "Definition 0.1". The 0 in 0.1 from xthm. I want to erase that. – user40276 May 02 '22 at 05:05

1 Answers1

3

I don't think you should force users of your package to have the numbering of definitions independent on theorems and friends.

There is no reason why an author should be forced to a style such as

Lemma 1
Theorem 1
Theorem 2
Definition 1
Lemma 2
Theorem 3

(maybe with section prefix, but that's irrelevant) instead of the more common and friendly

Lemma 1
Theorem 2
Theorem 3
Definition 4
Lemma 5
Theorem 6

(again with a possible prefix). I hate reading such a cross reference as

see chapter II, part II, paragraph VI

or

chapter V, part III, paragraph II, 2, theorem 2

These are from an actual book, which uses “part” instead of section, “paragraph” for subsection and doesn't name subsubsections. There is no clue whatsoever in the page headers about how to find the referenced theorem.

In your proposed “empty” style, the reader will have no clue as to whether Lemma 2 precedes or follows Theorem 1.

Anyway, if you insist on it (maybe because of strict faculty regulations), the trick is to define your own facility.

\DeclareOption{empty}{\let\package@thmprefix=F}
\DeclareOption{section}{\let\package@thmprefix=T\def\package@thmlevel{section}}
\DeclareOption{chapter}{\let\package@thmprefix=T\def\package@thmlevel{chapter}}

\DeclareOption{empty}{\let\package@thmprefix=F} \DeclareOption{section}{\let\package@thmprefix=T\def\package@thmlevel{section}} \DeclareOption{chapter}{\let\package@thmprefix=T\def\package@thmlevel{chapter}}

\ExecuteOptions{empty} \ProcessOptions\relax

\RequirePackage{amsthm}

\def\package@newtheorem#1#2{% \if\package@thmprefix F% \newtheorem{#1}{#2}% \else \newtheorem{#1}{#2}[\package@thmlevel] \fi }

\package@newtheorem{lemma}{Lemma} \package@newtheorem{theorem}{Theorem} \theoremstyle{definition} \package@newtheorem{definition}{Definition}

Here's the test file

\documentclass{book}

\usepackage{package} %\usepackage[section]{package} %\usepackage[chapter]{package}

\begin{document}

\chapter{Title}

\section{Title}

\begin{lemma} Text \end{lemma}

\begin{theorem} Text \end{theorem}

\begin{theorem} Text \end{theorem}

\section{Title}

\begin{definition} Text \end{definition}

\begin{lemma} Text \end{lemma}

\begin{theorem} Text \end{theorem}

\end{document}

Output with the empty option (default)

enter image description here

Output with the section option

enter image description here

Output with the chapter option

enter image description here

egreg
  • 1,121,712
  • Thanks for the answer. You probably hate French Bourbakists then :-) . I'm not very proficient in the TeX syntax, so maybe what follows is trivial. What's \package@thmprefix? Is it something already defined or you're defining it on the first line? Is that lack of space between T and \def in the second line ( ...=T\def\...) really necessary? Also, is it standard to use T and F for true and false in TeX? I've been searching for the correct syntax for a while. By the way, where can I find a list of all the logical operators defined in TeX? – user40276 May 02 '22 at 16:02
  • 1
    @user40276 Definitely, as far as cross-references are concerned. :-D The \package@thmprefix is a package command I'm defining (replace package with something distinctive for your package). A space would not be ignored in that context (although it would be harmless). The only important thing is that the letters used (here T and F) are different. – egreg May 02 '22 at 17:31