I often embed some elementary computations directly into my latex source via pythontex. I follow the convention that I give all numbers in my script in SI-units (eg. seconds not nanoseconds or kilogram and not gram etc.). From my latex document I can pass variables defined or calculated in my script to siunitx and print it via a command called \pySI:

\DeclareDocumentCommand{\pySI}{ O{} m m O{}}
\py[#1]{'\\SI[#4]{' + str(#2).replace('(','').replace(')','') + r'}{#3}'}%

Now suppose I have a variable l in my python script (for a length in meter) with the value l = 2.5e-3. In the main document however I want the number to be printed in millimeter, then I call it like \pySI{l*1e3}{\mm}. This is a bit inconvenient. So I am looking for a way of how to make siunitx to recognize the unit prefix \milli such that \pySI{l}{\mm} or \pySI{l}{\mili\meter} typesets 2.5 mm. This should work for all prefixes for example \pySI{l}{\m} should print 0.0025 m (or in scientific notation depending on the si settings) or \pySI{l}{\cm} should print 0.25 cm. It should also work for all units defined within siunitx.

  • 3
    I guess I'll ask python do the job (simply multiply by a power of ten, according to the second mandatory argument). – NBur Apr 24 '18 at 7:26
  • Please first provide an MWE. – Troy Apr 26 '18 at 12:14

Your Answer

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

Browse other questions tagged or ask your own question.