## LaTeX forum ⇒ Math & Science ⇒ a macro question

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
kent
Posts: 52
Joined: Thu Oct 20, 2016 3:41 pm

### a macro question

Define
\pgfmathsetmacro{\lpx}{veclen(\xpx,\ypy)}\pgfmathsetmacro{\lpxx}{veclen(\xpxx,\ypyy)}

where we assume that these definitions of the two above PGF macroes are OK.

If I then redefine as follows (ie. switching the content of \lpx and \lpxx)
\def\tmptmptmptmp{\lpxx}\def\lpxx{\lpx}\def\lpx{\tmptmptmptmp}

it seems that the pdflatex execution halts without any error message.
Any comments on why are appreciated.
Kent
Last edited by cgnieder on Sat Apr 07, 2018 2:42 pm, edited 1 time in total.

kaiserkarl13
Posts: 604
Joined: Tue Mar 25, 2008 5:02 pm
\def\tmptmptmptmp{\lpxx}\def\lpxx{\lpx}\def\lpx{\tmptmptmptmp}

The code above does this: (1) define a macro \tmptmptmptmp that expands to \lpxx; (2) define another one, \lpxx, that expands to \lpx; (3) define \lpx to expand to \tmptmptmptmp, which of course expands to \lpxx, which expands to\lpx, which expands to \tmptmptmptmp, which...I'm sensing an infinite loop here.

The command you probably want is \let, not \def. \let is a TeX primitive that takes the current definition of the second macro and copies that definition to the first. If the second later changes, the first is still the same.

Compare the output of these:
\def\macroa{world!}\def\macrob{\macroa}\def\macroa{Hello }% versus\def\macroc{world!}\let\macrod\macroc\def\macroc{Hello }\noindent\macroa\macrob\\\macroc\macrod