# How to change theorem numbering when macro file is involved

In my macro file, I have certain theorem environments defined, for example.

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}


Again, these lines are in my macros.sty file of shortcuts.

In the past, I have changed the numbering of theorem, etc... by using \numberwithin. I have recently learned that I can instead edit the \newtheorem line. For example, if I want results numbered according to section I can use

\newtheorem{theorem}{Theorem}[section]


My question now is about future situations when I have two documents, which both use the same macros.sty file, but in one document I want to number results by section and in the other document I want to number results by something else, say chapter.

How can I accomplish this without creating two different .sty files?

EDIT: I think I should add that I am looking for a way to do this without going back to \numberwithin. The reason for this is because \numberwithin doesn't seem to work well with the thmtools package.

Use options to the package, say, drivercounter, which is set to default as section in the package (mymacro.sty) and to chapter in the file demochapter.tex.

For simplification I did not add the other theorem definitions

There could be of course other package options, but this is the choice of the OP...

## mymacro.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mymacro}

\usepackage{amsthm}

\RequirePackage{xkeyval}

%% Declare a package option
\DeclareOptionX{drivercounter}{%
\def\TheoremDriverCounter{#1}%
}

%% Preset the option with 'section' value
\ExecuteOptionsX{drivercounter=section}

%% Process the options given to the package
\ProcessOptionsX

\newtheorem{theorem}{Theorem}[\TheoremDriverCounter]

\endinput


## demosection.sty

\documentclass{article}

\usepackage{mymacro}

\begin{document}

\section{First}

\begin{theorem}
Blabla
\end{theorem}

\begin{theorem}
Blabla
\end{theorem}

\section{Second}

\begin{theorem}
Blabla
\end{theorem}
\end{document}


## demochapter.tex

\documentclass{book}

\usepackage[drivercounter=chapter]{mymacro}

\begin{document}

\chapter{First}

\section{First}

\begin{theorem}
Blabla
\end{theorem}

\chapter{Second}

\section{First of 2nd}

\begin{theorem}
Blabla 1
\end{theorem}

\section{Second of 2nd}

\begin{theorem}
Blabla 2
\end{theorem}
\end{document}


Snapshot for demochapter.tex