我从以下地方学到了计数器这里。我的问题是,我可以使用十进制以外的计数器吗,比如十六进制的计数器?我该如何实现?
答案1
您可以使用expl3
:
\documentclass{article}
\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}
更一般地,对于任何基数,最多 36
\documentclass{article}
\usepackage{geometry} % to see more pages
\geometry{paperwidth=5cm,paperheight=5cm,bottom=2cm}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\cbase}{mm}
{
\egreg_cbase:nc { #1 } { c@#2 }
}
\cs_new:Npn \egreg_cbase:nN #1 #2 { \int_to_Base:nn { #2 } { #1 } }
\cs_generate_variant:Nn \egreg_cbase:nN { nc }
\NewDocumentCommand{\newrepresentation}{mm}
{% #1 = command name, #2 = base
\cs_new:Npn #1 ##1 { \cbase{#2}{##1} }
\cs_new:cpn { @\cs_to_str:N #1 } { \egreg_cbase:nN { #2 } }
}
\newrepresentation{\basexxxvi}{36}
\ExplSyntaxOff
\newcounter{test}
\renewcommand{\thetest}{\cbase{20}{test}}
\pagenumbering{basexxxvi}
\begin{document}
\raggedright
\setcounter{page}{33}
\setcounter{test}{17}
\stepcounter{test}\thetest, should be~I \\ page~X
\clearpage
\stepcounter{test}\thetest, should be~J \\ page~Y
\clearpage
\stepcounter{test}\thetest, should be~10 \\ page~Z
\clearpage
\stepcounter{test}\thetest, should be~11 \\ page~10
\clearpage
\setcounter{test}{65566}\thetest, should be~83I6 \\ page~11
\end{document}
这里test
以 20 为基数表示,而页面以 36 为基数表示。对于页码,\newrepresentation
需要 。
如果优先使用小写字母,请替换\int_to_Base:nn
为\int_to_base:nn
。
答案2
我不知道这是否是你想要的但是:
\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}
您还可以使用以下方法转换为任意基数(默认为 16)
\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}