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.
Winkelwagen
Very well put. It’s much nicer then using a modified \paragraph command to itemize questions and answers in a document.
Andy
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:-
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
\listoftodos
to print the list also.Very handy!
Thanks,
Andy
YL
Andy, thanks so much for this reply! I just cut and paste your macro in, and it works like a charm. I found this page on the search for how to make a to do list, and ta da! your reply supplied the answer I needed.
Tim Treis
Love this solution! I extended it with another piece of code I found, so it now has checkboxes in the “list of..”.
Todos: \todo{Task}{“\done” or “\open”}{“v” or “h”}
– “\done” leaves a checked square in the list, “open” an unchecked.
– “v” makes todo appear in the text, “h” hides it.
Sections must have the label: \label{exp\thesection}
– You can change exp, but must update it also in the definition of “\todolist”.
Output of list is like:
– :…
tom
Thanks much for posting your code here. I appreciate it. Best, Tom
Steve
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.
(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
Bill Bartmann
Cool site, love the info.
Aksel Bergfeldt
Hm.. But when I use this, it breaks fancyheaders.. Very irritating indeed. Anyone else noticed?
Moreno Mitrovic
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.
Leoncio Barros
Very nice. However, I cant include the package in my thesis! Please, is there some compatibility requirement with this package?
Ben Smith
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:
It’s on my blog here:
http://geekbs.blogspot.com/2011/02/latex-list-of-x.html
reapws2011
I got here tying to do a similar thing but with a float environment. Turns out it is much easier with floats.
see: (German:)
http://www.mrunix.de/forums/archive/index.php/t-58363.html
10 ways to customize toc/lof/lot « LaTeX Matters
[…] You can find a complete code example here. […]
Nam Nguyen
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:
With the aim of putting the figure in the list of graphs
Any help would be greatly appreciated.
Sincerely,
N
Nam Nguyen
Does it have to do with the ‘refstepcounter’?
Nam Nguyen
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:
as per the tocloft manual’s example. Any help on this matter would be greatly appreciated.
Nam Nguyen
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}
Will still only output the number and the caption, but not “Graph 1”
Any help would be greatly appreciated.
Sincerely,
N
tom
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
. Thetrivfloat
package provides a simple way to do this. Here is the documentation.Hope it works!
Best, Tom.
Nam
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
Nam
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 […]
tom
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.
Two things to note. Firstly, make sure you define
\trivfloat{graph}
after setting the caption type. Second, create thelistoffigures
and then do\renewcommand{...}
, otherwise the word “Graph” gets added also to lof-entries.I found help here and there.
Cheers, Tom.
larschrjensen
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!
tom
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).
Best, Tom.
larschrjensen
Works like a charm. Thank you very much!
Best regards, Lars
Melinda
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.
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?
tom
Hi Melinda,
Sorry it took a while. There is just a minor typo in the last argument of
\addcontentsline
where you typed\thechaper
instead of\thechapter
. Otherwise, your code is fine.Here is your minimal working examples with the typo fixed:
Melinda
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?
Tristan Hubsch
Hi,
How’s this:
Melinda
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.
tom
Hi Melinda,
I see what you are trying to do. The problem is that you want to place
\myequations
outside the environment. Your code encloses twoequation
environments.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
myequation
taking 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
melinda
I was able to get the the list of equations to compile but I now need to change the font of the title List of Equations. I have used :
\renewcommand{cft X titlefont}{\Large\bfseries} for other titles but I don’t know what to put in the X for myequations. I have tried LoE, and LME. Any idea?
tom
Hi Melinda,
Thanks for your question. I took this from the tocloft documentation on page 14. Using the example from my post above, you could change the font as follows (X: example, Z: exp):
Make sure you don’t forget the backslash when you redefine the command.
Cheers, Tom
Melinda
Thanks for your help with the font on myequations. I think I was trying to change the font too early in the code – before I defined myequations. Once I moved that line, and changed it as you suggested, it worked. Here is the code that worked.
tom
Great, thanks for posting your solution!
Raghu Ram
I get the following error, when I try to compile the following code.
Missing \begin{document}
Raghu Ram
Sorry for the first example not being in the proper format.
tom
No problem, I removed it. Thanks for the code. Tom
tom
There were a few other minor issues in your code. I corrected it below. Consider using the nomencl package instead.
tom
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
Raghu Ram
Thank you!!
Raghu Ram
I moved the table of contents after
But I still get the same error. And the compilation stops at
Any idea as to why this is happening?
tom
The reason being that there are a few more typos on your code. E.g. line 3,
\listofabbreviations
should be\listabbreviationname
. Start from the code I posted here, it works.Best, Tom.
fredFarzad
Hi,
I have a question about creating a customized toc. My goal is to define a new item called, \newitem, which has the following 4 entries,
\newitem{category}{style}{ref}{title}
The category and style is from the limited list. For example, in my case, category can be: spin, CQED, general and style is: Paper, thesis, lecture, website.
Now my goal is to list the given item according to category and style, as following,
Spin
Thesis
1
2
….
Lecture
4
5
….
CQED
Thesis
101
102
….
Papers
304
….
And finally make a bibtex from the “ref” input (this is not very essential).
Thanks,
Farzad.
tom
Hey!
Thanks for your question. A custom list as defined by
tocloft
takes one parameter only, the title. So you’d probably get 4 different lists, one for each category. If you need to implement it exactly as described, you might have to consider another approach. An alternative approach would be to stick to a single list and mark the titles, e.g. [general, paper] Title…. This can be done in an automated way using the approach discussed in the post.Best, Tom
fred
Thanks for your response, although I am not sure yet how to do it. I will play a little bit and ask you again, if I could not succeed. By the way, I was wondering where can I find the list of syntax for preamble and their functionality? I use Latex for a long time, but I am pretty new to writing something in the Preamble myself. When I look at the template cls file, they are full of signs, and abbreviation which I have no clue about. How can I go ahead and learn about writing my own template for latex.
It has been a long time that i am searching for a digital notebook which is LaTex friendly. However, I have not been successful yet. I desire to write a template similar to the Microsoft’s onenote.
Thanks again, Farzad.
tom
Hi fred,
Sure, let me know if you need additional help.
Concerning LaTeX resources, the “LaTeX companion” is the reference book and you might consider buying a copy. Otherwise, I can suggest searching online resources such as tex.SX.
There is also a package/class writing guide which you might find useful.
HTH, Tom.
tom
I should probably add that what you describe as “signs and abbreviations” is most likely TeX rather than LaTeX. Look for the TeXbook if you’d like to know more. Tom.
fred
Hi Tom,
Sorry to bother you. I was wondering how can I add “\the” to some input variable. For example, I like to use
where I previously defined some counters. I searched for a while but I could not find any! Cheers, Farzad.
tom
I’m not quite sure what you are trying to do. However, you don’t have to set the counter manually. It’s automatically incremented by 1 when you do
\chapter
. If you’d like to refer to a specific chapter, use\label
and\ref
.Hope this helps, Tom.
Cece
Hi there,
Thank you to everyone for posting their code and explanations. I am new to LaTex and am having difficulty formatting my MA thesis, a case study in the social sciences.
I am trying to make a “List of Songs” to follow my List of Tables and List of Figures. Ideally, the List of Songs would pull from songs within different sections of the article.
I’ve tried to adapt what’s listed above, but am running into trouble executing (and perhaps defining) \song. Also, is it a problematic that 1) I am in an article (not report) documentclass and 2) the .tex file will not recognize \refstepcounter, but will recognize \ref ?
Here’s a copy of my code thus far…
The output…
– Creates a List of Songs with “.0 Calm or Alarm” listed properly across from the page number. But, how can I number it properly with a sequential number preceding the period?
– Lists “??[1] yippee” under the section … I’m not sure how to define and execute \song
tom
Hi Cece,
Sorry for the delay. I hope this is still relevant. You almost got it right, I updated your code below. To print a counter value, use
\thecounter
. So,\thesection.\thesongs
would be what you want here. On my system\refstepcounter
works. The code also prints a basic title along with the text now. Let me know if you need any more help.Best, Tom
Vladimir
Hi,
thanks for your tutorial.
I made my list of revisions, so at the end of the document is list:
Revision history
1 Some material change, app. by Mr. X, Company X – Town X, 2019-07-11………………….2
2 Some dimension change, app. by Mr. Y, Company Y – Town Y, 2018-05-03……………….3
3 Some temperature change, app. by Mr. Z, Company Z – Town Z, 2015-03-12……………6
…etc…
Here is code:
Material: Steel
\revision{Some material change}{app. by Mr. X}{Company X - Town X}{2019-07-11}
Please, is there way to sort/order this list by date?
Thanks
tom
Hi Vladimir,
Thanks for this interesting question. There are ways to sort the contents lists. My suggestion would be to consider a glossary-type of list, which allows sorting by keyword.
Let me know if you get stuck with that approach.
Cheers, Tom
Raquel
It’s been 14 years and this is still very useful, thanks!
Salvador
Amazing! Thanks a lot