texblog

Define your own “list of …”

I got this very interesting question lately, how to define a custom list of examples, similar to the list of figures. But as this is not only applicable to examples, but to all sorts of things (e.g. questions, theorems, proofs, lemmas, answers, etc.) and hence might be useful for other people, I decided publish a new post on that issue.

As usual, you will need to import a package.

\usepackage{tocloft}

The package lets you customise your table of contents, list of figures or list of tables (which is basically what the name stands for).

In addition, the package also lets you define your custom lists easily and hence is exactly what we were looking for.

In order to keep this as general as possible, I will use “X” instead of “example”, “proof”, etc. At the end, I will post a complete example, showing how to create a “list of examples”, which you can simply copy-paste and adapt for your needs.

After having added the package, we want to define the name that appears as a title of our list:

\newcommand{\listXname}{List of Xs}

Next we use the command provided by tocloft to define a new list of whatever:

\newlistof{X}{ex}{\listXname}

Now we define the actual X-command (similar to figure or table), which has a counter that is increased by one every time an X is used:

\newcommand{\X}[1]{%
\refstepcounter{X}
\par\noindent\textbf{X \theexample. #1}
\addcontentsline{exp}{example}
{\protect\numberline{\thechapter.\theexample}#1}\par}

By using the X-command (\X{Your text}), Latex will print the following:

X 1 Your text

The last line will add all your typed Xs to the list of Xs.

That’s it for the definition. The command “\listofX” will create a list of all your defined Xs similar to the ToC, LoF and LoT.

\begin{document}
\tableofcontents
\newpage
\listofX
\newpage
...
\end{document}

You can also add references to the different Xs, by adding a label just after the X-command (see example below):

Example: List of Examples

\documentclass{report}
\usepackage{tocloft}
\usepackage[english]{babel}
\newcommand{\listexamplename}{List of Examples}
\newlistof{example}{exp}{\listexamplename}
\newcommand{\example}[1]{%
\refstepcounter{example}
\par\noindent\textbf{Example \theexample. #1}
\addcontentsline{exp}{example}
{\protect\numberline{\thechapter.\theexample}#1}\par}
\begin{document}
\tableofcontents
\newpage
\listofexample
\chapter{Two examples}
\example{Your first example}
\label{1st_ex}
\example{Your second example}
\label{2nd_ex}
\chapter{One example}
\example{Your third example. (See example \ref{1st_ex} and \ref{2nd_ex})}
\end{document}

If you want to reset your example counter for new chapters, you would need to add the following lines before the beginning of your document:

\makeatletter
\@addtoreset{example}{chapter}
\makeatother

Click here for the complete tocloft documentation as well as the package.

Exit mobile version