# add mult, etc in LaTeX

I tried to see how mult, div, add, etc work but I am unable to understand what is going on in one concrete example. Say i want to put a point at x = y = 2 but the x and y coordinate come from some complicated geometric computation. The following code snipped does this:

\begin{pspicture}[showgrid=true](0,0)(5,5)
\psdot(C)
\end{pspicture}


However, I am absolutely confused about what is going on: a point consists of two parts of information, x- and y-coordinate. The most intuitive thing I would expect is

\pnode(!0 2 add, !0 2 add){C}


but this produces an error. So it took me some time to figure out that

\pnode(!0 2 add 0 2 add){C}


does what I want. My question: What is going on? Why do people code it like this?

This is not intuitive, it is *ANTI*intuitive :-) Is there some sense in this that I cannot see right now?

-
It is not intuition just habits. In good hands old HP calculators (which uses this) can smoke any intuitive operation order. – percusse Nov 19 '13 at 20:17
it is the way how every computer works, at least in the end of every compiler – Herbert Nov 20 '13 at 7:45

The exclamation mark indicates raw postscript code. Since postscript is a stacking language, when you do

\pnode(!0 2 add 0 2 add){C}


the following happens.

• 0 2 is pushed onto the stack.
• add grabs the top two objects, adds them together and pushes the result onto the stack.
• 0 2 is pushed onto the stack, so now the top three objects on the stack are 2 0 2.
• add acts as before, so ultimately the stack contains 2 2 at the top.

Of course, you could achieve the same effect with

\pnode(!2 2){C}


Some good postscript manuals are available here.

-

From the pstricks documentation (section 54 Special coordinates, p 145):

(!ps) Raw PostScript code. ps should expand to a coordinate pair. The units xunit and yunit are used. For example, if we want to use polar coordinates (2, 45) and (1.5, 70) that are scaled along with xunit and yunit, we can write:

\SpecialCoor
\psset{dotscale=2,xunit=2,yunit=1.5}%
\psdot(2;45)
\psdot[linecolor=cyan](! 2 45 cos mul 2 45 sin mul)
\psset{dotstyle=triangle*}%
\psdot(1.5;70)
\psdot[linecolor=cyan](! 1.5 70 cos mul 1.5 70 sin mul)


A lot of things can be done with some PostScript programming, and sometimes in an easiest way than at the TeX level. Note also that some PostScript code can be executed in addition to the computation of the coordinates.

Note there are no commas that separate the coordinates!

So following a call to \SpecialCoor, the notation (! <stuff>) denotes a PostScript interface to setting coordinates. And, PostScript uses a Reverse Polish notation (RPN) approach to push/pop content onto a stack. In laymen's terms, this means that (using infix notation) 3 − 4 + 5 is transformed to 3 4 − 5 + in RPN - you first specify the operands, then the operator that acts on these operands. Specific to your example, the coordinate pair passed to the PostScript compiler requires the two top entries to resemble your (X,Y) pair. If you push an X value, followed by a comma ,, followed by a Y value, the PostScript compiler probably sees , as the X value, which doesn't make sense.

For a detailed discussion on the PostScript Language (extensively used by pstricks in the background), see the PostScript Language Reference Manual. In particular, you should read the chapter on operators (Chapter 8: Operators, p 505).

In sequence, \pnode(!0 2 add 0 2 add){C} has the following PostScript sequence:

• 0 is pushed on the stack (stack: 0)
• 2 is pushed on the stack (stack: 0 2)
• add takes the first two entries on the stack and replaces it with the sum (stack: 2)
• 0 is pushed on the stack (stack: 2 0)
• 2 is pushed on the stack (stack: 2 0 2)
• add takes the first two entries on the stack and replaces it with the sum (stack: 2 2)

As mentioned in the pstricks documentation, it is possible to do quite a bit on the PostScript side (for example, read any of the .pro files associated with pstricks). You may be required to understand PostScript if you wish to update certain pstricks functionality yourself.

-

It is an example of PostScript code. The so called reverse Polish notation is used in it. For example (2+3)*4 is written 2 3 add 4 mul, where add means addition and mul multiplication.

It is nor antiintuitive. For example, fraction is obtained in plain TeX by $a\over b$ (operator between arguments), in LaTeX \frac{a}{b} (operator before arguments). In reverse Polish notation it would be {a}{b}\frac.

-
• A point in post-fix expression (aka RPN) must leave 2 values (on the stack) which represent the abscissa and ordinate of the point in question.

• A point in post-fix expression has a starting delimiter !.

• No , between the abscissa and ordinate.

## Example

• (!1 2) is the same as (1,2).
• (!2 dup) is the same as (2 2) as dup represents duplicating the previous value.
• (!1 2 sub 3 4 add) is the same as (-1,7).
• (!1 2 exch) is the same as (2,1) as exch represents swapping the previous two values.