
如何傳遞巨集輸出\X{Tpeak}
,如\FPeval\Ypeak
下列程式碼所示?
\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
\X{Tpeak}
在傳遞給 之前,需要先擴展表達式\const
。
\edef\next{%
\noexpand\const{Ypeak}{\X{Tpeak}}%
}\next
首先\X
是完全擴展,但不是\const
因為\noexpand
.然後\next
包含:
\const {Ypeak}{Vinit*cos(Theta)*Tpeak}
巨集\next
只是暫時使用。因此,典型的模式是將其放在一個群組中:
\begingroup
\edef\next{\endgroup
\noexpand\const{Ypeak}{\X{Tpeak}}%
}\next
那麼之前定義的a\next
之後保持不變,\const
仍然在組外調用。