2

I want to write this command below in a new shorter command

\numrange[⟨options⟩]{⟨numbers⟩}{⟨number2⟩}

I want to write it like

\nums{number - number}

or anything similar

for example, \numrange[⟨options⟩]{20}{30} and the new command will be like \nums{20-30}

output: 20-30

dexteritas
  • 9,161
TThoye
  • 53

2 Answers2

6

You can use the argument processors built into ltcmd/xparse:

\documentclass{article}

\NewDocumentCommand \nums { O{} >{\SplitArgument{1}{-}}m } {\numrange[{#1}]#2}

\newcommand\numrange[3][]{from #2 to #3}

\begin{document} \nums{3-10} \end{document}

enter image description here

If you're indeed using siunitx you could consider using this instead:

\documentclass{article}

\usepackage{siunitx}

\ExplSyntaxOn \NewDocumentCommand \nums { O{} >{\SplitArgument{1}{-}}m } { \group_begin: \keys_set:nn { siunitx } {#1} \siunitx_number_range:nn #2 \group_end: } \ExplSyntaxOff

\begin{document} \nums{3-10} \end{document}

Skillmon
  • 60,462
  • 3
    Quite likely, the OP is using \numrange from siunitx. – egreg Dec 08 '22 at 15:18
  • 4
    @egreg likely yes... But do we know? No. Does it matter for the answer? Also no. :) – Skillmon Dec 08 '22 at 15:20
  • 1
    Well yes, as if it's using siunitx v3 there is a code-level API that new document commands should be using ;) – Joseph Wright Dec 08 '22 at 15:24
  • @JosephWright which isn't documented in siunitx.pdf :P Imho, you should include the expl3-level in the documentation (I know there is siunitx-code.pdf. Btw. what would be the correct way to set the options? Via \sisetup?) – Skillmon Dec 08 '22 at 15:32
  • @Skillmon I can add a link or comment, but adding the entire programmers API docs seems overkill. I'd use \keys_set:nn { siunitx } at the code level,; there's not really document level way that fits into what LaTeX itself documents (\begingroup/\endgroup are not after all in Lamport's book). – Joseph Wright Dec 08 '22 at 15:39
  • @Skillmon Huh? That's the documented interface in siunitx-code.pdf; like I said, the issue with using \sisetup in a document-level command is that there is no documented group command in LaTeX, only environments. – Joseph Wright Dec 08 '22 at 15:43
  • Oh, see that's what happens if one searches for too specific stuff using a search function :P – Skillmon Dec 08 '22 at 15:45
3

IMHO, \def primitive is the right tool for solving your issue.

\def\nums #1{\numsA #1;}
\def\numsA #1-#2;{from #1 to #2}

test: \nums{3-10}

wipet
  • 74,238