I am using TeX Live (MacTeX) 2023 with all updates (as of this morning). Consider two packages called packageone.sty and packagetwo.sty. packageone.sty contains a redefinition of \vec to include a starred version. packagetwo.sty contains a modified redefinition of vec to make the unstarred and starred versions identical if and only if packageone.sty is not loaded. For some reason, the custom \vec commands in each package cannot be seen from the main document and I do not understand why. The other commands defined in packagetwo.sty can be seen from the main document.
On one hand, I have been trying to understand this for the past two days and do not see anything obviously wrong because slightly different versions of the MWE have worked. On the other hand, I feel I am overlooking something obvious for some reason.
I was able to get the desired outcome by having packagetwo load packageone if it had not already been loaded (but that necessitated making the two new definitions of \vec identical, which is not a big deal in this case. They are different here merely for testing purposes). I was hoping to not make packagetwo load packageone but at least I now have a debugging strategy for figuring out why this extracted MWE doesn't work as I expected it to work.
Here is the MWE, which contains the two packages.
% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode
\begin{filecontents}{packageone.sty}
\RequirePackage[g]{esvect}
\RenewDocumentCommand{\vec}{ s m e{_^} }%
{%
\IfBooleanTF{#1}
{%
\vv{#2}%
\IfValueT{#4}%
{\sp{,#4\vphantom{\smash[t]{\big|}}}}
}%
{%
\symbfit{#2}
\IfValueT{#4}%
{\sp{#4\vphantom{\smash[t]{\big|}}}}
}%
\IfValueT{#3}%
{\sb{#3\vphantom{\smash[b]{|}}}}
}%
\end{filecontents}
\begin{filecontents*}{packagetwo.sty}
\IfPackageLoadedTF{packageone}
{}%
{%
\RequirePackage[g]{esvect}
\NewDocumentCommand{\foo}{}{Hello.}
\RenewDocumentCommand{\vec}{ s m }
{%
\IfBooleanTF{#1}
{%
\vv{#2}%
}%
{%
\vv{#2}%
}%
}%
}%
\NewDocumentCommand{\lhsmomentumprinciple}{ s }%
{%
\Delta
\IfBooleanTF{#1}%
{%
\vec{p}
}%
{%
\vec{p}%
}%
\sb{\symup{sys}}%
}%
\NewDocumentCommand{\rhsmomentumprinciple}{ s }%
{%
\IfBooleanTF{#1}%
{%
\vec{F}%
}%
{%
\vec{F}%
}%
\sb{\symup{sys,net}},\Delta t%
}%
\NewDocumentCommand{\momentumprinciple}{ s }%
{%
\IfBooleanTF{#1}%
{%
\lhsmomentumprinciple* = \rhsmomentumprinciple%
}%
{%
\lhsmomentumprinciple = \rhsmomentumprinciple%
}%
}%
\end{filecontents}
\documentclass{article}
\usepackage{unicode-math}
\usepackage{packageone}
\usepackage{packagetwo}
\begin{document}
1 ( \vec{E} )\par
2 ( \vec{E} )\par
\IfPackageLoadedTF{packagetwo}
{%
3 ( \momentumprinciple )\par
4 ( \momentumprinciple )\par
5 \foo \par
}%
{}%
\end{document}
fontspec, which is used byunicode-math. – LaTeXereXeTaL Jul 25 '23 at 04:43