[Tex/LaTex] define a new unit that behaves like \ang in siunitx

siunitx

The siunitx package provides support for "astronomy style" placement of the degree symbol using the angle-symbol-over-decimal switch, and for simple entry of degree-minutes-second values using the form

\ang{6;7;6.5}

Is there a way to used the package's \DeclareSIUnit macro to define a corresponding formatting for right ascension so that, for example, one could use something like

\ra[ra-symbol-over-decimal]{5.237}
\ra{5.237} 
\ra{5;15;6.5} 

to produce (roughly)

enter image description here

with spacing and other behavior that follows the packages current settings for \ang?

Best Answer

Reading the siunitx manual ( 5.15 “Symbols”, pp. 62/63) one can find two ways to achieve this:

  1. Redefinition of \SIUnitSymbolDegree, -Arcminute and -Arcsecond
  2. Using the options text-/math-degree, -arcminute and -arcsecond ( Table 43, p. 63)

For both cases applies:

The new macro \ra takes two arguments:

  • one optional that gets passed to \ang and
  • one mandatory (the angle itself).

All “arc format” options (angle-symbol-over-decimal, arc-separator, …) can be given to \ra, too.

Solution 1

\newcommand*{\ra}[2][]{{% extra pair of braces to keep the \def-intion local!
    \def\SIUnitSymbolDegree{\textsuperscript{h}}%
    \def\SIUnitSymbolArcminute{\textsuperscript{m}}%
    \def\SIUnitSymbolArcsecond{\textsuperscript{s}}%
    \ang[#1]{#2}}%
}

Solution 2

\newcommand*{\ra}[2][]{%
    \ang[
        math-degree=\textsuperscript{h},
        text-degree=\textsuperscript{h},
        math-arcminute=\textsuperscript{m},
        text-arcminute=\textsuperscript{m},
        math-arcsecond=\textsuperscript{s},
        text-arcsecond=\textsuperscript{s},
        #1]{#2}%
}

Code

\documentclass{article}
\usepackage{siunitx}

\newcommand*{\ra}[2][]{{% extra pair of braces for solution 1, doesn't hurt for solution 2
%   \def\SIUnitSymbolDegree{\textsuperscript{h}}%     Solution 1
%   \def\SIUnitSymbolArcminute{\textsuperscript{m}}%  Solution 1
%   \def\SIUnitSymbolArcsecond{\textsuperscript{s}}%  Solution 1
    \ang[
        math-degree=\textsuperscript{h},             % Solution 2
        text-degree=\textsuperscript{h},             % Solution 2
        math-arcminute=\textsuperscript{m},          % Solution 2
        text-arcminute=\textsuperscript{m},          % Solution 2
        math-arcsecond=\textsuperscript{s},          % Solution 2
        text-arcsecond=\textsuperscript{s},          % Solution 2
        #1]{#2}%
}}
\begin{document}
\ra[angle-symbol-over-decimal]{5.237}   \par
\ra{5.237}        \par \ra{5;15;6.5}    \par \vspace{2ex}
\ang{2.3;3.4;4.5} \par \ra{2.3;3.4;4.5} \par \ang{2.3;3.4;4.5} \par
\ra[angle-symbol-over-decimal,arc-separator=\,]{2.3}           \par \vspace{2ex}
$\ra[angle-symbol-over-decimal]{5.237} \quad \ra{5.237} \quad \ra{5;15;6.5}$
\end{document}

Output

Output