1

this is an extended question taken by Alphabetically display the items in itemize

The solution posted by Werner is amazing. But what I need is to sort by a value of a macro. For example \foo{Label} is a function that take Label as argument and give you SortLabel as string.

\foo{Label1} %Will print SortLabel1
\foo{Label2} %Will print SortLabel2    
\foo{Label3} %Will print SortLabel3

\begin{sortedlist}
    \sortitem[\foo{Label1}]{Bar1}
    \sortitem[\foo{Label2}]{Bar2}
    \sortitem[\foo{Label3}]{Bar3}
\end{sortedlist}

Tryed to use \expandafter, but seems that datatool can store only by \foo{Label1} and can't sort for SortLabel1....

To be more specific, the function \foo for me is \glossentryname{} that give me the name of a glossary entry. Maybe this could be the reason that expansion doesn't work. Here's a MnWE

\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\usepackage{glossaries, glossary-tree}
\makeglossaries

%%%%Sorted Functions%%%%
\newcommand{\sortitem}[1]{%
  \DTLnewrow{list}% Create a new entry
  \def\tmp{\DTLnewdbentry{list}{description}}%
  \expandafter\tmp\expandafter{#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}%
}

%%%The Glossary Part%%%%%
\longnewglossaryentry{MVARglossary}{name=MVAR}{
The Description of glossary (not useful at the moment)
}


\begin{document}

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

\def\foo{ISDYNSTP:  Is dynamic time step used ?}
\def\bar{\glossaryname{MVARglossary}}

\glossentryname{MVARglossary}

\bar

Sorted:
\begin{sortedlist}
  \sortitem{\foo}
  \sortitem{ISCDCA:}
  \sortitem{\bar}
  \sortitem{\glossentryname{MVARglossary}}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

suggests?

1 Answers1

2

Here, I modified Werner's solution by editing \sortitem as

\newcommand{\sortitem}[1]{%
  \DTLnewrow{list}% Create a new entry
  \def\tmp{\DTLnewdbentry{list}{description}}%
  \expandafter\tmp\expandafter{#1}% Add entry as description
}

in order to expand the argument being processed. Then, in the MWE, I can pass \foo as an item to be sorted (where \foo is a definition unrelated to glossary entry names).

The next problem was that \glossentryname is not expandable. So I employed the existing \glsentryname macro instead (originally, I had made a limited version of \glossentryname, that is expandable, but Nicola pointed out that also plays with the font). I then created a macro \sortgloss{<label>} that will invoke \sortitem on the expanded gloss-entry-name.

Here is the MWE (EDITED to take Nicola's suggestion at using the existing expandable \glsentryname rather than create a new expandable macro)

\documentclass{article}
\usepackage{glossaries}
\usepackage[T1]{fontenc}

\usepackage{datatool}% http://ctan.org/pkg/datatool
\makeatletter
\newcommand{\sortitem}[1]{%
  \DTLnewrow{list}% Create a new entry
  \def\tmp{\DTLnewdbentry{list}{description}}%
  \expandafter\tmp\expandafter{#1}% Add entry as description
}
\makeatother
\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}%
}

\makeatletter
\newcommand\sortgloss[1]{%
  \protected@edef\x@glossname{\glsentryname{#1}}%
  \sortitem{\x@glossname}%
}
\makeatother
\begin{document}

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

\newacronym{MVx}{MVAR}{Many Virgins Act Responsibly}


\def\foo{ISDYNSTP:  Is dynamic time step used ?}

Sorted:
\begin{sortedlist}
  \sortitem{\foo}
  \sortitem{ISCDCA:}
  \sortgloss{MVx}
  \sortitem{IS2TL}
\end{sortedlist}


\end{document}

The output is the same as Werner's:

enter image description here

Alternately, if one were to define \sortitem to fully expand its argument:

\makeatletter
\newcommand{\sortitem}[1]{%
  \DTLnewrow{list}% Create a new entry
  \protected@edef\tmpA{#1}%
  \def\tmp{\DTLnewdbentry{list}{description}}%
  \expandafter\tmp\expandafter{\tmpA}% Add entry as description
}
\makeatother

then one would be free to invoke it as \sortitem{\glsentryname{MVx}} rather than \sortgloss{MVx}, if consistency of invocation syntax were important.

  • Thank you! I've edited my question with a Minimal NonWorking Example, because it seems not working with \glossentryname{} . – Alberto Verdoja May 04 '16 at 14:04
  • @AlbertoVerdoja I have compiled a revised version that may be of use. – Steven B. Segletes May 04 '16 at 14:45
  • 1
    \Glossentryname is already defined in glossaries.sty. Why not just use \glsentryname (which is expandable) instead? – Nicola Talbot May 04 '16 at 17:50
  • @NicolaTalbot Oops! I will correct it. Thanks! – Steven B. Segletes May 04 '16 at 17:55
  • @StevenB.Segletes Sorry, I meant that \glsentryname is an existing expandable command, so it works better for sorting (without the font-changing \glsnamefont). Actually, perhaps it would be better to access the sort key for the entry, which would make more sense, but then you'd have to separate out the sort key and the displayed text. – Nicola Talbot May 04 '16 at 18:35
  • @NicolaTalbot Double-oops! I just deleted my (re)definition of \glsentryname and found, as you indicate, that the existing \glsentryname works just fine with my \sortgloss macro. I hope I have accomplished your intent. I am not a glossaries user and so don't feel comfortable expanding the answer to use sort keys and such. – Steven B. Segletes May 04 '16 at 19:00
  • @StevenB.Segletes That's fine +1 :-) (Perhaps use \protected@xdef instead of \xdef?) The sort key would only be needed if the name was a symbol or something involving non-expandable commands, which isn't the case here. – Nicola Talbot May 04 '16 at 19:09