5

OK, as a good novice I have come across an impressive feature (the ! symbol that allows direct injection of PostScript as coordinate arguments. I can really simplify my work with this feature so I started experimenting with it.

\documentclass[11pt]{book}
\usepackage{graphicx}
\usepackage{pstricks}
\begin{document}%
\begin{pspicture}(-5,-5)(5,5)%
% this works which means I could technically define points as macros
\newcommand{\pAa}{!1.5 1.5}%
\psdot(\pAa)%
% this OTOH does not work
\newcommand{\pAb}[1][1]{!1 #1}
\psdot(\pAb)%
% these don't work either
\psdot(\pAa mul 2)%
\psdot(\pAb{2})%
\end{pspicture}%
\end{document}%

Error message (1/47)

use of \psdot@ii doesn't match it's definition

I guess my question is twofold

  • Why doesn't the later work? I'm pretty sure it has something to do with macro expansions not ocurring in the right order
  • Is there a workaround for defining points and making arithmetic operations using and passing postscript to the pstricks macros?

Update

The example works without default arguments perfectly. I welcome anyone to give an explanation as to why a default would toss the monkey-wrench

  • Your command definition of \pAb takes an optional argument. It therefore has the form \pAb or \pAb[..]. Is this the intent? Is it needed? – Werner Aug 9 '15 at 14:06
  • No it was not a requirement but it seemed quite handy. I have distaste of having to leave empty braces {}. BTW, if there isn't any defaults, it works like a charm – hanzo2001 Aug 9 '15 at 19:01
4

The construction

\psdot(\pAa mul 2)

doesn't work because spaces are ignored after control sequences, so when \pAa is expanded what results is

!1.5 1.5mul 2

which is illegal PostScript code. If you say

\psdot(\pAa\space mul 2)

you get what you expect.

The construction

\psdot(\pAb{2})

doesn't work for two reasons: you have defined \pAb with an optional argument that should be enclosed in brackets, not braces. However, also

\psdot(\pAb[2])

wouldn't work, because the definition is not “fully expandable”: its action is producing the commands for printing !1 2, not simply to those characters.

The correct definition should be

\newcommand{\pAb}[1]{!1 #1}

and calling \psdot{\pAb{2}} will do. No default, however: the argument is mandatory.

Full example:

\documentclass[11pt]{book}
\usepackage{pstricks}

\begin{document}

\begin{pspicture}(-5,-5)(5,5)
%
\newcommand{\pAa}{!1.5 1.5}%
\newcommand{\pAb}[1]{!1 #1}%
%
\psdot(\pAa) 
\psdot(\pAb{1}) 
\psdot(\pAa\space mul 2) 
\psdot(\pAb{2}) 
\end{pspicture}

\end{document}

enter image description here

  • Could you please elaborate on: the definition is not fully expandable? With that info I think your answer would definetly close this case – hanzo2001 Aug 10 '15 at 7:29
  • @hanzo2001 “Fully expandable” means that no assignment is performed; a command with an optional argument requires TeX to look for the next token, in order to see if it's [ and this is done with an assignment (\futurelet). – egreg Aug 10 '15 at 7:57
  • Is there any way to force (La)TeX to fully expand? – hanzo2001 Aug 10 '15 at 10:02
  • @hanzo2001 In some cases, yes. Not with a macro with a single optional argument. – egreg Aug 10 '15 at 10:03
2

You can modify your macros then there will be no need for the \space:

\documentclass[11pt]{book}
\usepackage{graphicx}
\usepackage{pstricks}
\newcommand\pAb[1][1]{\psdot(!1 #1 )}

\begin{document}
    \begin{pspicture}[showgrid](-5,-5)(5,5)
    \newcommand\pAa{!1.5 1.5 }%  The space after 1.5 is important
    \psdot(\pAa)
    \pAb \pAb[3]%
    \psdot(\pAa mul 2)
    \pAb[2]
    \end{pspicture}
\end{document}
  • As a matter of fact, I do need \space. I have created simple Point + Vector macros that can stack and be combined very easily. When one of these macros ends with a macro \space is a must – hanzo2001 Aug 13 '15 at 18:27
  • That depends on how you have defined coordinates with or without a space at the end. – user2478 Aug 14 '15 at 18:25

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.