Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.