cleveref 计数器和 ref 标签数字格式,带填充零

cleveref 计数器和 ref 标签数字格式,带填充零

我正在使用cleverref自定义计数器来排版一种特殊的参考,其中我需要在定义端和参考端使用相同的文本。

目前我使用\textbf{F\arabic{thefeatures}}它来定义并 \crefformat{thefeatures}{#2\textbf{F#1}#3}格式化参考文献。

但是,我不想使用 ,而是F<number>想使用F<number padded by zeroes>

虽然我可以将fmtcount包用于定义端(并使用\padzeroes带有十进制输出的命令),但这对参考标签不起作用。

当前代码:

\documentclass{article}
\usepackage{cleveref}

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{
    \refstepcounter{thefeatures}
    \label{#1}
    \textbf{F\arabic{thefeatures}}
}

\crefformat{thefeatures}{#2\textbf{F#1}#3}


\begin{document}

\deffeat{test} feature test


\deffeat{test2} feature test2


\Cref{test} test ref

\end{document}

答案1

手动方法:

\textbf{%
  F%
  \ifnum\value{thefeatures}<1000 0\fi
  \ifnum\value{thefeatures}<100 0\fi
  \ifnum\value{thefeatures}<10 0\fi
  \arabic{thefeatures}%
}%

适合您的一种可能的实施方式是

\documentclass{article}
\usepackage{cleveref}

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{%
  \refstepcounter{thefeatures}%
  \label{#1}%
  \printfeat{\arabic{thefeatures}}%
}

\crefformat{thefeatures}{#2\protect\printfeat{#1}#3}

\newcommand{\printfeat}[1]{%
  \textbf{%
    F%
    \ifnum\value{thefeatures}<1000 0\fi
    \ifnum\value{thefeatures}<100 0\fi
    \ifnum\value{thefeatures}<10 0\fi
    #1%
  }%
}

\begin{document}

\deffeat{test} feature test


\deffeat{test2} feature test2


\Cref{test} test ref

\end{document}

注意定义中的虚假空格。

答案2

我使用 xstrings 包解决了这个问题,感谢使用 xstring 包控制填充

我使用的 pad 命令的代码是

\newcommand\padfrom[1]{%
\StrLen{#1}[\templen]%
\StrGobbleRight{000}{\templen}%
#1}

相关内容