0

I want to use \newcommand within an align environment. Furthermore, I need the newly defined command outside of the align environment. While this is easy for the case of \def as I can simply prefix it by \global. This solution does not work for \newcommand. However there are solution proposed here, here, and here. All of these solution work well within an equation environment. However, they fail within a align environment. In the following MWE I have commented out the failing commands.

\documentclass{article}
\usepackage{amsmath}

% taken from https://tex.stackexchange.com/a/51750/22187 \usepackage{etoolbox} \makeatletter \def\gnewcommand{\g@star@or@long\gnew@command} \def\grenewcommand{\g@star@or@long\grenew@command} \def\g@star@or@long#1{@ifstar{\let\l@ngrel@x\global#1}{\def\l@ngrel@x{\long\global}#1}} \def\gnew@command#1{@testopt{@gnewcommand#1}0} \def@gnewcommand#1[#2]{\kernel@ifnextchar [{@gxargdef#1[#2]}{@argdef#1[#2]}} \let@gxargdef@xargdef \patchcmd{@gxargdef}{\def}{\gdef}{}{} \let\grenew@command\renew@command \patchcmd{\grenew@command}{\new@command}{\gnew@command}{}{} \makeatother

% taken from https://tex.stackexchange.com/a/51751/22187 \makeatletter \def\reset@l@texglobal{\let\l@texglobal\relax} \reset@l@texglobal \def\latexglobal{\let\l@texglobal\global} \long\def@yargd@f#1#2{% \def\reserved@a##1#1##2##{% \expandafter\def\expandafter#2\reserved@b##1#1% }% \afterassignment\reset@l@texglobal \l@texglobal\l@ngrel@x\reserved@a 0##1##2##3##4##5##6##7##8##9###1% }
\long\def@xargdef#1[#2][#3]#4{% @ifdefinable#1{% \l@texglobal\expandafter\def\expandafter#1\expandafter{% \expandafter@protected@testopt\expandafter#1\csname\string#1\endcsname{#3}% } \expandafter@yargdef\csname\string#1\endcsname\tw@{#2}{#4}% }% } \makeatother

% testing functions
\newcommand\GlobalDef[1]{ \global\expandafter\def\csname GlobalDef#1\endcsname{GlobalDef#1} }
\newcommand\GlobaldefsNewcommand[1]{ \globaldefs=1 \expandafter\newcommand\csname GlobaldefsNewcommand#1\endcsname{GlobaldefsNewcommand#1} \globaldefs=0 }
\newcommand\Gnewcommand[1]{ \expandafter\gnewcommand\csname Gnewcommand#1\endcsname{Gnewcommand#1} }
\newcommand\LatexglobalNewcommand[1]{ \latexglobal\expandafter\newcommand\csname LatexglobalNewcommand#1\endcsname{LatexglobalNewcommand#1} }

\begin{document}

\begin{equation} \GlobalDef{Equation} \GlobaldefsNewcommand{Equation} \Gnewcommand{Equation} \LatexglobalNewcommand{Equation} \end{equation}

\begin{align} \GlobalDef{Align} % \GlobaldefsNewcommand{Align} % does not work % \Gnewcommand{Align} % does not work % \LatexglobalNewcommand{Align} % does not work \end{align}

\GlobalDefEquation{} \GlobalDefAlign{}

\GlobaldefsNewcommandEquation{} % \GlobaldefsNewcommandAlign{}

\GnewcommandEquation{} % \GnewcommandAlign{}

\LatexglobalNewcommandEquation{} % \LatexglobalNewcommandAlign{} \end{document}

Jan Hajer
  • 864
  • 1
    What's the reason for doing definitions inside align? You should be aware that align scans its input twice, so a \gnewcommand will fail at the second pass. – egreg Jun 22 '20 at 09:23
  • 1
    you can use \ifmeasuring@ to detect in which phase you are, but doing \newcommand in align sounds very odd. – Ulrike Fischer Jun 22 '20 at 09:25
  • I am playing around with defining my mathematical objects as macros the first time they appear and subsequent occurrences link then to the definition. If I implemented this in the header it would become quickly hard to maintain. – Jan Hajer Jun 22 '20 at 09:47
  • Why you does not use simply \gdef? I never used newcmmand, only \def of \gdef. It is much more simply and straithforward. – wipet Jun 22 '20 at 15:39
  • There are many text shorthand of one to two character length. It is very easy to override them accidentally by math definitions. This may work well most of the time if you stick to the English language, but can have nasty side effects in e.g. the bibliography. Hence I want to know what I override by using renewcommand. – Jan Hajer Jun 22 '20 at 16:48

0 Answers0