# Customise counters, cross references to environments

I would like to make cross references to own defined counter on environments. So far I have tried the following,

\documentclass{book}
\newcounter{entryc}[subsection]
\newcommand{\thetheorem}{\thesubsection.\arabic{entryc}}
\newenvironment{qd}[1]{\refstepcounter{entryc}\noindent\textbf{Quasi Definition \thetheorem.}\begin{itshape}}
{\end{itshape}}
%
\begin{document}

\chapter{First chapter}
This is the first chapter.

\section{First section}
This is the first section.

\subsection{First subsection}
This is the first subsection.

\begin{qd}{}\label{qd:emp} This is my environment. \end{qd}

\noindent In quasi definition \ref{qd:emp} it \dots

\end{document}


What happens is that

But I would prefer "...In quasi definition 1.1.1.1". Do anyone have some suggestions of how solve this? and How to avoid the \noindent thing before the continuation of the text?

Additional information: if possible I would try to not use ntheorem.

## 2 Answers

Here's a solution that doesn't require modifying \@currentlabel. Since you've defined the counter entryc via \newcounter, it suffices to add the following three lines of code to your preamble:

\makeatletter
\renewcommand{\p@entryc}{\thesubsection.}
\makeatother


The macro \p@entryc is empty by default, but can be changed via a \renewcommand statement to (just about) anything else -- say, \thesubsection.. The value of \p@entryc is prefixed to the value of \label to create the string that will be produced when a cross-reference to an entity (such as your qd environment) occurs that increments the counter entryc.

The following modified form of your MWE implements this idea. It also applies a couple of minor changes to the definition of the qd environment (such as using explicit \par statements), and it uses @karlkoeller's idea to load the noindentafter package and to execute \NoIndentAfterEnv{qd}.

\documentclass{book}
\newcounter{entryc}[subsection]
\makeatletter
\renewcommand{\p@entryc}{\thesubsection.} % "prefix" for cross-referencing
\makeatother
\newenvironment{qd}
{\par\refstepcounter{entryc}
\noindent\textbf{Quasi Definition \thesubsection.\arabic{entryc}.}
\em}  % suggest you use \em instead of \begin/\end {itshape}
{\par} % use explicit \par statements to play it safe
\usepackage{noindentafter}
\NoIndentAfterEnv{qd}
\usepackage[colorlinks=true]{hyperref}

\begin{document}
\chapter{First chapter}
This is the first chapter.
\section{First section}
This is the first section.
\subsection{First subsection}
This is the first subsection.
\begin{qd}\label{qd:emp} This is my environment. \end{qd}
In quasi definition \ref{qd:emp}, it is shown that \dots
\end{document}

• Thanks, but I did not really get why not require \protected@... or more precisely why is this required in the first case? – nunatak Dec 5 '13 at 23:01
• @nunatak - For every counter defined via a \newcounter instruction, a macro named \p@<countername> is created automatically; its default value is just the empty string. LaTeX automatically "prefixes" the contents of \p@<countername> to the representation of the counter produced by \ref{<countername>}. Given that this mechanism is around anyway, why not just use it for your purpose? The alternative that redefines \@currentlabel is not wrong -- it's just more work than needs to be performed. :-) Note, BTW, that it's not necessary to define a separate macro called \thetheorem. – Mico Dec 5 '13 at 23:20

The problem is that you have to define \@currentlabel to give the right value to your \label.

So, after assigning the new value to the counter entryc through \refstepcounter, you need to add \protected@edef\@currentlabel{\thetheorem} (in this case probably \def\@currentlabel{\thetheorem} is enough).

In other words, change the definition of your environment to

\makeatletter
\newenvironment{qd}{%
\refstepcounter{entryc}\protected@edef\@currentlabel{\thetheorem}%
\noindent\textbf{Quasi Definition \thetheorem.}\itshape}{}
\makeatother


Note that I've removed the argument since you are not using it.

MWE

\documentclass{book}
\newcounter{entryc}[subsection]
\newcommand{\thetheorem}{\thesubsection.\arabic{entryc}}

\makeatletter
\newenvironment{qd}{%
\refstepcounter{entryc}\protected@edef\@currentlabel{\thetheorem}%
\noindent\textbf{Quasi Definition \thetheorem.}\itshape}{}
\makeatother

\begin{document}

\chapter{First chapter}
This is the first chapter.

\section{First section}
This is the first section.

\subsection{First subsection}
This is the first subsection.

\begin{qd}\label{qd:emp} This is my environment. \end{qd}

\noindent In quasi definition \ref{qd:emp} it \dots

\end{document}


Output:

## ADDENDUM

I've just realised that you asked for a way to avoid issuing \noindent after the closure of your environment...

To achieve that, you can load the package noindentafter and issue the command

\NoIndentAfterEnv{qd}


The following MWE gives the same result of the image above.

\documentclass{book}
\newcounter{entryc}[subsection]
\newcommand{\thetheorem}{\thesubsection.\arabic{entryc}}

\makeatletter
\newenvironment{qd}{%
\par\refstepcounter{entryc}\protected@edef\@currentlabel{\thetheorem}%
\noindent\textbf{Quasi Definition \thetheorem.}\itshape}{}
\makeatother

\usepackage{noindentafter}
\NoIndentAfterEnv{qd}

\begin{document}

\chapter{First chapter}
This is the first chapter.

\section{First section}
This is the first section.

\subsection{First subsection}
This is the first subsection.

\begin{qd}\label{qd:emp} This is my environment. \end{qd}

In quasi definition \ref{qd:emp} it \dots

\end{document}


Note that now you can even remove blank lines before and after the environment...

• great, it seems to work excellent, even with hyperref Thanks – nunatak Dec 5 '13 at 17:36