Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 section/subsection/paragraph though.

Any hints? Thanks a lot.

like image 882
Thomas Avatar asked Feb 26 '11 08:02

Thomas


1 Answers

Use \refstepcounter instead of \stepcounter. This sets what the \label command will use, and redefine thefpcounter with \renewcommand{\thefpcounter}{\arabic{fpcounter}}. This yields

enter image description here

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}
like image 160
Peter Grill Avatar answered Sep 21 '22 22:09

Peter Grill