About these ads

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.

About these ads

35 Responses to “Define your own “list of …””

  • 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:-

    \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 \listoftodos to print the list also.

    Very handy!

    Thanks,

    Andy

  • 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.

    \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}

    \newpage
    \listofacronyms

    Then it prints the list of all acronyms and the page of first appearance! It’s great.

    Thanks for your help,

    Steve

  • 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:

    \NewListOfType{List of Examples}{example}{ex}

    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.

    \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

  • 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:

    \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

    • 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:

        \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.

  • 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}

    \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

    • 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. The trivfloat 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.

        \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 the listoffigures 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!

  • 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.

    \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?

    • 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:

      \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}
  • 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:

      \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}
  • 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.

    \let\oldequation = \equation
    \let\endoldequation = \endequation
    \renewenvironment{equation}{
        \begin{oldequation}
    }{
        \end{oldequation}
        \myequations{\@currentlabelname}
    }
    • 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 two equation 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

      \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}
  • Raghu Ram

    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}
    
    • 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.

        \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}
    • 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 713 other followers

%d bloggers like this: