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?

In the preamble use

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

Then both packages are loaded and \power is not defined.

in parts of the document where you want the math version use

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

in parts of the document where you want the matlab version use

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

It seems like you want to use one macro with two possible outputs. The best way to achive that is to use an optional paramater to specify which behaviour is desired:

enter image description here

Notes:

  • If this macro needs to be defined in two separate files, which may both be included, then repleace \newcommand* with \providecommand*.

Code:

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

Here is another way to solve this.

Under the assumption, that there may be more then one command with the same name in both .sty files, this is a way to switch between them all.

In the .sty file define the commands as (in matlab.sty)

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

or (in math.sty)

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

Then, after the definition of all of these commands, add (in matlab.sty)

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

or (in math.sty)

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

At the end add (in matlab.sty)

\switchtomatlab

or (in math.sty)

\switchtomath

After loading, the marcos of the last .sty file loaded are active. Inside the document you can change the meaning of \power and \squareroot by calling \switchtomatlab or \switchtomath.

The effect is limited to the current group. So writing, for example

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

will lead to

In Matlab ...

Power is ...

In Matlab ...

You could also define an environment to locally switch to one meaning

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

Caution: an environment math already exists!