[Tex/LaTex] PostScript dictionary for? How to know whether or not I need it

postscriptpstrickspstricks-add

Consider the following code,

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}
\psset{linecolor=red}
\pstVerb
{
    /updateCP {2 copy CPy add /CPy ED CPx add /CPx ED} bind def 
    /updateA  {1 sub sqrt 1 exch atan Angles add /Angles ED} bind def 
}

\def\WithoutDict{%
    \begin{pspicture}[showgrid](-5,-5)(5,5)
    \multido{\io=1+1}{15}
    {%
        \pstVerb{/Angles 0 def /CPx 1 def /CPy 0 def}% reset
        \def\points{(0,0)(1,0)}% reset
        \multido{\ii=1+1}{\io}{\xdef\points{\points(!1 \ii\space updateA Angles PtoC updateCP)}}
        \expandafter\psrline\points
        \psline(!CPx CPy)
    }
    \end{pspicture}}

\def\WithDict{%
    \begin{pspicture}[showgrid](-5,-5)(5,5)
    \multido{\io=1+1}{15}
    {%
        \pstVerb{tx@Dict begin /Angles 0 def /CPx 1 def /CPy 0 def end}% reset
        \def\points{(0,0)(1,0)}% reset
        \multido{\ii=1+1}{\io}{\xdef\points{\points(!1 \ii\space updateA Angles PtoC updateCP)}}
        \expandafter\psrline\points
        \psline(!CPx CPy)
    }
    \end{pspicture}}

\begin{document}
\WithoutDict
\WithDict
\end{document}

which produces the following outputs.

enter image description hereenter image description here

Question

\WithoutDict is different from \WithDict only in tx@Dict that \WithoutDict does not use. The use of tx@Dic in \WithDict was done by Christoph in his answer here. I really don't understand why I need it.

So the question is,

  1. What is PostScript dictionary for?
  2. How to use it in a simple but not trivial example?
  3. How do I know whether or not I need it?

Hopefully the answer can be used as a crash-course tutorial.

Best Answer

Postscript dictionaries implement the concept of name spaces which allow for control of variable visibility. To see the command line output from the Postscript interpreter, run ps2pdf on the PS file produced by dvips.

\documentclass{article}
\usepackage{pstricks}

\begin{document}
Dictionaries implement the concept of name spaces to control variable visibility
\pstVerb{
/myvar (hello!) def % `global' definition
/mydict 1 dict def
mydict begin % open namespace
  /myvar (goodbye!) def % `local' definition
  myvar == % print local value
end % close namespace
myvar == % print global value
mydict begin % reopen name space
  myvar == % print local value once more
end
}

\end{document}

If you don't know which of the existing dictionaries from the PSTricks packages you will have to put on the dictionary stack, you can always define your variables in a new dictionary of your own. Make your dictionary the current one (and the variables defined therein available) by putting it on the dictionary stack when needed.

The dictionary and the operand stacks are separate things. Putting and popping dictionaries on the dictionary stack don't affect the operand stack:

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

\begin{document}

\psset{linecolor=red}
\pstVerb
{
    /updateCP {2 copy CPy add /CPy ED CPx add /CPx ED} bind def
    /updateA  {1 sub sqrt 1 exch atan Angles add /Angles ED} bind def
}

\begin{pspicture}[showgrid](-5,-5)(5,5)
\multido{\io=1+1}{15}
{%
    \pstVerb{
      /mydict 1 dict def % roll your own dict
      mydict begin       % put it on top of the dict stack
        /Angles 0 def /CPx 1 def /CPy 0 def %define your variables
      end % pop your dict from the dict stack (it doesn't get lost!)
    }% reset
    \def\points{(0,0)(1,0)}% reset
    \multido{\ii=1+1}{\io}{\xdef\points{\points(!1 \ii\space
        mydict begin % make your dict the current (i.e. top-level) one by
                     %putting it on the dict stack 
          updateA Angles PtoC updateCP % work on your variables
        end % remove it from the dict stack, point coordinates remain on
            % the operand stack
    )}}
    \expandafter\psrline\points
    \psline(!mydict begin % make your dict the current dict
      CPx CPy
    end)                  % and remove it from dict stack, CPx CPy still on the operand stack
}
\end{pspicture}

\end{document}
Related Question