PSTricks 常量的定义

PSTricks 常量的定义

考虑以下示例。

代码

% 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}

输出

在此处输入图片描述

问题

为什么我不能更换

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

经过

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

为了摆脱一些\space

答案1

\numexpr仅适用于整数,但您的\height是 5.4。您可以改用\dimexpr并使用当前单位\psunit。但是,我不会使用的值,Xstart因为您可以从平原中心再次执行此操作:

\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}

答案2

因为\numexpr是整数。所以更好的(可能是最好的)解决方案如下。

我有一个包pst-sex(StackEXchange 的 postscript 常量声明器),有一天我会将其上传到 CTAN。

% ========================================================
% 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

此包可用于同时在 TeX 和 PostScript 世界中声明常量。例如:

您可以在序言中声明以下常量,

\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}

并在任何地方使用它们,无论是在 TeX 世界还是 PostScript 世界。

例如:

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

或者

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

preview对于包丢弃 PostScript 常量的特殊情况,我们可以通过\LoadConstants在内部调用来解决这个问题pspicture

pst-sex实际操作:

在此处输入图片描述

\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}

笔记:

我真的希望这里有人以他/她自己的名义将这个包上传到 CTAN 并维护它。

相关内容