# Definition of PSTricks constant

Consider the following example.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage[locale=DE]{siunitx}

\definecolor{nicered}{rgb}{0.796,0.255,0.329}

\begin{document}

\begin{figure}
\def\WidthPallet{46 }
\def\length{22.8 }
\def\height{5.4 }
\def\rows{2 }
\def\EachRow{8 }
\edef\faktor{\the\numexpr\EachRow/\rows\relax\space}
%\edef\startX{\the\numexpr(\WidthPallet-\EachRow*\height)/2\relax\space}
\def\startX{\WidthPallet \EachRow \height mul sub 2 div}
%\edef\startX{\the\numexpr(\WidthPallet-\rows*\length)/2\relax\space}
\def\startY{\WidthPallet \rows \length mul sub 2 div}
\centering
\psset{
unit=0.26cm
}
\begin{pspicture}(\WidthPallet,\WidthPallet)
\psframe[
fillstyle=solid,
fillcolor=brown
](0,0)(\WidthPallet,\WidthPallet)
\psset{
fillstyle=solid,
fillcolor=nicered,
dimen=middle
}
\rput(!\startX\space \startY\space){%
\multido{\rA=0+\height}{\EachRow}{%
\multido{\rB=0.0+\length}{\rows}{%
}
}
}
\psset{
linestyle=dashed,
fillcolor=nicered,
arrows=<->,
nrot=:U,
offset=-19pt
}
\pcline(!\startX\space 0.6 \WidthPallet mul)(!\startX\space \height add 0.6 \WidthPallet mul)
\ncput*{\scriptsize\SI{\height}{\cm}}
\ncput*{\SI{\length}{\cm}}
\pcline(!0 0.65 \WidthPallet mul)(!\WidthPallet 0.65 \WidthPallet mul)
\ncput*{\SI{\WidthPallet}{\cm}}
\end{pspicture}
\end{figure}

\end{document}


Output

Question

Why can't I replace

\def\startX{\WidthPallet \EachRow \height mul sub 2 div}
\def\startY{\WidthPallet \rows \length mul sub 2 div}


by

\edef\startX{\the\numexpr(\WidthPallet-\EachRow*\height)/2\relax\space}
\edef\startX{\the\numexpr(\WidthPallet-\rows*\length)/2\relax\space}


in order to get rid of some of the \spaces?

-

\numexpr works only with integers but your \heightis 5.4. You can use \dimexpr instead and use the current unit \psunit. However, I wouldn't use the values for Xstart because you can do it again from the center of the plaine:

\documentclass{article}

\usepackage[locale=DE]{siunitx}
\definecolor{nicered}{rgb}{0.796,0.255,0.329}
\begin{document}

\def\WidthPallet{46 }
\def\length{22.8 }
\def\height{5.4 }
\def\EachRow{8 }
\psset{unit=0.26cm}
\begin{pspicture}(\WidthPallet,\WidthPallet)
\psframe[fillstyle=solid,fillcolor=brown](\WidthPallet,\WidthPallet)
\psset{fillstyle=solid,fillcolor=nicered,dimen=middle}
\multido{\iA=0+1,\rA=0.0+\height}{\numexpr\EachRow/2}{%
\rput(!\WidthPallet 2 div dup){% relative to the center
\psframe(!\rA\space neg 0)(!\rA\space neg \height sub \length)
\psframe(!\rA\space 0)(!\rA\space \height add \length neg)
\psframe(!\rA\space neg 0)(!\rA\space neg \height sub \length neg)
}
}
\psset{linestyle=dashed,fillcolor=nicered,arrows=<->,nrot=:U}
\pcline[offset=3](!\WidthPallet 2 div \height 4 mul sub \length)%
(!\WidthPallet 2 div \height 3 mul sub \length)
\ncput*{\scriptsize\SI{\height}{\cm}}
\pcline(!\WidthPallet 2 div \height 3.5 mul sub 0)%
(!\WidthPallet 2 div \height 3.5 mul sub \length)
\ncput*{\SI{\length}{\cm}}
\pcline(!0 0.65 \WidthPallet mul)(!\WidthPallet 0.65 \WidthPallet mul)
\ncput*{\SI{\WidthPallet}{\cm}}
\end{pspicture}

\end{document}

-
I'm not exactly sure how to do it; it is probably some trivial mistake by me (I tried \psset{unit=\psunit}). Can I make you reuse my code and implement the corrections? –  Svend Tveskæg Apr 25 '13 at 8:58
see my edit, how I would do it. –  Herbert Apr 25 '13 at 9:24
Spot on! I can tell you, that you are the one who suggested the other method, when you helped be with the drawing sometime last year. :) –  Svend Tveskæg Apr 25 '13 at 13:59
nobody is perfect and maybe tomorrow I'll have another idea ... :-) –  Herbert Apr 25 '13 at 14:17
Indeed nobody is, but when it comes to PSTricks, you are darn close. ;) –  Svend Tveskæg Apr 25 '13 at 14:19

Because \numexpr for integers. So the better (might be the best) solution is as follows.

I have a package pst-sex (postscript constant declarator for StackEXchange) that I will upload to CTAN someday.

% ========================================================
% Copyright (c) 2013 Bugbusters. All rights reservered.
% LPPL LaTeX Public Project License
% ========================================================
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{pst-sex}[2013/04/23 v3.141592654 Constant declator for StackEXchange]
\RequirePackage[nomessages]{fp}

% removing a trailing space in the original fp.sty
\def\FP@pow#1#2#3{%
% #1 macro, which gets the result
% #2 base
% #3 exponent
%
\FP@beginmessage{POW}%
%
{\def\FP@beginmessage##1{}%
\def\FP@endmessage##1{}%
%
\FPifzero{#2}%
\FP@pow@zero{#3}%
\else%
\FPln\FP@tmpd{#2}%
\FPmul\FP@tmpd\FP@tmpd{#3}%
\FPexp\FP@tmp\FP@tmpd%
\fi%
%
\global\let\FP@tmp\FP@tmp%
}%
%
\FP@endmessage{}%
%
\let#1\FP@tmp%
}

% 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
}
\endinput
% pst-sex.sty


This package is useful to declare constants in both TeX and PostScript worlds at the same time. For example:

You can declare the following constants in the preamble,

\const[1]{WidthPallet}{46}% 1 digit after decimal
\const[1]{length}{22.8}
\const[1]{height}{5.4}
\const[0]{rows}{2}% 0 digit after decimal
\const[0]{EachRow}{8}

\const{startX}{(WidthPallet-EachRow*height)/2}% 6 digit after decimal (default)
\const{startY}{(WidthPallet-rows*length)/2}


and make use of them everywhere, either in TeX world or PostScript world.

For example:

\begin{pspicture}(\WidthPallet,\WidthPallet)% TeX world


or

\psframe[fillcolor=brown](!WidthPallet WidthPallet)% PostScript world


For a special situation where preview package discards the PostScript constansts, we can solve this issue by invoking \LoadConstants inside pspicture.

## pst-sex in action:

\documentclass[pstricks]{standalone}
\usepackage[locale=DE]{siunitx}
\usepackage{pst-sex}

\definecolor{nicered}{rgb}{0.796,0.255,0.329}

\const[1]{WidthPallet}{46}
\const[1]{length}{22.8}
\const[1]{height}{5.4}
\const[0]{rows}{2}
\const[0]{EachRow}{8}

\const{startX}{(WidthPallet-EachRow*height)/2}
\const{startY}{(WidthPallet-rows*length)/2}

\psset{unit=0.26cm}

\begin{document}

\begin{pspicture}(\WidthPallet,\WidthPallet)
\psset{fillstyle=solid}
\psframe[fillcolor=brown](!WidthPallet WidthPallet)
\psset{fillcolor=nicered,dimen=middle}
\rput(!startX startY){%
\multido{\rA=0.0+\height}{\EachRow}{%
\multido{\rB=0.0+\length}{\rows}{%
\psset{linestyle=dashed,arrows=<->,nrot=:U,offset=-19pt}
\pcline(!startX 0.6 WidthPallet mul)(!startX height add 0.6 WidthPallet mul)
\ncput*{\scriptsize\SI{\height}{\cm}}
\ncput*{\SI{\length}{\cm}}
\pcline(!0 0.65 WidthPallet mul)(!WidthPallet 0.65 WidthPallet mul)
\ncput*{\SI{\WidthPallet}{\cm}}
\end{pspicture}

\end{document}


## Note:

I really hope someone here upload this package to CTAN on his/her own behalf as well as maintain it.

-
I see. Thank you! –  Svend Tveskæg Apr 25 '13 at 8:35
This looks really nice! (I will definitely have a look at this tonight.) Any chance I can make you implement the corrections that Herbert suggested for me in my code (as asked in the comment to Herbert's answer)? –  Svend Tveskæg Apr 25 '13 at 8:53
Ok. Any chance I can make you do both, then? –  Svend Tveskæg Apr 25 '13 at 8:57
This is really nice! Thanks a lot, @Bugbusters. –  Svend Tveskæg Apr 25 '13 at 13:54
I come to like your package more and more, when I look at it. If you don't want your name on CTAN, can't you just use a fake name, say, Peter Peterson or something? –  Svend Tveskæg May 3 '13 at 7:50