Hypotheses and Subhypotheses with ntheorem

The goal is to have an environment that can, depending on a trigger, either create this:

Hypothesis 1 The better the answer, the higher the score.

or that

Hypothesis 2a Higher score is positive correlated with shoesize.

Hypothesis 2b Lower score is negative correlated with shoesize.

I tried a code I found in Google Groups, and it works. However it produces some error messages which I don't understand. Please see the minimal example below:

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{hyp}{Hypothesis}
\newcounter{subhyp}
\newcommand{\subhyp}{
\setcounter{subhyp}{0}
\renewcommand\thehyp{\protect\stepcounter{subhyp}%
}
\newcommand{\normhyp}{
\renewcommand\thehyp{\arabic{hyp}}
\stepcounter{hyp}
}
\begin{document}

\normhyp
\begin{hyp}
The better the answer, the higher the score.
\end{hyp}

\subhyp
\begin{hyp}
Higher score is positive correlated with shoesize.
\end{hyp}

\begin{hyp}
Lower score is negative correlated with shoesize.
\end{hyp}

\end{document}

• Use \protect\addtocounter{subhyp}{1} instead of \stepcounter{subhyp} I don't know why \stepcounter fails. – Marco Daniel Mar 4 '13 at 17:58

I'd avoid abusing \thehyp: you'll be in trouble if you want to get a list of hypotheses, for instance.

In my opinion, the best approach is to enclose the "subhypotheses" in an environment that changes the meaning of the hyp counter and of some related things.

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{hyp}{Hypothesis}

\makeatletter
\newcounter{subhyp}
\let\savedc@hyp\c@hyp
\newenvironment{subhyp}
{%
\setcounter{subhyp}{0}%
\stepcounter{hyp}%
\edef\saved@hyp{\thehyp}% Save the current value of hyp
\let\c@hyp\c@subhyp     % Now hyp is subhyp
\renewcommand{\thehyp}{\saved@hyp\alph{hyp}}%
}
{}
\newcommand{\normhyp}{%
\let\c@hyp\savedc@hyp % revert to the old one
\renewcommand\thehyp{\arabic{hyp}}%
}
\makeatother
\begin{document}

\begin{hyp}
The better the answer, the higher the score.
\end{hyp}

\begin{subhyp}
\begin{hyp}
Higher score is positive correlated with shoesize.
\end{hyp}

\begin{hyp}
Lower score is negative correlated with shoesize.
\end{hyp}
\end{subhyp}

\begin{hyp}
Something
\end{hyp}

\begin{subhyp}
\begin{hyp}
Again
\end{hyp}
\begin{hyp}
And again
\end{hyp}
\end{subhyp}

\begin{hyp}
Will it work?
\end{hyp}

\end{document}


You can still use \subhyp and \normhyp, if you prefer, but it's better to profit of the group structure of LaTeX. Your code works if you use the solution

\protect\addtocounter{subhyp}{1} instead of \stepcounter{subhyp}


On the other hand you can use the advantages of the command \newtheorem which has an interesting optional argument. Based on the example of Mico use:

\newtheorem{hyp}{Hypothesis}
\newtheorem{subhyp}{Hypothesis}[hyp]


Here the complete code:

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{hyp}{Hypothesis}
\newtheorem{subhyp}{Hypothesis}[hyp]
\renewcommand\thesubhyp{\thehyp.\alph{subhyp}}

\begin{document}

\begin{hyp}
The better the answer, the higher the score.
\end{hyp}

%\stepcounter{hyp}
%\setcounter{subhyp}{0}
\begin{subhyp}
A higher score is positively correlated with shoesize.
\end{subhyp}

\begin{subhyp}
A lower score is negatively correlated with shoesize.
\end{subhyp}

\begin{hyp}
\end{hyp}

%\setcounter{subhyp}{0} % Don't execute \stepcounter{hyp} in this case.
\begin{subhyp}
Pythagoras contributed something to geometry.
\end{subhyp}

\begin{subhyp}
\emph{Pythagoras} wasn't a single person but a group of like-minded philophers and mathematicians.
\end{subhyp}
\end{document}

• I like the simple approach but it doesn't work without further modification (stepcounter) if you have a sequence like H1a, H1b, H2a, H2b. Subhyp wouldn't know when to increase the hyp counter. – Roman Mar 4 '13 at 19:16

Compared with your code, the MWE below does the following:

• It defines an environment called subhyp explicitly.

• There's no need to keep typing \normhyp and \subhyp. Instead, at the start of a group of sub-hypotheses, you need to type either

• \stepcounter{hyp}\setcounter{subhyp}{0}

if the sub-hypotheses that aren't linked to a preceding (main) hypothesis, or

• \setcounter{subhyp}{0}

if the sub-hypotheses hould share their main counter with the preceding (main) hypothesis. \documentclass{article}
\usepackage{ntheorem}
\newtheorem{hyp}{Hypothesis}
\newtheorem{subhyp}{Hypothesis}
\renewcommand\thesubhyp{\thehyp\alph{subhyp}}

\begin{document}

\begin{hyp}
The better the answer, the higher the score.
\end{hyp}

\stepcounter{hyp}
\setcounter{subhyp}{0}
\begin{subhyp}
A higher score is positively correlated with shoesize.
\end{subhyp}

\begin{subhyp}
A lower score is negatively correlated with shoesize.
\end{subhyp}

\begin{hyp}