[pstricks] PostScript code within PSTricks

Denis Girou Denis.Girou at idris.fr
Tue Jun 12 22:12:46 CEST 2001


>>>>> "Vinay.Kanetkar" == Vinay Kanetkar <vkanetka at uoguelph.ca> writes:

    Vinay.Kanetkar> This may be trivial question for those familiar with Postscript 
    Vinay.Kanetkar> but in following code I am trying to add 0.1 to argument. 
    Vinay.Kanetkar> I am not having much success in do it. 

    Vinay.Kanetkar> In particular, following line:
    Vinay.Kanetkar> \rput(#1,! #2\space\a add}){\tiny 23.8\%}

    Vinay.Kanetkar> \documentclass{article}
    Vinay.Kanetkar> \usepackage[dvips]{graphics}     % From LaTeX distribution
    Vinay.Kanetkar> \usepackage{pst-all}      % From PSTricks

    Vinay.Kanetkar> \newpsobject{showgrid}{psgrid}{subgriddiv=1,griddots=10,gridlabels=6pt}
    Vinay.Kanetkar> \textheight=23cm
    Vinay.Kanetkar> \topmargin=-2cm
    Vinay.Kanetkar> \textwidth=16cm
    Vinay.Kanetkar> \oddsidemargin=0cm
    Vinay.Kanetkar> \evensidemargin=0cm
    Vinay.Kanetkar> \def\mydata1{.25}
    Vinay.Kanetkar> \def\myang{35 45}
    Vinay.Kanetkar> \def\a{.1}

    Vinay.Kanetkar> \newcommand{\myline}[3]{%
    Vinay.Kanetkar> \SpecialCoor
    Vinay.Kanetkar> \psframe[linewidth=1pt,fillstyle=solid,fillcolor=#3](! #1 \mydata1\space
    Vinay.Kanetkar> add 0)(! #1 \mydata1\space sub #2)
    Vinay.Kanetkar>           \pscustom{ \SpecialCoor 
    Vinay.Kanetkar>           %\rput(#1 ! #2\space\a add}){\tiny 23.8\%}  %%%%%%   This line
    Vinay.Kanetkar> does not work. 
    Vinay.Kanetkar> } 
    Vinay.Kanetkar> }

    Vinay.Kanetkar> \begin{document}

    Vinay.Kanetkar> \begin{figure}[!htbp]
    Vinay.Kanetkar>   \centering
    Vinay.Kanetkar> \begin{pspicture}(0,0)(3,3)\showgrid
    Vinay.Kanetkar> \myline{1}{1.376}{gray}

    Vinay.Kanetkar> \psline{->>}(3; ! \myang\space add)

    Vinay.Kanetkar> \end{pspicture}
    Vinay.Kanetkar> \end{figure}
    Vinay.Kanetkar> \end{document}

  Ulrich Dirr already give you some insights about your code, but there are
several remarks to formulate (using PostScript expressions for coordinates
is rather easy, but nevertheless not "trivial" at beginning). And some remarks
are general and doesn't concern specially your test case.

  - Never use digits inside macro names (\mydata1): this is a definitive
source of pitfalls with (La)TeX

  - Take care to side effects when defining you own variables. In LaTeX,
\newcommand has a protection mechanism to avoid to erase existing macros
by inadvertence. For instance, \a is an existing macro that you erase
(using \show\a before the redefinition will show you the existing
definition).

  - Don't put commands like \SpecialCoor inside a macro, but outside,
to avoid to execute it each time. \SpecialCoor just switch the mechanism
to analyse coordinates, allowing various ways to define them. By default,
you can't use them because this analysis step slow down the compilation
process a little bit (nevertheless, you will not notice a difference with
a not too old CPU and in any case only if your code handle thousands of
coordinates...)

  - The usage of \pscustom is not required in your case. The \rput macro
can directly handle it coordinates as PostScript expressions.

  - (#1 ! #2\space\a add}) is an incorrect syntax, as Ulrich said already:

    * correct syntax is (! ... ...)
    * \space is not useful after #2 but require after \a (and any other
(La)TeX macros)
    * } is of course to suppress

  - \psline{->>}(3; ! \myang\space add)

    * You can't use both polar and PostScript notations for the same
coordinate
    * In your case you must compute both coordinates in PostScript (there is
such an example page 72). So, I suppose:
\psline{->>}(! 3 \myang\space add cos mul 3 \myang\space add sin mul)


  To summary:

\documentclass{article}

\usepackage{pstricks}

\newpsobject{showgrid}{psgrid}{subgriddiv=1,griddots=10,gridlabels=6pt}

\def\mydata{.25}
\def\myang{35 45}
\def\mya{.1}

\newcommand{\myline}[3]{%
\psframe[linewidth=1pt,fillstyle=solid,fillcolor=#3]
        (! #1 \mydata\space add 0)(! #1 \mydata\space sub #2)
\rput(! #1 #2 \mya\space add){\tiny 23.8\%}}

\SpecialCoor

\pagestyle{empty}

\begin{document}

\begin{figure}[!htbp]
  \centering
  \begin{pspicture})(3,3)\showgrid
    \myline{1}{1.376}{gray}
%
    \psline{->>}(! 3 \myang\space add cos mul 3 \myang\space add sin mul)
  \end{pspicture}
\end{figure}

\end{document}


D.G.



More information about the PSTricks mailing list