[pstricks] Floating point calculations in LaTeX

Franco Bagnoli franco.bagnoli at unifi.it
Wed Feb 26 18:30:39 CET 2003


On Wed, 26 Feb 2003, Chris Sangwin wrote:

> What other approaches have people taken to this problem?  

Computations may be done using postscript, but the results cannot be "feed 
back" to latex easily. For instance, coordinates can be inserted using 

\def\x{5}
\def\y{6}
\def\scale{15}
\SpecialCoord
\psline(!\x\space\scale\space mul \x\space\scale\space mul}

but for instance, while developing forms for optical mark recognition i 
had to use rather unpleasant loops (with multido)  like   


\newgray{mygray}{0.5}
\newcommand{\ncols}{4}
\newcommand{\myboxhalfsize}{1.5}
\newcommand{\myboxhalfsep}{2}
\newcommand{\mybox}{%
  \rput(0,\myboxhalfsep){%
    \psframe%
     (-\myboxhalfsize,-\myboxhalfsize)%
     (\myboxhalfsize,\myboxhalfsize)%
  }
  \rput(0,-\myboxhalfsep){%
    \psframe%
      (-\myboxhalfsize,-\myboxhalfsize)%
      (\myboxhalfsize,\myboxhalfsize)%
  }
}
....
\SpecialCoor
\rput(10,-30){%
  \multido{\n=1+1}{\ncols}{%
    \psline[linewidth=0.5pt,linecolor=mygray](0,-2)(!\ncols\space.5 add -2) %
    \psline[linewidth=0.5pt,linecolor=mygray](0,5)(!\ncols\space.5 add 5) %
    \setcounter{label}{\n}%
    \psset{xunit=10mm}%
    \rput(\n,1){%
      \psset{xunit=1mm}%
      \Alph{label}%
    }% 
  }% 
  %
  \rput(0,0){
    \rput{90}(!-8 \ncols\space 2 div neg 10 mul){domande}
    \multido{\i=1+1}{\ncols}{
      \psset{yunit=-8mm} %
      \rput(0,\i){%
        \multido{\n=1+1}{\ncols}{%
          \psset{yunit=1mm} %
          \psline[linewidth=0.5pt,linecolor=mygray](0,5)(!\ncols\space.5 add 5) %
          \setcounter{label}{\n}%
          \rput(0,0){\i}
          \psset{xunit=10mm}%
          \rput(\n,0){%
            \psset{unit=1mm}%
            \mybox%
          } 
        } 
      }
    }
  }
} 

Clearly it would much better to have floating points calculations,
strings and vector  addressability, etc. 


I have also used the TT2 macro package (www.tt2.org) which has the
benefit of preprocessing the text BEFORE latex, so I can perform also 

[% labels = ["one", "two", "three"] %]
[% FOREACH l=labels %]
here is label [%l%]
[%END%]

but unfortunately the tt2 package lacks lot of mathematics, uses a
different syntax from latex, etc. 

So I developed a tt2 plugin that allows to use a latex-like syntax for
performing computations (I use it for the generation of dynamical 
multiple-choice quizzes). You can find some examples at 

http://www.complex.unifi.it/WebComplex/bin/view/Software/LcalcPlugin

If you are interested I can add more examples.

Please notice that it does not use a true latex parser, just some
regular expressions.


-- 
Franco Bagnoli (franchino) <franco.bagnoli at unifi.it> (bagnoli at dma.unifi.it)
virtual location: Dipartimento di Energetica "S. Stecco"
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787
GPG Key fingerprint = 169D 9EA5 8FD3 7EDA E43A  9830 255F BCEC 0D63 3728



More information about the PSTricks mailing list