How is possible to define labels and corresponding references to a self-defined environment within latex?
Example:
\newcounter{fpcounter}
\newenvironment{fp}[2]
{
\stepcounter{fpcounter}
\label{#1}
\textbf{Problem~\arabic{fpcounter}}
}
{}
Any references to the included label though get redirected to the surrounding section/subsection/paragraph though.
Any hints? Thanks a lot.
Use \refstepcounter
instead of \stepcounter
. This sets what the \label
command will use, and redefine thefpcounter
with \renewcommand{\thefpcounter}{\arabic{fpcounter}}
. This yields
Also, have provided some other options depending on how you want to label the custom environment.
\documentclass{book}
\newcounter{fpcounter}
%\renewcommand{\thefpcounter}{\thechapter.\arabic{fpcounter}}
%\renewcommand{\thefpcounter}{\thesection.\arabic{fpcounter}}
\renewcommand{\thefpcounter}{\arabic{fpcounter}}
\newenvironment{fp}[2]{%
\refstepcounter{fpcounter}%
\label{#1}%
\noindent\textbf{Problem~\thefpcounter}%
}%
{}%
\begin{document}
\chapter{Lorem}
\section{Ipsum}
\begin{fp}{fp:A}{}
content of environment 1
\end{fp}
\begin{fp}{fp:B}{}
content of environment 2
\end{fp}
\medskip\noindent
As shown in Problem~\ref{fp:A}, and Problem~\ref{fp:B}...
\end{document}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With