1

This is an extension to a previous question: Color alphabetically display the items in itemize. I would like to have color in my sorted items with indexes and footnotes. The solution provided by Schrödinger's cat works beautifully, but it seems when I have multiple items that start with the same letter, I get the following error:

! TeX capacity exceeded, sorry [input stack size=5000]. \reserved@a ->\def \reserved@a *{@sdtlgetdatatype }\reserved@a l.48 \end{sortedlist}

Here is a MWE:

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

\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing

\usepackage{xcolor} % Required for specifying colors by name
%  This is necessary to allow colorizing the mark for the footnotes, from https://tex.stackexchange.com/questions/26693/change-the-color-of-footnote-marker-in-latex
\definecolor{bondiblue}{rgb}{0.0, 0.58, 0.71}  %use this color for footnotes.

\makeatletter
\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{bondiblue}\@thefnmark}}}
\makeatother


\makeatletter
\newcommand\footnoteref[1]{\protected@xdef\@thefnmark{\ref{#1}}\@footnotemark}
\makeatother



\begin{document}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP and such:}
  \sortitem[\protect\color{red}]{ZVAR is good\footnote{about zvar}\index{Zvar}}
  \sortitem{ISCDCA:}
  \sortitem{PFT}
  \sortitem[\protect\color{orange}]{MVAR is okay too\footnote{about mvar}\index{mvar}}
  \sortitem[\protect\color{blue}]{MCAR is fine\footnote{about mcar}\index{mcar}}
  \sortitem{IS2TL}
\end{sortedlist}


\printindex % Output the index
\end{document}

The code above works if I remove either the 'MVAR' or 'MCAR' line, but I can't have both. It seems to have to do with the footnote and index.

If I have both 'MVAR' and 'MCAR' but no footnote or index for either, it works. But if I have an index or footnote for either 'MVAR' or 'MCAR' it doesn't work. Is there a way to fix this so that both appears? I would like to be able to have any number of items (with footnote and/or index) that begins with the same letter. Any help would be appreciated, thank you!

1 Answer 1

1

It is perhaps my bad, maybe I should have stressed more the role of \protect in this business: you need to \protect the macros that you are using in order to prevent an undue expansion. I did this for the \color command, but this also applies to the \footnote and \index commands. Doing this for the macros you have added yields the (presumably) expected result.

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

\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing

\usepackage{xcolor} % Required for specifying colors by name
%  This is necessary to allow colorizing the mark for the footnotes, from https://tex.stackexchange.com/questions/26693/change-the-color-of-footnote-marker-in-latex
\definecolor{bondiblue}{rgb}{0.0, 0.58, 0.71}  %use this color for footnotes.

\makeatletter
\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{bondiblue}\@thefnmark}}}
\makeatother


\makeatletter
\newcommand\footnoteref[1]{\protected@xdef\@thefnmark{\ref{#1}}\@footnotemark}
\makeatother



\begin{document}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP and such:}
  \sortitem[\protect\color{red}]{ZVAR is good\protect\footnote{about zvar}\protect\index{Zvar}}
  \sortitem{ISCDCA:}
  \sortitem{PFT}
  \sortitem[\protect\color{orange}]{MVAR is okay too\protect\footnote{about mvar}\protect\index{mvar}}
  \sortitem[\protect\color{blue}]{MCAR is fine\protect\footnote{about mcar}\protect\index{mcar}}
  \sortitem{IS2TL}
\end{sortedlist}


\printindex % Output the index
\end{document}

enter image description here

An index is generated upon running makeindex.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .