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}
\newcommand
. you could define both with\providecommand
; then whichever one is encountered first will be the one that is in effect. but that doesn't satisfy you're requirement that you want both of them to be available. presumably it's possible to specify an environment in which each should be active. in that case, you can redefine the command within each specific environment, or define\power
with a built-in\if
to determine which version to use in which context..sty
files each having a macro named\power
, how should LaTeX know which version it should apply? Same names are possible, but not this way.\usepackage
afoo.sty
package multiple times (fontenc
is an exception), i.e. LaTeX has a mechanism to recognize you already loaded it and will not load it a second time. Besides, you can only do this from the preamble, so you files are loaded once and the macros they define are defined once.