5

I have a definition with 20 cases or so, and a proof about that definition has one case for each case of the definition. I'd like the proof cases to reorder themselves automatically when I reorder cases in the definition. I'm trying to use this answer about sorting to sort the proof cases by the label they reference, but running into some trouble. A minimal example of what goes wrong is:

\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\newcounter{foo}
\refstepcounter{foo}\label{a}
\refstepcounter{foo}\label{b}
\ifnumcomp\ref{a}<\ref{b}{A}{B}
\end{document}

which produces rather a lot of errors:

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 2 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/etoolbox/etoolbox.sty
(/usr/share/texlive/texmf-dist/tex/latex/etex-pkg/etex.sty)) (./test.aux)
! Missing \endcsname inserted.
<to be read again> 
                   \relax 
l.7 \ifnumcomp\ref{a}<
                      \ref{b}{A}{B}
! Missing number, treated as zero.
<to be read again> 
                   \protect 
l.7 \ifnumcomp\ref{a}<
                      \ref{b}{A}{B}
! Missing = inserted for \ifnum.
<to be read again> 
                   \gdef 
l.7 \ifnumcomp\ref{a}<
                      \ref{b}{A}{B}
! Missing number, treated as zero.
<to be read again> 
                   \gdef 
l.7 \ifnumcomp\ref{a}<
                      \ref{b}{A}{B}

LaTeX Warning: Reference `\endcsname ' on page 1 undefined on input line 7.

! You can't use `\numexpr' in horizontal mode.
\ifnumcomp ...\ifnum \numexpr #1\relax #2\numexpr 
                                                  #3\relax \expandafter \@fi...
l.7 \ifnumcomp\ref{a}<
                      \ref{b}{A}{B}

LaTeX Warning: Reference `A' on page 1 undefined on input line 7.

[1{/usr/share/texlive/texmf-dist/fonts/map/pdftex/updmap/pdftex.map}]
(./test.aux)

LaTeX Warning: There were undefined references.

 )
(see the transcript file for additional information)</usr/share/texlive/texmf-d
ist/fonts/type1/public/amsfonts/cm/cmbx10.pfb></usr/share/texlive/texmf-dist/fo
nts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on test.pdf (1 page, 19390 bytes).
Transcript written on test.log.

In an attempt to wave a dead chicken over it, I also tried these incantations for the comparison:

\ifnumcomp{\ref{a}}<{\ref{b}}{A}{B}
\ifnumcomp\protect\ref{a}<\protect\ref{b}{A}{B}
\ifnumcomp{\protect\ref{a}}<{\protect\ref{b}}{A}{B}

Each of these produce their own long errors. Is there a way to compare references suitable for use in the sorting mechanism suggested above?

1 Answers1

6

You need some support, provided by refcount:

enter image description here

\documentclass{article}
\usepackage{etoolbox,refcount}% http://ctan.org/pkg/{etoolbox,refcount}
\begin{document}
\newcounter{foo}
\refstepcounter{foo}\thefoo\label{a}
\refstepcounter{foo}\thefoo\label{b}
\ifnumcomp{\getrefnumber{a}}{<}{\getrefnumber{b}}{A}{B}
\end{document}

refcount's \getrefnumber provides an expandable version of the reference number, which can be used in calculations/comparisons like \ifnumcomp.

Werner
  • 603,163