3

This is a follow-up question to "Notation and output in siunitx".

Consider the following code (courtesy of egreg):

\documentclass{article}

\usepackage{amsmath}
\usepackage[locale = DE]{siunitx}

\ExplSyntaxOn
\NewDocumentCommand\SIexpr{ O{,} m m }
 {
  \SIextension_siexpr:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Npn \SIextension_siexpr:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_SIextension_siexpr_input_seq { #1 } { #2 }
  \seq_pop_left:NN \l_SIextension_siexpr_input_seq \l_SIextension_siexpr_first_tl
  \seq_clear:N \l_SIextension_siexpr_output_seq
  \seq_put_right:Nx \l_SIextension_siexpr_output_seq
   {
    \fp_compare:nTF { \l_SIextension_siexpr_first_tl > 0 }
      { \num { \l_SIextension_siexpr_first_tl } }
      { - \num { \tl_tail:V \l_SIextension_siexpr_first_tl } }
   }
  \seq_map_inline:Nn \l_SIextension_siexpr_input_seq
   {
    \seq_put_right:Nx \l_SIextension_siexpr_output_seq
     {
      \fp_compare:nTF { ##1 > 0 }
        { + \num { ##1 } }
        { - \num { \tl_tail:n { ##1 } } }
     }
   }

  \SI[parse-numbers=false]
   {
    \sisetup{parse-numbers}
    ( \seq_use:Nn \l_SIextension_siexpr_output_seq { } )
   }
   { #3 }
}
\ExplSyntaxOff

\begin{document}

\begin{align*}
  l
  &= \SI{-38.0}{\cm} + 2 \cdot \SI{26.2}{\cm} + \SI{32.6}{\cm}\\
  &= \SIexpr{-38.0, 52.4, 32.6}{\cm}\\
%  &= \SIexpr{-\frac{38.0}{1}, 52.4, 32.6}{\cm}\\
  &= \SI{47.0}{\cm}.
\end{align*}

\end{document}

output

The code compiles just fine but assume I want a fraction as in the out-commented expression with automatic scaling of the parentheses.

How do I achieve that?

Update

I tried to change a single code line from

( \seq_use:Nn \l_SIextension_siexpr_output_seq { } )

to

\left( \seq_use:Nn \l_SIextension_siexpr_output_seq { } \right)

in order to get scaling parentheses but without luck; with or without the code change I get the following error when trying to use a fraction inside \SIexpr:

! Undefined control sequence.
<argument> \LaTeX3 error: 
                           Erroneous variable \protect used!
4
+50
\documentclass{scrartcl}

\usepackage{mathtools,mleftright}
\usepackage[locale = DE]{siunitx}

\ExplSyntaxOn
\NewDocumentCommand\SIexpr{ O{,} m m }
 {
  \SIextension_siexpr:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Npn \SIextension_siexpr:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_SIextension_siexpr_input_seq { #1 } { #2 }
  \seq_pop_left:NN \l_SIextension_siexpr_input_seq \l_SIextension_siexpr_first_tl
  \seq_clear:N \l_SIextension_siexpr_output_seq
  \seq_put_right:Nx \l_SIextension_siexpr_output_seq
   {
    \fp_compare:nTF { \l_SIextension_siexpr_first_tl > 0 }
      { \num { \l_SIextension_siexpr_first_tl } }
      { - \num { \tl_tail:V \l_SIextension_siexpr_first_tl } }
   }
  \seq_map_inline:Nn \l_SIextension_siexpr_input_seq
   {
    \seq_put_right:Nx \l_SIextension_siexpr_output_seq
     {
      \fp_compare:nTF { ##1 > 0 }
        { + \num { ##1 } }
        { - \num { \tl_tail:n { ##1 } } }
     }
   }

  \SI[parse-numbers=false,quotient-mode=fraction]
   {
    \sisetup{parse-numbers}
    \mleft( \seq_use:Nn \l_SIextension_siexpr_output_seq { } \mright)
   }
   { #3 }
}
\ExplSyntaxOff

\begin{document}

\begin{align*}
  l &= \SI{-38.0}{\cm} + 2 \cdot \SI{26.2}{\cm} + \SI{32.6}{\cm} \\
    &= \SIexpr{-38.0, 52.4/12, 32.6}{\cm} \\
    &= \SI{47.0}{\cm}.
\end{align*}

\end{document}

Gives no errors to me.

What did I do? I added the option quotient-mode=fraction to \SI[…] so you can use / to obtain a fraction.

By the way, I would load \usepackage{mleftright} and use \mleft( … \mright) rather than \left( … \right).

enter image description here

  • Not \ but /. – Manuel Oct 19 '14 at 12:50
  • Sorry. Of course! I actually did use / but simply forgot the option. Embarrassing that I missed that, since I use siunitx quite often. – Svend Tveskæg Oct 19 '14 at 12:52
  • May be it's “more correct” if you pass quotient-mode=fraction to \sisetup rather than \SI[…]. But I leave that to you. – Manuel Oct 19 '14 at 12:54
  • Btw. if you can make \frac{}{} work, that would be really cool, but I can do fine with the other solution! – Svend Tveskæg Oct 19 '14 at 13:05
  • I actually have another (trivial?) problem: How do I multiply two numbers? Using either * or \cdot gives me an error. – Svend Tveskæg Oct 20 '14 at 2:56

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.