[Tex/LaTex] creating newcommand on the own package

macrospackages

I'm sure I'm doing something wrong, but can't find what exactly.

Writing my thesis I created a package for my constants, they work OK:

\def \earthRadius {\num{6.371e6}\si{\metre}}
\def \lightSpeed {\num{2.99793e8}\si{\metre\per\second}}
\def \lightSpeedAprox {\num{3e8}\si{\metre\per\second}}

I have \num set to scientific by default, but stylewise I needed to add some decimal numbers at some places. So I decided to add a newcomand such as:

\newcommand{\thanum}[1]{\num[scientific-notation=false]{#1}}

The point is: I would like to put it in my package with the rest of my macros, but \latex doesn't seem to like it.
If I use the same command in the main file it works ok, but when I do it on the package it says:

! LaTeX Error: Command \thanum already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9 ...um}[1]{\num[scientific-notation=false]{#1}}

Same error both using TeXworks and ShareLatex. My approach for portability right now is to declare blank macros at the main.tex and renewcommand them on the package. It works, but I feel like the whole macro should be encapsulated on the package. What am I doing wrong?


I made a public project in sharelatex editable with the issue replicated: https://www.sharelatex.com/project/551003850f9d21382c0e5437

Best Answer

Do check and see if \thanum is inadertently being defined twice.

And, as Joseph Wright has already noted in a comment, you should be using \SI rather than \num and \si individually when typesetting combined numbers and units.

Also, consider using \newcommand rather than \def to define the shorthand macros. That way, you can define the macros to take an optional argument which, by default, is unused or empty. Doing so makes it easy, if necessary, to override some of the formatting settings "on the fly".

enter image description here

\documentclass{article}

\usepackage{siunitx}
\newcommand\earthRadius[1][]{%
    \SI[tight-spacing,#1]
    {6.371e6}{\metre}}
\newcommand\lightSpeed[1][]{%
    \SI[tight-spacing,per-mode=symbol,group-digits=false,#1]
    {2.99793e8}{\metre\per\second}}
\newcommand\lightSpeedApprox[1][]{%
    \SI[tight-spacing,per-mode=symbol,#1]
    {3e8}{\metre\per\second}}

\begin{document}
\renewcommand\arraystretch{1.25}
\begin{tabular}{ll}
\earthRadius      & \earthRadius[tight-spacing=false]\\
\lightSpeed       & \lightSpeed[group-digits=true]\\
\lightSpeedApprox & \lightSpeedApprox[per-mode=reciprocal]\\
\end{tabular}
\end{document}
Related Question