# Correctly order float counter (perpage.sty) while sharing eqn/thm counter

1. Sharing float (fig) and thm counter can be done with \newtheorem{theorem}[figure]{Theorem}.
2. Correctly ordering float (fig) counter based on actual location in the PDF (as opposed to location in source code) can be done with \MakeSorted{figure} from perpage.sty.
3. Sharing float (fig) and eqn counter can be done with \makeatletter\let\c@equation\c@figure\makeatother.

I can do 1 and 2 together (see mwe-b). I can do 1 and 3 together (see mwe-c). But I can't do all three together (see mwe-d).

Partial/fake solution. I can skirt the issue by making a myeqn environment (see mwe-a).

Question. Is there an actual solution that allows you to keep the equation environment? Maybe one can resolve the clash in mwe-d by swapping \c@figure out for something (based on the precise implementation of perpage.sty)? Any help would be greatly appreciated!

%% MWE-A
\documentlass{article}
\usepackage{blindtext}
\newtheorem{theorem}[figure]{Theorem} %1
\usepackage{perpage}\MakeSorted{figure} %2
\newenvironment{myeqn}
{\stepcounter{figure}\hfill\begin{math}}
{\end{math}\hfill(\arabic{figure})}
\begin{document}
\begin{theorem}\blindtext\end{theorem}
\begin{figure}\blindtext\caption{Caption}\end{figure}
\begin{myeqn}$$$$$$a^2+b^2=c^2$$$$$$\end{myeqn}
\end{document}

%% MWE-B
\documentclass{article}
\usepackage{blindtext}
\newtheorem{theorem}[figure]{Theorem} %1
\usepackage{perpage}\MakeSorted{figure} %2
\begin{document}
\begin{theorem}\blindtext\end{theorem}
\begin{figure}\blindtext\caption{Caption}\end{figure}
$$$$$$a^2+b^2=c^2$$$$$$
\end{document}

%% MWE-C
\documentclass{article}
\usepackage{blindtext}
\newtheorem{theorem}[figure]{Theorem} %1
\makeatletter\let\c@equation\c@figure\makeatother %3
\begin{document}
\begin{theorem}\blindtext\end{theorem}
\begin{figure}\blindtext\caption{Caption}\end{figure}
$$$$$$a^2+b^2=c^2$$$$$$
\end{document}

%% MWE-D
\documentclass{article}
\usepackage{blindtext}
\newtheorem{theorem}[figure]{Theorem} %1
\usepackage{perpage}\MakeSorted{figure} %2
\makeatletter\let\c@equation\c@figure\makeatother %3
\begin{document}
\begin{theorem}\blindtext\end{theorem}
\begin{figure}\blindtext\caption{Caption}\end{figure}
$$$$$$a^2+b^2=c^2$$$$$$
\end{document}


Instead of making a new myeqn environment, just modify the equation environment. The solution seems to play fine with perpage's \MakeSorted (the numbering looks as expected).

\usepackage{etoolbox}
\AtBeginEnvironment{equation}{%
\setcounter{equation}{\value{figure}}%
\stepcounter{figure}%
}
\renewcommand\theequation{\arabic{figure}}


The last line above replaces the equation counter's printout with the figure counter's printout, because the equation counter can be incorrect when \MakeSorted renumbers the floats based on display order. Thus, the line \setcounter{equation}{\arabic{figure}} above is unnecessary and can be deleted (the line is just to insure against accidental referral to the equation counter).

Similarly, because perpage clashes with chngcntr's \counterwithin, you can modify \section if you want to reset your counter each section.

\usepackage{etoolbox}
\preto\section{\setcounter{figure}0}


The patch seems to work as long as you don't have too many sections and floats close to each other, ie, on the same page.

Edit. Here are two mwe's: just the equation fix (mwe-e), and the equation fix with the count within section fix (mwe-f).

%% MWE-E
\documentclass{article}
\usepackage{lipsum, amsthm, perpage, etoolbox}
\newtheorem{theorem}[figure]{Theorem} % sync thm, float
\MakeSorted{figure} % renumber float
\AtBeginEnvironment{equation}{\stepcounter{figure}} % sync eqn, float
\renewcommand\theequation{\arabic{figure}} % sync eqn, float

\begin{document}
\section*{This is a section}
\begin{theorem}This is a theorem.\end{theorem}
\begin{figure}\lipsum[2]\caption{This is a caption}\end{figure}
$$$$a^2+b^2=c^2$$$$
\section*{Another section}
\begin{theorem}[This is a Title]\lipsum[2]\end{theorem}
$$$$a^2+b^2=c^2$$$$
\end{document}


%% MWE-F
\documentclass{article}
\usepackage{lipsum, amsthm, perpage, etoolbox}
\newtheorem{theorem}[figure]{Theorem}
\MakeSorted{figure}
\AtBeginEnvironment{equation}{\stepcounter{figure}}
\renewcommand\theequation{\arabic{section}.\arabic{figure}} % count within sec
\renewcommand\thefigure{\arabic{section}.\arabic{figure}} % count within sec
\preto\section{\setcounter{figure}0} % count within sec

\begin{document}
\section{This is a section}
\begin{theorem}This is a theorem.\end{theorem}
\begin{figure}\lipsum[2]\caption{This is a caption}\end{figure}
$$$$a^2+b^2=c^2$$$$
\section{Another section}
\begin{theorem}[This is a Title]\lipsum[2]\end{theorem}
$$$$a^2+b^2=c^2$$$$
\end{document}