Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}% 
  \arabic{hyp}\alph{subhyp}\protect\addtocounter{hyp}{-1}} 
} 
\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}
share|improve this question
    
Welcome to TeX.sx! –  Kurt Mar 4 '13 at 16:04
2  
Use \protect\addtocounter{subhyp}{1} instead of \stepcounter{subhyp} I don't know why \stepcounter fails. –  Marco Daniel Mar 4 '13 at 17:58

3 Answers 3

up vote 6 down vote accepted

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.

enter image description here

share|improve this answer

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}
Pythagoras had something to say.
\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}
share|improve this answer
    
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.

enter image description here

\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}
Pythagoras had something to say.
\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.