muzimuzhi / thmtools

New home for LaTeX package bundle thmtools
LaTeX Project Public License v1.3c
16 stars 3 forks source link

Make key `sibling` more sensible with \declaretheorem{<env list>} #20

Open muzimuzhi opened 3 years ago

muzimuzhi commented 3 years ago

Use case:

Define multiple theorem environments simultaneously and make them share the same counter. So with

\documentclass{article}
\usepackage{thmtools}

\declaretheorem{theorem, definition} % which (new) option here? to be decided

\begin{document}
\begin{theorem}     content \end{theorem}
\begin{definition}  content \end{definition}
\begin{theorem}     content \end{theorem}
\end{document}

one will get

Theorem 1
Definition 2
Theorem 3

(from user José Gil Férez by email)

Discussions:

Suppose \declaretheorem{theorem, definition}[<key>=theorem] does the work, then the logic is "use key value (theorem) as sibling value, or ignore it if it is the same as the env name to be defined". It seems this logic can be directly added to the existing key sibling, because normally it's non-sense to use \declaretheorem{env}[sibling=env].

Cons: This requires that the sibling value to be the first one in the env list. As a counterexample, \declaretheorem{theorem, definition}[sibling=definition] won't work.

muzimuzhi commented 3 years ago

Suppose \declaretheorem{theorem, definition}[<key>=theorem] does the work, then the logic is "use key value (theorem) as sibling value, or ignore it if it is the same as the env name to be defined". It seems this logic can be directly added to the existing key sibling, because normally it's non-sense to use \declaretheorem{env}[sibling=env].

This solely could be implemented as simple as

diff --git a/source/thm-kv.dtx b/source/thm-kv.dtx
index 7859126..dd34ecd 100644
--- a/source/thm-kv.dtx
+++ b/source/thm-kv.dtx
@@ -409,7 +409,9 @@
     \@nx\thmt@newtheorem
     \ifthmt@isnumbered
       {#1}%
-      \ifx\thmt@sibling\@empty\else [\thmt@sibling]\fi
+      \ifx\thmt@sibling\@empty
+      \else\ifx\thmt@sibling\thmt@envname
+      \else [\thmt@sibling]\fi\fi
       {\thmt@thmname}%
       \ifx\thmt@parent\@empty\else [\thmt@parent]\fi
     \else

A continued example:

\documentclass{article}
\usepackage{thmtools}
\usepackage{xpatch}

\makeatletter
\xpatchcmd\declaretheorem@iii
  {\ifx\thmt@sibling\@empty\else [\thmt@sibling]\fi}
  {\ifx\thmt@sibling\@empty
   \else\ifx\thmt@sibling\thmt@envname
   \else [\thmt@sibling]\fi\fi}
  {}{\fail}
\makeatother

\declaretheorem{theorem, definition}[sibling=theorem]

\begin{document}
\begin{theorem}     content \end{theorem}
\begin{definition}  content \end{definition}
\begin{theorem}     content \end{theorem}
\end{document}

This will give the desired output.

But since sibling/numberlike and parent/numberwithin are mutually exclusive, the above patch will NOT resolve José's request: use

\declaretheorem[style=theorem, numberwithin=chapter]
   {theorem, lemma, proposition, corollary}

to define env theorem numbered within chapter, and all the following envs numbered like theorem.

José's proposal, a new boolean option siblings, is closer (to the final decision).