5

The siunitx package provides a way of formatting (SI) units. It has a plethora of internal settings that the user can set with \sisetup. For example,

\documentclass{article}
\usepackage{siunitx}
\begin{document}
  \sisetup{round-precision=2, round-mode=places, round-half=up}
\end{document}

will cause \num{4.485} to print 4.49.

My question is, how can I extract the current settings for siuntx? For example, above round-precision is set to 2. How can I get siunitx to tell me this?

(The siunitx package is written using expl3 and I do not understand the key system it uses...)

  • If you say round-precision=2, what's the purpose of making siunitx to tell you what you already know? However, the number is stored in the integer variable \l__siunitx_process_precision_int (the name tells it's “private”). – egreg May 16 '15 at 22:43
  • \keys_show:nn {siunitx}{round-precision} will show you that it does \int_set:Nn \l__siunitx_process_precision_int {#1} (or you look in siunitx.sty). – Qrrbrbirlbel May 16 '15 at 22:54
  • @egreg @Qrrbrbirlbel Sure, in the MWE I know what the settings are but in general I won't as I want to import the current siunix settings into some other code. From reading siunitx.sty I'd guessed that round-precision is stored in \l__siunitx_process_precision_int but I can't see how to access this: using \l__siunitx_process_precision_int gives me a "Missing number, treated as zero." error. Are local expl3 variables inaccessible outside of their namespace? (I wouldn't have thought so since tex does not have namespaces...) – user30471 May 16 '15 at 23:38
  • @Andrew I can access it; probably you tried it without saying \ExplSyntaxOn (the expl3 analog of \makeatletter). However, as I said, the variable is “private”, so it's not guaranteed to be used in future releases. – egreg May 16 '15 at 23:41
  • 3
    @Andrew Not \the\.. better \int_use:N \l__siunitx_process_precision_int. – Manuel May 17 '15 at 1:10
2

With LuaLaTeX you can parse your imported file and access the siunitx parameters, and even resuse them :

enter image description here

\documentclass{article}
\usepackage{luacode,siunitx}

\begin{luacode}
function SiunitxSplit ( file )
    local param
    local i = 1

    local f = assert(io.open(file, "r"))
    local t = f:read("*all")
    f:close()
    for param in t:gmatch ("\\sisetup(%b{})")
    do
        param = param:sub(2,-2)
        tex.print (i.."--"..param..":\\\\")
        param = param:gsub("%s","")
        param = param:gsub("([^,]+)",
            function (s)
            s=s:gsub("([^=]+)=([^=]+)" , "%1 is : %2")
            return s
            end
            )
        param = param:gsub(",","\\\\")
        tex.print (param.."\\paragraph{}")
        i = i + 1
    end
end

function UseSiunitxSettings ( nb , file )
    local param
    local list = {}
    local f = assert(io.open(file, "r"))
    local t = f:read("*all")
    f:close()
    for param in t:gmatch ("\\sisetup(%b{})")
    do
        param = param:sub(2,-2)
        table.insert (list, param)
    end
    tex.print ("\\sisetup{"..list[nb].."}" )
end
\end{luacode}

\newcommand{\SiunitxSplit}[1]{%
    \directlua{SiunitxSplit("#1")}
}

\newcommand{\UseSiunitxSettings}[2][1]{%
    \directlua{UseSiunitxSettings(#1 , "#2")}
}


\begin{document}

\SiunitxSplit{bob.tex}

\UseSiunitxSettings{bob.tex}

\num{3.14159}

\UseSiunitxSettings[2]{bob.tex}

\num{3.14159}

\end{document}

and bob.tex contains as part :

\sisetup{round-precision=2, round-mode=places, round-half=up}
\sisetup{round-precision=3, round-mode=places, round-half=up}
  • I don't use LuaTex but at least this answers my question:) – user30471 Aug 5 '15 at 13:42
4

As observed in comments, the key settings are (largely) stored in a set of variables which are marked as private in siunitx. TeX doesn't prevent you accessing these (they are private by convention rather than at any technical level), so you can read the code and then extract the values. However, there is a reason this data is private: there is no documented interface for accessing the data and there should be no reason to access it.

  • One thing I wonder about is whether all key values should be stored systematically. With something like a .tl_set:N key there is a place to get the 'value', but that's not the case for a choice or whatever. it's at least arguable that the back end should track all key values. – Joseph Wright Aug 2 '15 at 9:43
  • Thanks for taking time to answer. The reason that I asked the question was that I was writing code that used siunitx and I wanted to be able to extract the siunitx settings for use in my code. I eventually realised that my approach was misguided and I found a better way off doing what I wanted. I am still interested in the answer, however, because Manuel's suggestion in the comments to use \int_use:N \l__siunitx_process_precision_int doesn't always work...of course, I have forgotten the example I found which I accept may well have been due to user error. – user30471 Aug 3 '15 at 6:58

Your Answer

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