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.

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.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

\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}
share|improve this answer
    
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

 
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.