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.

How to make ( and ) usable as macro delimiters when the macro is passed as a point in RPN notation?

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\def\f#1{#1 2 exp 1 sub}
\def\g[#1]{#1 2 exp 1 sub}
\def\h(#1){#1 2 exp 1 sub}
\def\i<#1>{#1 2 exp 1 sub}
\def\j|#1|{#1 2 exp 1 sub}

\begin{document}
\begin{pspicture}[showgrid=bottom](-4,-2)(4,4)
    \psplot{-2}{2}{\f x}
    \psdots(!0 \f{0})
    \psdots(!-2 \g[-2])
%   \psdots(!2 \h(2))% <== cannot be compiled!
    \psdots(!1.25 \i<1.25>)
    \psdots(!1.5 \j|1.5|)
\end{pspicture}
\end{document}
share|improve this question
1  
In \psdots(!2 \h(2)) the first ) is the delimiter for the argument to \psdots. You can use \edef\temp{\noexpand\psdots(!2 \h(2))}\temp, but of course a different delimiter is better. –  egreg Aug 24 '12 at 19:55
    
@MarcoDaniel: #1 doesn't need a \space –  Herbert Aug 25 '12 at 6:28
add comment

2 Answers

up vote 8 down vote accepted

that is the default problem with TeX and how it reads tokens. It looks for the first ( and the first ), the reason why you cannot use that kind of notation. It is a similiar problem as the well known [...[...]]. However, you can use the algebraic notation:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\def\f#1{#1 2 exp 1 sub}
\def\g[#1]{#1 2 exp 1 sub}
\def\h(#1){#1^2-1}

\begin{document}
\begin{pspicture}[showgrid=bottom](-4,-2)(4,4)
\psplot{-2}{2}{\f x}
\psdots(!0 \f{0})(!-2 \g[-2])(*2 {\h(x)})(*1 {x^2-1})
\end{pspicture}
\end{document}

as an alternative you can use

\def\h{dup 2 exp 1 sub}
\psdot(!2 \h)

it makes no sense to use an argument for h which is already present on top of the stack. Same for \g. The other way roung is (which also works):

\def\h(#1){#1 dup 2 exp 1 sub}
\psdot(!{\h(2)})

However, if one wants the double parentheses then it can be solved with

\psdot(!2 {\h(2)}/Y ED Y)
share|improve this answer
    
Could you elaborate a bit about /Y ED Y? –  I am who I say I am Aug 25 '12 at 12:30
1  
{..} /Y ED is a definition like /Y {...} def where ED is a short name for exch def. With {\h(2)}/Y ED Y the value of \h(2) is saved as Y which is then called. –  Herbert Aug 26 '12 at 7:54
add comment

As Herbert notes, \psdots takes the first right parenthesis as a delimiter, not the second. This common TeX problem is solved in the xparse package. Here I save the definition of \psdots and redefine it with xparse to take a "required" argument "r" delimited by ( and ). Those delimiters will be paired, so #1 is !2 \h(2) as wanted. I then feed this #1 to the saved \psdots within parentheses: the extra set of braces around #1 ensure that the correct argument is used.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\usepackage{xparse}
\let\savedpsdots\psdots
\RenewDocumentCommand {\psdots} {r()} {\savedpsdots({#1})}

\def\f#1{#1 2 exp 1 sub}
\def\g[#1]{#1 2 exp 1 sub}
\def\h(#1){#1 2 exp 1 sub}
\def\i<#1>{#1 2 exp 1 sub}
\def\j|#1|{#1 2 exp 1 sub}

\begin{document}
\begin{pspicture}[showgrid=bottom](-4,-2)(4,4)
    \psplot{-2}{2}{\f x}
    \psdots(!0 \f{0})
    \psdots(!-2 \g[-2])
    \psdots(!2 \h(2))% <== could be compiled!
    \psdots(!1.25 \i<1.25>)
    \psdots(!1.5 \j|1.5|)
\end{pspicture}
\end{document}

A more general solution would be to redefine \pst@object which seems to underlie a few pst-plot commands.

share|improve this answer
    
PSTricks can be used with TeX and LaTeX, but xparse only for LaTeX, right? And it is easier to solve it on PS level: \psdot(!2 {\h(2)}/Y ED Y) –  Herbert Aug 25 '12 at 11:22
    
@Herbert You're right, although it wouldn't be too hard to make xparse and expl3 available to TeX if people ask for it. The simplest solution if we're allowed to change the OP's input is \psdots({...}), since TeX will discard those braces when taking a delimited argument. –  Bruno Le Floch Aug 25 '12 at 18:42
    
sure, but then we need an additional expanding which is not easy to realize. The argument can have several optional parameters. –  Herbert Aug 25 '12 at 18:57
    
@Herbert: I don't understand your comment: \pst@@getcoors is defined with \def\pst@@getcoors(#1){...}. When TeX sees \pst@@getcoors({...}), it feeds the argument ... (without surrounding braces) to \pst@@getcoors, which is precisely what the OP wants, isn't it? –  Bruno Le Floch Aug 25 '12 at 19:07
    
yes when we have not activated special coordinates with an active ! * | ; >. That is the default case which is not used in the above example. –  Herbert Aug 25 '12 at 19:17
show 1 more comment

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.