8

Context: I have several environments with custom counters. As usual, I'm referencing them along to my document but I get only the corresponding counter value as reference.

Example:

\newcounter{myenvcounter}[section]\setcounter{myenvcounter}{0}
\def\theenv{\arabic{chapter}.\arabic{section}.\arabic{myenvcounter}}
\newenvironment{myenv}{
    \refstepcounter{myenvcounter}%
\\
    \noindent\textbf{Env. \theenv{}.}%
}{%
   \\
}

An instance of this environment:

\chapter{Chapter one} \label{ch:one}
   ...
   \section{Section one} \label{sec:one}
      ...
      \begin{myenv} \label{env:one} 
         Bla bla bla.
      \end{myenv}
      ... \\
      As shown in Env. \ref{env:one} ... etc.

And this produces:


1 Chapter one

...

1.1 Section one

...

Env. 1.1.1. Bla bla bla.

...

As shown in Env. 1 ... etc.


Question: How to customize the cross-reference numbering to get for example: "As shown in Env. 1.1.1" ?

8

Instead of using

\def\theenv{\arabic{chapter}.\arabic{section}.\arabic{myenvcounter}}

use

\renewcommand{\themyenvcounter}{\thesection.\arabic{myenvcounter}}

with a definition of your environment resembling:

\newenvironment{myenv}{%
  \refstepcounter{myenvcounter}%
  \par\medskip\noindent\textbf{Env.\ \themyenvcounter.}%
}{%
  \par\medskip\ignorespacesafterend%
}

The above assumes that \thesection is defined to be its usual definition (\thechapter.\arabic{section}).

There are two differences:

  1. I've used the counter representation of the counter that is actually incremented. That is how referencing of the labels work when using \refstepcounter (see Understanding how references and labels work).
  2. Instead of using \\ for inserting vertical space, I've used \par\medskip which sets the environment as a paragraph on its own.

Here is an actual MWE:

enter image description here

\documentclass{report}
\newcounter{myenvcounter}[section]%
\renewcommand{\themyenvcounter}{\thesection.\arabic{myenvcounter}}
\newenvironment{myenv}{%
  \refstepcounter{myenvcounter}%
  \par\medskip\noindent\textbf{Env.\ \themyenvcounter.}%
}{%
  \par\medskip\ignorespacesafterend%
}
\begin{document}
\chapter{Chapter one} \label{ch:one}
Some text...
\section{Section one} \label{sec:one}
Some text
\begin{myenv} \label{env:one} 
Bla bla bla.
\end{myenv}
As shown in Env.~\ref{env:one} \ldots\ etc.
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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