Math & Sciencea macro question

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

a macro question

Post by kent »

Define

Code: Select all

\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)

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

a macro question

Post by kaiserkarl13 »

Code: Select all

\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:

Code: Select all

\def\macroa{world!}
\def\macrob{\macroa}
\def\macroa{Hello }
% versus
\def\macroc{world!}
\let\macrod\macroc
\def\macroc{Hello }
\noindent
\macroa\macrob\\
\macroc\macrod
Post Reply