# Retain Leading Zero in an Integer with Siunitx \num macro

I would like to be able to retain a leading zero with the siunitx package. That is using \num{012345} produces 12 345 but I would like to have 012 345.

## Code:

\documentclass{article}
\usepackage{siunitx}

\begin{document}

Default Output: $012345 = \num{012345}$

Desired Output: $012345 = 012\,345$

\end{document}

• One thing that came to mind now as I posted the question is to to typset the number as if it is a decimal number but suppress the decial point as well as the leading zero. Jun 4, 2018 at 6:20
• Would a LuaLaTeX-based solution be of interest to you?
– Mico
Jun 4, 2018 at 7:02
• @Mico: Not to me personally right now (but perhaps in the future). I think it always useful to have different ways of doing things and may help others. Feel free to post. Even a pgfprintnumber option would be useful. I have a solution working based on my above comment and will post a solution if no one else does. Jun 4, 2018 at 7:07
• What's your working assumption regarding the mininum number of digits needed to perform grouping by triples? Should grouping be performed as if the option group-minimum-digits=4 were in place, or do you prefer the default, viz., group-minimum-digits=5?
– Mico
Jun 4, 2018 at 8:18
• @Mico: Looks like you provided a parameter for this, but I prefer the 4 (which is what I thought the default was). Jun 4, 2018 at 22:39

(modified the answer to allow setting the equivalent of the group-minimum-digits parameter of the siunitx package.

Here's a LuaLaTeX-based solution. It defines a user-level LaTeX macro called \Lnum, which acts as a front end to a Lua function called group_string. This Lua function does virtually all of the work.

The equivalent of the group-minimum-digits parameter of the siunitx package -- generally, either 5 or 4 -- may be set in the optional argument of the \Lnum macro.

% !TEX TS-program = lualatex

\documentclass{article}
\usepackage{luacode}

%% Lua-side code: define a Lua function called 'group_string'
\begin{luacode}

function group_string ( s , n )
-- s: Input string -- usually, an integer.
-- n: Minimum number of digits before grouping
--    is performed. This should be either 4 or 5.
-- If s contains n or fewer characters, do nothing,
--    i.e., return the string s without modifications.
-- Otherwise, insert thinspace into input string at
--    every third character, starting from the right.
-- Note: Leading zeros are preserved automatically.

if string.len ( s ) < n then
return s -- don't modify input string
else
t = ""   -- initialize output string
while string.len ( s ) > 3 do
t =   "\\," .. string.sub ( s, -3 ) .. t
s = string.sub ( s , 1, -4 ) -- drop last 3 chars
end
return s .. t
end
end

\end{luacode}

%% LaTeX-side code: A user macro called "\Lnum".
%% Default value of optional argument of \Lnum
%%   should be either 4 or 5.
\newcommand\Lnum[2][4]{\directlua{%
tex.sprint ( group_string ( "#2" , #1 ) )}}

\begin{document}
\begin{tabular}{rr}
input   & output         \\[1ex]
12345   & \Lnum{12345}   \\
012345  & \Lnum{012345}  \\
0000000 & \Lnum{0000000} \\
0123    & \Lnum{0123}    \\
0123    & \Lnum[5]{0123} \\ % set equivalent of 'group-minimum-digits' to 5
\end{tabular}
\end{document}