# Sort footnotes alphabetically or per-page glossary

I'd like to create a document where glossary entries are on each page in the footnotes and I've been able to do that (see MWE below), but I'd like the footnote glosses to be sorted alphabetically, but I've been unable to find a way to sort footnotes. Conversely, is there a per-page glossary?

MWE:

\documentclass{book}
\usepackage[nonumberlist]{glossaries}
\usepackage{xifthen}
\usepackage{everypage}
\makeglossaries

% enable counting the words
\glsenableentrycount

% reset counts every page

% populate some entries
\newglossaryentry{uno}{name=uno,description=one}
\newglossaryentry{dos}{name=dos,description=two}
\newglossaryentry{tres}{name=tres,description=three}
\newglossaryentry{quatro}{name=quatro,description=four}
\newglossaryentry{cinco}{name=cinco,description=five}

% displaying glossary entries
\newcommand\glossdisplay[2]{%
\ifthenelse{\glsentrycurrcount{\glslabel} = 0}{%
#1\let\thefootnote\relax\footnote{\textbf{\glslabel} #2}%
}{%
% not the first definition on this page so just display the word
#1%
}%
}
\renewcommand\glsdisplayfirst[4]{\glossdisplay{#1}{#2}}
\renewcommand\glsdisplay[4]{\glossdisplay{#1}{#2}}
\newcommand\gloss[2][\relax]{\glsdisp{#1}{#2}}

\begin{document}
Counting to three in Spanish, \gloss[uno]{uno}, \gloss[dos]{dos}, \gloss[tres]{tres}.
Counting to five in Spanish, \gloss[uno]{uno}, \gloss[dos]{dos}, \gloss[tres]{tres}, \gloss[quatro]{quatro}, \gloss[cinco]{cinco}.\\

Notice how \gloss[uno]{uno}, \gloss[dos]{dos}, and \gloss[tres]{tres} only appear once in the footnotes despite being glossed three times.
Now the real question: \textit{How do I sort the per-page glossary alphabetically?}  Perhaps there is a per-page option for the glossaries package?
\end{document}


In the produced document, the footnote/glossary entries are sorted

• uno
• dos
• tres
• quatro
• cinco

but I'd like to see

• cinco
• dos
• quatro
• tres
• uno

Also, if it helps I built the document with:

pdflatex repro.tex
makeindex -s repro.ist -o repro.gls repro.glo
pdflatex repro.tex

• How do you want the footnote numbering? Following the alphabet (1 cinco, 2 dos, 3 quatro, 4 tres, 5 uno) or following usage (5 cinco, 2 dos, 4 quartro, 3 tres, 1 uno)? (If according to usage, do you want the footnote numbering reset every page?) Are you likely to have other footnotes (unrelated to the glossary) that might interfere? – Nicola Talbot Feb 9 '16 at 16:10
• I'm suppressing the display of the footnote numbers so for this example I don't really care what they are and I don't currently have plans for other footnotes other than the per-page glossary, so I don't expect any interference. – Brett Forsgren - MSFT Feb 9 '16 at 18:34

This is the closest I've been able to get. I've used glossaries-extra which extends the glossaries package and provides per-unit counting in addition to the glossaries package's document-wide counting. This makes it easier to count per page.

\documentclass{book}

\usepackage{everypage}
\usepackage{glossaries-extra}

\gdef\entrylabellist{}%
}

\GlsXtrEnableEntryUnitCounting{general}{0}{page}

\newcommand*{\entrylabellist}{}

\makeatletter
\def\@glo@sortinghandler{\@glo@sorthandler@word}%
\newcommand*{\sortpageentries}{%
\forglsentries{\thisentry}{%
\ifnum\glsentryprevcount{\thisentry}>0\relax
\expandafter\@glo@sortedinsert\expandafter\entrylabellist\expandafter
{\thisentry}%
\fi
}%
}

\ifnum\glsentrycurrcount{\glslabel}=1\relax
\footnotemark[1]%
\ifdefempty\entrylabellist
{%
\sortpageentries
\footnotetext[1]{\@for\thisentry:=\entrylabellist\do{%
\glsentryname{\thisentry} \glsentrydesc{\thisentry}. }}%
}%
{}%
\fi
}
\makeatother

\newglossaryentry{uno}{name=uno,description=one}
\newglossaryentry{dos}{name=dos,description=two}
\newglossaryentry{tres}{name=tres,description=three}
\newglossaryentry{quatro}{name=quatro,description=four}
\newglossaryentry{cinco}{name=cinco,description=five}

\begin{document}

Counting to three in Spanish, \gls{uno}, \gls{dos},
\gls{tres}.  Counting to five in Spanish, \gls{uno},
\gls{dos}, \gls{tres}, \gls{quatro}, \gls{cinco}.

Notice how \gls{uno}, \gls{dos}, and \gls{tres} only appear once in
the footnotes despite being glossed three times.

\newpage

Test next page.

\gls{tres}, \gls{quatro} and \gls{cinco}.
And again:
\gls{tres}, \gls{quatro} and \gls{cinco}.

\end{document}


This requires two LaTeX runs as it uses the auxiliary file to store the total count from the previous run, so the footnotes won't appear on the first instance. This doesn't require makeindex/xindy unless you additionally want a complete glossary at the start or end of the document. The sorting is done using datatool-base's ordered insertion commands, which are accessed through \@glo@sortedinsert (provided by glossaries). This requires \@glo@sortinghandler to be set to the appropriate comparison handler (\@glo@sorthandler@word in this case).

The above example produces

at the top of the first page. The footnote at the bottom of the page appears as

The formatting can be adjusted by editing the lines

     \footnotetext[1]{\@for\thisentry:=\entrylabellist\do{%
\glsentryname{\thisentry} \glsentrydesc{\thisentry}. }}%


The top of the second page appears as

with the footnote

Caveat: due to TeX's asynchronous output routine, this may not work properly for entries that occur in the tail end of a paragraph that spans a page break.