如何在要加载到同一文档中的两个不同的 .sty 文件中定义两个具有相同名称和不同描述的命令?

如何在要加载到同一文档中的两个不同的 .sty 文件中定义两个具有相同名称和不同描述的命令?

我有两个文件数学MATLAB 教程。例如,我想在两个文件中定义同名的幂运算(重要的是它们必须具有完全相同的名称,以便我稍后为文件提供用途)并在同一个文档中调用它们。但是当它被调用时,它会引发错误。例如:

maths.sty 包含:

\newcommand\power{ Power is a mathematical operation that is
 represented with a superscript: $5^3$.}

并且 matlab.sty 包含

\newcommand\power{ In matlab the power is written with the symbol $
 \^{} $}

有没有其他替代方案newcommand

更新:文件数学MATLAB 教程有很多定义。另外,我必须添加文件 mathUSER.stymatlab用户手册。我希望所有定义都可以在模板中使用。而且这些新命令必须以简单的方式编写,以便孩子们可以毫无问题地使用它们并创建自己的命令。

我已经在中写了所有定义dictionary.sty。我想将这些定义分开放在 matlab.sty、maths.sty、matlabuser.sty 和 mathsuser.sty 中。

\newcommand\dictionary[2]{
  \IfEqCase{#1}{
            {matlab}{\matlab{#2}}
            {maths}{\maths{#2}}         
            {matlabuser}{\matlabuser{#2}}
            {mathsuser}{\mathsuser{#2}}       
  }       
}

\newcommand\matlab[1]{
\IfEqCase{#1}{
    {abs}{The absolute the absolute value in programming...}
    {addition}{The addition ...}
   }
}

\newcommand\maths[1]{
\IfEqCase{#1}{
    {addition}{The addition in maths...}
    ...
   }
}

\newcommand\matlabuser[1]{ \IfEqCase{#1}{
     ...
   } 
}

\newcommand\mathsuser[1]{ \IfEqCase{#1}{
    ...
   } 
}

调用:\dictionary{matlab}{abs} 我不想有 matlababs、matlababsuser、mathsabs 和 mathsabsuser,只想要 abs。这对小孩子来说太复杂了。有什么想法可以简化代码吗?

模板示例:

\documentclass[10pt,a4paper]{report}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage{dictionary}
\begin{document}
This is my text book and handbook.
Today, I have learned \dictionary{matlab}{abs} and \dictionary{maths}{abs}  ...
\end{document}

答案1

这是您的解决方案更新后的问题

这个想法是将文本存储在每个术语和词典组合的内部宏中,然后调用此宏来获取文本,但当然,不必使用内部宏本身。为此,需要两个宏,分别用于获取文本\dictionary{<dictionary>}{<term>}\DefineDictionaryEntry{<dictionary>}{<trem>}{<text>}定义文本。例如,在

\DefineDictionaryEntry{matlab}{power}{The function power ...}

线

\dictionary{matlab}{power}

将会给出

函数功率...

这些宏的核心如下所示:

\newcommand*{\DefineDictionaryEntry}[3]{%
    \expandafter\def\csname dict@#1@#2\endcsname{#3}%
}
\newcommand*{\dictionary}[2]{
    \csname dict@#1@#2\endcsname
}

在上面的例子中,第一行将定义一个宏\dict@matlab@power,然后用 调用它\dictionary。现在,这几乎没用,因为如果出现拼写错误,它会导致奇怪的错误消息和/或结果。最好检查一下词典和术语是否存在,并在必要时给出一个可理解的错误消息。

为了检查字典是否存在,该宏\NewDictionary{<dictionary>}定义了一个内部宏,稍后将使用该宏进行检查。其核心如下所示:

\newcommand*{\NewDictionary}[1]{%
    \expandafter\def\csname dict@#1\endcsname{y}%
}

这里也应该进行一些检查。

从 LaTeX 核心,\@ifundefined{<macro name>}{<code for macro undefined>}{<code for macro defined>}可以使用该命令检查宏是否存在。并\PackageError{<package name>}{<error message>}{<help text>}用于错误消息。

此外,软件包通常以 开头\ProvidesPackage{<package name>}[<date> <version> <short description>],而其他软件包则以 加载\RequirePackage{<package name>}

所有这些都可以放在一个包中dictionary,然后由其他包(、等)使用它matlabmaths定义术语。

% dictionary.sty
\ProvidesPackage{dictionary}[2017/10/07 v1.00 Simple dictionary package.]
\newcommand*{\dictionary}[2]{
    \@ifundefined{dict@#1}{%
        % error message, dictionary does not exist
        \PackageError{dictionary}{%
            The dictionary #1 does not exist or is not loaded.\MessageBreak
            Nothing will be printed.%
        }{%
            You may have a spelling error in your call or you forgot\MessageBreak
            to load the package. Please check and correct this.
        }%
    }{%
        \@ifundefined{dict@#1@#2}{%
            % error message, dictionary entry does not exist
            \PackageError{dictionary}{%
                The term #2 does not exist in dictionary #1.\MessageBreak
                Nothing will be printed.%
            }{%
                You may have a spelling error in your call or the term\MessageBreak
                is not defined yet. Please check and correct this.
            }%
        }{%
            % the core, for e.g. \dictionary{math}{power} the internal
            % command \dict@math@power is called
            \csname dict@#1@#2\endcsname
        }%
    }%
}
\newcommand*{\DefineDictionaryEntry}[3]{%
    \@ifundefined{dict@#1}{%
        % error message, dictionary does not exist
        \PackageError{dictionary}{%
            The dictionary #1 does not exist or is not loaded.\MessageBreak
            The new term #2 will not be defined.
        }{%
            You may have a spelling error in your call or you forgot\MessageBreak
            to load the package. Please check and correct this.
        }%
    }{%
        \@ifundefined{dict@#1@#2}{%
            % the core, for e.g. \DefineDictionaryEntry{math}{abs}{The abs ...} the internal
            % command \dict@math@abs is defined with the meaning 'The abs ...'
            \expandafter\def\csname dict@#1@#2\endcsname{#3}%
        }{%
            % error message, term already defined
            \PackageError{dictionary}{%
                The term #2 is already defined in dictionary #1.\MessageBreak
                You can not redefine it.
            }{%
                You may have a spelling error or you just copied the line\MessageBreak
                and forgot to change the term. Please check and correct this.
            }%
        }%
    }%
}
\newcommand*{\NewDictionary}[1]{%
    \@ifundefined{dict@#1}{%
        % the core, for e.g. \NewDictionary{mathuser} the internal
        % command \dict@mathuser is defined. With this, It's possible
        % to check, if the dictionary exists
        \expandafter\def\csname dict@#1\endcsname{y}%
    }{%
        % error message, dictionary already exists
        \PackageError{dictionary}{%
            The dictionary #1 already exist.\MessageBreak
            You can not define it again.
        }{%
            You may have a spelling error or you just copied the line\MessageBreak
            and forgot to change the dictionary name.\MessageBreak
            Please check and correct this.
        }%
    }%
}

现在其他的包都很简单:

matlab.sty

% matlab.sty
\ProvidesPackage{matlab}[2017/10/07 v1.00 Dictionary for matlab.]
\RequirePackage{dictionary}
\NewDictionary{matlab}
\DefineDictionaryEntry{matlab}{power}{The function power ...}
\DefineDictionaryEntry{matlab}{abs}{The function abs ...}
% more definitions here

maths.sty

% maths.sty
\ProvidesPackage{maths}[2017/10/07 v1.00 Dictionary for math.]
\RequirePackage{dictionary}
\NewDictionary{math}
\DefineDictionaryEntry{math}{power}{The power ...}
\DefineDictionaryEntry{math}{abs}{The absolute ...}
% more definitions here

请注意,的\ProvidesPackage文件<package name>名必须不带.sty

其他包也可以用同样的方法编写:

% mathuser.sty
\ProvidesPackage{mathuser}[2017/10/07 v1.00 Dictionary for math by User.]
\RequirePackage{dictionary}
\NewDictionary{mathuser}
\DefineDictionaryEntry{mathuser}{test}{This is a test entry, written by User.}

有了这些,您需要在文档中加载所有想要使用的词典:

\documentclass[10pt,a4paper]{article}
\usepackage{matlab}
\usepackage{maths}
\usepackage{mathuser}

% dictionaries and their entries can also be defined in the preamble
\NewDictionary{mydict}
\DefineDictionaryEntry{mydict}{addition}{Additionally, we learned about addition}

\begin{document}

Test the terms:

\dictionary{matlab}{power}

\dictionary{math}{power}

\dictionary{matlab}{abs}

\dictionary{math}{abs}

\dictionary{mathuser}{test}

\dictionary{mydict}{addition}

\end{document}

答案2

在序言中使用

\usepackage{math}
\let\mathpower\power
\let\power\undefined
\usepackage{matlab}
\let\matlabpower\power
\let\power\undefined

然后两个包都被加载并且\power没有被定义。

在文档中需要使用数学版本的部分

 \let\power\mathpower ........ \power

在文档中需要使用 matlab 版本的部分

 \let\power\matlabpower ........ \power

答案3

看起来您想使用一个具有两个可能输出的宏。实现此目的的最佳方法是使用可选参数来指定所需的行为:

在此处输入图片描述

笔记:

  • 如果这个宏需要在两个中定义分离文件,可能都包括在内,然后\newcommand*用替换\providecommand*

代码:

\documentclass{article}
\usepackage{xstring}

\newcommand*{\power}[1][]{%
    \IfStrEq{#1}{matlab}{%
        In matlab the power is written with the symbol \textasciicircum.%
    }{%
        Power is a mathematical operation that is represented with a superscript: $5^3$.%
    }%
}

\begin{document}

\power

\power[matlab]

\end{document}

答案4

这是解决这个问题的另一种方法。

假设两个.sty文件中可能有多个同名的命令,这是一种在它们之间切换的方法。

.sty文件中将命令定义为(in matlab.sty

\newcommand\matlabpower{In Matlab ...}
\newcommand\matlabsquareroot{In Matlab ...}

或(在math.sty

\newcommand\mathpower{Power is ...}
\newcommand\mathsquareroot{Squareroot is ...}

然后,在所有这些命令的定义之后,添加(在matlab.sty

\newcommand\switchtomatlab{%
    \let\power\matlabpower
    \let\squareroot\matlabsquareroot
}

或(在math.sty

\newcommand\switchtomath{%
    \let\power\mathpower
    \let\squareroot\mathsquareroot
}

最后添加(in matlab.sty

\switchtomatlab

或(在math.sty

\switchtomath

加载后,最后加载的文件的宏将处于活动状态。在文档中,您可以通过调用 或来更改和.sty的含义。\power\squareroot\switchtomatlab\switchtomath

效果仅限于当前组。例如,

\switchtomatlab
\power
\begin{someenvirontment}
\switchtomath
\power
\end{someenvironment}
\power

将导致

在 Matlab 中...

力量就是……

在 Matlab 中...

您还可以定义一个环境来本地切换到一个含义

\newenvironment{termsmatlab}{\begingroup\swtichtomatlab}{\endgroup}

注意:环境math已存在!

相关内容