4

Consider the following example.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{auto-pst-pdf,pstricks-add}
\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}{%
          \psframe(\rA,\rB)(!\rA\space \height add \length \rB\space add)
        }
      }
    }
   \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}}
    \pcline(!\startX\space \startY\space)(!\startX\space \length \startY\space add)
    \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

enter image description here

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?

2 Answers2

6

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
\def\LoadConstants{}
\newcommand\const[3][6]{%
    \edef\temporary{trunc(#3}%
    \expandafter\FPeval\csname#2\expandafter\endcsname
        \expandafter{\temporary:#1)}%
        \edef\LoadConstants{\LoadConstants
            \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:

enter image description here

\documentclass[pstricks]{standalone}
\usepackage{pstricks-add}
\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)
    \LoadConstants
    \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}{%
                \psframe(\rA,\rB)(!\rA\space height add \rB\space length add)}}}
    \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}}
    \pcline(!startX startY)(!startX length startY add)
    \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.

6

\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{auto-pst-pdf,pstricks-add}
\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 0)(!\rA\space \height add \length)
      \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}