1

Please consider the following MWE:

\documentclass{report}
\usepackage{amsthm}

\makeatletter
%Define variable
\gdef\@depRe{chapter}
\def\depRe#1{\gdef\@depRe{#1}}

%Usie variable
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[\@depRe]
\makeatother

%Set variable
\depRe{section}

\begin{document}
\chapter{Network}
\section{Cellular}
\begin{theorem}
This is an example.
\end{theorem}
\end{document}

This is the result:

enter image description here

As you can see, I defined 'depRe' variable and use it as theorem counter. But it does not worked for me and the value of 'depRe' (i.e. section) does not applied! Now put \depRe{section} before \newtheorem ..., so it works. Does any solution exist, which I can set 'depRe' after \newtheorem command?

It is worth noting that I have TexLive 2019.

  • Why would you want this? – Ulrike Fischer Jul 25 at 8:00
  • For example suppose I am going to prepare a Latex style for report, article and etc. I defined \newtheorem{theorem}{Theorem}[\@depRe] in one tex file named as env.tex. Then several files (e.g. myreport.tex, myarticle.tex) input the mentioned file (env.tex) and set appropriate \depRe parameter (e.g. chapter, section) – A Diyanat Jul 25 at 8:13
  • 2
    The trailing optional argument to \newtheorem should contain a counter name, which is permanently attached to the definition. When processing your \newtheorem declaration, \@depRe gets expanded to chapter and no trace of it remains in the definition of theorem. – egreg Jul 25 at 8:38
  • The simplest fix would be to create two different theorem environments which use different counters. – John Kormylo Jul 25 at 15:25
2

The problem is that \newtheorem's numbered-within-argument (which denotes the name of a counter) is fully evaluated at the time of carrying out \newtheoremn.

You can implement your environment using a fake-counter for \newtheorem's numbered-within-argument and have a preamble-macro \depRe for redefining which real counter the fake-counter points to:

\documentclass{report}

\makeatletter

% define fake-infrastructure of fake-counter:
\newcommand*\c@MyFakeCnt{}%
\newcommand*\cl@MyFakeCnt{}%
\newcommand*\p@MyFakeCnt{}%
\newcommand*\theMyFakeCnt{}%
\newcommand*\theHMyFakeCnt{}%
\newcommand*\MyFakeCntautorefname{}%
\newcommand*\MyFakeCntname{}%

% define \depRe to map MyFakeCnt to counter denoted by argument:
\newcommand\depRe[1]{%
  \expandafter\let\expandafter\c@MyFakeCnt\expandafter=\csname c@#1\endcsname
  \expandafter\let\expandafter\cl@MyFakeCnt\expandafter=\csname cl@#1\endcsname
  \expandafter\let\expandafter\p@MyFakeCnt\expandafter=\csname p@#1\endcsname
  \expandafter\let\expandafter\theMyFakeCnt\expandafter=\csname the#1\endcsname
  \expandafter\let\expandafter\theHMyFakeCnt\expandafter=\csname theH#1\endcsname
  \expandafter\let\expandafter\MyFakeCntautorefname\expandafter=\csname #1autorefname\endcsname
  \expandafter\let\expandafter\MyFakeCntname\expandafter=\csname #1name\endcsname
}%
\@onlypreamble\depRe
% some initialization:
\depRe{section}%
\makeatother


\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[MyFakeCnt]%

%\depRe{chapter}
\depRe{section}

\begin{document}
\chapter{Network}
\section{Cellular}
\begin{theorem}
This is an example.
\end{theorem}
\end{document}
3

When \newtheorem is processed, the trailing optional argument is expected to expand to a counter name; this counter is permanently attached to the environment's definition.

Technically speaking, the trailing argument is processed via \csname...\endcsname, which implies full expansion of the tokens in between.

If you want to make a package, I suggest using an option.

diyanat.sty

\RequirePackage{kvoptions}

\SetupKeyvalOptions{
  family=diyanat,
  prefix=diyanat@,
}

\DeclareStringOption[chapter]{theoremdepth}[chapter]
\define@key{diyanat}{theoremdepth}{\renewcommand{\diyanat@theoremdepth}{#1}}
\newcommand{\theoremdepth}{\diyanat@theoremdepth} % the user level interface

\ProcessKeyvalOptions*

test.tex

\documentclass{report}

%\usepackage{diyanat} % would use chapter
\usepackage[theoremdepth=section]{diyanat} % would use section

\usepackage{amsthm}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[\theoremdepth]

\begin{document}

\chapter{Network}

\section{Cellular}

\begin{theorem}
This is an example.
\end{theorem}

\end{document}

Output with \usepackage{diyanat} or \usepackage[theoremdepth=chapter]{diyanat}

enter image description here

Output with \usepackage[theoremdepth=section]{diyanat}

enter image description here

  • such a nice suggestion... – MadyYuvi Jul 25 at 9:12
0

After reading your comments to your question I wonder whether you might be interested in nesting calls to \@ifclassloaded:

%\documentclass{book}
\documentclass{report}
%\documentclass{article}

\usepackage{amsthm}
\theoremstyle{plain}

\makeatletter
%%
%%\@ifclassloaded{<someclass>}{<true code>}{<false coode>}%
%%
\@ifclassloaded{book}{%
  \newtheorem{theorem}{Theorem}[chapter]%
}{%
  \@ifclassloaded{report}{%
    \newtheorem{theorem}{Theorem}[chapter]%
  }{%
    \@ifclassloaded{article}{%
      \newtheorem{theorem}{Theorem}[section]%
    }{%
      \newtheorem{theorem}{Theorem}%
    }%
  }%
}%
\makeatother

\begin{document}
\csname @ifundefined\endcsname{chapter}{\section}{\chapter}{Network}
\begin{theorem}
This is an example in a chapter.
\end{theorem}
\section{Cellular}
\begin{theorem}
This is an example in a section.
\end{theorem}
\end{document}
  • Thx, but some one may be set section or chapter for report style. – A Diyanat Jul 25 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.