[Tex/LaTex] How to define two commands with the same name and different description in two different .sty files that are to be loaded in the same document

macrospackages

I have two files math.sty and matlab.sty. I want to define for example the power operation in the two with the same name (it is important that they have exactly the same name for the use that I have to give the files later) and invoke them in the same document. But when it's called, it raises an error. For example:

maths.sty contains:

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

and matlab.sty contains

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

Is there any alternative to newcommand?

UPDATE:
The files math.sty and matlab.sty have many definitions. Also, I have to add the files mathUSER.sty and matlabUSER.sty.
I want to have all the definitions available for use in a template. And these new commands have to be written in a simple way so that children can use them without problem and create their own commands.

I have written all definitions in dictionary.sty. I would like to separate these definitions in matlab.sty, maths.sty, matlabuser.sty and 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}{
    ...
   } 
}

Invoke: \dictionary{matlab}{abs}
I do not want to have matlababs, matlababsuser, mathsabs and mathsabsuser only abs. That is too complex for a child. Any idea to simplify code?

Template example:

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

Best Answer

Here is a solution to your question after the update.

The idea is to store the text in an internal macro for each combination of term and dictionary and later call this macro to get the text, but of course, without having to use the internal macro itself. For this, two macros are needed, \dictionary{<dictionary>}{<term>} to get the text and \DefineDictionaryEntry{<dictionary>}{<trem>}{<text>} to define the text. For example, after

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

the line

\dictionary{matlab}{power}

will give out

The function power ...

The core for these macros looks like this:

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

In the example above the first line will define a macro \dict@matlab@power, which is then called with \dictionary. Now, this is barely usable, because in case of typos it will lead to odd error messages and/or results. It would be nice to check, if the dictionary and the term exists and give out an understanable error message if necessary.

In order to check, if a dictionary exists, the macro \NewDictionary{<dictionary>} defines an internal macro, which is later used for checking. The core for this looks like this:

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

Here too, some checking should be done.

From the LaTeX core the command \@ifundefined{<macro name>}{<code for macro undefined>}{<code for macro defined>} can be used to check, if a macro does exist. And \PackageError{<package name>}{<error message>}{<help text>} is used for error messages.

Additionally, packages normaly start with \ProvidesPackage{<package name>}[<date> <version> <short description>] and in packages other packages are loaded with \RequirePackage{<package name>}.

All this can be put in a package dictionary, which is then used by the other packages (matlab, maths, etc.) to define the terms.

% 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.
        }%
    }%
}

Now the other packages are simple:

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

Please note that for \ProvidesPackage the <package name> must be the file name without .sty.

Other packages can be written the same way:

% 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.}

With all this, in the document you need to load all the dictionaries you want to use:

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