# How to get correct reference to label of custom counters?

I need several custom counters in my document which need to be referenced throughout the text. I figured how to create custom counters which can be labeled and referenced. Example: If I show the 3rd definition located in subsection 1 of section 2, \ref{bestdef} is supposed to give me "2.1.3". Now my problem is that \ref{bestdef} just shows "2.1" and leaves out the custom counter of definitions (the link works though). What is it I am missing? See my minimal example attached.

\documentclass[a4paper,fleqn]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[font=small,labelfont=bf,format=plain,labelsep=period]{caption}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%% Custom Counter: Definition %%%%%%%%%%%%%%%%%
\newcounter{mydefcounter}\setcounter{mydefcounter}{1}
\newcommand{\mydef}[1]{\arabic{section}.\arabic{subsection}.\arabic{mydefcounter}\textbf{#1}\refstepcounter{mydefcounter}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\section{First Section}
\subsection{First Subsection}
Definition \mydef{\label{nicedef}}: This is my first definition.

\subsection{Second Subsection}
Definition \mydef{\label{verynicedef}}: This is my second definition.

\section{Second Section}
\subsection{Third Subsection}
In Definition \ref{nicedef} we had seen that...

In Definition \ref{verynicedef} we had seen that...

Definition \mydef{\label{bestdef}}: This is my third definition.

In Definition \ref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

• Welcome to TeX.SE. I think you don't have understood the \label and \ref mechanism really. I find the \mydef macro weird for this purpose – user31729 Aug 28 '17 at 17:50

In my point of view, there's no need to use such a counter at all with the conjunction of \newtheorem{definition}{Definition}[subsection] and the usage of proper labels and cleveref package.

First an explanation why the OP way fails:

\label needs a previous \refstepcounter call, which defines \@currentlabel, which is explicitly using \themydefcounter in this example here.

\themydefcounter is nothing but \arabic{mydefcounter} so this gets out of line at some point.

See the example at the end:

\documentclass{article}

\newtheorem{definition}{Definition}[subsection]

\usepackage{hyperref}
\usepackage{cleveref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\section{First Section}
\subsection{First Subsection}
\begin{definition} \label{nicedef}

This is my first definition.
\end{definition}

\subsection{Second Subsection}
\begin{definition} \label{verynicedef}
This is my second definition.
\end{definition}

\section{Second Section}
\subsection{Third Subsection}

In \Cref{nicedef} we had seen that...

In \Cref{verynicedef} we had seen that...

\begin{definition}\label{bestdef}

This is my third definition.
\end{definition}

In \Cref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


Now the 'wrong' and 'correct' usage:

\documentclass{article}

\newcounter{foo}
\setcounter{foo}{1}
\newcommand{\mydef}[1]{\arabic{section}.\arabic{subsection}.\arabic{foo}\refstepcounter{foo}\textbf{#1}}

\begin{document}

\section{foo}
\subsection{foosubsection}

\makeatletter
A def: \mydef{\label{firstfoo}}

But this is the connected \verb!\@currentlabel!: \@currentlabel

\ref{firstfoo}

Another def: \mydef{\label{secondfoo}}

But this is the connected \verb!\@currentlabel!: \@currentlabel

\ref{secondfoo}

Now do it better:

\renewcommand{\thefoo}{\thesubsection.\arabic{foo}}
\renewcommand{\mydef}[1]{\refstepcounter{foo}#1\textbf{\thefoo}}

A def: \mydef{\label{thirdfoo}}

But this is the connected \verb!\@currentlabel!: \@currentlabel

\ref{thirdfoo}

Another def: \mydef{\label{fourthfoo}}

But this is the connected \verb!\@currentlabel!: \@currentlabel

\ref{fourthfoo}

\makeatother

\end{document}


Your use of \label and \ref is rather weird, and I didn't try to understand what is going wrong. Rearranging some of your definitions, however, makes everything work as expected:

\documentclass[a4paper,fleqn]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[font=small,labelfont=bf,format=plain,labelsep=period]{caption}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%% Custom Counter: Definition %%%%%%%%%%%%%%%%%
\newcounter{mydefcounter}
\renewcommand{\themydefcounter}{\arabic{section}.\arabic{subsection}.\arabic{mydefcounter}}
\newcommand{\mydef}[1]{\refstepcounter{mydefcounter}\label{#1}\themydefcounter}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\section{First Section}
\subsection{First Subsection}
Definition \mydef{nicedef}: This is my first definition.

\subsection{Second Subsection}
Definition \mydef{verynicedef}: This is my second definition.

\section{Second Section}
\subsection{Third Subsection}
In Definition \ref{nicedef} we had seen that...

In Definition \ref{verynicedef} we had seen that...

Definition \mydef{bestdef}: This is my third definition.

In Definition \ref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}