1

I'm trying to make a command which will create a new theorem environment and link the counter for it to a specified other counter. For example, I'd like to be able to do something where my theorem, proposition, definition, etc. environments all have the same counters. I've tried the following:

\usepackage{etoolbox} 
\usepackage{aliascnt} 

\DeclareDocumentCommand{\DeclareTheorem}{ m m m !o }{%
\theoremstyle{#1}%
\IfNoValueTF{#4} {%
    \newtheorem{#2}{#3}% 
}{% 
    \newaliascnt{#2}{#4}%
    \newtheorem{#2}[#2]{#3}%
}%
}

This doesn't seem to work. It doesn't throw an error, it just doesn't compile or something. If I require five arguments, as below, then it works.

\DeclareDocumentCommand{\DeclareTheorem}{ m m m m !o }{%
\theoremstyle{#1}%
\IfNoValueTF{#5} {%
    \newtheorem{#2}{#3}% 
}{% 
    \newaliascnt{#4}{#5}%
    \newtheorem{#2}[#4]{#3}%
}%
}

However, I'd prefer to automatically have the alias counter to have the same name as the theorem environment, so that I don't have to put in five different options every time. Any help?

1 Answer 1

2

The manual of the aliascnt-package says:

1.2 Syntax

Macro names in user land contain the package name aliascnt in order to prevent name clashes.

\newaliascnt{⟨ALIASCNT⟩}{⟨BASECNT⟩}

An alias counter ⟨ALIASCNT⟩ is created that does not allocate a new TeX counter register. It shares the count register and the clear list with counter ⟨BASECNT⟩. If the value of either the two registers is changed, the changes affects both.

\aliascntresetthe{⟨ALIASCNT⟩}

This fixes a problem with \newtheorem if it is fooled by an alias counter with the same name:

\newtheorem{foo}{Foo}% counter "foo"
\newaliascnt{bar}{foo}% alias counter "bar"
\newtheorem{bar}[bar]{Bar}
\aliascntresetthe{bar}

I suppose you need \aliascntresetthe:

\documentclass{article}

\usepackage{xparse}
%\usepackage{etoolbox}
\usepackage{aliascnt} 
\usepackage{amsthm}
\usepackage{hyperref}

\makeatletter
\DeclareDocumentCommand{\DeclareTheorem}{ m m m !o }{%
  % #1 = theoremstyle
  % #2 = name of theorem-emvironment
  % #3 = Phrase introducing the theorem.
  % #4 = the counter to use instead of allocating a new counter
  \theoremstyle{#1}%
  \IfNoValueTF{#4}{%
    \newtheorem{#2}{#3}% 
  }{% 
    % In case the counter to use is not defined define it:
    \@ifundefined{c@#4}{\newcounter{#4}}{}%
    \newaliascnt{#2}{#4}%
    \newtheorem{#2}[#2]{#3}%
    \aliascntresetthe{#2}%
  }%
}%
\makeatother

\DeclareTheorem{plain}{ThmA}{A-theorem}
\newcommand\ThmAname{A-theorem}
\DeclareTheorem{plain}{ThmB}{B-theorem}[ThmA]
\newcommand\ThmBname{B-theorem}
\DeclareTheorem{plain}{ThmC}{C-theorem}[ThmA]
\newcommand\ThmCname{C-theorem}

\begin{document}

\begin{ThmA}
\label{ThmA1}This is an \ThmAname.
\end{ThmA}

\begin{ThmB}
\label{ThmB2}This is a \ThmBname.
\end{ThmB}

\begin{ThmC}
\label{ThmC3}This is a \ThmCname.
\end{ThmC}

Referencing:

\verb|\autoref{ThmA1}| yields: \autoref{ThmA1}

\verb|\autoref{ThmB2}| yields: \autoref{ThmB2}

\verb|\autoref{ThmC3}| yields: \autoref{ThmC3}

\end{document}

enter image description here

2
  • Thanks! Actually, reading your comment, I realized that I could in fact use c@#2 in the place where I was confused, and that would fix it all!
    – boink
    Jul 4, 2020 at 14:43
  • @boink Be aware that a LaTeX-counter is more than just a \count-register. \newcounter also defines "infrastructure" like \p@<counter> , \the<counter>, \cl@<counter> and in case hyperref is loaded you also need to take care of things like \theH<ccounter> and \<counter>autorefname/\<counter>name. Some of these things are done by the aliascnt-package. More details about LaTeX's counter-infrastructure can be found in the commented LaTeX 2e sources, file m:ltcounts.dtx , 22.1 Environment Counter Macros. Jul 5, 2020 at 10:28

You must log in to answer this question.

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