Skip to content

Instantly share code, notes, and snippets.

@jmclawson
Last active June 14, 2021 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmclawson/3496659647bedbf27c4dd0c9c71181b9 to your computer and use it in GitHub Desktop.
Save jmclawson/3496659647bedbf27c4dd0c9c71181b9 to your computer and use it in GitHub Desktop.
Provides an alternative to \mkcomprange for printing a leading zero in a compressed range of numbers less than 10.
\newrobustcmd*{\mkcomprangezero}{%
\begingroup
\@ifstar
{\blx@range@aux\blx@comprange@ii}
{\blx@range@aux\blx@comprange@i}}
\def\blx@comprange@i[#1][#2]#3{%
\let\blx@tempa\@empty
\protected\def\blx@range@out@value{\appto\blx@tempa}%
\def\blx@range@out@item@process{#2}%
\let\blx@range@out@delim\blx@range@out@value
\let\blx@range@split\blx@genrange@split
\let\blx@range@process\blx@comprange@check
\blx@range@chunk{#3}%
\begingroup
\edef\blx@tempa{\endgroup
\unexpanded{#1}{\expandonce\blx@tempa}}%
\blx@tempa
\endgroup}
\def\blx@comprange@ii[#1][#2]#3{%
\protected\def\blx@range@out@value{#1}%
\def\blx@range@out@item@process{#2}%
\let\blx@range@out@delim\@firstofone
\let\blx@range@split\blx@genrange@split
\let\blx@range@process\blx@comprange@check
\blx@range@chunk{#3}%
\endgroup}
\def\blx@comprange@check#1#2{%
\blx@imc@ifinteger{#1}
{\blx@imc@ifinteger{#2}
{\@firstoftwo}
{\@secondoftwo}}
{\@secondoftwo}
{\blx@comprange@comp{#1}{#2}}
{\begingroup
\protected@edef\blx@tempc{\endgroup
\blx@range@out@value{%
\blx@range@out@item@process{\unexpanded{#1}}%
\noexpand\bibrangedash
\blx@range@out@item@process{\unexpanded{#2}}}}%
\blx@tempc}}
\def\blx@comprange@comp#1#2{%
\def\blx@tempb{#1}%
\def\blx@tempc{#2}%
\let\blx@tempd\blx@tempc
\ifnum\c@maxcomprange<\abx@rangeproclimit\relax
\numdef\blx@tempe\abx@rangeproclimit
\else
\numdef\blx@tempe\c@maxcomprange
\fi
\blx@tempcntc=\blx@tempe\relax
\ifnum
\ifnum\c@mincompwidth<1\space1\fi
\ifnum\c@maxcomprange<10\space1\fi
\ifnum\c@mincomprange<\blx@tempb\space\else1\fi
\ifnum\blx@tempb<\numexpr\blx@tempcntc*10\relax\else1\fi
0=\z@
\expandafter\blx@comprange@comp@div
\else
\expandafter\blx@comprange@end
\fi}
\def\blx@comprange@end{%
\numdef\blx@tempb\blx@tempb
\ifnum\blx@tempe>\c@maxcomprange\relax
\numdef\blx@tempc\blx@tempc
\else
\numdef\blx@tempc\blx@tempd
\fi
\begingroup
\protected@edef\blx@tempb{\endgroup
\blx@range@out@value{%
\blx@range@out@item@process{\blx@tempb}%
\noexpand\bibrangedash
% mkcomprangezero changes start here
\ifnum\blx@tempc<10
\ifnum\blx@tempb<10
\blx@range@out@item@process{\blx@tempc}
\else
\blx@range@out@item@process{0\blx@tempc}% force leading zero
\fi
\else
\blx@range@out@item@process{\blx@tempc}
\fi}}% mkcomprangezero changes end here
\blx@tempb}
\def\blx@comprange@comp@div{%
\unless\ifnum\blx@tempb<\blx@tempcntc
\blx@tempcnta\blx@tempb\relax
\blx@tempcntb\blx@tempc\relax
\divide\blx@tempcnta\blx@tempcntc
\divide\blx@tempcntb\blx@tempcntc
\ifnum\blx@tempcnta=\blx@tempcntb
\edef\blx@tempd{\expandafter\@gobble\blx@tempd}%
\numdef\blx@tempe\blx@tempcntc
\fi
\fi
\divide\blx@tempcntc10\relax
\ifnum
\ifnum\blx@tempcntc<10 1\fi
\ifnum\blx@tempcntc>\c@mincompwidth\else 1\fi
0=\z@
\expandafter\blx@comprange@comp@div
\else
\expandafter\blx@comprange@end
\fi}
@jmclawson
Copy link
Author

jmclawson commented Jun 14, 2021

The only changes from \mkcomprange are in lines 73 to 82, but the code may be helpful to others. Simply use \mkcomprangezero anywhere a Biblatex style might use \mkcomprange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment