3

I have learned about counters from here. My question is, can I have a counter in other than 10 base, say hexadecimal one? How do I achieve that?

  • Related: tex.stackexchange.com/a/17707/134144 You might also be interested in the fmtcount package. – leandriis Oct 6 '19 at 16:37
  • if this is related to use with \char or \symbol then you do not need hex – David Carlisle Oct 6 '19 at 16:39
  • 2
    the counter itself does not have a base, the base is just a print form for the value. – David Carlisle Oct 6 '19 at 16:55
  • @DavidCarlisle OK. How do I print hex of a counter? WHich package to use? – user2925716 Oct 6 '19 at 16:57
  • there are several packages,fmtcount is one, but why do you need hex? – David Carlisle Oct 6 '19 at 17:05
5

You can use expl3:

\documentclass{article}
\usepackage{xparse}

\usepackage{geometry} % to see more pages
\geometry{paperwidth=5cm,paperheight=5cm,bottom=2cm}


\ExplSyntaxOn
\NewExpandableDocumentCommand{\hexa}{m}
 {
  \egreg_hexa:c { c@#1 }
 }
\cs_new:Npn \egreg_hexa:N #1 { \int_to_Hex:n { #1 } }
\cs_generate_variant:Nn \egreg_hexa:N { c }
\cs_set_eq:cN { @hexa } \egreg_hexa:N % for \pagenumbering
\ExplSyntaxOff

\newcounter{test}
\renewcommand{\thetest}{\hexa{test}}

\pagenumbering{hexa}

\begin{document}

\setcounter{page}{7}
\setcounter{test}{13}

\stepcounter{test}\thetest
\clearpage

\stepcounter{test}\thetest
\clearpage

\stepcounter{test}\thetest
\clearpage

\stepcounter{test}\thetest
\clearpage

\setcounter{test}{65566}\thetest

\end{document}

enter image description here

|improve this answer|||||
3

I don't know if this is what you're after but:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\tohex}{m}{\int_to_hex:n {#1}}
\ExplSyntaxOff

\begin{document}

\newcounter{mycounter}
\setcounter{mycounter}{43}

\tohex{\themycounter}

\end{document}

You could also convert to an arbitrary base (default 16) using

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\tobase}{O{16}m}{\int_to_base:nn {#2}{#1}}
\ExplSyntaxOff

\begin{document}

\newcounter{mycounter}
\setcounter{mycounter}{43}

\tobase[8]{\themycounter}


\end{document}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.