# list of newcounter

I've set up a newcounter in a LaTeX document:

\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\textbf{Example~\theexample. #1} \rmfamily}{\medskip}


so that I can reference them throughout my document:

\begin{example}[title]
This is the first example. The counter will be reset at each section.
\end{example}


my question is, how can I generate a list of all my examples? In a similar way to a list of figures, I was hoping I could get a list of examples, with their titles, and the page number they appear on. Is this possible?

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Sep 1 '14 at 11:39
• Have a look into tocloft package and its \newlistof command. See also this recent question, where some example is shown tex.stackexchange.com/questions/198326/…. Perhaps, you should use a theorem-like environment from amsthm etc. for the framework of your example as well – user31729 Sep 1 '14 at 11:40

A 'quick and dirty' solution, with \newlistof command from tocloft package.

\documentclass{article}%

\usepackage{blindtext}
\usepackage[titles]{tocloft}

\usepackage{etoolbox}

\newcounter{example}[section]
\renewcommand{\theexample}{\arabic{section}.\arabic{example}}%
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\ifblank{#1}{%
}{%
}%
\par\medskip%
\noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}%

\newcommand{\listexampletitle}{List of Examples}

\newlistof[section]{examples}{exp}{\listexampletitle}%

\cftsetindents{examples}{1.5em}{3.0em}%
%\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}

\begin{document}

\section{Introduction}%

\listofexamples

\begin{example}[title]

This is the first example. The counter will be reset at each section.
\end{example}

\begin{example}
This is the 2nd  example, but has no entry to the toc!
\end{example}

\section{A section}
\blindtext[1]
\begin{example}[Another title]
\blindtext[2]

Another example.
\end{example}

\begin{example}[Yet Another title]
\blindtext[3]%

Yet Another example.
\end{example}

\end{document}


## Another version without section numbers for examples

Disclaimer I do not recommend dropping the section number for examples, as it will lead to more difficult identification which example is meant, especially if examples have the same title (due to some reason)

\documentclass{article}%

\usepackage{blindtext}
\usepackage[titles]{tocloft}

\usepackage{etoolbox}

\newcounter{example}
\renewcommand{\theexample}{\arabic{example}}% To be on the safe side!
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\ifblank{#1}{%
}{%
}%
\par\medskip%
\noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}%

\newcommand{\listexampletitle}{List of Examples}

\newlistof[section]{examples}{exp}{\listexampletitle}%

\cftsetindents{examples}{1.5em}{3em}% Some indentation -- change to 0pt if needed
%\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}%

\begin{document}

\section{Introduction}%

\listofexamples

\begin{example}[title]

This is the first example. The counter will be reset at each section.
\end{example}

\begin{example}
This is the 2nd  example, but has no entry to the toc!
\end{example}

\section{A section}
\blindtext[1]
\begin{example}[Another title]
\blindtext[2]

Another example.
\end{example}

\begin{example}[Yet Another title]
\blindtext[3]%

Yet Another example.
\end{example}

\end{document}


• Great! Seems to work pretty well to me! Two questions: 1) Why do you consider it dirty, is it not the "ideal" solution? 2) Could we change it so that the identifier excludes the section it's in? And it just incremented by one regardless of which section it's in? – Mr Goobri Sep 3 '14 at 10:57
• @MrGoobri: It's not really dirty, but fast, there might be better approaches. If you mean exclude, you say, e.g. Another title instead of 2.1 Another title and you want to have continous counting, without any resetting? – user31729 Sep 3 '14 at 11:01
• @MrGoobri: See my updated/alternative answer (I hope, I understood your requests correctly) – user31729 Sep 3 '14 at 11:12
• That's great thanks @christian-hupfer but I would still like to see the example number in list if possible. So from your example above, I would like to see 1 title....1; 2 Another title.....1; 3 Yet another title.....2. – Mr Goobri Sep 3 '14 at 11:21
• @MrGoobri: You want to see in the list, but not in the example in place, where it appears? – user31729 Sep 3 '14 at 11:24

It's very easy to do with the ntheorem package: I define example as a new theorem, whose counter is reset at each section, and the use the \listtheorems{example} command:

\documentclass[12pt,a4paper, x11names]{article}%]

\usepackage[utf8]{inputenc}%
\usepackage[T1]{fontenc}

\usepackage{amsmath}
\usepackage[thmmarks, amsmath, framed]{ntheorem}%

\theoremstyle{plain}
\theorembodyfont{\mdseries}
\theoremseparator{.}

\newtheorem{example}{Example}[section]

\newcommand\listofexamples{\section*{List of Examples}\listtheorems{example}}

\begin{document}

\section{A First Section}

\begin{example}[Title]
This is the first example. The counter will be reset at each section.
\end{example}

\begin{example}[Title the Second]
This is the second example of the first section.
\end{example}

\section{Another Section}

\begin{example}[Title title]
This is the first example of the second section. The counter was reset.
\end{example}

\listofexamples

\end{document}


Another way would be to use package tocbasic coming with KOMA-script. You can easily set up a floating environment (or a nonfloating if you want to) using simple key-value pairs. All the listof-macros you know are there. The list will look just like your list of figures and tables (if you modified them a safe way).

You can style the environment using, for example, the caption package. If you are using package hyperref and its \autoref functionality, surprise ... works as well :-)

If you are resetting the counter each section, \thecounter will be longer making the output look ugly. Package tocstyle and it tocautoindent feature come to help. You need a few LaTeX runs till the needed space is calculated correctly.

\documentclass{article}%
\usepackage[tocindentauto]{tocstyle}
\usepackage{blindtext}
\usepackage{tocbasic}
\DeclareNewTOC[%
type=example,
counterwithin=section,
float,
%nonfloat, %If you don't want them to float
name=Example,
listname={List of Examples},
]{xpl}

\usepackage{hyperref}
\begin{document}

\listoffigures
\listofexamples\clearpage

\section{Introduction}%
\begin{example}
\caption{title}
This is the first example. The counter will be reset at each section.
\end{example}

\begin{example}
\caption{example with ducks}
Duck and cover!
\end{example}

\section{Quack}
Ducks are quite nice creatures, find a picture of a duck in
\autoref{xpl:duck}.
\blindtext[1]
\begin{example}
\caption{More Ducks}
\label{xpl:duck}
\blindtext[2]
\rule{5cm}{2cm}
Another example.
\end{example}

\begin{example}
\caption{funny penguins}
\blindtext[3]%

Yet Another example.
\end{example}
\begin{figure}
\caption{figure}
\caption{figure}
\caption{figure}
\end{figure}
\end{document}


Using standard hyperref to show the link

• Works well for floating, but note that nonfloat didn't work for me. – jkmey Apr 9 '20 at 21:18
• @jk What exactly doesn't work? If you use nonfloat, you have to omit float. Maybe asking a new question (including a link to this answer) might be a good idea. – Johannes_B Apr 10 '20 at 3:34
• It should be noted, that package tocstyle is obsolete now. – Johannes_B Apr 10 '20 at 3:34