Text FormattingProblem with referencing items in a list environment

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
profmortimer
Posts: 2
Joined: Mon Nov 09, 2009 5:19 pm

Problem with referencing items in a list environment

Post by profmortimer »

I'm new to this forum, so please excuse me if I'm asking a question which has already been answered.

I wrote a small environment to show examples in a list fashion. The code is:

Code: Select all

\newcounter{Example}[chapter]%

\newenvironment{example}%
	{%	
		\def\example{\item}%
		\setcounter{Example}{1}%
		\renewcommand{\theExample}{\arabic{chapter}.\arabic{Example}}%
   	\begin{list}{%
			\refstepcounter{Example}%
		}% 
		{%
			\renewcommand{\makelabel}{\textbf{Example~\theExample:}}%
			\newcommand{\labelExample{\theExample}%
			\setlength{\labelsep}{10pt}\setlength{\leftmargin}{\parindent}%
			\setlength{\labelwidth}{0pt}%
			\setlength{\listparindent}{0.5\parindent}%
		}%
	}%
	{\end{list}}
It works fine, except that when I try to cross-reference somewhere an item in the example list (with \label-\ref) I get the section number instead of the correct reference to the item (that is, instead of \theExample). What am I doing wrong? Thanks for the assistance.

Recommended reading 2024:

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

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

profmortimer
Posts: 2
Joined: Mon Nov 09, 2009 5:19 pm

Problem with referencing items in a list environment

Post by profmortimer »

No one answered the question, but I found the answer by looking to the definition of enumerate in the latex definition file. In fact, the correct form of the code should be

Code: Select all

\newenvironment{exercise}%
	{%
		\section*{Problems}\markright{Problems}
		\def\problem{\item}%
		\setcounter{exnum}{1}%
		\renewcommand{\theexnum}{\arabic{chapter}.\arabic{exnum}}%
   	\begin{list}{%
		}% 
		{%
			\usecounter{exnum}
			\renewcommand{\makelabel}{\textbf{Exercise~\theexnum:}}%
			\newcommand{\labelexnum}{\theexnum}%
			\setlength{\labelsep}{8pt}\setlength{\leftmargin}{\parindent}%
			\setlength{\labelwidth}{0pt}%
			\setlength{\listparindent}{0.5\parindent}
		}%
	}%
	{\end{list}}
The whole trick is to use the \usecounter command in the second argument of list (and take out \refstepcounter in the first argument).
Post Reply