7

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?

Mr Goobri
  • 195
  • 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}. –  Sep 01 '14 at 11:39
  • 1
    Have a look into tocloft package and its \newlistof command. See also this recent question, where some example is shown http://tex.stackexchange.com/questions/198326/tocloft-cross-references-a-section-as-a-subsection. Perhaps, you should use a theorem-like environment from amsthm etc. for the framework of your example as well –  Sep 01 '14 at 11:40

3 Answers3

7

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}{%
    }{%
      \addcontentsline{exp}{examples}{\protect\numberline{\theexample}#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}

enter image description here

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}{%
    }{%
      \addcontentsline{exp}{examples}{\protect\numberline{\theexample}#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}

enter image description here

  • 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 03 '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? –  Sep 03 '14 at 11:01
  • @MrGoobri: See my updated/alternative answer (I hope, I understood your requests correctly) –  Sep 03 '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 03 '14 at 11:21
  • @MrGoobri: You want to see in the list, but not in the example in place, where it appears? –  Sep 03 '14 at 11:24
  • Sorry, perhaps I'm not clear. What I'm looking for in the main body, for instance, is: Example 1: title1 more text Example 2: title2 more text Example 3: title3. Then, in the list, I'm looking for: 1 title1.......1, 2 title2.........5, 3 title3......7 - Where the number after all the dots is the page number, and the number before the title is the example number. Does that make more sense? Thanks for all your help. – Mr Goobri Sep 03 '14 at 11:37
  • @MrGoobri: Sorry, next confusion: number before all the dots is the page number --- Before or after -- if after, my last example is perhaps what you want –  Sep 03 '14 at 11:40
  • Quite correct. I made a mistake, sorry. The last one is perfect. Thank you for all your help. I've learnt a lot! :) – Mr Goobri Sep 03 '14 at 11:42
  • @MrGoobri: Ok -- that's what TeX.SX is designed for. Happy TeXing in future! You're welcome! –  Sep 03 '14 at 11:44
  • @ChristianHupfer can you explain plz how I can add space between examples of different chapters – Ans Piter Dec 16 '16 at 22:37
3

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 then 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} \theoremheaderfont{\bfseries} \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}

enter image description here

Bernard
  • 271,350
2

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}
%\hypersetup{hidelinks}
\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}

mrgooniList

Using standard hyperref to show the link

mrgoobiAutoref

Johannes_B
  • 24,235
  • 10
  • 93
  • 248