# Figure number adjusted with theorems

I am trying to adjust the figure numbers linked to the theorem it is displayed in. In case of a theorem contains exactly one figure, the figure number will be theorem number; for multiple figures, the figures will be numbered like theorem number.figure number.

I have tried:

\documentclass[10pt]{article}
\usepackage{graphicx, thmtools, lipsum, float}
\declaretheorem{Job}

\makeatletter
\let\c@equation\c@figure
\makeatother
\renewcommand{\thefigure}{\arabic{Job}.\arabic{equation}}

\begin{document}

\begin{Job}[Off home]
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{coalminer}
\caption{Coal-miner}
\end{figure}

\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{engineer}
\caption{Engineer}
\end{figure}
\end{Job}

\begin{Job}[Home]
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{police}
\caption{Police}
\end{figure}
\end{Job}
\end{document}


It works fine for the multiple figure case, but for the single figure case it is not working (the figure number for Police job should be just 2):

How can I do that?

-

You need to know as with the first figure whether there are more figures to follow within the same environment, or not.

Unfortunately, LaTeX is not able to perform that sort of “look ahead” on its own.

Anyhow, you can use .aux-files to store the total number of figures within each Job-environment and process that very number at the beginning of each Job-enviroment with next latex run.

Since it is quite a mess to re-define the thmtool's enviroments, i'm gonna use a temporary environment as “wrapper” to store and process the code for the aux-file.

First, we rename the thmtools-Environment, since we won't use it directly within the further document:

\declaretheorem[name=Jobs]{TempJob}


We overwrite the displayed theorem name because it should read “Job” rather than “TempJob”.

Then we make up our own Job-environment, put the TempJob-Environment inside; and pass the optional argument to rescue the environment's local label:

\newenvironment{Job}[1][]{%
\begin{TempJob}[#1]%
}{%
\end{TempJob}%
}


Now, for the tricky part:

At the end of our Job-Environment we need to store the total number of figures within that environment in the .aux-file. Therefore, we add

...
}{%
\immediate\write\@auxout{\noexpand\auxcontent{\arabic{TempJob}}{\arabic{equation}}}%
\end{TempJob}%
}


to the third argument of \newenvironment.

In this step, we write the command \auxcontent to the .aux-file which gets two arguments: #1 is the number of our current Job-environment, #2 is the current (and total) value of the figure-counter (or, as in your case, equation-counter).

The auxcontent command is priorly defined as follows:

\def\auxcontent#1#2{%
\def\@rgi{#1}%
\def\@rgii{#2}%
\global\expandafter\let\csname totalFigsIn\@rgi\endcsname\@rgii%
}


That is, for each Job-environment we create a unique command called \totalFigsIn<job-counter> that represents the toatal number of figures within that very environment.

Finally, we can do some stuff with that number: We define the figures-counter depending on whether the total figures number is 1 or not. That evaluation takes at the beginning of our Job-environment:

  \edef\CurrentTotalFigs{\csname totalFigsIn\arabic{TempJob}\endcsname}
\ifx\my@ne\CurrentTotalFigs
\renewcommand{\thefigure}{\arabic{TempJob}}%
\else
\renewcommand{\thefigure}{\arabic{TempJob}.\arabic{equation}}%
\fi


First of all, we pass the the number to a temporary macro since LaTeX's if-constructions cannot handle complex instruction chains. This temp. macro is called \CurrentTotalFigs in the upper code snippet.

The number one is represented by the custom \my@ne macro, which is simply defined as:

\def\my@ne{1}


Now, we can make a distinction: If \CurrentTotalFigs equals 1, \thefigure will be simply the same as our current TempJob-counter (part before \else). If the total number of figures is different from 1 we use the Job.Figure-variant (the part after \else)

Finally, in order to start the figure-count at 0 for each Job-environemnt we need to reset the counter at the very beginning of each Job-environment:

\setcounter{equation}{0}%


All in all, your preamble up to \begin{document} should look like this, now:

\documentclass[10pt]{article}
\usepackage{graphicx, thmtools, lipsum, float}
\declaretheorem[name=Jobs]{TempJob}

\makeatletter
\def\my@ne{1}
\let\c@equation\c@figure
\def\auxcontent#1#2{%
\def\@rgi{#1}%
\def\@rgii{#2}%
\global\expandafter\let\csname totalFigsIn\@rgi\endcsname\@rgii%
}
\newenvironment{Job}[1][]{%
\setcounter{equation}{0}%
\begin{TempJob}[#1]%
\edef\CurrentTotalFigs{\csname totalFigsIn\arabic{TempJob}\endcsname}
\ifx\my@ne\CurrentTotalFigs
\renewcommand{\thefigure}{\arabic{TempJob}}%
\else
\renewcommand{\thefigure}{\arabic{TempJob}.\arabic{equation}}%
\fi
}{%
\immediate\write\@auxout{\noexpand\auxcontent{\arabic{TempJob}}{\arabic{equation}}}%
\end{TempJob}%
}

\makeatother


One last note: Since we are using .aux-files, we will need two latex-runs in order to get the desired results after changes.

-

You can use chngcntr package and do

\usepackage{chngcntr}
\counterwithin{equation}{Job}    %% you can do this too because you defined it
%% \counterwithin{figure}{Job}


Full code:

\documentclass[10pt,demo]{article}                %%% remove demo
\usepackage{graphicx, thmtools, lipsum, float}
\declaretheorem{Job}

\makeatletter
\let\c@equation\c@figure
\makeatother
\renewcommand{\thefigure}{\arabic{Job}.\arabic{equation}}
\usepackage{chngcntr}
\counterwithin{equation}{Job}
\begin{document}

\begin{Job}[Off home]
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{coalminer}
\caption{Coal-miner}
\end{figure}

\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{engineer}
\caption{Engineer}
\end{figure}
\end{Job}

\begin{Job}[Home]
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{police}
\caption{Police}
\end{figure}
\end{Job}
\end{document}


-

If it is practicable for you, you can reset the figure counter in each theorem. Maybe, you want to redefine the theorem environment, to do this automatically, but I don't know, how to do this. One line per theorem should be OK.

For your exact problem: I don't know, how LaTeX shall notify, if one or two figures are included in one theorem. I think, it is much easier to name it 2.1 and not 2.

\documentclass[10pt]{article}
\usepackage{thmtools, lipsum, float}
\usepackage[demo]{graphicx}
\declaretheorem{Job}

\makeatletter
\let\c@equation\c@figure
\makeatother
\renewcommand{\thefigure}{\arabic{Job}.\arabic{equation}}

\begin{document}

\begin{Job}[Off home]
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{coalminer}
\caption{Coal-miner}
\end{figure}

\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{engineer}
\caption{Engineer}
\end{figure}
\end{Job}

\begin{Job}[Home]
\setcounter{figure}{0}
\lipsum[2]
\begin{figure}[H]
\centering
\includegraphics[height=.6in]{police}
\caption{Police}
\end{figure}
\end{Job}
\end{document}

-