# 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):

\documentclass{book}

\newcounter{hypCNT}
\newcommand{\hypCnt}[2]{\refstepcounter{hypCNT}\label{#1}H-\arabic{chapter}.\arabic{hypCNT}}

\begin{document}
\chapter{Lorem}

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

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


Chapter 1
Lorem
Hypotheses:
Pigs can ﬂy (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!

• Welcome to TeX SE! Have you looked at fancyref or cleverref? – cfr Apr 8 '15 at 23:45
• The ) is swallowed and digested by the fact that \hypcnt has two arguments but you call it with just one! – user31729 Apr 8 '15 at 23:46
• ...the closing bracket disappears because you've defined \hypCnt to take 2 arguments yet you only supply one (explicitly). As such, TeX grabs whatever token follows your use of \hypCnt{.}, which is ) in this case. – Werner Apr 8 '15 at 23:46
• Ah, thx @ChristianHupfer and @Werner: That sounds reasonable. Strange thing: I would swear 30 min ago Latex did not compile when defining \hypCnt with one argument only. Now, one argument is perfectly fine. @cfr: Thanks for the hint. I am going to check these. – ToJo Apr 8 '15 at 23:57

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>
\renewcommand{\thehypCNT}{H-\thechapter.\arabic{hypCNT}}
\newcommand{\hypCnt}[1]{%
\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.

• You may wish to have the hypCNT counter reset with every chapter (not sure). For that, use \newcounter{hypCNT}[chapter]... – Werner Apr 8 '15 at 23:55