在我自己的包上創建 newcommand

在我自己的包上創建 newcommand

我確信我做錯了什麼,但我無法找到到底是什麼。

在寫我的論文時,我為我的常量創建了一個包,它們工作正常:

\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 中聲明空白巨集並在套件中 renewcommand 它們。它有效,但我覺得整個巨集應該封裝在套件上。我究竟做錯了什麼?


我在 sharelatex 中建立了一個可編輯的公共項目,並複製了問題:https://www.sharelatex.com/project/551003850f9d21382c0e5437

答案1

請檢查並查看是否\thanum無意中被定義了兩次。

而且,正如約瑟夫·賴特(Joseph Wright)在評論中已經指出的那樣,在排版組合數字和單位時,您應該使用\SI而不是單獨使用\numand 。\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 它們是新命令時,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}

固定宏

相關內容