GeneralReferencing problem with \newenvironment in book class

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bullit
Posts: 4
Joined: Sun Mar 06, 2011 7:42 am

Referencing problem with \newenvironment in book class

Post by bullit »

Greetings to everybody.

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}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Referencing problem with \newenvironment in book class

Post by gmedina »

Hi,

I couldn't notice any problems with the references: there was an option "hypertex" to the hyperref package that made your document non compilable with pdflatex; I removed that option and also corrected a misplaced \label. My example code also includes the necessary adjustments to produce a List of Examples:

Code: Select all

\documentclass[11pt,a4paper,notitlepage,fleqn]{book}
\usepackage[colorlinks]{hyperref}

% 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]
{\addcontentsline{xmp}{example}{#1}% add the entry to the new list
  \refstepcounter{exampleCounter}%
  \vspace{\baselineskip}\par%
  \begin{quote}%
    \noindent\smaller%
    \textbf{Example~\theexampleCounter~~#1}
  \end{quote}%
  \par\begin{quotation}\noindent\smaller%
    }
{\end{quotation}\noindent}

% formatting of title and entries of the new List of Examples
\newcommand\listexamplename{List of Examples}
\makeatletter
\newcommand\listofexamples{%
  \chapter*{\listexamplename}\@starttoc{xmp}}
\newcommand\l@example{\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother

\begin{document}

\frontmatter
\tableofcontents
\listofexamples

\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~\ref{Ex:1.1}
\begin{example}{Some example}
  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}{Another example}
  i am the second example
  \label{Ex:2.1}
\end{example}
the results of Example~\ref{Ex:1.1} are confirmed.

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
bullit
Posts: 4
Joined: Sun Mar 06, 2011 7:42 am

Re: Referencing problem with \newenvironment in book class

Post by bullit »

gmedina thanks a lot. I have to say that i don't use pdflatex because it encounters some problems with eps figures. So, currently i use either hypertex to view the results in yap, or dvipdfm for viewing the pdf file. In both cases my problem still exists. I will examine further your code and maybe i will also try floats...

Thanks again.
Post Reply