0

I have a some important content "flying" through the document, and I want to make a list of these contents. These results are in a lot of different parts (sections, endnotes...) so, their position (and number) are changeable, in this case, a manual order is inviable.

I am trying to combine zref package with \zref@extract commands and the sort field of glossaries. The problem is that, when I put the extract commands, the glossaries package does not actualize the value, it just uses the default value and I got the wrong order.

\documentclass{scrbook}
\usepackage[xindy, nomain, sanitizesort=false]{glossaries-extra}
\usepackage[titleref, user, abspage]{zref}

\newcounter{test}

\makeatletter
\zref@newprop{test}{\arabic{test}}
\zref@addprop{main}{test}
\makeatother

\newglossary*{here}{Test}
\makeglossaries
\makeatletter
\newglossaryentry{a\zref@extractdefault{a1}{test}{0}}{name={Here \zref@extractdefault{a1}{test}{0}},sort={a\zref@extractdefault{a1}{test}{0}},type=here, description={Here \zref@extractdefault{a1}{test}{0}}}
\newglossaryentry{a\zref@extractdefault{a2}{test}{1}}{name={Here \zref@extractdefault{a2}{test}{1}},sort={a\zref@extractdefault{a2}{test}{1}},type=here, description={Here \zref@extractdefault{a2}{test}{1}}}
\newglossaryentry{a\zref@extractdefault{a3}{test}{2}}{name={Here \zref@extractdefault{a3}{test}{2}},sort={a\zref@extractdefault{a3}{test}{2}},type=here, description={Here \zref@extractdefault{a3}{test}{2}}}
\newglossaryentry{a\zref@extractdefault{a4}{test}{3}}{name={Here \zref@extractdefault{a4}{test}{3}},sort={a\zref@extractdefault{a4}{test}{3}},type=here, description={Here \zref@extractdefault{a4}{test}{3}}}
\newglossaryentry{a\zref@extractdefault{a5}{test}{4}}{name={Here \zref@extractdefault{a5}{test}{4}},sort={a\zref@extractdefault{a5}{test}{5}},type=here, description={Here \zref@extractdefault{a5}{test}{4}}}
\newglossaryentry{a\zref@extractdefault{a6}{test}{5}}{name={Here \zref@extractdefault{a6}{test}{5}},sort={a\zref@extractdefault{a6}{test}{5}},type=here, description={Here \zref@extractdefault{a6}{test}{5}}}
\makeatother
\begin{document}

Here\setcounter{test}{4}\zlabel{a1}

Here\setcounter{test}{6}\zlabel{a2}

Here\setcounter{test}{10}\zlabel{a3}

Here\setcounter{test}{2}\zlabel{a4}

Here\setcounter{test}{3}\zlabel{a5}

Here\setcounter{test}{8}\zlabel{a6}

\glsaddall
\printglossary[style=list, nonumberlist, type=here]

\end{document} 

Here, I get the order from the default values of \zref@extractdefault{a1}{test}{0} command. I want to put the reference value (i.e., the value of the counter) on the sort engine.

I.e., with this code I got:

enter image description here

And I want:

Here 2
Here 3
Here 4 and etc.

1 Answer 1

1

The problem in your setup is that the .aux file, which sets the labels, is read at begindocument. Hence, in the preamble, the labels are not available at all, so you get the default values in those \zref@extractdefault calls.

You can put them in a hook, so that the glossary entries are done when the labels are available:

\documentclass{scrbook}
\usepackage[xindy, nomain, sanitizesort=false]{glossaries-extra}
\usepackage[titleref, user, abspage]{zref}

\newcounter{test}

\makeatletter
\zref@newprop{test}{\arabic{test}}
\zref@addprop{main}{test}
\makeatother

\newglossary*{here}{Test}
\makeglossaries
\makeatletter
\AddToHook{begindocument/end}{%
  \newglossaryentry{a\zref@extractdefault{a1}{test}{0}}{name={Here \zref@extractdefault{a1}{test}{0}},sort={a\zref@extractdefault{a1}{test}{0}},type=here, description={Here \zref@extractdefault{a1}{test}{0}}}%
  \newglossaryentry{a\zref@extractdefault{a2}{test}{1}}{name={Here \zref@extractdefault{a2}{test}{1}},sort={a\zref@extractdefault{a2}{test}{1}},type=here, description={Here \zref@extractdefault{a2}{test}{1}}}%
  \newglossaryentry{a\zref@extractdefault{a3}{test}{2}}{name={Here \zref@extractdefault{a3}{test}{2}},sort={a\zref@extractdefault{a3}{test}{2}},type=here, description={Here \zref@extractdefault{a3}{test}{2}}}%
  \newglossaryentry{a\zref@extractdefault{a4}{test}{3}}{name={Here \zref@extractdefault{a4}{test}{3}},sort={a\zref@extractdefault{a4}{test}{3}},type=here, description={Here \zref@extractdefault{a4}{test}{3}}}%
  \newglossaryentry{a\zref@extractdefault{a5}{test}{4}}{name={Here \zref@extractdefault{a5}{test}{4}},sort={a\zref@extractdefault{a5}{test}{5}},type=here, description={Here \zref@extractdefault{a5}{test}{4}}}%
  \newglossaryentry{a\zref@extractdefault{a6}{test}{5}}{name={Here \zref@extractdefault{a6}{test}{5}},sort={a\zref@extractdefault{a6}{test}{5}},type=here, description={Here \zref@extractdefault{a6}{test}{5}}}%
}
\makeatother
\begin{document}

Here\setcounter{test}{4}\zlabel{a1}

Here\setcounter{test}{6}\zlabel{a2}

Here\setcounter{test}{10}\zlabel{a3}

Here\setcounter{test}{2}\zlabel{a4}

Here\setcounter{test}{3}\zlabel{a5}

Here\setcounter{test}{8}\zlabel{a6}

\glsaddall
\printglossary[style=list, nonumberlist, type=here]

\end{document}

enter image description here

You still got a sorting problem to solve there, since glosarries is sorting by the first digit, rather than considering those as numbers. But I understand your question here was how to get the label's values, and that works as long as you query the labels after begindocument.

However, this seems much more suited for a ToC than for glossaries. A simple example, using tocloft (just an example, it does not go along with scrbook, but you can get equivalent functionality there):

\documentclass{book}

\usepackage{tocloft}

\newlistof{endoftheworldpart}{prt}{My Parts}

\newcommand{\mypart}[1]{%
  \stepcounter{endoftheworldpart}
  \addcontentsline{prt}{endoftheworldpart}{Here \arabic{endoftheworldpart}}}

\begin{document}

Here\mypart

Here\mypart

Here\mypart

Here\mypart

Here\mypart

Here\mypart

\listofendoftheworldpart

\end{document}

enter image description here

4
  • Thank you for your answer! The correct order for indices +10 is easy to correct, just add automatic a zero before the number 1,...,9 with ifnumcomp command. Also, thank for your suggestion, this will be very useful! Oct 16, 2021 at 13:13
  • @TheEndofTheWorld And what happens if you hit 100? If you don't actually need the counter value to appear in your glossary entries, you could get away really easy just initializing the counter to, say, 100000.
    – gusbrs
    Oct 16, 2021 at 13:25
  • The counter is the number of the item that I want to put in order, so it will appear in the sort and description fields. If it hit 100 (something that will be difficult to occur), I will make another workaround with ifnumcomp, for example, \ifnumcomp{x}{<}{100}{\ifnumcomp{x}{<}{10}{00x}{0x}}{x}, I think that this is very newbie, but it solves the problem :-). Oct 16, 2021 at 13:55
  • 1
    @TheEndofTheWorld Ah, if you need the counter for the description, you really have to handle it more carefully. Btw, if you happen to use the ToC alternative, I've edited the answer to use \stepcounter instead of \refstepcounter, as the latter might have some undesired side effects on your cross-references in this case. I had adapted the example from tocloft's documentation, but I must have missed something on why it needed \refstepcounter there.
    – gusbrs
    Oct 16, 2021 at 14:55

You must log in to answer this question.

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