2

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}

1 Answers1

7

The method to debug this sort of problems is to find out where the definition changes by planting \ShowCommand everywhere:

\documentclass{article}
\usepackage{unicode-math}
\renewcommand\vec{blub} % from package 1
\ShowCommand\vec
\renewcommand\vec{duck} % from package 2
\ShowCommand\vec
\begin{document} %what happens here??
\ShowCommand\vec
xxx
\end{document}

This will give answers like this (on the terminal or in the log):

> \vec=\long macro:
->blub.
<argument> \vec

> \vec=\long macro: ->duck. <argument> \vec

> \vec=\protected macro: ->\Umathaccent fixed 7\symoperators "020D7\scan_stop: . <argument> \vec

So obviously the definition changes in \begin{document} again, and the \Umath points to unicode-math as "culprit". If you want to learn more you could then inspect the begindocument hook (with \ShowHook{begindocument}, or search for answers with unicode-math and begindocument. Or you simply do your redefinition in begindocument too.

Ulrike Fischer
  • 327,261