LaTeX forum ⇒ Math & Sciencea macro question

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

a macro question

Postby kent » Thu Apr 05, 2018 1:39 pm

  1. \pgfmathsetmacro{\lpx}{veclen(\xpx,\ypy)}
  2. \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)
  1. \def\tmptmptmptmp{\lpxx}
  2. \def\lpxx{\lpx}
  3. \def\lpx{\tmptmptmptmp}

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

Posts: 587
Joined: Tue Mar 25, 2008 5:02 pm

Postby kaiserkarl13 » Thu Apr 05, 2018 3:50 pm

  1. \def\tmptmptmptmp{\lpxx}
  2. \def\lpxx{\lpx}
  3. \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:
  1. \def\macroa{world!}
  2. \def\macrob{\macroa}
  3. \def\macroa{Hello }
  4. % versus
  5. \def\macroc{world!}
  6. \let\macrod\macroc
  7. \def\macroc{Hello }
  8. \noindent
  9. \macroa\macrob\\
  10. \macroc\macrod

Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 5 guests