# How to implement a constant declarating macro to behave differently in preamble and pspicture?

The example below is extremely trivial just to illustrate the problem in the simplest way.

\documentclass{beamer}

\usepackage[nomessages]{fp}
% constants declarator
\newcommand\const[3][6]{%
\edef\temporary{trunc(#3}%
\expandafter\FPeval\csname#2\expandafter\endcsname
\expandafter{\temporary:#1)}%
\noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}%
\ignorespaces}

\usepackage{pstricks,multido}
\SpecialCoor
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=25pt\relax

\const{Side}{root(2,17)}% the square root of 17

\begin{document}
\begin{frame}
\begin{pspicture}(\Side,\Side)
\psframe(!Side dup)\pause
\multido{\i=0+30}{12}{%
\const{temp}{cos(\i*pi/180)}
\rput{\i}(!Side 2 div dup 2 copy temp mul add 3 1 roll \i\space sin mul add){\temp}\pause
}
\end{pspicture}
\end{frame}
\end{document}


## Background:

1. I have to use beamer document class to take advantage of overlays to create a step-by-step animation.
2. I also have to use preview to get a tight output.
3. Using beamer and standalone document classes is not possible to substitute for the 2 requirements above.
4. I have to define constants in TeX world and PostScript world at the same time. The constants are most likely defined by algebraic expressions involving trigonometry and logarithm.
5. If possible constants should be defined in the preamble.
6. But preview discards PostScript constants defined in the preamble. That is why \LoadConstants must be invoked inside pspicture environment to deal with this issue.
7. \LoadConstants by design should be invoked just once.
8. However, sometimes I need additional constants to be defined inside pspicture. Invoking \LoadConstants for the additional constants will make redundancies because \LoadConstants made of concatenated strings of \pstVerb.
9. Mostly I use standalone document class so \LoadConstants can also be invoked in the preamble.

## Objectives:

1. I want to have a single \const to be smart enough to detect whether it is invoked in the preamble or inside pspicture.
2. When \const is invoked in the preamble, it must prepare \LoadConstants by concatenating the corresponding \pstVerb.
3. But when \const is invoked inside pspicture, it just invoke \pstVerb at that location. NOTE: Be careful because \pstVerb has a trailing white space.
4. As a bonus, if you can, please inject \LoadConstants to pspicture environment such that I don't need to invoke \LoadConstants at first.
5. Any idea to improve the code is welcome!
-
You can check if you're past \begin{document} by checking whether \document is \ifx-equal to \@notprerr (but this is not any more true if you use the standalone package). –  egreg Apr 26 '13 at 17:13
@egreg: I don't use standalone package but standalone document class. –  kiss my armpit Apr 26 '13 at 17:28
Oh my ghost! When \i=300 the output is not 0.500000 but 0.499999. What is wrong? –  kiss my armpit Apr 26 '13 at 17:54

\documentclass{beamer}

\usepackage[nomessages]{fp}
% constants declarator
\newcommand\const[3][6]{%
\edef\temporary{trunc(#3}%
\expandafter\FPeval\csname#2\expandafter\endcsname
\expandafter{\temporary:#1)}%
\noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}}

\usepackage{pstricks,multido}
\SpecialCoor
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=25pt\relax
\makeatletter
\def\pst@@picture@i[#1]#2(#3,#4){%
\renewcommand\const[3][6]{%
\edef\temporary{trunc(##3}%
\expandafter\FPeval\csname##2\expandafter\endcsname\expandafter{\temporary:##1)}%
\pstVerb{/##2 \csname##2\endcsname\space def}\ignorespaces}%
\@ifnextchar(%   ignore anything between [] and ()
\makeatother

\const{Side}{root(2,17)}% the square root of 17

\begin{document}
\begin{frame}
\begin{pspicture}(\Side,\Side)
\psframe(!Side dup)\pause
\multido{\i=0+30}{12}{%
\const{temp}{cos(\i*pi/180)}
\rput{\i}(!Side 2 div dup 2 copy temp mul add 3 1 roll \i\space sin mul add){\temp}\pause
}
\end{pspicture}
\end{frame}
\end{document}

-
+1 Thank you but you just did the bonus question that is injecting the \LoadConstants inside pspicture. –  kiss my armpit Apr 26 '13 at 17:41
and added the other part ... –  Herbert Apr 26 '13 at 18:14
There is a bug as mentioned here –  kiss my armpit May 3 '13 at 10:49