首先应该给出一个最小的工作样本:
\documentclass{article}
\newcounter{mynumber}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{mynumber}{\number\value{mynumber}}%
}%
}
\makeatother
\AtBeginDocument{%
\stepcounter{mynumber}
}
\begin{document}
the version number is \themynumber.
\end{document}
使用XeLateX编译它,我们将获得版本号,例如1。然后我的问题是
- 如何让 1 变成 0001,23 变成 0023 等等(即四位数字)?请注意,我想要的格式是 0001,而不是 0 001(即没有单位,没有空格)。
答案1
带包装siunitx
:
the version number is \num[minimum-integer-digits = 4]{\themynumber}.
以及 Sans Serif
\textsf{\num[detect-family,minimum-integer-digits = 4]{\themynumber}}.
答案2
也许是最快(并且完全可扩展,如果这很重要)的解决方案,不需要额外的软件包:
\zeropad{<template>}{<integer>} % <template> string of zeros defining desired width of output
\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\zeropad[2]{%
\ifnum#2<0\relax%
{\ensuremath-}\zeropadA{#1}{\the\numexpr#2*-1\relax}%
\else%
\zeropadA{#1}{#2}%
\fi%
}
\def\zeropadA#1#2{%
\ifnum1#2<1#1
\zeropadA{#1}{0#2}%
\else%
#2%
\fi%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{mynumber}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{mynumber}{\themynumber}%
}%
}
\makeatother
\AtBeginDocument{%
\stepcounter{mynumber}
}
\begin{document}
the version number is \zeropad{0000}{\themynumber}
this is a negative number: \zeropad{0000}{-\themynumber}
expandibility check: \edef\foo{\zeropad{0000}{-\themynumber}}\foo
\end{document}
答案3
完全可扩展的版本
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\padinteger}{mm}
{
\int_compare:nNnTF { #2 } < { 0 }
{
- \kuttens_padinteger:nn { #1 } { -#2 }
}
{
\kuttens_padinteger:nn { #1 } { #2 }
}
}
\cs_new:Nn \kuttens_padinteger:nn
{
\prg_replicate:nn { #1 - \tl_count:f { \int_to_arabic:n { #2 } } } { 0 }
\int_to_arabic:n { #2 }
}
\cs_generate_variant:Nn \tl_count:n { f }
\ExplSyntaxOff
% define the macro based on the number of digits you need, here four
\makeatletter
\newcommand{\padfour}[1]{\expandafter\@padfour\csname c@#1\endcsname}
\newcommand{\@padfour}[1]{\padinteger{4}{#1}}
\makeatother
\newcounter{mynumber}
\renewcommand{\themynumber}{\padfour{mynumber}}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{mynumber}{\themynumber}%
}%
}
\makeatother
\AtBeginDocument{%
\stepcounter{mynumber}
}
\begin{document}
\section{The version number}
Number: \themynumber
\section{Other tests}
\newcounter{test}
\padinteger{4}{\value{test}}
\setcounter{test}{23}
\padinteger{4}{\value{test}}
%\padinteger{4}{12345} % would raise an error
\padinteger{8}{12345}
\setcounter{test}{-123}
$\padinteger{4}{\value{test}}$
\end{document}
答案4
@koleygr 谢谢!事实上,在你告诉我之前我就试过你的方法,但失败了。现在我找到了正确的方法。
\documentclass{article}
\usepackage{numprint}
\newcounter{mynumber}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{mynumber}{\number\value{mynumber}}%
}%
}
\makeatother
\AtBeginDocument{%
\stepcounter{mynumber}
}
\newcommand{\myversionnumber}{\npfourdigitnosep\nplpadding{4}\numprint{\themynumber}}
\begin{document}
the version number is \myversionnumber.
\end{document}