References to self-created environments in latex
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 sectio开发者_如何转开发n/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}
精彩评论