Time of the day or time period using the package siunitx
I've been asked about this a few times. It's an awkward one, as the format people want ('18 h 30 min', for example) is really not a single idea for expressing a time in scientific work. You'd normally want to express in one unit, so in either hours or minutes, but not both. That said, it's not too hard to construct a function \hms
which works in a similar way to \ang
(the multiple-unit argument applies to the later too!). This needs a code test as well as \IfNoValueTF
, which is a little awkward for end users, but something like
\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn
\NewDocumentCommand \hms { o > { \SplitArgument { 2 } { ; } } m }
{
\group_begin:
\IfNoValueF {#1}
{ \keys_set:nn { siunitx } {#1} }
\siunitx_hms_output:nnn #2
\group_end:
}
\cs_new_protected:Npn \siunitx_hms_output:nnn #1#2#3
{
\IfNoValueF {#1}
{
\tl_if_blank:nF {#1}
{
\SI {#1} { \hour }
\IfNoValueF {#2} { ~ }
}
}
\IfNoValueF {#2}
{
\tl_if_blank:nF {#2}
{
\SI {#2} { \minute }
\IfNoValueF {#3} { ~ }
}
}
\IfNoValueF {#3}
{ \tl_if_blank:nF {#3} { \SI {#3} { \second } } }
}
\ExplSyntaxOff
\begin{document}
\hms{10}\\
\hms{;10}\\
\hms{;;10}\\
\hms{10;10}\\
\hms{10;;10}\\
\hms{10;10;10}\\
\end{document}
should do what you want. I've included an optional first argument for local set up in the usual siunitx
way.