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.
July 13th, 2008 at 9:46 am
Very well put. It’s much nicer then using a modified \paragraph command to itemize questions and answers in a document.
July 21st, 2008 at 12:07 pm
Hi there,
Great tip! I have a macro so that I can write a to-do list of things as I go along, and now I’ve used your example to roll in the ability to collate these into a list too.
Here for all to share:-
\newcommand{\listtodotitle}{To-do list} \newlistof{todos}{tod}{\listtodotitle} \newcommand{\todolist}[1]{% \refstepcounter{todos} \addcontentsline{tod}{todos} {\protect\numberline{\thechapter.\thetodos} #1}} \newcommand{\todo}[1]{\todolist{#1}\textbf{\underline{TODO}: #1}}And then when you are writing and need to make a note, you just need to write \todo{Blah. Something I’ve not done yet}. Of course, you’ll need to put in
\listoftodosto print the list also.Very handy!
Thanks,
Andy
January 5th, 2009 at 12:47 pm
Thanks for the great tutorial!
I too have defined a new list, but it is a list of acronyms. Now, every time I introduce a new acronym I add it to the list, so as to act as a reference if someone doesn’t want to read my document from the beginning.
\newcommand{\listacronymname}{List of Acronyms} \newlistof{acronyms}{exp}{\listacronymname} \newcommand{\acronym}[1]{\textsuperscript{\#}% \refstepcounter{acronyms}% \addcontentsline{exp}{acronyms}{#1}}(in the document)
I hate WYSIWYG\acronym{WYSIWYG = What You See Is What You Get} editors.
(at the end of the document}
Then it prints the list of all acronyms and the page of first appearance! It’s great.
Thanks for your help,
Steve
September 3rd, 2009 at 9:21 pm
Cool site, love the info.
October 11th, 2009 at 11:40 pm
Hm.. But when I use this, it breaks fancyheaders.. Very irritating indeed. Anyone else noticed?
November 16th, 2010 at 7:05 pm
Thanks very much for this! Helps a lot. However, my style file does not seem to interact well with tocloft: when I use it, it distorts the style of headings etc. Any ideas guys? Cheers.
December 8th, 2010 at 12:41 pm
Very nice. However, I cant include the package in my thesis! Please, is there some compatibility requirement with this package?
February 11th, 2011 at 8:40 pm
This is very useful, thanks. I made it easier to use by writing a command that does all the setup for you.
So you can do:
\NewListOfType{List of Examples}{example}{ex}It’s on my blog here:
http://geekbs.blogspot.com/2011/02/latex-list-of-x.html
August 26th, 2011 at 2:08 pm
I got here tying to do a similar thing but with a float environment. Turns out it is much easier with floats.
\floatstyle{plain} \newfloat{X}{thp}{lop} \floatname{X}{NameForX} \listof{X}{List of X}see: (German:)
http://www.mrunix.de/forums/archive/index.php/t-58363.html
September 9th, 2011 at 2:52 am
[...] You can find a complete code example here. [...]
March 5th, 2012 at 5:21 am
Hello,
I am writing because I would like to use this method to make a list of “graphs”. I do not know how to take the pictures of my graphs that are in the “figure” environment and list them in the graphs. I do not understand how to go from taking the
\graph{Your Second Example}
and turning it into something that will display my graph’s caption in the list of graphs.
Someone else has done some work with the list of the floats, but the help is incomplete (i.e. without a minimum working example) and the german translation is difficult to read. At the moment, trying to work with your example:
\documentclass{report} \usepackage{tocloft} \usepackage[english]{babel} \newcommand{\listgraphname}{List of Graphs} \newlistof{graph}{gra}{\listgraphname} \newcommand{\graph}[1]{% \refstepcounter{graph} \addcontentsline{gra}{graph} {\protect\numberline{\thechapter.\thegraph}#1}\par} \begin{document} \tableofcontents \newpage \listofgraph \chapter{Two examples} \graph{first example} \begin{figure}[h] \centering \includegraphics[scale =0.5]{graph.jpg} \caption{Example Graph} \label{Fig: 1} \end{figure} \label{1st_ex} \graph{Your second example} \label{2nd_ex} \chapter{One example} \graph{Your third example. (See example \ref{1st_ex} \& \ref{2nd_ex})} \end{document}With the aim of putting the figure in the list of graphs
Any help would be greatly appreciated.
Sincerely,
N
March 5th, 2012 at 5:22 am
Does it have to do with the ‘refstepcounter’?
March 5th, 2012 at 6:12 am
Hello,
I have managed to include the figure floats in the List of Graphs. However, I have been unable to change the typeset of the entries in the List of Graphs via:
\renewcommand{\cftgratitlefont}{\Large \bfseries} \newcommand\cftgrapresnum{} \newcommand{\cftgraaftersnum}{} \renewcommand{\cftgrapresnum}{Graph } \renewcommand{\cftgraaftersnum}{:} \newcommand{\cftgranumwidth} \newlength{\mylen} \settowidth{\mylen}{\bfseries\cftgrapresnum\cftgraaftersnum} \newcommand{\addtolength}{\cftgranumwidth}{1in}as per the tocloft manual’s example. Any help on this matter would be greatly appreciated.
March 5th, 2012 at 8:05 am
Hello,
I am having a bit of an issue with the renaming of the titles of the captions for my new floats in the tocloft package. While the same:
\renewcommand{cftfigpresnum}{Figure} works,
my new list won’t allow me to change the name
new list: graphs, extension {gra}
\renewcommand{\cftgratitlefont}{\Large \bfseries} \newcommand\cftgrapresnum{} \newcommand{\cftgraaftersnum}{} \renewcommand{\cftgrapresnum}{Graph } \renewcommand{\cftgraaftersnum}{:} \newcommand{\cftgranumwidth}Will still only output the number and the caption, but not “Graph 1″
Any help would be greatly appreciated.
Sincerely,
N
March 5th, 2012 at 1:59 pm
Hey Nam,
Thanks for your comments with the examples. The problem is that the code in the post is for a simple command only. What you need is a new float environment, basically a copy of
figure. Thetrivfloatpackage provides a simple way to do this. Here is the documentation.Hope it works!
Best, Tom.
March 5th, 2012 at 4:17 pm
Thank you for you suggestion of trivfloat, however, I still cannot find the proper code to change the listing on the “List of Graphs” from just the number. Any assistance would be appreciated for changing the listing style for trivfloat.
Sincerely,
N
March 5th, 2012 at 4:35 pm
Also, the trivfloat won’t allow the placement of the \caption to govern the placement, as I wish to place it above the graph, but it will not allow me to.
Below is a working example, showing the position of the caption as well as the output of the list of graphs.
[...] Code remove by Tom [...]
March 6th, 2012 at 11:04 am
Hey Nam,
I removed your code, it was slightly more than the bare minimum, hope you don’t mind
. Here is a minimal example that should solve both problems, adding the word “Graph” to entries of your list-of-graphs and place the caption above graphs.
\documentclass{article} \usepackage{trivfloat, graphicx, caption} \DeclareCaptionType{graph} \floatstyle{plaintop} \restylefloat{graph} \trivfloat{graph} \begin{document} \renewcommand\numberline[1]{Graph #1:\hspace{1.8em}} \listofgraphs \clearpage \begin{graph} \centering \caption{My graph} \includegraphics[width=0.3\textwidth]{test} \end{graph} \end{document}Two things to note. Firstly, make sure you define
\trivfloat{graph}after setting the caption type. Second, create thelistoffiguresand then do\renewcommand{...}, otherwise the word “Graph” gets added also to lof-entries.I found help here and there.
Cheers, Tom.
May 9th, 2012 at 7:52 am
Hi, this post is getting old, but I’ll try my luck.
The code works great, but when I add the list to my toc it is not formatted like the rest of my toc (not correctly indented, not bold faced, etc.)
I add it to toc by using:
\addcontentsline{Sequence}{toc}{List of Sequences}
after
\listofSequences
Any help is appreciated!
May 9th, 2012 at 4:52 pm
Hi,
The following line should work. Add it before you create the list to get the page number right (in case the list occupies more than one page).
\addcontentsline{toc}{chapter}{List of Sequences}Best, Tom.
May 9th, 2012 at 5:39 pm
Works like a charm. Thank you very much!
Best regards, Lars
March 14th, 2013 at 7:15 pm
I have been unable to get any of this code to work in my project. I am trying to create a list of equations at the begining of a dissertation.
I have the following as part of my overall code.
\usepackage{tocloft} \newcommand{\listmyequationsname}{List of Equations} \newlistof{myequations}{equ}{\listmyequationsname} \newcommand{\myequations}[1]{% \refstepcounter{myequations}% \par\noindent\textbf{myequations \themyequations. #1} \addcontentsline{equ}{myequations} {\protect\numberline{\thechaper.\themyequations}#1}\par} \begin{document} \begin{romanpages} \tableofcontents{} \listofmyequations \listoftables \end{romanpages} \include{Chapter1/Introduction} \end{document} In chapter1/introduction, I have the following: \begin{equation}\label{eq:SOE} g(\pi_j)= \alpha_{jk} \alpha_{kg}\theta_g + \alpha_{jk}\xi_k + \beta_j \end{equation} \myequations{Second Order Equation}This will not compile with the “\myequations{Second Order Equation}” included. When I comment that out the List of Equations title is generated, but no equations are listed. What am I missing?
March 19th, 2013 at 2:45 am
Hi Melinda,
Sorry it took a while. There is just a minor typo in the last argument of
\addcontentslinewhere you typed\thechaperinstead of\thechapter. Otherwise, your code is fine.Here is your minimal working examples with the typo fixed:
\documentclass[11pt]{report} \usepackage{tocloft} \newcommand{\listmyequationsname}{List of Equations} \newlistof{myequations}{equ}{\listmyequationsname} \newcommand{\myequations}[1]{% \refstepcounter{myequations}% \par\noindent\textbf{Equation \themyequations. #1} \addcontentsline{equ}{myequations} {\protect\numberline{\thechapter.\themyequations}#1}\par} \begin{document} \tableofcontents \listofmyequations \listoftables \chapter{Introduction} \begin{equation}\label{eq:SOE} g(\pi_j)= \alpha_{jk} \alpha_{kg}\theta_g + \alpha_{jk}\xi_k + \beta_j \end{equation} \myequations{Second Order Equation} \end{document}March 20th, 2013 at 7:46 pm
I may be confused but I see a period after \thechapter. in my code and yours. With or without the (.) the file does not compile as long as I have the \myequations tag after the equation. I still do not see a way to get the equations list to populated. Do I need to redefine equation so that it will automaticaly include my \myequations tag?
March 21st, 2013 at 4:05 am
Hi,
How’s this:
\documentclass[11pt]{report} \usepackage{tocloft} \newcommand{\listmyequationsname}{List of Equations} \newlistof{myequations}{equ}{\listmyequationsname} % Redefining \theequation to make the entry in the list of equations \makeatletter \renewcommand\theequation{\ifnum \c@chapter >\z@ \thechapter .\fi \@arabic \c@equation \protect\refstepcounter{myequations}% \protect\addcontentsline{equ}{myequations}% {Eq.~\thechapter.\@arabic\c@equation}% } \makeatother \begin{document} \tableofcontents \listofmyequations \listoftables \chapter{Introduction} \begin{equation}\label{eq:SOE} g(\pi_j)= \alpha_{jk} \alpha_{kg}\theta_g + \alpha_{jk}\xi_k + \beta_j \end{equation} \newpage \begin{equation}\label{eq:SE} x= y \end{equation} \end{document}March 20th, 2013 at 8:30 pm
The error that I get is
! Undefined control sequence.
Equations \theequations
.\@currentlabelname
1.78 \end{equation}
I get the above error by adding the following code which I thought maybe I needed so that the list of equations would populate.
\let\oldequation = \equation \let\endoldequation = \endequation \renewenvironment{equation}{ \begin{oldequation} }{ \end{oldequation} \myequations{\@currentlabelname} }March 21st, 2013 at 3:43 am
Hi Melinda,
I see what you are trying to do. The problem is that you want to place
\myequationsoutside the environment. Your code encloses twoequationenvironments.Try the code below which uses the xparse package. If it doesn’t work, you might have to update to the latest version of LaTeX. It creates a new environment
myequationtaking care of the label as well as producing the caption/list entry. ‘m’ stands for mandatory argument. You can make them optional using ‘o’. See this article on xsparse for some more details.Hope it helps,
Tom
\documentclass[11pt]{report} \usepackage{tocloft, xparse} \newcommand{\listmyequationsname}{List of Equations} \newlistof{myequations}{equ}{\listmyequationsname} \newcommand{\myequations}[1]{% \refstepcounter{myequations}% \par\noindent\textbf{Equation \themyequations. #1} \addcontentsline{equ}{myequations} {\protect\numberline{\thechapter.\themyequations}#1}\par} \DeclareDocumentEnvironment{myequation}{ m m }{% \equation\label{#1}}{\endequation\myequations{#2}} \begin{document} \tableofcontents \listofmyequations \listoftables \chapter{Introduction} \begin{myequation}{eq:SOE}{Second Order Equation} g(\pi_j)= \alpha_{jk} \alpha_{kg}\theta_g + \alpha_{jk}\xi_k + \beta_j \end{myequation} \end{document}May 13th, 2013 at 3:20 pm
I get the following error, when I try to compile the following code.
Missing \begin{document}
\documentclass{report} \usepackage{tocloft} \newcommand{\listofabbreviations}{List of Abbreviations} \newlistof{abbreviations}{abb}{\listofabbreviations} \newcommand{\abbreviation} \refstepcounter{abbreviations} \par\noindent\textbf{ Abbreviation \theabbreviation. #1} \addcontentsline{abb}{abbreviation} {\protect\numberline{\thechapter.\theabbreviation}#1}\par} \tableofcontents \clearpage \listofabbreviations \begin{document} Land use and land cover are commonly referred to as LULC\abbreviation{LULC=Land Use and Land Cover}. \end{document}May 13th, 2013 at 3:22 pm
Sorry for the first example not being in the proper format.
May 13th, 2013 at 4:53 pm
No problem, I removed it. Thanks for the code. Tom
May 13th, 2013 at 5:06 pm
There were a few other minor issues in your code. I corrected it below. Consider using the nomencl package instead.
\documentclass{report} \usepackage{tocloft} \newcommand{\listabbreviationname}{List of Abbreviations} \newlistof{abbreviations}{abb}{\listabbreviationname} \newcommand{\abbreviations}[1]{% \refstepcounter{abbreviations} \par\noindent\textbf{Example \theabbreviations. #1} \addcontentsline{abb}{abbreviations} {\protect\numberline{\thechapter.\theabbreviations}#1}\par} \begin{document} \tableofcontents \clearpage \listofabbreviations \chapter{Dummy chapter} Land use and land cover are commonly referred to as LULC\abbreviations{LULC=Land Use and Land Cover}. \end{document}May 13th, 2013 at 4:53 pm
Hi,
Thanks for the code. Move the three highlighted lines into the document content and your code should work fine. You can define commands in the preamble, but producing the lists has to be done later.
HTH, Tom
May 15th, 2013 at 12:11 pm
Thank you!!
May 15th, 2013 at 1:39 pm
I moved the table of contents after
\begin{document}But I still get the same error. And the compilation stops at
\refstepcounter{abbreviations}Any idea as to why this is happening?
May 15th, 2013 at 4:09 pm
The reason being that there are a few more typos on your code. E.g. line 3,
\listofabbreviationsshould be\listabbreviationname. Start from the code I posted here, it works.Best, Tom.