# defining Postscript procedures with global scope

How can I define a Postscript procedure whose definition survives page breaks, that is, which has global scope? Here is a trivial example, where the procedure invocations after a \newpage fail during ps2pdf conversion:

\documentclass{article}
\usepackage{pstricks}

\begin{document}
test
\pstVerb{/printany {256 string cvs print (\string\n) print} def}

\pstVerb{12345 printany}  % works
\pstVerb{(john) printany} % works

\newpage
test
\pstVerb{12345 printany}  % printany undefined
\pstVerb{(john) printany} % --"--

\end{document}

-

You can write the definition with the environment filecontents into an external file and then read it by \pstheader{file}.

See http://tug.org/mailman/htdig/pstricks/2006/004226.html for an example

-
Thanks, this works well! I wonder whether I could add procedure and variable definitions to globaldict at any place in the document, as \pstheader only works in the LaTeX preamble. I would like to be able to store postscript calculation results in postscript variables that can be used later in the document. –  AlexG Jul 17 '12 at 8:22
I have nö Running TeX System for the next three Werks, so I can Not Test ansteigt. Howeverr, nodes are saved globally and you can Save variables in the Same Way. –  Herbert Jul 17 '12 at 11:09

I found a way for global definitions of simple or composite Postscript objects, such as procedures and arrays, to be made at any place in the document. In this way content of Postscript variables that e. g. hold results of calculations can be used later in the document:

\documentclass{article}
\usepackage{pstricks}
\pstVerb{%
true setglobal    % global allocation mode for PS objects
globaldict begin  % following definitions being put in the global dictionary
/printany {256 string cvs print (\string\n) print} def
%   more definitions to follow
end
false setglobal
}

\begin{document}
test
\pstVerb{12345 printany}

\pstVerb{ %global definition of array variable
true setglobal
globaldict /somearray [(john) (linda) (albert) 5678] put
false setglobal
}
\pstVerb{somearray 1 get printany} % linda

\newpage
test
\pstVerb{somearray 3 get printany} % 5678

\end{document}

-
Sorry, how to define a global constant, for example, \pstVerb{/theta 30 def}? I have tried to use true setglobal before /theta but it seemed not to work. –  kiss my armpit Jul 30 '12 at 21:37
true setglobal only sets global allocation mode (global memory); the variable itself must be put into globaldict (global namespace). Thus you must say \pstVerb{true setglobal globaldict /theta 30 put false setglobal} or \pstVerb{true setglobal globaldict begin /theta 30 def end false setglobal} –  AlexG Aug 1 '12 at 6:51