# How to sort alphabetically a custom list defined with tocloft?

I have defined a "list of definitions" using the following code:

\documentclass[a4paper, 12pt]{report}

\usepackage{subcaption}
\usepackage[subfigure]{tocloft}
\usepackage{blindtext}

\newcommand{\listdefinitionname}{List of Definitions}
\newlistof{definition}{def}{\listdefinitionname}

\newcommand{\define}[3]{
\refstepcounter{definition}
\label{definition:#2}
\vspace*{2mm}
\fbox{
\begin{minipage}{\textwidth}
\par\noindent\textbf{Definition \thedefinition.\\\linebreak \textit{#1}} #3
\end{minipage}
}
\vspace*{2mm}
{\hspace{6mm} #1}\par
}

\newcommand{\listofdefinitions}{
\phantomsection
\listofdefinition
}

\begin{document}

\listofdefinitions
\clearpage

\blindtext

\define{Silicon}{silicon}{
The chemical element of atomic number 14, a non-metal with semiconducting properties, used in making electronic
circuits. Pure silicon exists in a shiny dark grey crystalline form and as an amorphous powder.
}

\blindtext

\define{Latex}{latex}{
A milky fluid found in many plants, such as poppies and spurges, which exudes when the plant is cut and
coagulates on exposure to the air. The latex of the rubber tree is the chief source of natural rubber.
}

\end{document}


## Screenshots:

### Page 2

After a few discussions with my PhD. Supervisors, it would be more appropriate to be able to sort this list, not by appearance order, but by alphabetical order.

How can I perform such a thing ?

• tocloft is meant for creation of ToCs and changing their appearance, but it is not for sorting. You require something like a glossary or a index, which is sorted by default, however. Your macros leaves spurious spaces in the text as well.... and the \label must be placed after \refstepcounter, not before... – user31729 Mar 21 '18 at 12:54
• Hum maybe you have some ideas for me to reference something by alphabetical order ? I am 100% ok to change the functionment of my macros. Btw. the formating is not stable, it is just a snippet. In my main report, everything look fine. – Jonathan DEKHTIAR Mar 21 '18 at 12:58
• btw. thanks for your remark about the label, I have fixed it. – Jonathan DEKHTIAR Mar 21 '18 at 12:59

tocloft is meant to edit ToC features, i.e. the appearance, amongst others, but it has no sorting capabilities. Sorting is always a little bit awkward in TeX etc., therefore such external programs like makeindex exist.

I suggest to define a glossary database, say def and add the definitions of the terms there, then use \define{foo} in order to display the glossary entry 'foo'. The reason for a new glossary is to prevent mangling with the regular 'main' glossary.

The sorting is done by makeglossaries script, for example.

The contentlike style is a new glossary style which displays the glossary entries like in a ToC.

Run pdflatex foo.tex ; makeglossaries foo ; pdflatex foo, if the document is called foo.tex.

Here is the sorted output:

\documentclass[a4paper, 12pt]{report}

\usepackage{blindtext}

\usepackage[nomain,toc]{glossaries}

\newcounter{definition}

\newglossary{def}{defi}{defo}{List of Definitions}

\makeglossaries

\newglossaryentry{silicon}{
type=def,
name=Silicon,
description={%
The chemical element of atomic number 14, a non-metal with semiconducting properties, used in making electronic
circuits. Pure silicon exists in a shiny dark grey crystalline form and as an amorphous powder.
}
}

\newglossaryentry{latex}{
type=def,
name=Latex,
description={%
A milky fluid found in many plants, such as poppies and spurges, which exudes when the plant is cut and
coagulates on exposure to the air. The latex of the rubber tree is the chief source of natural rubber.
}
}

\newcommand{\define}[1]{%
\refstepcounter{definition}
\label{definition:#1}
\vspace*{2mm}
\fbox{%
\begin{minipage}{\textwidth}
\par\noindent\textbf{Definition \thedefinition.\\\linebreak \textit{\glsentrytext{#1}}}
\glsdesc{#1}%
\end{minipage}
}
\vspace*{2mm}
{\hspace{6mm}}\par
}

\newglossarystyle{contentlike}{%
\renewenvironment{theglossary}%
{\parindent0em}{}
% have nothing after \begin{theglossary}:
% have nothing between glossary groups:
\renewcommand*{\glsgroupskip}{}
\renewcommand*{\glossentry}[2]{%
\textbf{\glstarget{##1}{\glossentryname{##1}}}\dotfill{##2}

}%
}

\newcommand{\listofdefinitions}{%
\setglossarystyle{contentlike}
\printglossary[type=def,]
\clearpage
}

\begin{document}
\tableofcontents
\listofdefinitions

\blindtext

\define{silicon}

\blindtext

\define{latex}

\end{document}

• @JonathanDEKHTIAR: You're welcome. Happy TeXing! glossaries is a very powerful package (bundle) – user31729 Mar 21 '18 at 22:22