11

I want to call a PSTricks macro with a node name which involves the result of an arithmetic expression with counters.

Consider the following, not working example:

\documentclass{article}
\usepackage{pst-node}
\newcounter{mycnt}
\setcounter{mycnt}{2}
\def\nodename#1{%
  A\numexpr\themycnt-#1\relax B}%
\begin{document}
\begin{pspicture}[showgrid](5,5)
  \pnode(5,5){A1B}
  \psdot(\nodename{1})
\end{pspicture}
\end{document}

The macro call \nodename{1} should evaluate to A1B inside the parenthesis of the PSTricks macro. But the above example doesn't work when compiled with latex -> dvips -> ps2pdf. It gives the Postscript error

Error: /undefined in 2-1\relax
Operand stack:
   --dict:51/400(L)--   N@A\numexpr

which shows, that the \numexpr is not evaluated but passed verbatim to the Postscript file.

Is it possible to define the macro \nodename in such a way, that it works in the above example? This must not affect, how the macro is used, but must work as \psdot(\nodename{1}). And the arithmetic expression involves counters.

8

\psdot expands its argument, but \numexpr is not expandable; it expands after \number or \the. In the context of a \numexpr it's better to use \value{mycnnt}, that refers to the “abstract” value, instead of \themycnt that refers to its representation.

\documentclass{article}
\usepackage{pst-node}
\newcounter{mycnt}
\setcounter{mycnt}{2}
\newcommand\nodename[1]{%
  A\the\numexpr\value{mycnt}-#1\relax B}

\begin{document}
\begin{pspicture}[showgrid](5,5)
  \pnode(5,5){A1B}
  \psdot(\nodename{1})
\end{pspicture}
\end{document}
  • Perfect, thanks. I always thought, that \numexpr itself is expandable, so I was a bit puzzled, that my version didn't work. – Christoph Dec 2 '13 at 22:32

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.