我怎样才能告诉 int_to_bin 我想要多少个前导零?
例子:
\documentclass{standalone}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new:Npn \displayasbinary #1 {0b\int_to_bin:n{#1}}
\ExplSyntaxOff
\begin{document}
\displayasbinary{3}, but should be: 0b0011
\end{document}
答案1
作为\int_to_bin:n
“编程”函数,它不进行填充。我们可以通过计算转换后的输出中的标记,然后进行足够的0
填充来添加它:
\documentclass{standalone}
\ExplSyntaxOn
\cs_new:Npn \hk_int_to_bin:nn #1#2
{
\exp_args:Ne \__hk_int_to_bin:nn { \int_to_bin:n {#2} } {#1}
}
\cs_new:Npn \__hk_int_to_bin:nn #1#2
{
0b
\prg_replicate:nn { \int_max:nn { 0 } { (#2) -\tl_count:n {#1} } } { 0 }
#1
}
\NewExpandableDocumentCommand \displayasbinary { O{4} m }
{ \hk_int_to_bin:nn {#1} {#2} }
\ExplSyntaxOff
\begin{document}
\displayasbinary{3}, but should be: 0b0011 or \displayasbinary[10]{3}
or \displayasbinary[0]{3}
\end{document}