3

I use \rput[B](1.5,4.1){\( 1 \)} to write a number at a specific place, now I would like to express an expression at the place of coordinates, for instance \rput[B](2+#1,4.1){\( 1 \)} where #1 is an input of my macro, it returns an error ERROR: Illegal unit of measure (pt inserted). Could anyone help?

enter image description here

  • A MWE would be useful. – cacamailg Mar 5 '13 at 18:09
2

For allow arithmetic in coordinates, you can use Reverse Polish Notation (or RPN for short). Here's a quick view on what you might be after:

enter image description here

\documentclass{article}
\usepackage{pstricks}% http://tug.org/PSTricks/main.cgi/
\newcommand{\myput}[2][0]{%
  \rput[B](!1.5 #1 add 4.1){#2}
}
\begin{document}
\begin{pspicture}(5,5)
  \SpecialCoor
  \rput[B](1.5,4.1){$1$}
  \myput[1]{$2$}
\end{pspicture}
\end{document}

The RPN notation (used under PostScript) is described in section 54 Special coordinates of the pstricks documentation, and requires the use of \SpecialCoor.

RPN in PostScript works on the principle of maintaining a stack. For coordinates, the top two entries represent the x- and y-coordinates being used. By means of example, consider the RPN calculation

2 3 add 4 1 sub mul

I'll use the notation [a b c ...] to denote the PostScript stack, where a is the item at the bottom, with b above that, and c above that...

Read left to right, the following PostScript actions are taken:

  • 2 pushes the number 2 on the stack ~ [2];
  • 3 pushes the number 3 on the stack ~ [2 3];
  • add takes the top two elements from the stack, and adds them together, leaving the result on the stack ~ [5];
  • 4 pushes the number 4 on the stack ~ [5 4];
  • 1 pushes the number 1 on the stack ~ [5 4 1];
  • sub takes the top two elements from the stack, and subtracts them from one another, leaving the result on the stack ~ [5 3];
  • mul takes the top two elements from the stack, and multiplies them together, leaving the result on the stack ~ [15].

Using this, it should be clear that

1.5 #1 add 4.1
  • 1.5 is pushed on the stack ~ [1.5];
  • #1 (the first/optional argument) is pushed on the stack ~ [1.5 #1];
  • add pops the top two elements, adds them and pushes the result r back on the stack ~ [r];
  • 4.1 is pushed on the stack ~ [r 4.1].

Now \rput, knowing that it has been provided with the ! (RAW PostScript code) directive, pops the top two elements which provide the x- and y-coordinates of the point. This takes some getting used to, for sure.

A complete list/reference of PostScript commands (including the above), is available as part of the PostScript LANGUAGE REFERENCE (third edition). Sections of interest include:

  • 3.4 Stacks (p 45), which includes an example like the above;
  • 8.1 Operator Summary (p 508)

When using nodes, however, it offers a more intuitive way of looking at such arithmetic using [nodesep=..,offset=..].

3

That should do the trick:

\documentclass{standalone}

\usepackage{pstricks}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\newcommand{\xyput}[3]{\rput[B](\calc{#1},\calc{#2}){#3}}

\begin{document}

\begin{pspicture}(5,5)
  \psframe(0,0)(5,5)
  %\rput[B](0,0){0}
  \xyput{0+1}{0+1}{0}
  \xyput{0+4}{0+1}{1}
  \xyput{0+1}{0+4}{2}
  \xyput{0+4}{0+4}{3}
\end{pspicture}

\end{document}

See the picture below An example with numbers

  • Sorry, did you try your code? It gives me ERROR: Undefined control sequence. around \calc, and it shows also +1, I don't where it comes from... – SoftTimur Mar 5 '13 at 18:50
  • I tried the code. I am using the latest TeXLive 2012. See if your LaTeX distribution is updated. – cacamailg Mar 5 '13 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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