Generalhow to make a reference to a custom counter as 1.1.1 etc...

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

how to make a reference to a custom counter as 1.1.1 etc...

Post by bjorn victor »

Hey all,

I want to make a custom counter (example) and use that to count examples (duh...).
The problem starts when I want to add \label and \ref commands.

I set up the counters so the counter is displayed as the section number + counter number. I use refstepcounter to enable the \label \ref functionality, but all \ref commands just give me the counter number. Not the section+ counter number.

EG: I get 1, but I want 1.1.1

I attached a sample of the code for you. Any help is much appreciated.

Bjorn

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage[pdftex,bookmarks,colorlinks=true,linkcolor=black]{hyperref}

\newcounter{example}
\newcommand{\example}{\refstepcounter{example} EXAMPLE \thesection.\theexample}
\setlength{\parindent}{0cm} 
\begin{document}

\chapter{One}

\section{Introduction}\setcounter{example}{0}
\example \label{ex1}\\ 
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text \\

\example \\
this is a reference to example \ref{ex1}.\\

Here I would like to see "this is a reference to example 1.1.1".


\section{second section}\setcounter{example}{0}
\example \\
this is a reference to example \ref{ex1}.\\

Here I would like to see "this is a reference to example 1.1.1".



\chapter{Two}
\section{Introduction}\setcounter{example}{0}
\example \label{ex2}\\

\chapter{Three}
\section{Introduction}\setcounter{example}{0}
\example \label{ex3}\\ 
This is a reference to example \ref{ex2}.\\

Here I would like to see "this is a reference to example 2.1.1".

\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.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

how to make a reference to a custom counter as 1.1.1 etc...

Post by Stefan Kottwitz »

Hi Bjorn,

very good sample code, compilable for testing. Redefining \theexample will work:

Code: Select all

\renewcommand*\theexample{\thesection.\arabic{example}}
Stefan
LaTeX.org admin
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

how to make a reference to a custom counter as 1.1.1 etc...

Post by meho_r »

If I may add, when you do as Stefan_K suggested, don't forget to omit \thesection. when defining \example command too. If you do not do this, you'll get, e.g., EXAMPLE 3.1.3.1.1 instead of EXAMPLE 3.1.1. So, the code should look something like this:

Code: Select all

\newcommand{\example}{\refstepcounter{example} EXAMPLE \theexample}
\renewcommand*\theexample{\thesection.\arabic{example}}
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

Re: how to make a reference to a custom counter as 1.1.1 etc...

Post by bjorn victor »

Brilliant!!

Thank you gentlemen!!

But,...

When using the hyperlinks, when I click on the link to example 2.1.1, I am directed to example 1.1.1??

Is this because TeXshop is acting weird (under snow leopard) or is it something wrong in my code? My guess is that resetting the counter is acting weird as TeX sees a lot example 1's and doesn't know I mean 2.1.1 or 1.1.1. TeX only sees 1. Makes sense right? but how to solve the problem??

Again, all help is appreciated!

Thanks.

Bjorn
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

how to make a reference to a custom counter as 1.1.1 etc...

Post by meho_r »

Don't use \setcounter{example}{0} at the beginning of every section. This process should be automated. Simply use this code:

Code: Select all

\newcounter{example}[section]
It means that the counter example will be reset every time counter section increases. Apart from saving you lot of typing it solves the referencing problem you mentioned.
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

Re: how to make a reference to a custom counter as 1.1.1 etc...

Post by bjorn victor »

Thank you!
I'm not at home right now, but I'll try this out as soon as I get back.
Bjorn
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

Re: how to make a reference to a custom counter as 1.1.1 etc...

Post by bjorn victor »

I just tried this at home and it works!!

One more question if I may:

Is there a way to make a list of examples (like you would make a list of tables or figures)?

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

how to make a reference to a custom counter as 1.1.1 etc...

Post by gmedina »

Hi Bjorn,

the idea is to create a new contents file (in my example it will be a new auxiliary file with extension .exa) and to define a command (\listofexamples in my example) to read the info written to the contents file .exa and then typeset it in the document, imitating the mechanism used for the standard ToC, LoF and LoT. The command \l@example will control the actual typesetting of the entries in the .exa file.

Please take a look at the following code (a variation on your code that incorporates the new list of examples as well as the suggestions made by Stefan_K and meho_r):

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage[pdftex,bookmarks,colorlinks=true,linkcolor=black]{hyperref}

\newcounter{example}[section]
\renewcommand*\theexample{\thesection.\arabic{example}}
\newcommand{\example}{%
  \refstepcounter{example} EXAMPLE \theexample%
  \addcontentsline{exa}{example}{Example \theexample}}

\makeatletter
\newcommand\listofexamples{%
  \chapter*{List of Examples}\@starttoc{exa}\clearpage}
\newcommand\l@example[2]{%
  \par\noindent#1\dotfill#2\par}
\makeatother

\setlength{\parindent}{0cm}

\begin{document}
\listofexamples

\chapter{One}

\section{Introduction}
\example \label{ex1}\\
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text \\

\example \\
this is a reference to example \ref{ex1}.\\

Here I would like to see "this is a reference to example 1.1.1".


\section{second section}
\example \\
this is a reference to example \ref{ex1}.\\

Here I would like to see "this is a reference to example 1.1.1".



\chapter{Two}
\section{Introduction}
\example \label{ex2}\\

\chapter{Three}
\section{Introduction}
\example \label{ex3}\\
This is a reference to example \ref{ex2}.\\

Here I would like to see "this is a reference to example 2.1.1".

\end{document}
Of course, the definition of the \listofexamples command can be improved.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

how to make a reference to a custom counter as 1.1.1 etc...

Post by bjorn victor »

That's it!!

I thought I had to use the tocloft package, so I was playing around with this (from the tocloft pdf):

Code: Select all

\newlistof[⟨within ⟩]{⟨entry ⟩}{⟨ext ⟩}{⟨listofname ⟩}
The first required argument, ⟨entry⟩ is used to define a new counter called entry. The optional ⟨within⟩ argument can be used so that entry gets reset to one every time the counter called within is changed. That is, the first two arguments are equivalent to calling \newcounter{⟨entry⟩}[⟨within⟩].
The next argument, ⟨ext⟩, is the file extension for the new List of. The last argument, ⟨listofname⟩, is the text for the heading of the new List of. As an example:

Code: Select all

\newcommand{\listanswername}{List of Answers} \newlistof[chapter]{answer}{ans}{\listanswername}
I was trying something along these lines:

Code: Select all

\newcommand{\listexamplename}{List of Examples}
\newlistof[section]{example}{exp}{\listexamplename}
\newcommand{\example}[1]{%
\refstepcounter{example}
\par\noindent\textbf{Example \theexample. #1}
\addcontentsline{exp}{example}
{\protect\numberline{\thechapter.\theexample}#1}\par}
and some more modifications (most of which just gave me errors...).

Anyway, your code works!

I owe all you who helped a beer!

bjorn
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

how to make a reference to a custom counter as 1.1.1 etc...

Post by meho_r »

bjorn victor wrote:
I owe all you who helped a beer!

bjorn
Hehe, but only if it's alcohol-free ;-)
Post Reply