在自己的包上创建新命令

在自己的包上创建新命令

我确信我做错了什么,但找不到具体是什么。

在撰写我的论文时,我为我的常量创建了一个包,它们运行正常:

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

我默认将 \num 设置为科学型,但从风格上讲,我需要在某些地方添加一些小数。因此,我决定添加一个新命令,例如:

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

重点是:我想将它与其余的宏一起放入我的包中,但 \latex 似乎不喜欢它。如果我在主文件中使用相同的命令,它可以正常工作,但当我在包上执行它时,它会显示:

! 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}}

使用 TeXworks 和 ShareLatex 时都出现同样的错误。我现在的可移植性方法是在 main.tex 中声明空白宏,然后在包中重新命令它们。这可行,但我觉得整个宏应该封装在包中。我做错了什么?


我在 sharelatex 中创建了一个可编辑的公共项目,并复制了以下问题:https://www.sharelatex.com/project/551003850f9d21382c0e5437

答案1

请检查是否\thanum无意中被定义了两次。

而且,正如 Joseph Wright 在评论中指出的那样,在排版组合数字和单位时,您应该使用\SI而不是单独使用\num和。\si

另外,考虑使用\newcommand而不是\def来定义简写宏。这样,你可以定义宏来采取可选参数默认情况下,该字段未使用或为空。这样做可以轻松(如有必要)“动态”覆盖某些格式设置。

在此处输入图片描述

\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}

答案2

请不要发布外部网站链接,而要构建适当的最小示例。这会使帮助变得更加困难,并且您的问题对未来遇到类似问题的用户几乎没有价值。

问题是你的主.tex文件包含这些行

\input{thabeatmacros.sty}
\usepackage{thabeatmacros}

读取\input您的包文件并处理其内容。它创建命令等。

然后,您加载包,该包再次输入文件并再次处理其内容。这会尝试再次创建已经存在的命令。当然,名称已被占用,当您告诉 LaTeX 它们是新命令时,它会拒绝覆盖现有命令。

解决方案是删除第一行。包不应该是\input那样的。你应该总是使用\usepackage{}

\usepackage{thabeatmacros}

还要注意,重新定义命令是\renewcommand而不是\rewnewcommand。然而,这显然不是你想要的。

因此:

\begin{filecontents}{thabeatmacros.sty}
\ProvidesPackage{thabeatmacros}[2016/02/23 v1.0 My own macros]

\newcommand* \earthRadius {\SI{6.371e6}{\metre}}
\newcommand* \lightSpeed {\SI{2.99793e8}{\metre\per\second}}
\newcommand* \lightSpeedAprox {\SI{3e8}{\metre\per\second}}
\newcommand*{\thanum}[1]{\num[scientific-notation=false]{#1}}

\endinput
\end{filecontents}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{thabeatmacros}

\begin{document}

\section{Introduction}

There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
There is another theory which states that this has already happened.

\subsection*{TESTING DEFs WORK}

\begin{itemize}
  \item {\huge\earthRadius\par}
  \item {\huge\lightSpeed\par}
\end{itemize}

\end{document}

固定宏

相关内容