1

I am wondering whether it could be possible to color the items sorted alphabetically. This is an extension of the previous question. Alphabetically display the items in itemize

Here is a MWE:

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

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:}
  \sortitem{ISCDCA:}
  \sortitem{\textcolor{magenta}{MVAR}}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

I would like "MVAR" to appear in a specific color. I also tried replacing the third item with \sortitem{color{magenta}MVAR}, but that doesn't seem to work either.

Any help would be greatly appreciated, thank you!

sml
  • 77

1 Answers1

1

You could add an optional argument that can be used to set the color, or other stylistic options.

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

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:}
  \sortitem{ISCDCA:}
  \sortitem{PFT}
  \sortitem[\protect\color{magenta}]{MVAR}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

enter image description here