1

Hey' I'm trying to use the radians unit with siunitx package like so:

\newcommand{\rad}[1]{\SI{#1}{\radian}}
\rad{\dfrac{\pi}{3}}

and I get:

Package siunitx Error: Invalid token '\protect ' in numerical input. ^^I\rad{\dfrac{\pi}{3}}
Package siunitx Error: Invalid numerical input '\protect {\displaystyle(siunitx) {\begingroup \pi \endgroup \@@over \relax 3}}'. ^^I\rad{\dfrac{\pi}{3}}

I guess fractions aren't considered numerical inputs? What can I do about that?

1
  • 4
    \SI parses the first argument, and thus cannot handle abitrarily formatted input. You might want to try if \SI[parse-numers=false]{#1}{\radian} (untested). Also please post a full but minimal example that others can just copy and test instead of a sniplet link this. Here we have to guess and a lot of people does not bother and passes on the question. Others may try it but their choices for the rest of the code might be different than yours
    – daleif
    Commented Oct 29, 2020 at 15:26

1 Answer 1

4

siunitx v3 Update

In version 3 of siunitx fractions are no longer supported except as literal input (cf. this question). You now have to disable number parsing and typeset the fraction yourself.

\documentclass{article}

\usepackage{amsmath}
\usepackage{siunitx}

\begin{document}

\qty[parse-numbers=false]{\dfrac{\pi}{3}}{\radian}

\end{document}

MWE output

If you need to do this often, a helper macro is surely a good idea.

Original (v2) Answer

siunitx parses numbers for you. It also provides options for almost every aspect of how they are then formatted. You can just tell it to use \dfrac for quotients (cf. section 5.7 of the documentation).

\documentclass{article}

\usepackage{amsmath}
\usepackage{siunitx}

\sisetup{
  quotient-mode=fraction,
  fraction-function=\dfrac,
}

\begin{document}

\SI{\pi/3}{\radian}

\end{document}

MWE output

If you really want to, you can of course still use the \rad macro you defined.

5
  • You're missing \sisetup{input-digits = 0123456789\pi} in your example, for the pi symbol to display without error. Commented Feb 8, 2022 at 18:53
  • @MacroController Well, no, that will still throw an error in version 3 (and was not necessary in version 2). Thanks for the ping, though, I updated the answer for v3.
    – schtandard
    Commented Feb 8, 2022 at 21:01
  • You're welcome for the ping; and thanks for the answer / update of your answer. What's weird though, is that I added my comment specifically because I needed to print a pi in a \SI command. Specifically, \SI{\pi}{\radian}. I couldn't make it work without adding \sisetup{input-digits = 0123456789\pi} (hence my comment). I found that command in the package user manual (ctan.org/pkg/siunitx), p. 61/62, section 9.15. I use texlive-core v2021.61403-1 and texlive-latexextra v202161405-1 (plus a bunch of others) with pdflatex. Any thoughts ? Commented Feb 9, 2022 at 10:05
  • 1
    @MacroController Yes, all of that is absolutely correct (though you should use \qty instead of \SI with version 3). However, number fractions are no longer supported directly, so we have to disable parsing anyway which also means that we can use \pi without additional setup (we could use any math mode code there). In your example, you want \pi to be treated as a digit (with no fraction present) which is why you need input-digits.
    – schtandard
    Commented Feb 10, 2022 at 9:10
  • Thank you, @schtandard! I wasn't familiar enough with the package, so I was missing the \qty macro ^^ Commented Feb 11, 2022 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .