[Tex/LaTex] How to create custom labels which can be referenced


I would like to label the hypotheses in my thesis similar to equations (e.g., the first hypothesis in chapter 3 should be “H-3.1”). Also similar to equations, I would like to reference these hypotheses.
What I got so far is the following (which is based on this answer):




Hypotheses: \\
Pigs can fly (\hypCnt{hyp:H1}) \\
The world is a disk (\hypCnt{hyp:H2})\\

Hypotheses \ref{hyp:H1} on page \pageref{hyp:H1}

Which leads to:

Chapter 1



Pigs can fly (H-1.1

The world is a disk (H-1.2

Hypotheses 1 on page 1

This solution is close but not exactly what I would like to have. Actually I would like the H-\arabic{chapter} to be part of the label. Of course I could define another \newcommand{\refHyp} also containing the H-\arabic{chapter} part. However, if I would reference a H-label out of another chapter I would get the chapter number of the current chapter and not the one I am actually referencing to.

Another strange thing: Why does the closing bracket disappear behind the \hypCnt{}?

Any ideas appreciated a lot!

Best Answer

Apart from the use of you hypothesis construction, the following is a better suit for what you're after:

\newcounter{hypCNT}[chapter]% Hypothesis counter
% Representation of hypothesis counter: H-<chap>.<hypCNT>
  \refstepcounter{hypCNT}% Step hypothesis counter
  \thehypCNT% Print hypothesis counter
  \label{#1}}% Mark with label

Note the use of \thechapter rather than \arabic{chapter}. This is to let the reference be dependent on whatever type of representation is used for the chapter counter, rather than explicitly stating it will be \arabic. Such recursive/dependent usages is common when dealing with sub-counters.

Consider reading up on Understanding how references and labels work where it is discussed what happens with a call to \label{..}. What is stored for future reference is \@currentlabel, which is set based on the last \refstepcounter's representation - \the<cnt>. Hence the fact that I've updated the way the counter looks via \thehypCNT.

Related Question