[pstricks] Suggestions for \psPrintValue
Herbert Voss
Herbert.Voss at FU-Berlin.DE
Fri Apr 24 12:06:34 CEST 2009
Buddy Ledger schrieb:
> I have been playing with \psPrintValue and I've found that it does not
> round values it truncates. The code below rounds properly. I've also
> included an option to display numbers in scientific notation.
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
\documentclass[]{article}
\usepackage{pst-func}
\parindent=0pt
\makeatletter
\define at boolkey[psset]{pst-func}[Pst@]{round}[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}
\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 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
\psk at xShift\space 0 moveto mfont Output show
} ifelse
}%
\end at SpecialObj%
}
\makeatother
\begin{document}
psPrintValueNew Output \newline
\psPrintValueNew[decimals=-1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=0]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=2]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=3]{1.63725E+002 sqrt}
\bigskip
\psPrintValueNew[algebraic,decimals=-1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=0,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=2,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=3,science]{1.63725E+002,sqrt(x)}\newline
\bigskip
psPrintValueNew with round Output \newline
\psset{round=true}
\psPrintValueNew[decimals=-1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=0]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=2]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=3]{1.63725E+002 sqrt}
\bigskip
\psPrintValueNew[algebraic,decimals=-1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=0,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=2,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=3,science]{1.63725E+002,sqrt(x)}\newline
\bigskip
psPrintValueNew with round and comma Output \newline
\psset{round=true,comma}
\psPrintValueNew[decimals=-1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=0]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=1]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=2]{1.63725E+002 sqrt}\newline
\psPrintValueNew[decimals=3]{1.63725E+002 sqrt}
\bigskip
\psPrintValueNew[algebraic,decimals=-1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=0,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=1,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=2,science]{1.63725E+002,sqrt(x)}\newline
\psPrintValueNew[algebraic,decimals=3,science]{1.63725E+002,sqrt(x)}\newline
\end{document}
More information about the PSTricks
mailing list