pstricks and calc (A2)

Denis Girou Denis.Girou at idris.fr
Fri Aug 7 23:39:28 CEST 1998


-----------------------------------------------------------------------------
This is the PSTricks mailing list, devoted to discussions about computational
graphics in (La)TeX using the PSTricks package from Timothy van Zandt.
For help using this mailing list, see instructions at the end of message.
-----------------------------------------------------------------------------

>>>>> "Werenfried.Spit" == Werenfried Spit <w.spit at witbo.nl> writes:

    Werenfried.Spit> This is precisely one of the things I find so valuable about pstricks. 
    Werenfried.Spit> Maybe it is wanting a bit too much to try to combine this power with 
    Werenfried.Spit> the ease of directly inputting simple arithmetics. Things like
    Werenfried.Spit>  \setcounter{left}{-10}
    Werenfried.Spit>  \setcounter{right}{10}
    Werenfried.Spit>  \setcounter{xa}{10}
    Werenfried.Spit>  \setcounter{xb}{50}
    Werenfried.Spit>  \setcounter{x}{\c at xa}\addtocounter{x}{\c at left}\rput(\c at x,0){a left}
    Werenfried.Spit>  \setcounter{x}{\c at xa}\addtocounter{x}{\c at right}\rput(\c at x,0){a right}
    Werenfried.Spit>  \setcounter{x}{\c at xb}\addtocounter{x}{\c at left}\rput(\c at x,0){b left}
    Werenfried.Spit>  \setcounter{x}{\c at xb}\addtocounter{x}{\c at right}\rput(\c at x,0){b right}
    Werenfried.Spit> do the job well. I was `only' looking for a way to make the input a bit
    Werenfried.Spit> less clumsy, maybe the way calc does.

  If you want to use counters rather than dimension registers, we must do
otherwise that I did yesterday (anyway, there was a serious bug in it that
I see tonigh, and coordinates like 12 or 2.3 did work any more... So, just
forget this solution.

  Here is a better one (I hope...). It manage transparentely counters and
dimensions. Nevertheless there are some pitfalls when using length variables.
And I don't test it a lot too...

D.G.

\documentclass{article}

\usepackage{calc}
\usepackage{pstricks}
\usepackage{pst-plot}

\makeatletter

% Interface between `calc' and PSTricks.
% We redefine the \pssetlength, \pssetxlength, \pssetylength and
% \psaddtolength macros to use \setlength if we detect that the coordinate
% is a formula (a formula is a string which contain an operation using
% "+", "-", "*" or "/".
% One value can contain integers only or dimensions only (real numbers follow
% by the ``pt'' string, as it must be TeX dimensions), but you can't mix
% integers and real values.

% Take care that there are pitfalls when using length variables.
%   - don't forget that the "string" pt must appear in there is a computation,
%     so add "+ 0pt" if needed
%   - if you use length variables, you must set locally "xunit=1pt" before
%     because in this case the unit will not be correct
%   - some complicated computations with length variables seems to doesn't work

% Take care also that it is a LaTeX solution only and that it obviously
% doesn't work in plain.

% Not heavily tested, take care!

% D.G. - August 7, 1998

\ifx\setlength\@undefined
% We are in plain, so we keep original versions and do nothing
\else
% We are in LaTeX
\newif\ifPst at Boolean
\newcounter{Pst at CounterA}

\def\pssetlength#1#2{%
\Pst at ComputeLength{#2}{\@tempa}%
\let\@psunit\psunit
\afterassignment\pstunit at off
#1 \@tempa\@psunit}

\def\psaddtolength#1#2{%
\Pst at ComputeLength{#2}{\@tempa}%
\let\@psunit\psunit
\afterassignment\pstunit at off
\advance#1 \@tempa\@psunit}

\def\pssetxlength#1#2{%
\Pst at ComputeLength{#2}{\@tempa}%
\let\@psunit\psxunit
\afterassignment\pstunit at off
#1 \@tempa\@psunit}

\def\pssetylength#1#2{%
\Pst at ComputeLength{#2}{\@tempa}%
\let\@psunit\psyunit
\afterassignment\pstunit at off
#1 \@tempa\@psunit}
\fi                             % End LaTeX test

% Return in #2 the length to use, allowing `calc' doing his job
% if #1 is a formula containing integers or real values (lengths)
\def\Pst at ComputeLength#1#2{%
\Pst at IsFormula{#1}%
\ifPst at Boolean
  % Formula detected: let `calc' do his job...
  % Do formula use integer values (counters) or real values (lengths)?
  \Pst at IsContainDimensions{#1}%
  \ifPst at Boolean
    % Lengths (dimensions)
    \setlength\pst at dima{#1}%
    \pst at dimtonum\pst at dima{#2}%
  \else
    % Counters
    \setcounter{Pst at CounterA}{#1}%
    \edef#2{\value{Pst at CounterA}}%
  \fi
\else
  % No formula: "normal" way
  \edef#2{#1}%
\fi}

\def\Pst at IsFormula#1{%
\Pst at Booleanfalse
\Pst at IsFormula@i#1+\@nil}

\def\Pst at IsFormula@i#1+#2\@nil{%
\edef\@tempa{#2}%
\ifx\@tempa\@empty
  \Pst at IsFormula@ii#1-\@nil%
\else
  % Formula: contain a "+"
  \Pst at Booleantrue
\fi}

\def\Pst at IsFormula@ii#1-#2\@nil{%
\edef\@tempa{#2}%
\ifx\@tempa\@empty
  \Pst at IsFormula@iii#1*\@nil%
\else
  % Formula: contain a "-"
  \Pst at Booleantrue
\fi}

\def\Pst at IsFormula@iii#1*#2\@nil{%
\edef\@tempa{#2}%
\ifx\@tempa\@empty
  \Pst at IsFormula@iv#1/\@nil%
\else
  % Formula: contain a "*"
  \Pst at Booleantrue
\fi}

\def\Pst at IsFormula@iv#1/#2\@nil{%
\edef\@tempa{#2}%
\ifx\@tempa\@empty
  % No "+", "-", "*" or "/": not a formula
\else
  % Formula: contain a "/"
  \Pst at Booleantrue
\fi}

\def\Pst at IsContainDimensions#1{%
\Pst at Booleanfalse
\Pst at IsContainDimensions@i#1pt\@nil}

\def\Pst at IsContainDimensions@i#1pt#2\@nil{%
\edef\@tempa{#2}%
\ifx\@tempa\@empty
\else
  % The argument contains the "pt" string, so we suppose that is contains
  % dimension values
  \Pst at Booleantrue
\fi}

\makeatother

\pagestyle{empty}

\Huge

\begin{document}

\newcommand{\LeftShift}{-1}
\newcommand{\RightShift}{1}

\newlength{\MyLengthA}
\newlength{\MyLengthB}

\setlength{\MyLengthA}{7.3cm}
\setlength{\MyLengthB}{1.7cm}

\begin{pspicture}(5,5)\psgrid[subgriddiv=0]
  \rput(1+\LeftShift,0){left}
  \rput(1+\RightShift,0){right}
  \rput(1+\LeftShift,1){once more}
  \rput(1+\RightShift,1){and again}
  %
  \rput(1,3.4){\pscirclebox{A}}
  %
  \rput(1,1 + 1){\pscirclebox{B}}
  % (3.44,4.56)
  \rput(2.1pt + 4.3pt / \real{3.2},1.2pt + 1.6pt * \real{2.1}){\pscirclebox{C}}
  \rput(6.3pt - 4pt,3 - \LeftShift){\pscirclebox{D}} % (2.3,4)
  \begingroup
   \psset{xunit=1pt} % Required as we will use length variables
   % (3.58,2)
   \rput(\textheight - \textwidth * \real{1.3} + 0pt,2){\pscirclebox{E}}
   \rput(\MyLengthA - \MyLengthB * \real{2} + 0pt,1){\pscirclebox{F}} % (3.9,1)
  \endgroup
\end{pspicture}

\vspace{1cm}

\newcommand{\Left}{-10}
\newcommand{\Right}{10}

\newcounter{xa}
\newcounter{xb}
\setcounter{xa}{10}
\setcounter{xb}{50}

\psset{unit=0.1}

\begin{pspicture}(80,10)
 \psaxes[Dx=10](80,0)
 \rput(\value{xa} + \Left,0){a left}
 \rput(\value{xa} + \Right,0){a right}
 \rput(\value{xb} + \Left,0){b left}
 \rput(\value{xb} + \Right,0){b right}
\end{pspicture}

\end{document}


-----------------------------------------------------------------------------
The list interface (subscription, information, access to the archives) is on:
http://www.tug.org/cgi-bin/lwgate/pstricks
Otherway to unsubscribe, send mail to pstricks-request at mail.tug.org
with a blank subject and in body the line unsubscribe <email-address>
-----------------------------------------------------------------------------



More information about the PSTricks mailing list