siunitx 中的离散范围/间隔

siunitx 中的离散范围/间隔

使用siunitx,我可以输入$V = \SIrange[range-phrase=\text{ to },range-units=single]{100}{125}{\kilo\m\per\hour}$来获取

在此处输入图片描述

然而,从数值上来说,我必须指定范围的步骤才能准确知道我将进行计算的每个速度值。

因此,我想了解如何创建一个命令,例如\SIsteps,它接受以下参数:

<start>

<step>(可选,默认值为1)可以是整数或浮点数,

<size>(可选)它是范围/间隔内元素的总数

<end>, 和

<unit>

因此,例如,以下所有代码应具有相同的输出

在此处输入图片描述

The speed range is $V = \SIsteps[step=5]{100}{125}{\km}$

The speed range is $V = \SIsteps[size=6]{100}{}{\km}$

The speed range is $V = \SIsteps[size=6]{}{125}{\km}$

\documentclass{article}
\usepackage{unicode-math,siunitx}
\sisetup{
    load-configurations = abbreviations,
    inter-unit-product = \ensuremath{{\hspace{-0.5ex}}\cdot{\hspace{-0.5ex}}}
}
\setmathfont{Cambria Math}
\begin{document}
The speed range is $V = \left\{100,105,\ldots,125\right\}~\si{\km\per\hour}$
\end{document}

答案1

\SIsteps

  1. 要传递给的选项的可选参数\num
  2. 带有步骤选项的强制参数
  3. 带有选项的可选参数\si
  4. 该单位的强制性论点

正如您所要求的,默认步长为 1,但可以在选项中指定,也可以(遵循正常范围规则)使用 指定\stepsetup

关键是

  • start设置起点
  • end设置终点
  • step设置步骤
  • size设置要显示的项目数
  • full指定我们想要完整显示值(默认为 false)
  • sep指定项目之间的分隔符(默认逗号)

最好在startend和中指定两个键size,但不要指定全部三个键。

\documentclass{article}
\usepackage{siunitx,xfp}

\sisetup{
    load-configurations = abbreviations,
    inter-unit-product = \ensuremath{{\cdot}}
}


\ExplSyntaxOn

\keys_define:nn { diaa/steps }
 {
  start .fp_set:N   = \l__diaa_steps_start_fp,
  start .initial:n  = -\c_inf_fp,
  end   .fp_set:N   = \l__diaa_steps_end_fp,
  end   .initial:n  = \c_inf_fp,
  step  .fp_set:N   = \l__diaa_steps_step_fp,
  step  .initial:n  = 1,
  size  .int_set:N  = \l__diaa_steps_size_int,
  full  .bool_set:N = \l__diaa_steps_full_bool,
  full  .initial:n  = false,
  full  .default:n  = true,
  sep   .tl_set:N   = \l__diaa_steps_sep_tl,
  sep   .initial:n  = {,},
 }

\NewDocumentCommand{\SIsteps}{O{}mO{}m}
 {
  \group_begin:
  \keys_set:nn { diaa/steps } { #2 }
  \int_compare:nT { \l__diaa_steps_size_int > 0 }
   {% size has been specified
    \fp_compare:nTF { \l__diaa_steps_end_fp = \c_inf_fp }
     {% end has not been specified
      \fp_set:Nn \l__diaa_steps_end_fp
       {
        \l__diaa_steps_start_fp + (\l__diaa_steps_size_int-1)*\l__diaa_steps_step_fp
       }
     }
     {% start has not been specified
      \fp_set:Nn \l__diaa_steps_start_fp
       {
        \l__diaa_steps_end_fp - (\l__diaa_steps_size_int-1)*\l__diaa_steps_step_fp
       }
     }
   }
  \ensuremath
   {
    \{
    \bool_if:NTF \l__diaa_steps_full_bool
     {% full display
      \fp_step_inline:nnnn
       { \l__diaa_steps_start_fp } % start
       { \l__diaa_steps_step_fp } % step
       { \l__diaa_steps_end_fp - \l__diaa_steps_step_fp } % end
       {
        \num[#1]{ \fp_eval:n { ##1 } } \l__diaa_steps_sep_tl
       }
       \num[#1]{ \fp_eval:n { \l__diaa_steps_end_fp } }
     }
     {% short display
      \num[#1]{ \fp_eval:n { \l__diaa_steps_start_fp } }
      \l__diaa_steps_sep_tl
      \num[#1]{ \fp_eval:n { \l__diaa_steps_start_fp + \l__diaa_steps_step_fp } }
      \l__diaa_steps_sep_tl
      \dots\l__diaa_steps_sep_tl
      \num[#1]{ \fp_eval:n { \l__diaa_steps_end_fp } }
     }
    \}
    \,\si[#3]{#4}
   }
  \group_end:
 }
\NewDocumentCommand{\stepsetup}{m}
 {
  \keys_set:nn { diaa/steps } { #1 }
 }

\ExplSyntaxOff

\begin{document}

The speed range is $V = \SIsteps{step=5,start=100,end=125}{\km\per\hour}$

The speed range is $V = \SIsteps{size=6,start=100}{\km\per\hour}$ % step is one

The speed range is $V = \SIsteps{size=6,start=100,step=5}{\km\per\hour}$

\stepsetup{step=5}

The speed range is $V = \SIsteps{size=6,end=125}{\km\per\hour}$

The speed range is $V = \SIsteps{size=6,end=125,full,sep={;}}{\km\per\hour}$

\end{document}

在此处输入图片描述

\,括号和单位之间的显式可能是\l__siunitx_number_unit_product_tl,但这是内部的siunitx,使用它是不好的做法。将来可能会将其作为用户级令牌列表提供。

您还可以添加对角度的支持(但不是分钟和秒的支持,因为那样您会被收取一千次费用)。

\documentclass{article}
\usepackage{siunitx,xfp}

\sisetup{
    load-configurations = abbreviations,
    inter-unit-product = \ensuremath{{\cdot}}
}


\ExplSyntaxOn

\keys_define:nn { diaa/steps }
 {
  start .fp_set:N   = \l__diaa_steps_start_fp,
  start .initial:n  = -\c_inf_fp,
  end   .fp_set:N   = \l__diaa_steps_end_fp,
  end   .initial:n  = \c_inf_fp,
  step  .fp_set:N   = \l__diaa_steps_step_fp,
  step  .initial:n  = 1,
  size  .int_set:N  = \l__diaa_steps_size_int,
  full  .bool_set:N = \l__diaa_steps_full_bool,
  full  .initial:n  = false,
  full  .default:n  = true,
  sep   .tl_set:N   = \l__diaa_steps_sep_tl,
  sep   .initial:n  = {,},
 }

\NewDocumentCommand{\SIsteps}{O{}mO{}m}
 {
  \group_begin:
  \diaa_sisteps:nnnnNn { #1 } { #2 } { #3 } { #4 }
                       \num 
                       { \l__siunitx_number_unit_product_tl \si[#3]{#4} }
  \group_end:
 }
\NewDocumentCommand{\anglesteps}{O{}mO{}}
 {
  \group_begin:
  \diaa_sisteps:nnnnNn { #1 } { #2 } { #3 } { }
                       \ang 
                       { }
  \group_end:
 }

\cs_new_protected:Nn \diaa_sisteps:nnnnNn
 {
  \keys_set:nn { diaa/steps } { #2 }
  \int_compare:nT { \l__diaa_steps_size_int > 0 }
   {% size has been specified
    \fp_compare:nTF { \l__diaa_steps_end_fp = \c_inf_fp }
     {% end has not been specified
      \fp_set:Nn \l__diaa_steps_end_fp
       {
        \l__diaa_steps_start_fp + (\l__diaa_steps_size_int-1)*\l__diaa_steps_step_fp
       }
     }
     {% start has not been specified
      \fp_set:Nn \l__diaa_steps_start_fp
       {
        \l__diaa_steps_end_fp - (\l__diaa_steps_size_int-1)*\l__diaa_steps_step_fp
       }
     }
   }
  \ensuremath
   {
    \{
    \bool_if:NTF \l__diaa_steps_full_bool
     {% full display
      \fp_step_inline:nnnn
       { \l__diaa_steps_start_fp } % start
       { \l__diaa_steps_step_fp } % step
       { \l__diaa_steps_end_fp - \l__diaa_steps_step_fp } % end
       {
        #5[#1]{ \fp_eval:n { ##1 } } \l__diaa_steps_sep_tl
       }
       #5[#1]{ \fp_eval:n { \l__diaa_steps_end_fp } }
     }
     {% short display
      #5[#1]{ \fp_eval:n { \l__diaa_steps_start_fp } }
      \l__diaa_steps_sep_tl
      #5[#1]{ \fp_eval:n { \l__diaa_steps_start_fp + \l__diaa_steps_step_fp } }
      \l__diaa_steps_sep_tl
      \fp_compare:nT
       { (\l__diaa_steps_end_fp-\l__diaa_steps_start_fp)/\l__diaa_steps_step_fp > 3 }
       { \dots\l__diaa_steps_sep_tl }
      #5[#1]{ \fp_eval:n { \l__diaa_steps_end_fp } }
     }
    \}
    #6
   }
 }
\NewDocumentCommand{\stepsetup}{m}
 {
  \keys_set:nn { diaa/steps } { #1 }
 }

\ExplSyntaxOff

\begin{document}

The speed range is $V = \SIsteps{step=5,start=100,end=125}{\km\per\hour}$

The speed range is $V = \SIsteps{size=6,start=100}{\km\per\hour}$ % step is one

The speed range is $V = \SIsteps{size=6,start=100,step=5}{\km\per\hour}$

The speed range is $V = \SIsteps{size=6,start=84.37,step=5}{\km\per\hour}$

\stepsetup{step=5}

The speed range is $V = \SIsteps{size=6,end=125}{\km\per\hour}$

The speed range is $V = \SIsteps{size=6,end=125,full,sep={;}}{\km\per\hour}$

The speed range is $V = \SIsteps{step=5,start=20,end=30}{\km\per\hour}$

\stepsetup{step=1}

Angle ranges:

\anglesteps{step=5,start=100,end=125}

\anglesteps{size=6,start=100}

\anglesteps{size=6,start=100,step=5}

\anglesteps{size=6,start=84.37,step=5}

\stepsetup{step=5}

\anglesteps{size=6,end=125}

\anglesteps{size=6,end=125,full,sep={;}}

\anglesteps{step=5,start=20,end=30}

\end{document}

在此处输入图片描述

siunitx版本 3更新

load-configurations选项不再有效,并且第一个代码的工作方式与版本 2 相同。

对于第二段代码,将其替换\l__siunitx_number_unit_product_tl\l__siunitx_quantity_product_tl(似乎还没有接口)。

此外\si还应该变成\unit和,为了统一,\SIsteps或许应该改名为\qtysteps

相关内容