내 패키지에 새 명령 만들기

내 패키지에 새 명령 만들기

내가 뭔가 잘못하고 있다고 확신하지만 정확히 무엇인지 찾을 수 없습니다.

논문을 작성하면서 상수에 대한 패키지를 만들었는데 제대로 작동합니다.

\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아니라 사용하는 것이 좋습니다.\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}

고정 매크로

관련 정보