# How do I feed evaluated references into this sorting routine?

Below is some code I cobbled together from examples found on the net. I would like the sorted list routine to use the results of evaluating \ref rather than using the literal argument of \ref.

Unfortunately, this is not happening in the correct order or the sort routine is not remembering the results of evaluating multiple \ref instances.

Thoughts?

            \documentclass{report}

\newcounter{TableNoteCounter}
\renewcommand{\theTableNoteCounter}{\alph{TableNoteCounter}}
\newcommand{\tablenotelabel}[1]{\refstepcounter{TableNoteCounter}\alph{TableNoteCounter}\label{#1}}

\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2]{%
\DTLnewrow{list}%
\DTLnewdbentry{list}{label}{\ref{#1}}%
\DTLnewdbentry{list}{description}{#2}%
}

\newenvironment{sortedlist}%
{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}%
}%
{%
\DTLsort{label}{list}%
\begin{description}%
\DTLforeach*{list}{\theLabel=label,\theDesc=description}{%
\item[\theLabel] \theDesc
}%
\end{description}%
}

\begin{document}

\tablenotelabel{stars}
\tablenotelabel{galaxies}
\tablenotelabel{planets}

\begin{sortedlist}
\sortitem{planets}{Some planets are inhabited.}
\sortitem{galaxies}{Some galaxies are grand.}
\sortitem{stars}{All stars burn'' hydrogen.}
\end{sortedlist}

\end{document}

• Welcome to TeX.SE. Add  \dtlexpandnewvalue% to the \sortitem macro. I think this does what you desire. – Peter Grill Sep 30 '14 at 0:48
• That absolutely did it. Making \dtlexpandnewvalue the first line of the \sortitem macro solved the problem. Thank you @Peter! – Pax Per Scientiam Sep 30 '14 at 1:02
• Please see revised solution. The earlier solution won't work if the .aux file does not already exist from a prior run. – Peter Grill Sep 30 '14 at 4:59

You can use \dtlexpandnewvalue to expand the \ref in \sortitem. However, this can only be done once \ref has been defined and which is only after the .aux file has been created. Hence, the following is the output after the second run:

During the first run this will be displayed as:

## Notes:

• To allow for use with hyperref I now check to see if \HyPsd@@@ref is defined (which mans that hyperref is being used) and if so I use \HyPsd@@@ref instead of \ref.

## Code:

\documentclass{report}

\usepackage{alphalph}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\usepackage{hyperref}

\newcounter{TableNoteCounter}
\renewcommand{\theTableNoteCounter}{\alphalph{\value{TableNoteCounter}}}
\newcommand{\tablenotelabel}[1]{\refstepcounter{TableNoteCounter}\alphalph{\value{TableNoteCounter}}\label{#1}}

\makeatletter
\newcommand{\sortitem}[2]{%
\ifcsname r@#1\endcsname% Only expand once the \ref has been defined
\fi
\DTLnewrow{list}%
\ifdefined\HyPsd@@@ref
\DTLnewdbentry{list}{label}{\HyPsd@@@ref{#1}}%
\else
\DTLnewdbentry{list}{label}{\ref{#1}}%
\fi
\DTLnewdbentry{list}{description}{#2}%
}
\makeatother

\newenvironment{sortedlist}%
{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}%
}%
{%
\DTLsort{label}{list}%
\begin{description}%
\DTLforeach*{list}{\theLabel=label,\theDesc=description}{%
\item[\theLabel] \theDesc
}%
\end{description}%
}

\begin{document}

\tablenotelabel{stars}
\tablenotelabel{galaxies}
\tablenotelabel{planets}

\begin{sortedlist}
\sortitem{planets}{Some planets are inhabited.}
\sortitem{galaxies}{Some galaxies are grand.}
\sortitem{stars}{All stars burn'' hydrogen.}
\end{sortedlist}
\end{document}

• Hi @Peter, there appears to be a conflict with the hyperref package. How might we make above place nicely with hyperref? – Pax Per Scientiam Sep 30 '14 at 16:07
• @PaxPerScientiam: Have updated code to allow for use with (or without) hyperref. Please be aware that this is venturing into unfamiliar territory as far as my TeX experience so please test thoroughly to make sure it works as desired. – Peter Grill Sep 30 '14 at 20:49