3

I work a lot with the siunitx package and I've got many numbers with a high decimal precision. Now I'd like to be able to just copy these numbers in full precision in the relevant positions in text, equations and tables and adjust the precision in the preamble of the document.

Is there any way to pass options to commands by predefining the option string? I can easily make it work when the string it is not used as a command option, but for command options it is not working...

My minimal example:

\documentclass[10pt,a4paper,twosided]{article}
\usepackage{siunitx}
\newcommand{\rndprc}{round-mode=places,round-precision=4}%    command option string
\newcommand{\Tk}{\Theta}%    this works...

\begin{document}        
    \paragraph{testing commands...}
    \num[\rndprc]{12.34567890123}

    This is working: $\Tk$ 

\end{document}

Any idea how I can make this work? Best case would be to have something like \newcommand{\rndprc}[1]{round-mode=places,round-precision={#1}}, so I can enter the precision in place.

4

Macros aren't expanded by the key-val parser. You can, instead of a macro, define a .meta key which sets the options you want in one go. You can also give an argument and a default value to the key:

\documentclass[10pt,a4paper,twosided]{article}
\usepackage{siunitx}
\ExplSyntaxOn
\keys_define:nn { siunitx }
  {
    rndprc .meta:n =
      {
        round-mode = places,
        round-precision = #1,
      },
    rndprc .default:n = 4,
  }
\ExplSyntaxOff
\newcommand{\Tk}{\Theta}

\begin{document}
\paragraph{testing commands...}
\num[rndprc]{12.34567890123}

\num[rndprc=3]{12.34567890123}

This is working: $\Tk$
\end{document}
  • Thanks for your answer. Is this also working with an additional argument to locally set the precision? – Scotty1- Apr 30 at 13:46
  • @Scotty1- Yes, it is :) I expanded the answer. – Phelype Oleinik Apr 30 at 13:47
  • Awesome and thanks alot! – Scotty1- Apr 30 at 13:49
1

Locally change the sisetup:

\documentclass[10pt,a4paper,twosided]{article}
\usepackage{siunitx}

\newcommand{\rndprc}[1][4]{%
    \sisetup{round-mode=places,round-precision=#1}%  # command option string
}
\newcommand{\Tk}{\Theta}%  # this works...

\begin{document}        
    \paragraph{testing commands...}
    {\rndprc\num{12.34567890123}}

    {\rndprc[3]\num{12.34567890123}}

    This is working: $\Tk$ 

\end{document}
  • Thanks. Is there also any possibility to pass the command to the options of \num, as in \num[\rndprc[3]]{12.34567890123}? Prepending the sisetup each time within brackets seems like a workaround. – Scotty1- Apr 30 at 13:43
  • @Scotty1- No, this has to be done outside. – user187808 Apr 30 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.