It seems that there's an inherent difficulty in manipulating counters for new environments, at least with respect to referencing and especially in the book class. By performing a related search i didn't find any solution to my problem which is:
i want to define an examples environment within a book class. I want the environment to be numbered with two arguments, that is the chapter and the ascending example per chapter, e.g. Example 2.1 (first example of the second chapter). In the code i include below i have managed to correctly perform the numbering, yet the \label and / or \ref commands don't work. After spending some hours trying to fix this, i finally ask for your kind suggestions.
Best regards.
PS1: i don't want to use \newtheorem like solutions.
PS2: having managed to solve this, how difficult it is to create a List of Examples after TOC?
Code: Select all
\documentclass[11pt,a4paper,notitlepage,fleqn]{book}
% define example environment
%---------------------------
\usepackage{relsize} % for setting smaller fonts in the example environment
\newcounter{exampleCounter}[chapter] % define enviroment counter
\renewcommand\theexampleCounter{\arabic{chapter}.\arabic{exampleCounter}}
\newenvironment{example}[1]
{\vspace\baselineskip\par\refstepcounter{exampleCounter}\begin{quote}\noindent\smaller%
\textbf{Example~\theexampleCounter~~#1}\end{quote}%
\par\begin{quotation}\noindent\smaller%
}
{\end{quotation}\noindent}
\usepackage[hypertex,colorlinks]{hyperref}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{I am the first chapter}\label{Ch:01}
\section{Intro of the first Chapter}\label{Sec:1.1}
Here's some text before the Example~\label{Ex:1.1}
\begin{example}{Ex1}
i am the first example
\label{Ex:1.1}
\end{example}
and some text after the Example~\ref{Ex:1.1}, or before the example~\ref{Ex:2.1}
\chapter{I am the second chapter}\label{Ch:02}
\section{Intro of the second Chapter}\label{Sec:2.1}
In the following example
\begin{example}{Ex2.1}
i am the second example
\label{Ex:2.1}
\end{example}
the results of Example~\ref{Ex:1.1} are confirmed.
\end{document}