josephwright / josephwright/siunitx

Expose a list of prefixes and their multiplicator (and units)

Open
#391 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TeX
Stars
407
Forks
29
Avg merge
11h 20m
Merged PRs (30d)
3

Description

TLDR: Have you considered exposing a list of all prefixes and their respective multiplier to the user/package authors? Furthermore have you considered doing the same for composite units?

For university I would like to be able to evaluate simple calculation inside latex with units. Using sympy or similar would definitely work but more annoying to setup and the documents would be harder to share to colleagues. I already figured out a small proof of concept though currently I need to declare each prefix manually. This then allows me to write something like:

\SIeval{\SI{240}{\kilo\meter} + \SI{40}{\meter}} % compiles to 240040

Currently I am working on detecting \per in the unit so they are able to cancel each other out. Afterwards I can try to accumulate how often each base unit occured. The problem is that we do calculations with in a large variety of sizes and also a lot of different units... Therefore declaring all prefixes and units at the start is cumbersome and error prone.

While such a functionality (limited calculation) is probably out of the scope for this package it would already be very nice if it would be possible to expose the list of all prefixes, their respective multiplier and units should they just be an alias to a composition of base units.

Full proof of concept
% !TeX TS-program = lualatex
\documentclass{article}
\usepackage{xparse}
\usepackage{xfp}
\usepackage{siunitx}
\sisetup{binary-units}

\ExplSyntaxOn
\NewDocumentCommand{\definefpconstant}{mm}
 {
  \fp_const:Nn #1 { #2 }
 }
\NewDocumentCommand{\SIeval}{ o m }{
  {
    \RenewDocumentCommand \SI { m m } { ##1 ##2 }
    \cs_set:Npn \kilo { * 10**3 }
    \cs_set:Npn \mega { * 10**6 }
    \cs_set:Npn \giga { * 10**9 }
    \cs_set:Npn \byte { * 8 }
    \cs_set:Npn \meter {}

    \fp_set:Nn \l_tmpa_fp { #2 }
    \IfValueT { #1 } {\cs_new:Npx #1 { \fp_use:N \l_tmpa_fp }}
    \fp_use:N \l_tmpa_fp
  }
}
\ExplSyntaxOff

\listfiles

\begin{document}

\definefpconstant{\constantA}{0.25}
\definefpconstant{\constantB}{1-\constantA}
\definefpconstant{\constantC}{round(sqrt(\constantA),2)+\constantB/2}

\begin{itemize}
  \item The value of \verb|\constantA| is \fpeval{\constantA}
  \item The value of \verb|\constantB| is \fpeval{\constantB}
  \item The value of \verb|\constantC| is \fpeval{\constantC}
  \item \fpeval{round(\constantA-\constantC+exp(2),4)}
  \item[]
  \item \SIeval{\SI{240}{\kilo\meter} + \SI{40}{\meter}}
  \item \SIeval[\result]{\SI{240}{\kilo\meter} + \SI{\constantB}{\meter}}
  \item \fpeval{\result}
  \item \SI{\result}{\meter}
  \item[]
  \item \SI{250}{\kilo\byte}
  \item \SI{9000}{\percent}
\end{itemize}

\end{document}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the package's prefix and unit definitions, using the proof of concept's \SIeval and \sisetup{binary-units} examples as the intended consumer context. Determine how prefixes expose their multipliers and how composite or aliased units are represented. Done means package authors can access both lists without manually redeclaring entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
latex
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.