[pstricks] Suggestions for \psPrintValue

Buddy Ledger buddyledger at gmail.com
Sun Apr 26 22:42:46 CEST 2009


>  Buddy, it's a good idea! But there is one thing left. When usind
>  descimals=2 you should always get two decimals 12.799344 -> 12.80,
>  but your solution gives 12.8. However, I changed the code to take the
>  comma and algebraic option into account.

Herbert,

That is great! Thanks.

I've been trying to incorporate another option, where horizontal space 
can be preserved to create compound objects without the need for manual 
makebox{} commands.  My attempts have been unsuccessful, but I think it 
is possible.  knowing \psk at decimals and \psk at fontscale we should be able 
to calculate the space required for the \psPrintValue object.  For 
scientific notation the character space required is roughly 
3+\psk at decimals+floor(log(expon))+1 and for regular numbers 
floor(log(#))+\psk at decimals (FP package crashed on me when trying 
this).  This could also be done with count registers if you could read 
an input value one character at a time and test for decimal point or "E" 
along the way (I don't know how to do this). 

The motivation here is to allow \psPrintValue to mesh properly with text 
or other \psPrintValue objects.  I've included an example of my failed 
attempt at a generic solution.  As well I have included another routine 
which demontrates what I want to be able to achieve, this routine is 
very specific and would have to be re-written to achieve different 
output, whereas if a space preservation option was possible within 
\psPrintValue generic objects could be created using the basic 
\psPrintValue routine.

Thanks

\documentclass[]{article}
\usepackage{pst-all}
\usepackage{pst-func}
\usepackage{pstricks-add}
\usepackage{amsfonts}
\usepackage{fp}
\parindent=0pt

\makeatletter
\define at key[psset]{pst-func}{SpSc}[0]{\def\psk at SpSc{#1}}
\define at boolkey[psset]{pst-func}[Pst@]{round}[true]{}%
\define at boolkey[psset]{pst-func}[Pst@]{prespace}[true]{}%
\define at boolkey[psset]{pst-func}[Pst@]{science}[true]{%
  \ifPst at science\def\psk at Scin{true }\else\def\psk at Scin{false }\fi}
\psset[pst-func]{science=false,round=false,prespace=false,SpSc=1}
%
\newlength{\rubberspace}
%
\def\psPrintValueNew{\pst at object{psPrintValueNew}}
\def\psPrintValueNew at i#1{\expandafter\psPrintValueNew at ii#1,,\@nil}
\def\psPrintValueNew at ii#1,#2,#3\@nil{%  #1,#2 only for algebraic code
  \begin at SpecialObj
  \addto at pscode{
     /mfont { \psk at PSfont findfont \psk at fontscale scalefont setfont }
     bind def
     /mfontexp { \psk at PSfont findfont \psk at fontscale 1.2 div scalefont
     setfont } bind def
     /s1 { /Symbol findfont \psk at fontscale scalefont setfont } bind def
     \ifPst at algebraic
        /x #1 def
        /Func (#2) tx at AlgToPs begin AlgToPs end cvx def
        Func
     \else #1 \fi
     /value ED
     \psk at Scin {
       value 0 ne { value log floor cvi /expon ED }{ /expon 0 def } ifelse
       value 10 expon exp div
       \psk at decimals -1 gt { 10  \psk at decimals exp dup 3 1 roll mul
         \ifPst at round round \else cvi \fi  exch div } if
       \psk at decimals 0 eq { cvi } if /numb ED
       expon \psk at valuewidth string cvs /expon exch def
       numb \psk at valuewidth string cvs /Output exch def
       \ifPst at comma   % do we have to change dot to comma
         0 1 Output length 1 sub {
           /Index exch def
           Output Index get 46 eq { Output Index 44 put } if
         } for
       \fi
       /txspc \psk at fontscale 4 div def
       \psk at xShift\space 0 moveto mfont Output show
       txspc 0 rmoveto s1 (\string\264) show
       txspc 0 rmoveto mfont (10) show
       txspc 2 div txspc 1.5 mul rmoveto mfontexp expon show }
     { value
       \psk at decimals -1 gt { 10 \psk at decimals exp dup 3 1 roll mul
         \ifPst at round round \else cvi \fi exch div } if
       \psk at decimals 0 eq { cvi } if %inserted to handle decimals=0
       \psk at valuewidth string cvs /Output ED
       \ifPst at comma      % do we have tochange dot to comma
         0 1 Output length 1 sub {
           /Index exch def
           Output Index get 46 eq { Output Index 44 put } if
         } for
       \fi
       \psk at xShift\space 0 moveto mfont Output show
     } ifelse
  }%
  \gdef\test{0}
  \ifPst at prespace
     \gdef\test{1}
     \gdef\tempA{}
     \gdef\tempB{}
     \gdef\tempC{}
    \ifPst at science
%     \FPadd\tempA{5}{\number\psk at decimals}
%     \FPmul\tempA{\tempA}{10}
%     \gdef\tempC{\tempA}
    \gdef\tempC{5}
    \else
%      if test #1 ifnumber
%      \def\getn{#1}
%      else
%      \GetSciNum(#1){\getn}
%      fi
%     \FPln\tempA{\number\getn}
%     \FPln\tempB{10}
%     \FPtrunc\tempA{\tempA}{3}
%     \FPtrunc\tempB{\tempB}{3}
%     \FPdiv\tempC{\tempA}{\tempB}
%     \FPtrunc\tempC{\tempC}{0}
%     \FPadd\tempC{\tempC}{2}
%     \FPadd\tempC{\tempC}{\number\psk at decimals}
%     \FPmul\tempC{\tempC}{10}
     \gdef\tempC{5}
    \fi
  \fi
  \end at SpecialObj%
%
\ifnum\test=1\relax
  \settowidth{\rubberspace}{W}
  \makebox[\tempC\rubberspace]{}
\fi
}
%\def\GetSciNum(#1E#2)#3#4{
%\FPpow\tempA{10}{#2}
%\FPmul\tempB{\tempA}{#1}
%\FPround\tempB{\tempB}{#3}
%\gdef#4{\tempB}
%}%

\define at key[psset]{pst-func}{yShift}[0]{\def\psk at yShift{#1}}
\define at boolkey[psset]{pst-func}[Pst@]{xyPlotLabels}[true]{}%
\psset[pst-func]{yShift=0,xyPlotLabels=true}

\def\psPrintCoor{\pst at object{psPrintCoor}}
\def\psPrintCoor at i(#1)#2#3{%
  \ifPst at xyPlotLabels
  \begin at SpecialObj
  \use at par \pst at getcoor{#1}\pst at tempA
  \addto at pscode{
     /mfont {\psk at PSfont findfont \psk at fontscale scalefont setfont} bind def
     /mfontexp { \psk at PSfont findfont \psk at fontscale 1.2 div scalefont
     setfont } bind def
     /s1 {/Symbol findfont \psk at fontscale scalefont setfont} bind def
     /ttxspc \psk at fontscale 4 div def
     /txspc ttxspc 4 div def
%    
     \psk at Scin {%
     \pst at tempA pop \pst at number\psyunit div log floor
     cvi /xcoorexp exch def   
     \pst at tempA pop \pst at number\psyunit div 10 xcoorexp exp div
     \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /xcoor exch def
     \pst at tempA exch pop \pst at number\psyunit div log floor
     cvi /ycoorexp exch def   
     \pst at tempA exch pop \pst at number\psyunit div 10 ycoorexp exp div
     \psk at decimals -1 gt
     { 10  \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /ycoor exch def
     xcoorexp \psk at valuewidth string cvs /xcoorexp exch def
     ycoorexp \psk at valuewidth string cvs /ycoorexp exch def
     xcoor \psk at valuewidth string cvs /xcoor exch def
     ycoor \psk at valuewidth string cvs /ycoor exch def
     #2 \pst at tempA pop \pst at number\psyunit div mul 0 ne
     {#2 \pst at tempA pop \pst at number\psyunit div mul log floor
     cvi /delxexp exch def
     #2 \pst at tempA pop \pst at number\psyunit div mul 10 delxexp exp div
     \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /delx exch def
     delxexp \psk at valuewidth string cvs /delxexp exch def
     delx \psk at valuewidth string cvs /delx exch def} if
     #3 \pst at tempA exch pop \pst at number\psyunit div mul 0 ne
     {#3 \pst at tempA exch pop \pst at number\psyunit div mul log floor
     cvi /delyexp exch def
     #3 \pst at tempA exch pop \pst at number\psyunit div mul 10 delyexp exp div
     \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /dely exch def
     delyexp \psk at valuewidth string cvs /delyexp exch def
     dely \psk at valuewidth string cvs /dely exch def} if}
     %ELSE
     {\pst at tempA pop \pst at number\psyunit div \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /xcoor exch def
     \pst at tempA exch pop \pst at number\psyunit div \psk at decimals -1 gt
     { 10  \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /ycoor exch def
     xcoor \psk at valuewidth string cvs /xcoor exch def
     ycoor \psk at valuewidth string cvs /ycoor exch def
     %
     #2 \pst at tempA pop \pst at number\psyunit div mul \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /delx exch def
     #3 \pst at tempA exch pop \pst at number\psyunit div mul \psk at decimals -1 gt
     { 10   \psk at decimals exp dup 3 1 roll mul round exch div } if
     \psk at decimals 0 eq {cvi} if /dely exch def
     delx \psk at valuewidth string cvs /delx exch def
     dely \psk at valuewidth string cvs /dely exch def} ifelse
%     
     newpath
     \psk at xShift\space \psk at yShift\space moveto s1 (\string\050) show  
     ttxspc 0 rmoveto mfont xcoor show
       \psk at Scin {
       txspc 0 rmoveto mfont (\string\264) show
       txspc 0 rmoveto mfont (10) show
       txspc ttxspc rmoveto mfontexp xcoorexp show
       } if
     #2 \pst at tempA pop \pst at number\psyunit div mul 0 ne
     {\psk at Scin {
       ttxspc ttxspc neg rmoveto s1 (\string\261) show
       ttxspc 0 rmoveto mfont delx show
       txspc 0 rmoveto mfont (\string\264) show
       txspc 0 rmoveto mfont (10) show
       txspc ttxspc rmoveto mfontexp delxexp show
       ttxspc ttxspc neg rmoveto s1 (,) show}
       {ttxspc 0 rmoveto s1 (\string\261) show
       ttxspc 0 rmoveto mfont delx show
       ttxspc 0 rmoveto s1 (,) show} ifelse}
     {\psk at Scin {
       ttxspc ttxspc neg rmoveto s1 (,) show}
      {ttxspc 0 rmoveto s1 (,) show} ifelse} ifelse
%start on ycoor    
     ttxspc 0 rmoveto mfont ycoor show
       \psk at Scin {
       txspc 0 rmoveto mfont (\string\264) show
       txspc 0 rmoveto mfont (10) show
       txspc ttxspc rmoveto mfontexp ycoorexp show
       } if     
     #3 \pst at tempA exch pop \pst at number\psyunit div mul 0 ne
     {\psk at Scin {
       ttxspc ttxspc neg rmoveto s1 (\string\261) show
       ttxspc 0 rmoveto mfont dely show
       txspc 0 rmoveto mfont (\string\264) show
       txspc 0 rmoveto mfont (10) show
       txspc ttxspc rmoveto mfontexp delyexp show
       ttxspc ttxspc neg rmoveto s1 (\string\051) show}
       {ttxspc 0 rmoveto s1 (\string\261) show
       ttxspc 0 rmoveto mfont dely show
       ttxspc 0 rmoveto s1 (\string\051) show} ifelse}
     {ttxspc 0 rmoveto s1 (\string\051) show} ifelse
  }%
  \end at SpecialObj%
  \fi
}
\makeatother

\begin{document}

psPrintValueNew Output (Latex and Postscript) \newline
$($\psPrintValueNew[decimals=3,round,prespace,science]{1.252525}$\pm$
\psPrintValueNew[decimals=3,round,prespace,science]{0.1252525}$,$
\psPrintValueNew[decimals=3,round,prespace,science]{2.252525}$\pm$
\psPrintValueNew[decimals=3,round,prespace,science]{0.2252525}$)$\newline
$($\psPrintValueNew[decimals=1,round,prespace,science]{1.252525}$\pm$
\psPrintValueNew[decimals=1,round,prespace,science]{0.1252525}$,$
\psPrintValueNew[decimals=1,round,prespace,science]{2.252525}$\pm$
\psPrintValueNew[decimals=1,round,prespace,science]{0.2252525}$)$\newline
$($\psPrintValueNew[decimals=-1,round,prespace,science]{1.252525}$\pm$
\psPrintValueNew[decimals=-1,round,prespace,science]{0.1252525}$,$
\psPrintValueNew[decimals=-1,round,prespace,science]{2.252525}$\pm$
\psPrintValueNew[decimals=-1,round,prespace,science]{0.2252525}$)$\newline
$($\psPrintValueNew[decimals=1,round,prespace]{1.252525}$\pm$
\psPrintValueNew[decimals=1,round,prespace]{0.1252525}$,$
\psPrintValueNew[decimals=1,round,prespace]{2.252525}$\pm$
\psPrintValueNew[decimals=1,round,prespace]{0.2252525}$)$\newline

psPrintCoor Output (Postscript) \newline
\psPrintCoor[decimals=3,round,science](1.252525,2.252525){0.1}{0.1}\newline
\psPrintCoor[decimals=1,round,science](1.252525,2.252525){0.1}{0.1}\newline
\psPrintCoor[decimals=-1,round,science](1.252525,2.252525){0.1}{0.1}\newline
\psPrintCoor[decimals=1,round](1.252525,2.252525){0.1}{0.1}\newline
\psPrintCoor[decimals=2,round](1.252525,2.252525){0}{0}\newline
\end{document}



More information about the PSTricks mailing list