2

How to pass a macro output \X{Tpeak} to \FPeval\Ypeak as shown in the following code?

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}
\usepackage[nomessages]{fp}
\newcommand\const[3][3]{%
    \expandafter\FPeval\csname#2\endcsname{round(#3:#1)}%
    \pstVerb{/#2 \csname#2\endcsname\space def}%
}

\const{Vinit}{10}
\const{Theta}{75/180*pi}
\const{Gravity}{10}

\def\X#1{Vinit*cos(Theta)*#1}
\def\Y#1{Vinit*sin(Theta)*#1-Gravity*pow(2,#1)/2}

\const{Tpeak}{Vinit*sin(Theta)/Gravity}

\const{Ypeak}{\X{Tpeak}}

\const{Xpeak}{pow(2,Vinit)*sin(2*Theta)/(2*Gravity)}
\begin{document}
\begin{pspicture}[showgrid=bottom](2\dimexpr\Xpeak\psxunit\relax,\Ypeak)
\end{pspicture}
\end{document}

1 Answers1

5

The expression \X{Tpeak} needs to be expanded first, before passing to \const.

\edef\next{%
  \noexpand\const{Ypeak}{\X{Tpeak}}%
}\next

First \X is fully expanded, but not \const because of \noexpand. Then \next contains:

\const {Ypeak}{Vinit*cos(Theta)*Tpeak}

Macro \next is only temporarily used. Therefore a typical pattern is to put this in a group:

\begingroup
\edef\next{\endgroup
  \noexpand\const{Ypeak}{\X{Tpeak}}%
}\next

Then a previously defined \next remains unchanged afterwards and \const is still called outside the group.

Heiko Oberdiek
  • 271,626