# Custom Numbering in Derivation / Proof Environment

Just a quick background, I am writing a thesis, which has theorems (proofs and derivations) and possibly a few other 'theorem' type environments. The code in this question actually works fine, it is doing what I want however I feel that it could be tidied up / improved.

In terms of equations, I have elected to have a numbering format which is local to the actual proof or derivation. So this involves storing the existing equation number, resetting it to zero, changing the style (for the proof or derivation respectively) executing the contents, and then restoring to the existing value.

Furthermore, some of the derivations / proofs can be condensed by using multicol, and one of my earlier posts Shaded Multicols serves to distinguish the proof / derivation from surrounding content, however, for the purposes of this question, these formatting styles have been removed.

Here is a stripped down version of the code, which is a full MWE:

\documentclass{article}

%Load Single Packages Together
\usepackage{amsmath,amsfonts,amssymb,amsthm,mathtools,mathpazo,calc,ifthen}
\usepackage{environ,multicol,xcolor}

%DEFINE MY THEOREM STYLE
\let\oldproof\proof\let\proof\relax
\let\oldendproof\endproof\let\endproof\relax
\newcommand{\theoremLetter}{}
\newtheoremstyle{mythm} % name
{0pt}                        % Space above
{0pt}                        % Space below
{\small}% Body font
{}                           % Indent amount
{\bf\color{blue!50!black}}% Theorem head font
{.}% Punctuation after theorem head
{.5em}                       % Space after theorem head
{}  % Theorem head spec (can be left empty, meaning ‘normal’)
\theoremstyle{mythm} %SELECT THE THEOREM
\newtheorem{derivation}{Derivation}
\newtheorem{proof}{Proof}

%Renew Formats, Equation, Figure, Table.
\renewcommand{\thederivation}{D\arabic{derivation}}
\renewcommand{\theproof}{P\arabic{proof}}

\newcounter{mytemp}%
\newenvironment{myenv}[3]{%
\def\envvar{#1}%
\def\mycols{#2}%

%Set the first letter depending on the type of environment.
%DERIVATION
\providecommand{\theoremLetter}{}%RESET
\ifthenelse{\equal{\envvar}{derivation}}{%
\renewcommand{\theoremLetter}{D}%
}{}%
%PROOF
\ifthenelse{\equal{\envvar}{proof}}{%
\renewcommand{\theoremLetter}{P}%
}{}%

\begin{\envvar}{\textbf{#3}}%
\setcounter{mytemp}{\value{equation}}%
\providecommand{\existingEquationFormat}{\theequation}%
\setcounter{equation}{0}%
\renewcommand{\theequation}{%
\theoremLetter\arabic{\envvar}.\arabic{equation}%
}%
\setlength{\abovedisplayskip}{0pt}%Set DisplaySkip Value.
\ifthenelse{\equal{\mycols}{1}}{}{\begin{multicols}{\mycols}}%
}{%BODY CONTENT OF ENVIRONMENT
\ifthenelse{\equal{\mycols}{1}}{}{\end{multicols}}%
\setcounter{equation}{\value{mytemp}}%
\renewcommand{\theequation}{\existingEquationFormat}%
\renewcommand{\existingEquationFormat}{\relax}%
\end{\envvar}%
}


And for a sample output:

\begin{document}
\hrule

\section{Default Equation Format}
\begin{align}
y &= A\\
&= B
\end{align}

\hrule

\section{Sample Derivation and Proof (Multicolumn)}
\begin{myenv}{proof}{2}{Sample Proof}
Column 1
\begin{align}%
y &= A\\
&= B
\end{align}%
Column 2
\begin{align}
y &= C\\
&= D
\end{align}
\end{myenv}

\begin{myenv}{derivation}{2}{Sample Derivation}
Column 1
\begin{align}%
y &= A\\
&= B
\end{align}%
Column 2
\begin{align}
y &= C\\
&= D
\end{align}
\end{myenv}

\hrule

\section{Demonstrate Reset/Continuation of Format}
\begin{align}
y &= A\\
&= B
\end{align}

\hrule

\section{Further Derivation and Proof (Single Column)}
\begin{myenv}{proof}{1}{Another Proof}
\begin{align}%
y &= A\\
&= B
\end{align}%
\end{myenv}

\begin{myenv}{derivation}{1}{Another Derivation}
\begin{align}%
y   &= A\\
&= B
\end{align}%
\end{myenv}

\hrule

\end{document}


The above code produces the following result:

In this output, it is clear to see the following features:

1. The numbering style of the Derivation and Proofs is local to the particular Derivation and Proof, the style has been changed to be preceeded by a P or a D, which serves to clearly distinguish the numbering from the standard equation numbering as part of the rest of the document.
2. Standard equation numbering is broken and resumed either side of the thm
3. Use of Single and Multi column mode.

So my question here is, how can I replace the following in the code:

%Set the first letter depending on the type of environment.
%DERIVATION
\providecommand{\theoremLetter}{}%RESET
\ifthenelse{\equal{\envvar}{derivation}}{%
\renewcommand{\theoremLetter}{D}%
}{}%
%PROOF
\ifthenelse{\equal{\envvar}{proof}}{%
\renewcommand{\theoremLetter}{P}%
}{}%


...So that it takes the first letter (uppercase) of the particular theorem style, at the moment, the reformatting is effectively 'hard-coded', and I would like to remove this limitation.

Is there a more efficient way of coding what I have done here, I am sure there is, perhaps the \newenvironment{myenv}{...} can be removed completely.

-

You can extract the first letter by de following definition which needs no extra package:

\def\tempa##1##2!{##1}%
\uppercase{\def\theoremLetter{\tempa#1!}}%%


The package xstring is providing a similar approach by

\uppercase{\StrLeft{#1}{1}[\theoremLetter]}%


Here your modified environment

\newenvironment{myenv}[3]{%
\def\envvar{#1}%
\def\mycols{#2}%
%Set the first letter depending on the type of environment.
%DERIVATION
%Approach 1
\def\tempa##1##2!{##1}%
\uppercase{\def\theoremLetter{\tempa#1!}}%%
%Approach 2
%   \uppercase{\StrLeft{#1}{1}[\theoremLetter]}%%
%
\begin{\envvar}{\textbf{#3}}%
\setcounter{mytemp}{\value{equation}}%
\providecommand{\existingEquationFormat}{\theequation}%
\setcounter{equation}{0}%
\renewcommand{\theequation}{%
\theoremLetter\arabic{\envvar}.\arabic{equation}%
}%
\setlength{\abovedisplayskip}{0pt}%Set DisplaySkip Value.
\ifthenelse{\equal{\mycols}{1}}{}{\begin{multicols}{\mycols}}%
}{%BODY CONTENT OF ENVIRONMENT
\ifthenelse{\equal{\mycols}{1}}{}{\end{multicols}}%
\setcounter{equation}{\value{mytemp}}%
\renewcommand{\theequation}{\existingEquationFormat}%
\renewcommand{\existingEquationFormat}{\relax}%
\end{\envvar}%
}


Improvements (from my point of view):

• Definitions/redefinitions with \renewcommand are working local. So every resetting isn't necessary.
• normally setcounter are also local but if you load the package calc those assignments are always global. So you have to reset the counter.
• I prefer a smaller definition of environments in this way that most assignment are done in an extra macro.

If you wrote a package you should do more tests whether the environment exists or not.

\let\innerenv\relax
\let\endinnerenv\relax
\newenvironment{myenv}[3]{%
\def\envvar{#1}%
\begin{\envvar}{\textbf{#3}}%
\def\tempa##1##2!{##1}%
\uppercase{\def\theoremLetter{\tempa#1!}}%%
\setcounter{mytemp}{\value{equation}}%
\setlength{\abovedisplayskip}{0pt}%
\setcounter{equation}{0}%
\expandafter\let\expandafter\theequaction\csname the#1\endcsname
\ifnum#2>1
\def\innerenv{\begin{multicols}{#2}}%
\def\endinnerenv{\end{multicols}}%
\fi%
\innerenv
}{%BODY CONTENT OF ENVIRONMENT
\endinnerenv%
\setcounter{equation}{\value{mytemp}}%
\end{\envvar}%
}

-
Thanks, however I don't understand the following syntax \def\tempa##1##2!{##1}% How does it work? – Nicholas Hamilton May 26 '13 at 10:03
The command tempa has two mandatory arguments which must be end by a ! if you put a string which contains of single tokens as an argument to \tempa the following happens: \tempa'proof'! -- the first token is p and the rest is roof. – Marco Daniel May 26 '13 at 10:05
ok but what does ##1 and ##2 mean, ie, what does the double hash format mean? – Nicholas Hamilton May 26 '13 at 10:07
@ADP: See: tex.stackexchange.com/questions/42463/… – Marco Daniel May 26 '13 at 10:12
Cheers. 7654321 – Nicholas Hamilton May 26 '13 at 10:14