23

I am wondering whether it is possible to display the items alphabetically regardless of the order we type on the backend? For example I am using itemize as follows:

\begin{itemize}
    \item ISDYNSTP:  Is dynamic time step used?
    \item ISCDCA:
    \item MVAR
    \item IS2TL_
\end{itemize}

But I want to display the items in the alphabetical order. I don't know whether it is possible or not. I have like hundreds of itemize items. Actually I am trying to create a glossary for the different parameters.

29

Taking some code from How to sort an alphanumeric list, a mild change to your interface works for sorting via the datatool package:

enter image description here

\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[1]{%
  \DTLnewrow{list}% Create a new entry
  \DTLnewdbentry{list}{description}{#1}% Add entry as description
}
\newenvironment{sortedlist}{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
  \DTLsort{description}{list}% Sort list
  \begin{itemize}%
    \DTLforeach*{list}{\theDesc=description}{%
      \item \theDesc}% Print each item
  \end{itemize}%
}
\begin{document}

Default:
\begin{itemize}
  \item ISDYNSTP:  Is dynamic time step used ?
  \item ISCDCA:
  \item MVAR
  \item IS2TL
\end{itemize}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:  Is dynamic time step used ?}
  \sortitem{ISCDCA:}
  \sortitem{MVAR}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

To allow for formatting of sorted elements, it's best to modify the syntax. To that end, the following MWE supplied an updated version of \sortitem[<sort label>]{<label/description>} that takes an optional <sort label> (used as the label to sort upon):

enter image description here

\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2][\relax]{%
  \DTLnewrow{list}% Create a new entry
  \ifx#1\relax
    \DTLnewdbentry{list}{sortlabel}{#2}% Add entry sortlabel (no optional argument)
  \else
    \DTLnewdbentry{list}{sortlabel}{#1}% Add entry sortlabel (optional argument)
  \fi%
  \DTLnewdbentry{list}{description}{#2}% Add entry description
}
\newenvironment{sortedlist}{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
  \DTLsort{sortlabel}{list}% Sort list
  \begin{itemize}%
    \DTLforeach*{list}{\theDesc=description}{%
      \item \theDesc}% Print each item
  \end{itemize}%
}
\begin{document}

Default:
\begin{itemize}
  \item ISDYNSTP:  Is dynamic time step used ?
  \item ISCDCA:
  \item MVAR
  \item IS2TL
\end{itemize}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:  Is dynamic time step used ?}
  \sortitem[ISCDCA]{\textit{ISCDCA:}}
  \sortitem[MVAR]{\textbf{MVAR}}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}
  • Werner, I have a quick question here. Whenever I try to bold the first word on the itemize, it automatically goes to the first in the sortedlist. Is there any way I can automatically change the first word to be bold ? – Jdbaba Jun 28 '13 at 15:10
  • @Jdbaba: See my updated post, which now provides \sortitem[<sort label>]{<label/description>}, allowing you to supply a different label to sort on from the description. This is very commonly done when sorting is involved, since you could use macros that don't have any relevant sorting reference. – Werner Jun 28 '13 at 15:41
  • @ Werner: thank you for this addition. This works perfect. I wish I could double vote. – Jdbaba Jun 28 '13 at 16:04
  • @Werner: This is an awesome idea (wish I had thought of it). But and having some difficulty in using it and can't quite figure out what is going on. With Sorted 2: \begin{sortedlist} \sortitem{foo} \sortitem{abc} \sortitem[yyx]{\textbf{yyx}} \end{sortedlist} I get two issues: 1. The title line appears as Sorted 2: x (not sure where the x is coming from) and 2. yyz appears first? The spacing betwen the title and the x depends on the number of \sortitems. Screenshot. – Peter Grill Dec 21 '16 at 9:07
  • Seems that there is some issue with having an optional parameter in \sortitem. If I make it have two mandatory parameters (and adjust the definition), things work just fine. It seems that the x in the output seems to any characters after the first two yy in the optional parameter. So, using yyz4 as the first parameter, results in the title being Sorted 2: z4. You can simply replace [MVAR] in your example with [yyMVAR] to see this problem. – Peter Grill Dec 21 '16 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.