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.

According to the pstricks-add manual on near the last page, dup is used to duplicate the value of the previous stack. Duplicating can simplify an RPN expression.

For the sake of simplicity, let's consider the following example. A line of length 3 unit at a slope of 30 degrees is first drawn. The end point of this line (which is not on the origin) will be marked with a dot. I wrote 4 possible RPN notations for the dot, as follows:

enter image description here

\documentclass{article}
\usepackage{pstricks-add}
\usepackage{enumerate}

\def\theta{30}
\def\radius{3}

\parskip=40pt
\parindent=0pt

\begin{document}

\begin{enumerate}[a.]
\item\verb|\theta\space dup cos \radius\space mul sin \radius\space mul|\\[20pt]
\begin{pspicture}[showgrid](3,2)
    \psline(\radius;\theta)
    \psdot[linecolor=red](!\theta\space dup cos \radius\space mul sin \radius\space mul)
\end{pspicture}\\[20pt]

\item\verb|\radius\space dup \theta\space cos mul \theta\space sin mul|\\[20pt]
\begin{pspicture}[showgrid](3,2)
    \psline(\radius;\theta)
    \psdot[linecolor=green](!\radius\space dup \theta\space cos mul \theta\space sin mul)
\end{pspicture}\\[20pt]

\item\verb|\radius\space \theta\space cos mul \radius\space \theta\space sin mul|\\[20pt]
\begin{pspicture}[showgrid](3,2)
    \psline(\radius;\theta)
    \psdot[linecolor=blue](!\radius\space \theta\space cos mul \radius\space \theta\space sin mul)
\end{pspicture}\\[20pt]

\item\verb|\theta\space cos \radius\space mul \theta\space sin \radius\space  mul|\\[20pt]
\begin{pspicture}[showgrid](3,2)
    \psline(\radius;\theta)
    \psdot(!\theta\space cos \radius\space mul \theta\space sin \radius\space  mul)
\end{pspicture}\\[20pt]
\end{enumerate}
\end{document}

In the first two examples, I got wrong dots. But the last 2 examples, the dots are drawn correctly. How to use the keyword dup ?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

Every PostScript operator pops the required operands from the top of the operand stack and pushes the result back. In case of your first expression we get (the right side of => shows the operand stack from bottom to top):

THETA dup   => THETA THETA
cos         => THETA cos(THETA)
RADIUS mul  => THETA cos(THETA)*RADIUS
sin         => THETA sin(cos(THETA)*RADIUS)
RADIUS mul  => THETA sin(cos(THETA)*RADIUS)*RADIUS

This is obviously not the result you're looking for, since the sin operator takes the result of the cosoperation previously applied. In order to move the value THETA (located at the bottom of the stack) to the top, insert exch after the first multiplication. It swaps the two topmost stack elements:

THETA dup   => THETA THETA
cos         => THETA cos(THETA)
RADIUS mul  => THETA cos(THETA)*RADIUS
exch        => cos(THETA)*RADIUS THETA
sin         => cos(THETA)*RADIUS sin(THETA)
RADIUS mul  => cos(THETA)*RADIUS sin(THETA)*RADIUS

Thus, your first variant should look like this to get the desired result:

\psdot[linecolor=red](!\theta\space dup cos \radius\space mul exch sin \radius\space mul)

The second expression evaluates like so:

RADIUS dup  => RADIUS RADIUS
THETA cos   => RADIUS RADIUS cos(THETA)
mul         => RADIUS RADIUS*cos(THETA)
THETA sin   => RADIUS RADIUS*cos(THETA) sin(THETA)
mul         => RADIUS RADIUS*cos(THETA)*sin(THETA)

In order to fix it, it's necessary to move the value RADIUS from the bottom to the top before applying the second multiplication. This can be achieved with the roll operator that circularly moves a given number of stack elements. 3 2 roll circularly shifts the three topmost elements "upwards" two times:

RADIUS dup  => RADIUS RADIUS
THETA cos   => RADIUS RADIUS cos(THETA)
mul         => RADIUS RADIUS*cos(THETA)
THETA sin   => RADIUS RADIUS*cos(THETA) sin(THETA)
3 2 roll    => RADIUS*cos(THETA) sin(THETA) RADIUS
mul         => RADIUS*cos(THETA) sin(THETA)*RADIUS

This leads to the following PSTricks statement:

\psdot(!\radius\space dup \theta\space cos mul \theta\space sin 3 2 roll mul)

These expressions are, however, unnecessarily complicated so that the 3rd or 4th variant given in the question are the much better way to go. :)

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.