gusbrs / zref-clever

Clever LaTeX cross-references based on zref
LaTeX Project Public License v1.3c
11 stars 4 forks source link

Enable shared counters by default #31

Closed mathjiajia closed 2 months ago

mathjiajia commented 2 months ago

Currently, I have to add the following, which looks silly:

\AddToHook{env/axiom/begin}{%
\zcsetup{countertype={theorem=axiom}}}
\AddToHook{env/conjecture/begin}{%
\zcsetup{countertype={theorem=conjecture}}}
\AddToHook{env/corollary/begin}{%
\zcsetup{countertype={theorem=corollary}}}
\AddToHook{env/hypothesis/begin}{%
\zcsetup{countertype={theorem= hypothesis}}}
\AddToHook{env/lemma/begin}{%
\zcsetup{countertype={theorem=lemma}}}
\AddToHook{env/proposition/begin}{%
\zcsetup{countertype={theorem=proposition}}}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{axiom}[theorem]{Axiom}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{hypothesis}[theorem]{Hypothesis}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}

Without the above part, zcref will treat everything as theorem

gusbrs commented 2 months ago

Looks about what I'd do for this case.

Could you elaborate what do you mean by "enable shared counters"? How do you expect zref-clever to distinguish the same counter in different situations without telling it what to do?

mathjiajia commented 2 months ago

When using cleaveref, it can detect that: for a lemma it will use the correct type of”lemma” and correct counter, even shared with theorem, proposition…

mathjiajia commented 2 months ago

I just expected zref-cleaver to recognise the correct type of each label, such as section, lemma, remark … As for the counter number, just follow the built in \ref command

Is that possible? Thanks for your help.

gusbrs commented 2 months ago

You mean that the first (mandatory) argument of \newtheorem should be used to set the type instead of the second (optional) one (like in https://tex.stackexchange.com/a/19106), if I understand correctly. Yes, cleveref does that, but it does so by redefining the internals of \newtheorem (for the kernel, amsthm and ntheorem), but I have no intention of doing the same for zref-clever. Indeed, it is a matter of policy of zref-clever to be the least intrusive possible of other people's code.

The way to handle this case for zref-clever is documented in the manual, and is the one you are already using. In my view, it is not silly at all, but rather an improvement: a little more document setup for a lot less headaches. The way to do it is simple enough, and there's no way I'll touch \newtheorem for the sake of such a slight convenience.

If you find that that annoying, you could redefine things yourself, e.g.:

\documentclass{article}
\usepackage{amsthm}
\usepackage{zref-clever}

\NewDocumentCommand{\mynewtheorem}{momo}{%
  \IfValueTF{#4}
    {\newtheorem{#1}{#3}[#4]}
    {%
      \IfValueTF{#2}
        {%
          \AddToHook{env/#1/begin}{\zcsetup{countertype={#2=#1}}}%
          \newtheorem{#1}[#2]{#3}%
        }
        {\newtheorem{#1}{#3}}%
    }%
}

\theoremstyle{plain}
\mynewtheorem{theorem}{Theorem}[section]
\mynewtheorem{axiom}[theorem]{Axiom}
\mynewtheorem{conjecture}[theorem]{Conjecture}
\mynewtheorem{corollary}[theorem]{Corollary}
\mynewtheorem{hypothesis}[theorem]{Hypothesis}
\mynewtheorem{lemma}[theorem]{Lemma}
\mynewtheorem{proposition}[theorem]{Proposition}

\begin{document}

\section{Section 1}

\begin{theorem}\label{thm:test}
theorem
\end{theorem}

\begin{lemma}\label{lem:test}
lemma
\end{lemma}

\begin{corollary}\label{cor:test}
corollary
\end{corollary}

As shown in \zcref{thm:test,lem:test,cor:test}

\end{document}
mathjiajia commented 2 months ago

I see. Thanks for your explanation.